Exercise: Build Your Own CNN

Learn to implement a simplified VGG-8 architecture.


  • Implement a simplified VGG-8 architecture with batch normalization.

  • Load and preprocess an input image.

  • Pass the input image through the CNN and collect intermediate feature maps.

  • Visualize the feature maps of each ReLU layer in the model.


  • After each Conv2d layer, add the following:

    • A batch normalization layer

    • A ReLU layer

    • A MaxPool2D layer with kernel size and stride = 2

Hints [to build a model from scratch]

  • Refer to the PyTorch documentation for implementing a custom CNN architecture using the nn.Module. You can start with a smaller version of the VGG architecture, known as VGG-8. Use batch normalization after each convolutional layer.

  • Load an image using the PIL.Image.open() function. Preprocess the image using the torchvision.transforms to resize, center-crop, convert to tensor, and normalize it. Create a batch by adding an extra dimension to the input tensor using unsqueeze().

  • Instantiate the VGG-8 model and set it to evaluation mode. Then, loop through each layer of the model’s features’ attribute and pass the input batch through each layer, collecting the intermediate outputs after each ReLU layer.

Get hands-on with 1200+ tech skills courses.