Hello! Welcome to the next lesson in our journey through advanced neural network architectures.
In the past two lessons, we've broken down the fundamental building blocks of Convolutional Neural Networks (CNNs). We first explored the convolutional layer, which acts as a feature detector using shared weights, and then the pooling layer, which downsamples feature maps to reduce dimensionality and create local translation invariance.
Today, we're putting it all together. Your learning goal for this lesson is to assemble a basic Convolutional Neural Network for an image classification task. We will connect the feature extraction components (Conv + Pool) to a standard neural network classifier, and walk through the entire process of building, training, and evaluating the model on a real dataset.
1. The Anatomy of a CNN for Classification
At a high level, a CNN designed for image classification can be split into two main parts: the Feature Extractor and the Classifier.

-
The Feature Extractor (Convolutional Base): This part's job is to process the raw input image and convert it into a set of abstract features. It typically consists of a sequence of:
- Convolutional Layers (
Conv2D): To detect patterns like edges, textures, and shapes. - Activation Functions (
ReLU): To introduce non-linearity. - Pooling Layers (
MaxPooling2D): To make the feature representation smaller and more robust to an object's position.
As we go deeper, the layers learn more complex and abstract features (e.g., from edges to eyes, from eyes to faces).
- Convolutional Layers (
-
The Classifier: This is a standard feed-forward neural network that takes the final, high-level feature maps from the extractor and makes a decision. To do this, we must first:
- Flatten the 2D feature maps into a single 1D vector.
- Pass this vector through one or more Dense (fully-connected) layers to perform the classification. The final layer will have one output neuron for each class.
This elegant structure allows the network to learn the best features for the task on its own, which is why CNNs are so powerful for image-related problems.
2. Building a CNN: A Practical Walkthrough
We'll now build a CNN to classify images from the CIFAR-10 dataset, a classic benchmark dataset containing 60,000 32x32 color images across 10 classes (like 'airplane', 'dog', 'truck').
The following video provides a complete, hands-on demonstration using TensorFlow and Keras. We'll go through it step-by-step. The presenter first builds a simple Artificial Neural Network (ANN) and shows its poor performance, which provides excellent motivation for why a CNN is necessary.
Image classification using CNN (CIFAR10 dataset) | Deep Learning Tutorial 24 (Tensorflow & Python)
Let's begin by watching the first part of 'Image classification using CNN' from the channel codebasics. This section covers loading and preparing the CIFAR-10 data, and then demonstrates the performance of a simple ANN as a baseline.
Please watch from 00:39 to 12:28. Focus on these key steps: Data Loading and Exploration: How the CIFAR-10 dataset is loaded and its structure (x_train, y_train). Data Preprocessing: The crucial step of normalizing pixel values by dividing by 255. Baseline Model: Observe the architecture of the simple ANN and its resulting accuracy (around 47%). This highlights the limitations of using dense networks directly on pixel data.
As you saw, the simple ANN struggles with this image task. The dense layers treat pixels as independent features and lose all spatial information (e.g., that a pixel is next to another pixel). This is where the convolutional base shines.
Now, let's build the actual CNN. The core logic involves stacking the Conv2D and MaxPooling2D layers we've discussed, then attaching the dense classifier.
Image classification using CNN (CIFAR10 dataset) | Deep Learning Tutorial 24 (Tensorflow & Python)
Now for the main event. This next segment of the video shows how to construct the CNN model, compile it, and train it.
Watch from 12:28 to 19:36. Pay close attention to: The CNN Architecture: The sequence of Conv2D, MaxPooling2D, Flatten, and Dense layers. Layer Parameters: The number of filters (32, 64) and the kernel size (3, 3) chosen for the convolutional layers. Compilation: The choice of optimizer (adam) and loss function (sparse_categorical_crossentropy). Training: The fit process and the significant improvement in accuracy compared to the ANN.
The difference is remarkable! With just a few convolutional layers, the accuracy jumps from ~47% to over 70% on the test set. This demonstrates the power of a model architecture designed specifically for the data's structure.
For a clear, text-based reference with all the code in one place, the official TensorFlow tutorial is an excellent resource. It follows the exact same pattern we just saw.
Convolutional Neural Network (CNN) | TensorFlow Core
To solidify your understanding and have a clean code reference, please review the 'Convolutional Neural Network (CNN)' tutorial from TensorFlow.
Read the tutorial from the beginning down to the 'Compile and train the model' section. Notice how the code directly maps to the concepts we've discussed: Load and normalize data. Create the convolutional base (stack of Conv2D and MaxPooling2D). Add the dense layers on top (Flatten, then Dense). Compile the model.
3. Evaluating the Model and Making Predictions
After training, the final step is to evaluate our CNN's performance on the unseen test data and see how it makes predictions.
Image classification using CNN (CIFAR10 dataset) | Deep Learning Tutorial 24 (Tensorflow & Python)
Let's watch the final segment of the codebasics video to see how to evaluate the model and use it to predict the class of a new image.
Watch from 19:36 to 25:48. Focus on: The evaluate method to get the final test accuracy. The predict method, and how the output is a set of probabilities for each class. The use of np.argmax to find the class with the highest probability, which gives the final prediction.
Test your understanding!
You are building a CNN for the CIFAR-10 dataset (10 classes, 32x32x3 images). Your model architecture is:
- A
Conv2Dlayer. - A
MaxPooling2Dlayer. - A
Flattenlayer. - A
Denselayer. - The final output
Denselayer.
What should be the number of output units (neurons) for the final Dense layer and why?
Show answer
The final Dense layer should have 10 output units. This is because there are 10 distinct classes in the CIFAR-10 dataset. Each output unit will correspond to the model's predicted score or probability for one of those classes.
4. An Alternative: The PyTorch Approach
While TensorFlow/Keras provides a very straightforward, sequential way to build models, it's useful to see how another major framework, PyTorch, handles this. PyTorch uses a more object-oriented approach where you define your model as a Python class.
This gives you more explicit control over the data flow, which can be helpful for more complex or custom architectures you might encounter later.
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
To give you a glimpse of an alternative implementation, watch this short clip from Patrick Loeber's 'PyTorch Tutorial 14 - Convolutional Neural Network (CNN)'.
Watch the segments from 06:46-09:36 and 17:18-20:06. You don't need to memorize the code, but notice the key differences: The model is a class that inherits from nn.Module. Layers are defined in the __init__ method. The forward method explicitly defines how an input x passes through each layer. This structure is common across many PyTorch projects.
Conclusion
In this lesson, you have successfully assembled your first complete Convolutional Neural Network. We've seen how to combine convolutional and pooling layers into a feature extractor, and then connect it to a dense classifier to make predictions.
Key Takeaways:
- A typical CNN architecture consists of a convolutional base for feature extraction and a dense classifier for prediction.
- The process involves preparing the data (e.g., normalization), stacking
Conv2DandMaxPooling2Dlayers, flattening the output, and feeding it intoDenselayers. - Frameworks like TensorFlow/Keras make this assembly process very accessible with APIs like
Sequential(). - The spatial feature-learning capability of CNNs makes them far superior to simple ANNs for image classification tasks.
Preview of the Next Lesson:
The simple CNN we built is effective, but as we try to solve more complex problems, we often need deeper networks (with more layers). However, simply stacking more and more layers can lead to a problem called "vanishing gradients," making the network difficult to train. In our next lesson, we will explore a revolutionary idea that solved this: residual connections (ResNets), which enable the training of incredibly deep and powerful networks.