ad
ad
Topview AI logo

Image Classification with Neural Networks in Python

Science & Technology


Introduction

In this tutorial, we are going to create an image classification script using Python, TensorFlow, and Convolutional Neural Networks (CNNs). CNNs are specialized neural networks designed to process image and audio data by recognizing patterns within the data. We will utilize the CIFAR-10 dataset provided by Keras, which contains a variety of images categorized into ten classes, such as airplanes, trucks, and horses. The objective is to train our neural network to classify these images accurately, and subsequently, we will test the model with images sourced from the internet.

Setting Up the Environment

First, we need to install the necessary libraries. Open a terminal (or command prompt) and activate your Python environment if you are using one. Then, install the following libraries:

pip install numpy matplotlib tensorflow opencv-python

Once the libraries are installed, we can start coding by importing them into our script:

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import datasets, layers, models

Loading and Normalizing the Dataset

Next, we will load the CIFAR-10 dataset, which is conveniently available in Keras:

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

We can normalize the image data by scaling pixel values to a range between 0 and 1:

train_images = train_images / 255.0
test_images = test_images / 255.0

We should also define the class names for the CIFAR-10 dataset:

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

Visualizing Sample Images

Before building the model, we will visualize a sample of 16 images from the dataset to understand what we are working with:

plt.figure(figsize=(10, 10))
for i in range(16):
    plt.subplot(4, 4, i + 1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i])
    plt.xlabel(class_names[train_labels[i][0]])
plt.show()

Building the Convolutional Neural Network

Now we will define the architecture of our neural network:

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'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

After defining the model, we need to compile it:

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Training the Model

We will now proceed to train the model while validating it with test data:

model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))

Once training is complete, we evaluate the model:

loss, accuracy = model.evaluate(test_images, test_labels)
print(f"Loss: (loss)")
print(f"Accuracy: (accuracy)")

Saving and Loading the Model

To avoid training the model every time, we can save it after training:

model.save('image_classifier.model')

Later, we can load the model to make predictions:

model = keras.models.load_model('image_classifier.model')

Making Predictions

We will now use images from the internet to test our model. First, we will load an image and preprocess it before making predictions:

image = cv.imread('path_to_image.jpg')
image = cv.cvtColor(image, cv.COLOR_BGR2RGB) / 255.0
prediction = model.predict(np.array([image]))
index = np.argmax(prediction)
print(f"Prediction: (class_names[index])")

Finally, visualize the image along with the prediction.

Conclusion

By following this tutorial, you have successfully created an image classification model using Python and TensorFlow. You can continue experimenting with different architectures, datasets, and hyperparameters to improve your model. The results from the model can be impressive, especially with clear images.


Keywords

  • Image Classification
  • Neural Networks
  • Python
  • TensorFlow
  • Convolutional Neural Networks
  • CIFAR-10 Dataset
  • Model Training
  • Prediction

FAQ

Q: What is image classification?
A: Image classification is a process where a model categorizes an image into predefined classes based on its content.

Q: What libraries do I need to install for this tutorial?
A: You need to install numpy, matplotlib, tensorflow, and opencv-python.

Q: What dataset is used in this tutorial?
A: The CIFAR-10 dataset, which contains 60,000 32x32 color images in 10 different classes.

Q: How does the model make predictions?
A: The model outputs a probability distribution over the classes, and the class with the highest probability is selected as the prediction.

ad

Share

linkedin icon
twitter icon
facebook icon
email icon
ad