Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Comprehensive Tutorial on Convolutional Neural Networks (CNN)

Introduction to Convolutional Neural Networks

Convolutional Neural Networks (CNNs) are a class of deep neural networks that have proven to be extremely effective for image recognition and classification tasks. They are designed to process data with a grid-like topology, such as images, and are inspired by the visual cortex of animals.

Basic Building Blocks of CNN

CNNs are composed of several key layers:

  • Convolutional Layer: This layer applies a set of convolutional filters to the input image to produce feature maps.
  • Activation Layer: Typically uses the ReLU activation function to introduce non-linearity.
  • Pooling Layer: This layer reduces the dimensionality of feature maps, typically using max pooling.
  • Fully Connected Layer: These layers are used at the end of the network to perform classification.

Step-by-Step Example

Let's build a simple CNN to classify images from the MNIST dataset using Python and Keras.

Step 1: Import Libraries

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

Step 2: Load and Prepare Data

# Load data
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()

# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0

# Reshape data to add a single channel
train_images = train_images.reshape((train_images.shape[0], 28, 28, 1))
test_images = test_images.reshape((test_images.shape[0], 28, 28, 1))

Step 3: Build the CNN Model

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

Step 4: Compile the Model

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

Step 5: Train the Model

history = model.fit(train_images, train_labels, epochs=5, 
                    validation_data=(test_images, test_labels))

Step 6: Evaluate the Model

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

Conclusion

In this tutorial, we have covered the basics of Convolutional Neural Networks and walked through a step-by-step example of building a simple CNN using the Keras library. CNNs are powerful tools for image classification and have numerous applications in the field of computer vision.