Open In App

Convolutional Neural Network (CNN) in Tensorflow

Last Updated : 08 Oct, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Convolutional Neural Networks (CNNs) are used in the field of computer vision. There ability to automatically learn spatial hierarchies of features from images makes them the best choice for such tasks. In this article we will explore the basic building blocks of CNNs and show us how to implement a CNN model using TensorFlow.

1. Importing Libraries

We will import matplotlib and tensorflow for its implementation.

Python
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt

2. Loading and Preprocessing the Dataset

We will be using CIFAR-10 dataset. It is a popular benchmark dataset used for machine learning and computer vision tasks particularly for image classification. It contains 60,000, 32x32 color images divided into 10 classes with 6,000 images per class.

  • Normalization: The pixel values in images range from 0 to 255. We normalize the images by dividing by 255 to scale them to a range of 0 to 1. This helps with model convergence during training.
  • to_categorical(): Converts the integer labels into a one-hot encoded format where each label is represented as a binary vector indicating the class.
Python
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0
train_labels = to_categorical(train_labels, 10)
test_labels = to_categorical(test_labels, 10)

3. Defining the CNN Model

  • models.Sequential(): Initializes a linear stack of layers where each layer has exactly one input and one output.
  • Conv2D: Adds a convolutional layer with 32 filters, each of size (3, 3).
  • MaxPool2D: Adds a max pooling layer to downsample the feature maps from the previous convolutional layer.
  • Flatten: Flattens the output from the convolutional layers into a 1D vector which is required for the fully connected (dense) layers.
  • Dense: Fully connected layer with 128 units and ReLU activation.
Python
model = models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
    layers.MaxPooling2D(pool_size=(2, 2)),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])

4. Compiling the Model

  • The Adam optimizer is used for gradient-based optimization. It adjusts the learning rate based on first and second moments of the gradients.
  • Categorical cross entropy is used as the loss function for multi-class classification problems.
  • metrics=['accuracy']: Specifies that we want to track accuracy during training and evaluation.
Python
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

5. Training the Model

  • The model will train for 10 iterations over the entire dataset.
  • The model will process 64 images at a time before updating the weights.
  • The test set is used for validation after each epoch to track the model's performance on unseen data.
Python
history = model.fit(train_images, train_labels, epochs=10, batch_size=64, validation_data=(test_images, test_labels))

Output:

Screenshot-2025-02-28-133847

6. Evaluating the Model

Python
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc * 100:.2f}%")

Output:

Test accuracy: 70.05%

Test accuracy is 70% which is good for simple CNN model we can increase its accuracy further by optimizing the model based on our task.

7. Plotting Training History

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

plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Model Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend(loc='upper right')
plt.show()

Output:

download-
Epochs vs Accuracy

From the graph we can observe that the training accuracy increases steadily indicating that the model is learning and improving over time. However the validation accuracy shows some fluctuation particularly in the earlier epochs before stabilizing. This suggests that the model is generalizing well to the unseen validation data, although there is still room for improvement particularly in reducing the gap between training and validation accuracy.


Convolutional Neural Network (CNN) Implementation in Deep Learning

Explore