5 min read

Getting Started with Image Classification using Convolutional Neural Networks (CNNs)

Programming Tutorials

Hey folks! Ever wondered about the magic behind your phone gallery’s ability to group images or how Instagram identifies what’s in your posts? Well, today, we’re pulling back the curtain on that magic – introducing Convolutional Neural Networks, or CNNs for short. We’re diving deep, but we promise to keep the waters clear. Ready? Let’s go!

Object Detection

SHARE THIS:

InstagramLinkedinImage

What’s the Deal with CNNs?

At their core, CNNs are pattern-recognizing maestros. Think of them as the tech equivalent of that friend who can identify any bird by its song. Specifically designed for image data, these neural networks excel at detecting patterns and objects in pictures. From cats and dogs to more abstract stuff, if it’s in a picture, CNNs can probably spot it!


Convolutional Neural Networks

1. Setting Up Our Workspace:

Before diving into the code, we need to set up a proper workspace. This will ensure everything runs smoothly. Follow along:

a) Install Python:

Make sure you have Python installed. You can check if you have it and its version by running:

python --version

or

python3 --version

If you don’t have Python, download and install it. For TensorFlow, it’s advisable to use Python 3.6 to 3.8.

b) Virtual Environment (Highly Recommended):

Using a virtual environment isolates the packages you’ll use for this project from other projects. It’s like having a separate, neat workspace for each of your project1. s.

1. Install virtualenvif you haven’t

pip install virtualenv

2. Create a new virtual environment:

python3 -m venv cnn_workspace

3. Activate the virtual environment:

  • On macOS and Linux:

source cnn_workspace/bin/activate

  • On Windows:

.\cnn_workspace\Scripts\activate

You should see (cnn_workspace)on your terminal, indicating the environment is active.

c) Install Necessary Libraries:

With our virtual environment activated, we’ll install TensorFlow, the main library we’ll be using for this endeavor.

pip install tensorflow

If you face any compatibility issues, consider installing a specific version of TensorFlow that’s compatible with your Python version:

pip install tensorflow==2.4

d) Verify Installation:

After installing, you can check if TensorFlow was installed correctly:

python -c "import tensorflow as tf; print(tf.__version__)"

2. Diving into the Code:

For our maiden voyage, we’ll play with the CIFAR-10 dataset. It’s a treasure trove of 60,000 images spread across 10 categories (from birds to trucks).

a) Import Necessary Libraries:

import tensorflow as tf from tensorflow.keras import layers, models, datasets

b) Load and Prep the Data

CIFAR-10 is so popular that TensorFlow kindly included it out-of-the-box.

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

# To ensure our model doesn't struggle, we normalize the pixel values.

train_images, test_images = train_images / 255.0, test_images / 255.0

c) Designing our CNN

This is where we lay the bricks of our model’s architecture. Imagine stacking Lego blocks, but for image recognition.

model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu'))

d) Adding Dense Layers

Post-CNN, we introduce dense layers, finalizing our model’s structure and getting it ready for action.

model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))

e) Compile and Train the Model:

This stage is like teaching our model the difference between a cat and a dog. We’re setting ground rules and then letting it learn.

model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))

3. Taking It For A Spin:

Once our model is trained, it’s time to test its capabilities truly.

Test the Model:

First, let’s evaluate our model using the test dataset. This will give us a clear picture of its accuracy.

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) print("\nTest accuracy:", test_acc)

Tweak and Optimize:

If the accuracy isn’t satisfactory, you might consider adjusting the model’s architecture, using data augmentation, or playing around with the learning rate.

Real-world Application:

Want to test the model on your own images? Here’s a simple way to do it:

from tensorflow.keras.preprocessing import image

import numpy as np


# Load your image

img_path = 'path_to_your_image.jpg'

img = image.load_img(img_path, target_size=(32, 32))


# Convert image to numpy array and normalize

img_array = image.img_to_array(img) / 255.0

img_array = np.expand_dims(img_array, axis=0) # Model expects a batch of images


# Make prediction

predictions = model.predict(img_array)

predicted_class = np.argmax(predictions[0])

print("Predicted class:", predicted_class)

Visualization:

Visualizing predictions can make the results tangible. Let’s display an image with its predicted label.

import matplotlib.pyplot as plt


# Load a random image from the test dataset

idx = np.random.randint(0, len(test_images))

img, true_label = test_images[idx], test_labels[idx][0]


# Predict using our model

predicted_label = np.argmax(model.predict(np.expand_dims(img, axis=0)))


# Plot

plt.figure(figsize=(6, 3))

plt.imshow(img)

plt.title(f"True: {true_label} | Predicted: {predicted_label}")

plt.axis("off")

plt.show()

Experimentation is key in machine learning. The more you tinker and test, the better you’ll understand the intricacies and behaviors of your models. Dive deep and enjoy the process!