;
Artificial intelligence April 24 ,2025

Creating an Image Classifier with Convolutional Neural Networks (CNNs)

Introduction

In the world of artificial intelligence (AI), image classification is one of the most exciting and widely-used applications. Whether it's sorting photos, identifying products in a store, or even detecting diseases in medical images, AI-powered image classification plays a key role in many industries. Convolutional Neural Networks (CNNs) are the go-to model for image-related tasks because they are specifically designed to process visual data.

In this blog, we will guide you through the process of building an image classifier using CNNs, from data preprocessing to model training and evaluation.

How AI Works in Image Classification Using CNNs

AI works by training a machine learning model on labeled image data. Here's how CNNs work in the context of image classification:

  1. Image Input: Images are input into the CNN, which typically consists of several layers like convolutional layers, pooling layers, and fully connected layers.
  2. Convolutional Layer: This layer applies filters (also known as kernels) to the image, helping the model to identify features like edges, textures, and shapes.
  3. Activation Function: The ReLU (Rectified Linear Unit) activation function is often used to introduce non-linearity, allowing the model to learn complex patterns.
  4. Pooling Layer: The pooling layer reduces the spatial dimensions of the image, helping to reduce computational cost and overfitting.
  5. Fully Connected Layer: After several convolutions and pooling operations, the fully connected layer classifies the image into one of the target categories.
  6. Output Layer: The output layer assigns probabilities to each class, and the class with the highest probability is chosen as the predicted label.

By the end of this blog, you'll have an understanding of how CNNs can be applied to classify images effectively.

Steps to Build an Image Classifier with CNNs

Let’s go through the process of building an image classifier step by step.

Certainly! Here's the complete process for building an image classifier with Convolutional Neural Networks (CNNs) in one cohesive explanation, including the outputs at each step.

Step 1: Install Required Libraries

To get started, we need to install the necessary libraries, primarily TensorFlow (for building the model) and Matplotlib (for visualizations). To install TensorFlow, run:

pip install tensorflow

Once installed, you can verify that the libraries are installed correctly by attempting to import them:

import tensorflow as tf
import matplotlib.pyplot as plt

There is no direct output from this step other than the successful installation of the libraries.

Step 2: Load the Dataset

We will use the CIFAR-10 dataset, which is available directly through TensorFlow.

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

Output:

  • x_train and x_test are numpy arrays with shape (num_samples, 32, 32, 3), where 32x32 is the size of each image and 3 is the number of color channels (RGB).
  • y_train and y_test are numpy arrays with shape (num_samples, 1) containing the class labels (0 to 9).

To verify the dataset shape:

print(x_train.shape)  # Output: (50000, 32, 32, 3)
print(y_train.shape)  # Output: (50000, 1)

Step 3: Preprocess the Data

For better performance, we'll normalize the pixel values of the images to be between 0 and 1.

x_train = x_train / 255.0
x_test = x_test / 255.0

Output:

  • The x_train and x_test arrays are now scaled to values between 0 and 1, which helps the model learn better.

If you're using data augmentation, you can also augment the training data to improve model robustness:

train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    rescale=1./255, 
    rotation_range=20, 
    width_shift_range=0.2, 
    height_shift_range=0.2, 
    shear_range=0.2, 
    zoom_range=0.2, 
    horizontal_flip=True, 
    fill_mode='nearest'
)

train_datagen.fit(x_train)

To visualize an augmented image:

augmented_image = train_datagen.flow(x_train, y_train, batch_size=1).next()[0]
plt.imshow(augmented_image[0])  # Display the first image of the batch
plt.show()

Step 4: Build the CNN Model

We will now build the CNN model using Keras. Here's an example architecture:

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

Output:

  • If you print the model summary:
model.summary()

You'll get the architecture of the CNN:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 30, 30, 32)        896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 13, 13, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2D) (None, 6, 6, 64)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 4, 4, 128)         73856     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2D) (None, 2, 2, 128)       0         
_________________________________________________________________
flatten (Flatten)            (None, 512)               0         
_________________________________________________________________
dense (Dense)                (None, 128)               65664     
_________________________________________________________________
dense_1 (Dense)              (None, 10)                1290      
=================================================================
Total params: 162,202
Trainable params: 162,202
Non-trainable params: 0
_________________________________________________________________

This shows you the number of parameters in each layer and the model's total parameters.

Step 5: Compile the Model

Next, we compile the model with an appropriate optimizer, loss function, and evaluation metric:

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

No direct output here, but the model is now ready to be trained.

Step 6: Train the Model

Now, we train the model using the training data. We’ll train for 10 epochs and validate using the test set.

history = model.fit(train_datagen.flow(x_train, y_train, batch_size=64), epochs=10, validation_data=(x_test, y_test))

Output:

  • During training, the model will output the loss and accuracy for both the training and validation data after each epoch:
Epoch 1/10
782/782 [==============================] - 10s 12ms/step - loss: 1.5682 - accuracy: 0.4312 - val_loss: 1.3457 - val_accuracy: 0.5131
Epoch 2/10
782/782 [==============================] - 9s 12ms/step - loss: 1.2415 - accuracy: 0.5541 - val_loss: 1.2001 - val_accuracy: 0.5789
...
Epoch 10/10
782/782 [==============================] - 9s 12ms/step - loss: 0.6272 - accuracy: 0.7895 - val_loss: 0.9016 - val_accuracy: 0.7003

This shows you the progress of training, the model's accuracy on the training set, and its accuracy on the validation set.

Step 7: Evaluate the Model

Once training is complete, we evaluate the model on the test dataset to see how well it generalizes to new, unseen data:

test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test Accuracy: {test_acc}")

Output:

  • The test accuracy will give you a final assessment of how well the model performs on new data:
Test Accuracy: 0.7003

Step 8: Visualize the Training Results

To understand the model's performance better, we can visualize the training and validation accuracy over epochs.

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()

Output:

  • A graph showing how the model's accuracy improved over time for both training and validation sets. It helps you understand if the model is overfitting or underfitting.

Conclusion

By following these steps, you've successfully built and trained an image classifier using a Convolutional Neural Network (CNN) on the CIFAR-10 dataset. At each step, we observed outputs such as dataset shapes, model summaries, training progress, evaluation metrics, and visualizations, all of which contribute to understanding the model's performance.

 

Next Blog- Developing a Sentiment Analysis Tool    

Purnima
0

You must logged in to post comments.

Related Blogs

What is Ar...
Artificial intelligence March 03 ,2025

What is Artificial I...

History an...
Artificial intelligence March 03 ,2025

History and Evolutio...

Importance...
Artificial intelligence March 03 ,2025

Importance and Appli...

Narrow AI,...
Artificial intelligence March 03 ,2025

Narrow AI, General A...

AI vs Mach...
Artificial intelligence March 03 ,2025

AI vs Machine Learni...

Linear Alg...
Artificial intelligence March 03 ,2025

Linear Algebra Basic...

Calculus f...
Artificial intelligence March 03 ,2025

Calculus for AI

Probabilit...
Artificial intelligence March 03 ,2025

Probability and Stat...

Probabilit...
Artificial intelligence March 03 ,2025

Probability Distribu...

Graph Theo...
Artificial intelligence March 03 ,2025

Graph Theory and AI

What is NL...
Artificial intelligence March 03 ,2025

What is NLP

Preprocess...
Artificial intelligence March 03 ,2025

Preprocessing Text D...

Sentiment...
Artificial intelligence March 03 ,2025

Sentiment Analysis a...

Word Embed...
Artificial intelligence March 03 ,2025

Word Embeddings (Wor...

Transforme...
Artificial intelligence March 03 ,2025

Transformer-based Mo...

Building C...
Artificial intelligence March 03 ,2025

Building Chatbots wi...

Basics of...
Artificial intelligence March 03 ,2025

Basics of Computer V...

Image Prep...
Artificial intelligence March 03 ,2025

Image Preprocessing...

Object Det...
Artificial intelligence March 03 ,2025

Object Detection and...

Face Recog...
Artificial intelligence March 03 ,2025

Face Recognition and...

Applicatio...
Artificial intelligence March 03 ,2025

Applications of Comp...

AI-Powered...
Artificial intelligence March 03 ,2025

AI-Powered Chatbot U...

Implementi...
Artificial intelligence March 03 ,2025

Implementing a Basic...

Implementa...
Artificial intelligence March 03 ,2025

Implementation of Ob...

Implementa...
Artificial intelligence March 03 ,2025

Implementation of Ob...

Implementa...
Artificial intelligence March 03 ,2025

Implementation of Fa...

Deep Reinf...
Artificial intelligence March 03 ,2025

Deep Reinforcement L...

Deep Reinf...
Artificial intelligence March 03 ,2025

Deep Reinforcement L...

Deep Reinf...
Artificial intelligence March 03 ,2025

Deep Reinforcement L...

Introducti...
Artificial intelligence March 03 ,2025

Introduction to Popu...

Introducti...
Artificial intelligence March 03 ,2025

Introduction to Popu...

Introducti...
Artificial intelligence March 03 ,2025

Introduction to Popu...

Introducti...
Artificial intelligence March 03 ,2025

Introduction to Popu...

Tools for...
Artificial intelligence March 03 ,2025

Tools for Data Handl...

Tool for D...
Artificial intelligence March 03 ,2025

Tool for Data Handli...

Cloud Plat...
Artificial intelligence April 04 ,2025

Cloud Platforms for...

Deep Dive...
Artificial intelligence April 04 ,2025

Deep Dive into AWS S...

Cloud Plat...
Artificial intelligence April 04 ,2025

Cloud Platforms for...

Cloud Plat...
Artificial intelligence April 04 ,2025

Cloud Platforms for...

Visualizat...
Artificial intelligence April 04 ,2025

Visualization Tools...

Data Clean...
Artificial intelligence April 04 ,2025

Data Cleaning and Pr...

Explorator...
Artificial intelligence April 04 ,2025

Exploratory Data Ana...

Explorator...
Artificial intelligence April 04 ,2025

Exploratory Data Ana...

Feature En...
Artificial intelligence April 04 ,2025

Feature Engineering...

Data Visua...
Artificial intelligence April 04 ,2025

Data Visualization w...

Working wi...
Artificial intelligence April 04 ,2025

Working with Large D...

Understand...
Artificial intelligence April 04 ,2025

Understanding Bias i...

Ethics in...
Artificial intelligence April 04 ,2025

Ethics in AI Develop...

Fairness i...
Artificial intelligence April 04 ,2025

Fairness in Machine...

The Role o...
Artificial intelligence April 04 ,2025

The Role of Regulati...

Responsibl...
Artificial intelligence April 04 ,2025

Responsible AI Pract...

Artificial...
Artificial intelligence April 04 ,2025

Artificial Intellige...

AI in Fina...
Artificial intelligence April 04 ,2025

AI in Finance and Ba...

AI in Auto...
Artificial intelligence April 04 ,2025

AI in Autonomous Veh...

AI in Gami...
Artificial intelligence April 04 ,2025

AI in Gaming and Ent...

AI in Soci...
Artificial intelligence April 04 ,2025

AI in Social Media a...

Building a...
Artificial intelligence April 04 ,2025

Building a Spam Emai...

Developing...
Artificial intelligence April 04 ,2025

Developing a Sentime...

Implementi...
Artificial intelligence April 04 ,2025

Implementing a Recom...

Generative...
Artificial intelligence April 04 ,2025

Generative AI: An In...

Explainabl...
Artificial intelligence April 04 ,2025

Explainable AI (XAI)

AI for Edg...
Artificial intelligence April 04 ,2025

AI for Edge Devices...

Quantum Co...
Artificial intelligence April 04 ,2025

Quantum Computing an...

AI for Tim...
Artificial intelligence April 04 ,2025

AI for Time Series F...

Emerging T...
Artificial intelligence May 05 ,2025

Emerging Trends in A...

AI and the...
Artificial intelligence May 05 ,2025

AI and the Job Marke...

The Role o...
Artificial intelligence May 05 ,2025

The Role of AI in Cl...

AI Researc...
Artificial intelligence May 05 ,2025

AI Research Frontier...

Preparing...
Artificial intelligence May 05 ,2025

Preparing for an AI-...

4 Popular...
Artificial intelligence May 05 ,2025

4 Popular AI Certifi...

Building a...
Artificial intelligence May 05 ,2025

Building an AI Portf...

How to Pre...
Artificial intelligence May 05 ,2025

How to Prepare for A...

AI Career...
Artificial intelligence May 05 ,2025

AI Career Opportunit...

Staying Up...
Artificial intelligence May 05 ,2025

Staying Updated in A...

Part 1-  T...
Artificial intelligence May 05 ,2025

Part 1- Tools for T...

Implementi...
Artificial intelligence May 05 ,2025

Implementing ChatGPT...

Part 2-  T...
Artificial intelligence May 05 ,2025

Part 2- Tools for T...

Part 1- To...
Artificial intelligence May 05 ,2025

Part 1- Tools for Te...

Technical...
Artificial intelligence May 05 ,2025

Technical Implementa...

Part 2- To...
Artificial intelligence May 05 ,2025

Part 2- Tools for Te...

Part 1- To...
Artificial intelligence May 05 ,2025

Part 1- Tools for Te...

Step-by-St...
Artificial intelligence May 05 ,2025

Step-by-Step Impleme...

Part 2 - T...
Artificial intelligence May 05 ,2025

Part 2 - Tools for T...

Part 4- To...
Artificial intelligence May 05 ,2025

Part 4- Tools for Te...

Part 1- To...
Artificial intelligence May 05 ,2025

Part 1- Tools for Te...

Part 2- To...
Artificial intelligence May 05 ,2025

Part 2- Tools for Te...

Part 3- To...
Artificial intelligence May 05 ,2025

Part 3- Tools for Te...

Step-by-St...
Artificial intelligence May 05 ,2025

Step-by-Step Impleme...

Part 1- To...
Artificial intelligence June 06 ,2025

Part 1- Tools for Im...

Implementa...
Artificial intelligence June 06 ,2025

Implementation of D...

Part 2- To...
Artificial intelligence June 06 ,2025

Part 2- Tools for Im...

Part 1- To...
Artificial intelligence June 06 ,2025

Part 1- Tools for Im...

Implementa...
Artificial intelligence June 06 ,2025

Implementation of Ru...

Part 1- To...
Artificial intelligence June 06 ,2025

Part 1- Tools for Im...

Part 2- To...
Artificial intelligence June 06 ,2025

Part 2- Tools for Im...

Step-by-St...
Artificial intelligence June 06 ,2025

Step-by-Step Impleme...

Part 1-Too...
Artificial intelligence June 06 ,2025

Part 1-Tools for Ima...

Part 2- To...
Artificial intelligence June 06 ,2025

Part 2- Tools for Im...

Implementa...
Artificial intelligence June 06 ,2025

Implementation of Pi...

Get In Touch

123 Street, New York, USA

+012 345 67890

techiefreak87@gmail.com

© Design & Developed by HW Infotech