CS5720 - Week 4
Slide 78 of 80

Building Your First CNN

Step-by-Step Guide

  • 1
    Define the Problem
    Identify your task: classification, detection, or segmentation
  • 2
    Prepare Your Data
    Load, preprocess, and split your image dataset
  • 3
    Design Architecture
    Choose layers: Conv → ReLU → Pool → FC
  • 4
    Implement the Model
    Code your CNN using PyTorch or TensorFlow
  • 5
    Train and Validate
    Train your model and monitor performance
  • 6
    Test and Deploy
    Evaluate on test set and deploy if ready

Simple CNN Architecture

Input
Image Data
32×32×3
Conv2D
32 filters, 3×3, ReLU
30×30×32
MaxPool
2×2 pool size
15×15×32
Conv2D
64 filters, 3×3, ReLU
13×13×64
MaxPool
2×2 pool size
6×6×64
Flatten
Reshape to 1D
2304
Dense
10 units, Softmax
10

Complete CNN Implementation

# PyTorch Implementation import torch import torch.nn as nn import torch.nn.functional as F class SimpleCNN(nn.Module): def __init__(self, num_classes=10): super(SimpleCNN, self).__init__() # First convolutional block self.conv1 = nn.Conv2d(3, 32, kernel_size=3) self.pool1 = nn.MaxPool2d(2, 2) # Second convolutional block self.conv2 = nn.Conv2d(32, 64, kernel_size=3) self.pool2 = nn.MaxPool2d(2, 2) # Fully connected layers self.fc1 = nn.Linear(64 * 6 * 6, 128) self.fc2 = nn.Linear(128, num_classes) self.dropout = nn.Dropout(0.5) def forward(self, x): # First conv block: Conv → ReLU → Pool x = self.pool1(F.relu(self.conv1(x))) # Second conv block: Conv → ReLU → Pool x = self.pool2(F.relu(self.conv2(x))) # Flatten for fully connected layers x = x.view(-1, 64 * 6 * 6) # Fully connected layers with dropout x = F.relu(self.fc1(x)) x = self.dropout(x) x = self.fc2(x) return x # Create model instance model = SimpleCNN(num_classes=10) print(model)
# TensorFlow/Keras Implementation import tensorflow as tf from tensorflow.keras import layers, models def create_simple_cnn(num_classes=10): model = models.Sequential([ # First convolutional block layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), layers.MaxPooling2D((2, 2)), # Second convolutional block layers.Conv2D(64, (3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), # Flatten and fully connected layers layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dropout(0.5), layers.Dense(num_classes, activation='softmax') ]) return model # Create and compile model model = create_simple_cnn() model.compile( optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] ) # Display model summary model.summary()
# Complete Training Loop import torch.optim as optim from torchvision import datasets, transforms # Data preprocessing transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) # Load CIFAR-10 dataset train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True) # Initialize model, loss, and optimizer model = SimpleCNN(num_classes=10) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.001) # Training loop model.train() for epoch in range(10): running_loss = 0.0 for i, (inputs, labels) in enumerate(train_loader): # Zero gradients optimizer.zero_grad() # Forward pass outputs = model(inputs) loss = criterion(outputs, labels) # Backward pass and optimize loss.backward() optimizer.step() running_loss += loss.item() if i % 100 == 99: print(f'Epoch {epoch+1}, Batch {i+1}, Loss: {running_loss/100:.3f}') running_loss = 0.0 print('Training completed!')
Expected Output
Epoch 1, Batch 100, Loss: 2.234
Epoch 1, Batch 200, Loss: 1.987
Epoch 1, Batch 300, Loss: 1.756
...
Epoch 10, Batch 700, Loss: 0.456
Training completed!
Prepared by Dr. Gorkem Kar