Course Content
Convolutional Neural Networks
CNNs for image processing and computer vision
Introduction
Convolutional Neural Networks (CNNs) are specialized neural networks for processing grid-like data, such as images, and are at the core of many computer vision breakthroughs.
In this tutorial, you will:
β
Understand the key building blocks of CNNs.
β
Learn how CNNs process and learn from image data.
β
Build and train your first CNN using Keras.
Why CNNs for Images?
Images have spatial structure (height, width, channels) that fully connected layers ignore.
CNNs:
β
Capture local patterns using convolutional filters.
β
Use parameter sharing, reducing the number of parameters.
β
Learn translation-invariant features.
Key Components of CNNs
1οΈβ£ Convolution Layers
- Apply filters (kernels) to learn local features.
- Use multiple filters to detect edges, textures, and patterns.
2οΈβ£ Pooling Layers
- Reduce spatial dimensions to lower computation and control overfitting.
- Commonly used: MaxPooling, AveragePooling.
3οΈβ£ Fully Connected Layers
- Used at the end of CNNs for classification.
4οΈβ£ Activation Functions
- ReLU is commonly used for faster training and non-linearity.
Example: CNN on CIFAR-10 Dataset
1οΈβ£ Install and Import Libraries
pip install tensorflow
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt
2οΈβ£ Load and Preprocess Data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Normalize pixel values
x_train, x_test = x_train / 255.0, x_test / 255.0
# Class names for visualization
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
3οΈβ£ Visualize Sample Images
plt.figure(figsize=(10,5))
for i in range(10):
plt.subplot(2,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[i])
plt.xlabel(class_names[y_train[i][0]])
plt.show()
4οΈβ£ Define the CNN Model
model = models.Sequential([
layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
5οΈβ£ Compile and Train the Model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=10,
validation_data=(x_test, y_test))
6οΈβ£ Evaluate the Model
test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test accuracy:", test_acc)
Visualizing Training
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Training vs Validation Accuracy')
plt.show()
Conclusion
β
CNNs effectively learn from image data using convolutional layers.
β
They significantly improve image classification tasks.
β
Keras makes building CNNs approachable for experimentation and learning.
Whatβs Next?
β
Explore advanced architectures (ResNet, VGG, Inception).
β
Learn about data augmentation for improving CNN performance.
β
Understand transfer learning to apply pre-trained models to your projects.
Join our SuperML Community to share your CNN projects and learn advanced deep learning techniques collaboratively.
Happy Learning! πΈ