Advanced Applied Deep Learning
Practice Course
Sheng Yun Wu
Practice Course
Sheng Yun Wu
In Week 2, the focus is on building a deeper understanding of Convolutional Neural Networks (CNNs), their architecture, and key layers. This includes learning about convolutional layers, pooling layers, activation functions, and how they come together to create a CNN model for image classification. The examples are designed to progressively build the student's skills by implementing CNNs from scratch and visualizing key components.
Description:
This example introduces the concept of convolutional layers in CNNs. The code demonstrates how convolutional layers are used to extract features from images.
import tensorflow as tf
from tensorflow.keras import layers
# Input image (randomly generated for demonstration)
input_image = tf.random.normal([1, 28, 28, 1])
# Apply a convolutional layer
conv_layer = layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))
output = conv_layer(input_image)
# Show the shape of the output
print(f"Output shape after convolution: {output.shape}")
Description:
Students learn about max pooling layers and how they reduce the spatial dimensions of the feature maps while retaining important features.
# Apply a max pooling layer
pooling_layer = layers.MaxPooling2D((2, 2))
pooled_output = pooling_layer(output)
# Show the shape of the output after pooling
print(f"Output shape after pooling: {pooled_output.shape}")
Description:
In this example, students build their first CNN for image classification using the MNIST dataset.
from tensorflow.keras import models
# Load MNIST dataset
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
# Reshape and normalize the 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
# Build a CNN model
cnn_model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Compile and train the CNN
cnn_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
cnn_model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
Description:
This example demonstrates how to visualize the feature maps produced by the convolutional layers, helping students understand how the model extracts different features from the input image.
# Create a model to extract feature maps
feature_map_model = models.Model(inputs=cnn_model.input, outputs=cnn_model.layers[0].output)
# Predict the feature maps for a single image
feature_maps = feature_map_model.predict(train_images[:1])
# Display the first feature map
import matplotlib.pyplot as plt
plt.imshow(feature_maps[0, :, :, 0], cmap='viridis')
plt.show()
Description:
In this example, students explore the impact of using different activation functions (ReLU, Sigmoid, Tanh) in CNNs.
# Build a CNN with Sigmoid activation
cnn_model_sigmoid = models.Sequential([
layers.Conv2D(32, (3, 3), activation='sigmoid', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='sigmoid'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='sigmoid'),
layers.Dense(10, activation='softmax')
])
# Compile and train the model
cnn_model_sigmoid.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
cnn_model_sigmoid.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
Description:
Students learn how to apply data augmentation techniques (e.g., rotation, flipping, shifting) to increase the diversity of training data and improve model generalization.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Define data augmentation generator
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)
# Train CNN using augmented data
cnn_model.fit(datagen.flow(train_images, train_labels, batch_size=64), epochs=5, validation_data=(test_images, test_labels))
Description:
This example demonstrates how to add batch normalization layers to CNNs to speed up training and stabilize the learning process.
# Build a CNN with batch normalization
cnn_model_bn = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.BatchNormalization(),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.BatchNormalization(),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.BatchNormalization(),
layers.Dense(10, activation='softmax')
])
# Compile and train the model
cnn_model_bn.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
cnn_model_bn.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
Description:
In this example, students explore the impact of using different pooling strategies, such as max pooling and average pooling.
# Build a CNN with average pooling
cnn_model_avg_pool = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.AveragePooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.AveragePooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Compile and train the model
cnn_model_avg_pool.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
cnn_model_avg_pool.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
Description:
This example introduces global average pooling, which reduces the spatial dimensions to a single value per feature map and is often used in modern CNN architectures.
# Build a CNN with global average pooling
cnn_model_gap = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.GlobalAveragePooling2D(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Compile and train the model
cnn_model_gap.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
cnn_model_gap.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
Description:
In this final example for Week 2, students build a deeper CNN by adding more convolutional layers, experimenting with how depth affects model performance.
# Build a deeper CNN model
deep_cnn_model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(128, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Compile and train the model
deep_cnn_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
deep_cnn_model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
Objective: Dive into CNNs and understand their architecture, components, and the impact of different layers on performance.
Skills Developed:
Understanding convolution, pooling, batch normalization, data augmentation, and various activation functions.
Hands-on experience building and experimenting with different CNN architectures.
Tools: TensorFlow, Keras, Matplotlib.
These 10 examples in Week 2 offer a comprehensive introduction to CNN architecture and components, giving students the foundational skills they need to build more complex and effective deep learning models.