Skillversitas

CNN (Convolutional Neural Network)

A CNN is a neural network architecture designed specifically for processing grid data such as images. It uses convolution operations to extract spatial features.

CNN Architecture

Input → [Conv → ReLU → Pool] × N → Flatten → Dense → Output
Layer Function
Convolution Extract local features (edges, textures)
Pooling Dimensionality reduction
Flatten Convert 2D to 1D
Dense Classification

CNN with Keras (MNIST)

from tensorflow import keras
from tensorflow.keras import layers

model = keras.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(64, (3, 3), activation='relu'),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(10, activation='softmax')
])

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

Training on MNIST

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 28, 28, 1).astype('float32') / 255
X_test = X_test.reshape(-1, 28, 28, 1).astype('float32') / 255
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

model = keras.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.Dropout(0.5),
    layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=5, batch_size=128, validation_split=0.1)

loss, acc = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {acc:.4f}")

Pooling Layers

Type Explanation
MaxPooling Take the maximum value in the window
AveragePooling Average within the window
GlobalAveragePooling Average over the entire feature map

Filters and Kernels

from tensorflow import keras
from tensorflow.keras import layers

# Feature map visualization
model = keras.Sequential([
    layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1))
])
model.compile()

# Displaying the filters
weights = model.layers[0].get_weights()[0]
print(f"Filter shape: {weights.shape}")  # (3, 3, 1, 16)

Data Augmentation

from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
    rotation_range=10,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True,
    zoom_range=0.1
)

Transfer Learning

from tensorflow import keras
from tensorflow.keras import layers

base_model = keras.applications.MobileNetV2(
    weights='imagenet',
    include_top=False,
    input_shape=(224, 224, 3)
)
base_model.trainable = False

model = keras.Sequential([
    base_model,
    layers.GlobalAveragePooling2D(),
    layers.Dense(256, activation='relu'),
    layers.Dropout(0.3),
    layers.Dense(num_classes, activation='softmax')
])

CNN Hyperparameters

Parameter Tips
Filter size 3x3 or 5x5 are most common
Number of filters 32, 64, 128 (doubling)
Pooling size 2x2
Stride 1 for conv, 2 for pooling
Padding 'same' to preserve dimensions

Next chapter: RNN, an architecture for sequential data.

Next: RNN →