SarPhat Author
In this tutorial, we'll walk through creating a simple image classifier that can differentiate between dogs, cats, and other animals. We'll use a neural network and a well-known dataset, the CIFAR-10 dataset, which includes images of different classes, including dogs and cats.
Prerequisites
- Basic knowledge of Python programming
- Familiarity with machine learning concepts
- Jupyter Notebook (recommended) or any Python environment
- Libraries: TensorFlow/Keras, NumPy, Matplotlib
Step 1: Set Up Your Environment
First, make sure you have the necessary libraries installed. You can install them using pip:
pip install tensorflow numpy matplotlib
Step 2: Import Libraries
Start by importing the necessary libraries.
import tensorflow as tf from tensorflow.keras import datasets, layers, models import matplotlib.pyplot as plt import numpy as np
Step 3: Load and Explore the Dataset
We'll use the CIFAR-10 dataset, which contains 60,000 32x32 color images in 10 classes, with 6,000 images per class.
# Load the CIFAR-10 dataset (train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data() # Class names in CIFAR-10 class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] # Explore the dataset print("Training images shape:", train_images.shape) print("Training labels shape:", train_labels.shape) print("Testing images shape:", test_images.shape) print("Testing labels shape:", test_labels.shape)
Step 4: Preprocess the Data
Normalize the images to a range of 0 to 1 by dividing by 255.
train_images, test_images = train_images / 255.0, test_images / 255.0
Step 5: Visualize the Data
Let's visualize some images from the dataset.
plt.figure(figsize=(10,10)) for i in range(15): plt.subplot(5,3,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_images[i], cmap=plt.cm.binary) plt.xlabel(class_names[train_labels[i][0]]) plt.show()
Step 6: Create the Model
We will create a Convolutional Neural Network (CNN) with several layers.
model = models.Sequential([ layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), layers.MaxPooling2D((2, 2)), layers.Conv2D(64, (3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), layers.Conv2D(64, (3, 3), activation='relu'), layers.Flatten(), layers.Dense(64, activation='relu'), layers.Dense(10) ]) model.summary()
Step 7: Compile the Model
Compile the model with an appropriate loss function, optimizer, and metrics.
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
Step 8: Train the Model
Train the model using the training data.
history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
Step 9: Evaluate the Model
Evaluate the model on the test dataset to check its performance.
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) print("\nTest accuracy:", test_acc)
Step 10: Make Predictions
Use the trained model to make predictions on new data.
probability_model = models.Sequential([model, layers.Softmax()]) predictions = probability_model.predict(test_images) # Check the first prediction print("Predicted class for the first test image:", class_names[np.argmax(predictions[0])]) print("Actual class for the first test image:", class_names[test_labels[0][0]])
Step 11: Visualize Predictions
Let's visualize some predictions.
def plot_image(i, predictions_array, true_label, img): true_label, img = true_label[i], img[i] plt.grid(False) plt.xticks([]) plt.yticks([]) plt.imshow(img, cmap=plt.cm.binary) predicted_label = np.argmax(predictions_array) if predicted_label == true_label: color = 'blue' else: color = 'red' plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], 100*np.max(predictions_array), class_names[true_label]), color=color) def plot_value_array(i, predictions_array, true_label): true_label = true_label[i] plt.grid(False) plt.xticks(range(10)) plt.yticks([]) thisplot = plt.bar(range(10), predictions_array, color="#777777") plt.ylim([0, 1]) predicted_label = np.argmax(predictions_array) thisplot[predicted_label].set_color('red') thisplot[true_label].set_color('blue') # Plot the first X test images, their predicted label, and the true label num_rows = 5 num_cols = 3 num_images = num_rows * num_cols plt.figure(figsize=(2*2*num_cols, 2*num_rows)) for i in range(num_images): plt.subplot(num_rows, 2*num_cols, 2*i+1) plot_image(i, predictions[i], test_labels, test_images) plt.subplot(num_rows, 2*num_cols, 2*i+2) plot_value_array(i, predictions[i], test_labels) plt.tight_layout() plt.show()
Conclusion
Congratulations! You have successfully built a neural network to classify images as dogs, cats, or other categories using the CIFAR-10 dataset.
This is a fundamental step in understanding how neural networks and image classification work. To improve your model, consider experimenting with more layers, different hyperparameters, or other datasets.
Will publish this article in burmese as premium. More details will come in burmese.
Keep Reading