DEV Community

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

Posted on

7 1 1 1 1

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

Image of Timescale

Timescale โ€“ the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (2)

Collapse
 
ankush_mahore profile image
Ankush Mahore โ€ข

informative content

Collapse
 
abhinowww profile image
Abhinav Anand โ€ข

Thank You

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

๐Ÿ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay