Module 4 of 25 · TensorFlow & Keras · Intermediate

Convolutional Neural Networks (CNNs)

Duration: 7 min

This module delves into Convolutional Neural Networks (CNNs), a class of deep neural networks most commonly applied to analyzing visual imagery. Understanding CNNs is crucial for tasks like image classification, object detection, and more. This module will cover the architecture of CNNs, how they differ from traditional neural networks, and practical implementation using TensorFlow and Keras.

Understanding CNN Architecture

CNNs are designed to automatically and adaptively learn spatial hierarchies of features from input images. They consist of an input layer, convolutional layers, pooling layers, fully connected layers, and an output layer. Convolutional layers apply a convolution operation to the input, passing the result to the next layer. Pooling layers reduce the spatial size of the representation, reducing the amount of parameters and computation in the network.

import tensorflow as tf
from tensorflow.keras import layers, models

# Building a simple CNN
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

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

Try it in Google Colab: Open in Colab

Model: "sequential"
__________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 32)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 11, 11, 64)       18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)         0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 3, 3, 64)         36928     
_________________________________________________________________
flatten (Flatten)            (None, 576)              0         
_________________________________________________________________
dens (Dense)                  (None, 64)               36928     
_________________________________________________________________
dens_1 (Dense)               (None, 10)               650       
=================================================================
Total params: 92,322
Trainable params: 92,322
Non-trainable params: 0
_________________________________________________________________

Training and Evaluating CNNs

After building a CNN, the next step is to train it using a dataset. The training process involves feeding the network inputs and their corresponding labels, allowing it to learn the weights and biases. Once trained, the model can be evaluated on a separate test dataset to assess its performance. It's important to split your data into training, validation, and test sets to ensure the model generalizes well to unseen data.

import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Loading and preprocessing the MNIST dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# Training the model
history = model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_split=0.2)

# Evaluating the model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f'Test accuracy: {test_acc}')

💡 Tip: When training CNNs, it's crucial to monitor for overfitting, where the model performs well on the training data but poorly on unseen data. Techniques like dropout, data augmentation, and early stopping can help mitigate overfitting.

❓ What is the primary function of convolutional layers in a CNN?

❓ Which technique is commonly used to prevent overfitting in CNNs?

← Previous Continue interactively → Next →

Related Courses