Build Your Own Image Recognition Model with Python

Step-by-Step Guide for Beginners

In partnership with

Learn AI in 5 Minutes a Day

AI Tool Report is one of the fastest-growing and most respected newsletters in the world, with over 550,000 readers from companies like OpenAI, Nvidia, Meta, Microsoft, and more.

Our research team spends hundreds of hours a week summarizing the latest news, and finding you the best opportunities to save time and earn more using AI.

Hello, budding AI enthusiasts! 🌟

Today, we're going to embark on an exciting journey into the world of image recognition using Python. By the end of this tutorial, you'll have built a model that can recognize images of various objects from the CIFAR-10 dataset.

Let's make this fun, easy to understand, and totally engaging! πŸš€

 

What You'll Learn

  • How to load and preprocess image data.

  • How to build a Convolutional Neural Network (CNN).

  • How to train and evaluate your model.

  • How to visualize predictions.

Step 1: Setting Up Your Environment

First, make sure you have TensorFlow installed. You can do this by running:

 

pip install tensorflow

 

Step 2: Import Libraries and Load Data

We'll start by importing the necessary libraries and loading the CIFAR-10 dataset, which consists of 60,000 32x32 color images in 10 classes.

 

import tensorflow as tf

from tensorflow.keras import datasets, layers, models

import numpy as np

import matplotlib.pyplot as plt

 

Now, let's load the data:

 

(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

Step 3: Normalise the Images

We need to normalise the images to a range of 0 to 1 for better performance.

 

train_images, test_images = train_images / 255.0, test_images / 255.0

 

Step 4: Visualise the Data

Let's take a peek at some of the images and their corresponding labels.

 

class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

 

plt.figure(figsize=(10,10))

for i in range(25):

    plt.subplot(5, 5, i+1)

    plt.xticks([])

    plt.yticks([])

    plt.grid(False)

    plt.imshow(train_images[i])

    plt.xlabel(class_names[train_labels[i][0]])

plt.show()

 

Explanation: 

  • We use plt.imshow to display the images and label them with class_names.

Step 5: Build the Convolutional Neural Network (CNN)

CNNs are great for image recognition tasks. We'll build a simple CNN model.

 

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, activation='softmax')

])

 

Explanation:

  • Conv2D layers: These layers are responsible for detecting features like edges and textures.

  • MaxPooling2D layers: These layers reduce the spatial dimensions, making the model more efficient.

  • Flatten: Converts the 2D matrix into a 1D vector.

  • Dense layers: Fully connected layers that make the final prediction.

 

Step 6: Compile and Train the Model

Now, we'll compile the model and train it.

 

model.compile(optimizer='adam',

              loss='sparse_categorical_crossentropy',

              metrics=['accuracy'])

 

history = model.fit(train_images, train_labels, epochs=10,

                    validation_data=(test_images, test_labels))

 

Explanation:

  • Compile: We specify the optimizer and loss function.

  • Fit: We train the model on the training data for 10 epochs.

 

Step 7: Evaluate the Model

Let's see how well our model performs on the test set.

 

test_loss, test_acc = model.evaluate(test_images, test_labels)

print("\nTest accuracy:", test_acc)

 

Explanation:

  • evaluate method returns the loss value & metrics values for the model in test mode.

 

Step 8: Make Predictions

We can use the trained model to make predictions on new data.

 

predictions = model.predict(test_images)

 

Explanation:

  • predict method generates output predictions for the input samples.

 

Step 9: Visualise Predictions

Finally, let's visualise some predictions along with their probabilities.

 

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(f"{class_names[predicted_label]} ({class_names[true_label[0]]})", 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[0]].set_color('blue')

 

plt.figure(figsize=(10, 10))

for i in range(5):

    plt.subplot(5, 2, 2*i+1)

    plot_image(i, predictions[i], test_labels, test_images)

    plt.subplot(5, 2, 2*i+2)

    plot_value_array(i, predictions[i], test_labels)

plt.show()

 

Explanation:

  • plot_image: Displays the image and the predicted label.

  • plot_value_array: Displays a bar graph of the prediction probabilities for each class.

Summary

In this post, you learned how to build an image recognition model using Python and TensorFlow. We covered data preprocessing, model building, training, evaluation, and visualisation of predictions. This journey into AI image recognition was fun and insightful! πŸŽ‰

Coding with a Smile πŸ€£ πŸ˜‚

Boolean Logic:

Trying to explain Boolean logic to a non-programmer is like trying to explain why pineapple on pizza is a controversial topic. "So, True is False unless it's not?" "Exactly!"

What’s Next? πŸ“…

Stay tuned for our next post on Music Genre Classification 🎡. We'll create a neural network that can listen to a song and predict its genre. It’s like having a mini DJ who knows exactly what you’re listening to!

Ready for More Python Fun? πŸ“¬

Subscribe to our newsletter now and get a free Python cheat sheet! πŸ“‘ Dive deeper into Python programming with more exciting projects and tutorials designed just for beginners.

Keep learning, keep coding πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’», and keep discovering new possibilities! πŸ’»βœ¨

Enjoy your journey into artificial intelligence, machine learning, data analytics, data science and more with Python!

Stay tuned for our next exciting project in the following edition!

Happy coding!πŸš€πŸ“Šβœ¨

πŸŽ‰ We want to hear from you! πŸŽ‰ How do you feel about our latest newsletter? Your feedback will help us make it even more awesome!

Login or Subscribe to participate in polls.