DEV Community

Cover image for Building a Basic Convolutional Neural Network (CNN) in Python
Abhinav Anand
Abhinav Anand

Posted on

Building a Basic Convolutional Neural Network (CNN) in Python

Convolutional Neural Networks (CNNs) are powerful tools for image processing and recognition tasks. They are designed to automatically and adaptively learn spatial hierarchies of features through backpropagation. Let’s dive into building a basic CNN using Python and TensorFlow/Keras.

πŸ“š Prerequisites

Before you begin, ensure you have the following libraries installed:

pip install tensorflow numpy matplotlib
Enter fullscreen mode Exit fullscreen mode

πŸ—οΈ Step 1: Import Necessary Libraries

Start by importing the essential libraries:

import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
Enter fullscreen mode Exit fullscreen mode

🏞️ Step 2: Load and Preprocess the Dataset

For this example, we’ll use the CIFAR-10 dataset, which consists of 60,000 32x32 color images in 10 classes.

# Load the CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

# Normalize the pixel values to be between 0 and 1
x_train, x_test = x_train / 255.0, x_test / 255.0
Enter fullscreen mode Exit fullscreen mode

🧠 Step 3: Build the CNN Model

Now, let’s construct the CNN model. This model will include the key layers: Convolutional, Pooling, and Dense layers.

model = models.Sequential()

# First Convolutional Layer
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))

# Second Convolutional Layer
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))

# Third Convolutional Layer
model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# Flatten the output and add Dense layers
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Step 4: Compile the Model

Compiling the model involves specifying the optimizer, loss function, and metrics to monitor during training.

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

πŸš€ Step 5: Train the Model

Train the CNN model on the training data for a few epochs.

history = model.fit(x_train, y_train, epochs=10, 
                    validation_data=(x_test, y_test))
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Step 6: Evaluate the Model

After training, evaluate the model on the test data to see how well it performs.

test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f'\nTest accuracy: {test_acc}')
Enter fullscreen mode Exit fullscreen mode

πŸ–ΌοΈ Step 7: Visualize Training Results

Finally, let's visualize the accuracy and loss over the training epochs.

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')
plt.show()
Enter fullscreen mode Exit fullscreen mode

πŸ” Conclusion

This basic CNN model serves as a great starting point for tackling image classification tasks. By understanding and modifying this model, you can experiment with different architectures and techniques to enhance your model's performance. Keep exploring and tweaking the layers to build even more powerful neural networks! πŸš€


This code is designed to be easy to follow and modify, making it suitable for beginners and those looking to get started with CNNs in Python.

Blog Link For CNN Architecture :https://dev.to/abhinowww/demystifying-cnn-neural-network-layers-a-deep-dive-into-ai-architecture-12d2

Top comments (2)

Collapse
 
ankush_mahore profile image
Ankush Mahore

informative content

Collapse
 
abhinowww profile image
Abhinav Anand

Thank You