Home  >  Article  >  Backend Development  >  Building a Basic Convolutional Neural Network (CNN) in Python

Building a Basic Convolutional Neural Network (CNN) in Python

WBOY
WBOYOriginal
2024-08-28 18:33:07870browse

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

?️ 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

?️ 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

? 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'))

? 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'])

? 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))

? 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}')

?️ 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()

? 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

The above is the detailed content of Building a Basic Convolutional Neural Network (CNN) in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn