
Kiến trúc và phép tích chập trong CNN
Convolutional Neural Networks (CNNs) have become the backbone of modern computer vision and many other AI applications. This course breaks down the fundamental concepts tested in the quiz…

Vì sao CNN thường xử lý ảnh hiệu quả hơn ANN?
Một ảnh RGB kích thước $32\times32\times3$ đi qua lớp tích chập có 16 bộ lọc, stride 1 và padding giữ nguyên kích thước. Kích thước đầu ra là bao nhiêu?
Kernel $3\times3$ trượt trên ảnh $6\times6$ với stride 1 và không padding. Kích thước feature map là bao nhiêu?
Trong CNN, các giá trị của kernel thường được cập nhật như thế nào?
Ảnh xám được biểu diễn với bao nhiêu kênh trong ví dụ minh họa?
Một lớp tích chập nhận ảnh RGB. Nếu lớp dùng một filter, độ sâu của filter phải tương ứng với điều gì?
Một feature map có giá trị lớn tại một vị trí thường cho biết điều gì?
Trong khối xử lý CNN cơ bản được mô tả, hàm ReLU làm gì với giá trị âm?
Trong kiến trúc phân loại CNN, Softmax chuyển các điểm số thô thành dạng nào?
Understanding Convolutional Neural Networks (CNNs): Architecture and Core Operations
Convolutional Neural Networks (CNNs) have become the backbone of modern computer vision and many other AI applications. This course breaks down the fundamental concepts tested in the quiz "Kiến trúc và phép tích chập trong CNN" and expands them into a comprehensive, SEO‑optimized learning module. By the end of this lesson, you will be able to explain how CNNs process images, calculate output dimensions, and understand the role of kernels, bias, and weight sharing.
1. Why CNNs Outperform Traditional Artificial Neural Networks (ANNs) on Images
Traditional ANNs treat every pixel as an independent feature and connect each input neuron to every neuron in the next layer. This fully‑connected approach quickly becomes computationally expensive and ignores the spatial relationships that are intrinsic to images.
- Local connectivity: Each convolutional filter (kernel) looks at a small, localized region of the image, capturing patterns such as edges or textures.
- Weight sharing: The same set of filter weights is applied across the entire image, dramatically reducing the number of parameters that need to be learned.
- Exploitation of spatial hierarchy: Stacking multiple convolutional layers enables the network to learn increasingly abstract features, from simple edges in early layers to complex objects in deeper layers.
These properties make CNNs more efficient and more accurate for visual tasks compared to a plain ANN that would require a separate weight for every pixel‑to‑neuron connection.
2. The Convolution Operation: From Kernel to Feature Map
A convolutional layer applies a set of learnable kernels (also called filters) to an input image. Each kernel slides (or strides) across the spatial dimensions, performing an element‑wise multiplication followed by a sum. The result at each position is a single value that forms a feature map.
Consider a 3×3 kernel moving over a 6×6 image with a stride of 1 and no padding. The output size for each dimension is calculated as:
(input_size - kernel_size) / stride + 1 = (6 - 3) / 1 + 1 = 4
Thus the feature map size is 4×4. This example mirrors the quiz question about kernel movement and illustrates why padding is often added to preserve spatial dimensions.
3. Padding: Preserving Spatial Dimensions
When a kernel reaches the border of an image, there are fewer pixels to multiply, which reduces the output size. Zero‑padding adds a border of zeros around the input, allowing the kernel to be centered on edge pixels and keeping the output dimensions unchanged.
For instance, an RGB image of size 32×32×3 passed through a convolutional layer with 16 filters, stride 1, and padding that preserves size will produce an output of 32×32×16. The height and width stay at 32 because padding compensates for the kernel’s reduction, while the depth becomes 16—the number of filters applied.
4. Stride: Controlling the Down‑Sampling Rate
The stride determines how many pixels the kernel jumps after each convolution. A stride larger than 1 reduces the spatial resolution of the feature map, effectively performing down‑sampling without a separate pooling layer. While the quiz focused on stride 1, understanding stride is essential for designing architectures such as ResNet or EfficientNet where spatial reduction is strategically placed.
5. Depth of Filters: Matching Input Channels
When a convolutional layer processes a multi‑channel image (e.g., RGB), each filter must have the same depth as the input. This ensures that the filter can interact with every channel simultaneously. In the quiz, the question about a single filter receiving an RGB image highlighted that the filter depth must equal the number of input channels—3 for RGB.
Mathematically, a filter for an RGB image has dimensions k_h × k_w × 3, where k_h and k_w are the kernel’s height and width. The convolution operation then sums across all three channels, producing a single scalar for each spatial location.
6. Bias in Convolutional Layers
After the convolution sum, a bias term is added to each output value. The bias allows the activation to shift, providing the network with additional flexibility. In the quiz, the correct output after adding bias was 2, emphasizing that bias is added **after** the convolution sum, not multiplied or omitted.
Biases are learnable parameters just like the kernel weights, and they are updated during back‑propagation.
7. Learning Kernel Values: Back‑Propagation and Gradient Descent
Kernel weights are not fixed; they are learned from data. During training, the network performs a forward pass to compute predictions, then calculates the loss, and finally propagates the error backward through the network. The gradients with respect to each kernel weight are computed, and an optimizer (e.g., SGD, Adam) updates the weights to minimize the loss.
This learning process is what enables CNNs to automatically discover useful features such as edges, textures, and object parts, without any hand‑crafted engineering.
8. Interpreting Feature Map Values
A high activation value at a particular location in a feature map indicates that the corresponding kernel has detected a strong pattern in that region of the input image. For example, a large value might mean that a vertical edge detector has found a clear edge at that spot. The quiz reinforced this idea by stating that a large feature map value signifies that the kernel has detected a strong feature in the associated image region.
Understanding these activations can also help in visualizing what a CNN has learned, using techniques like activation maximization or Grad‑CAM.
9. Grayscale vs. Color Images: Channel Representation
Grayscale images contain a single intensity channel, while color images (e.g., RGB) contain three separate channels. The quiz asked how many channels a grayscale image uses in the illustration, and the answer was one channel. This distinction is crucial because the depth of the convolutional filters must match the number of input channels, affecting the total number of parameters.
10. Practical Example: Building a Simple CNN Layer
Let’s walk through a concrete example that combines the concepts discussed:
- Input: An RGB image of size
32×32×3. - Layer configuration:
- Number of filters:
16 - Kernel size:
3×3 - Stride:
1 - Padding:
same(adds zeros to keep spatial size)
- Number of filters:
- Output dimensions: Using the formula
(W - K + 2P) / S + 1, whereW=32,K=3,P=1(same padding), andS=1, we get32for both height and width. Depth equals the number of filters, so the output is32×32×16. - Parameters: Each filter has
3×3×3 = 27weights plus one bias, totaling28parameters per filter. For16filters, the layer learns16 × 28 = 448parameters.
This example illustrates how padding, stride, and filter depth interact to shape the network’s architecture.
11. Key Takeaways for Designing CNN Architectures
- Use local connectivity and weight sharing to keep parameter counts low and exploit spatial structure.
- Choose padding wisely:
samepadding preserves dimensions, whilevalidpadding reduces them. - Adjust stride to control down‑sampling; larger strides reduce spatial resolution faster.
- Match filter depth to the number of input channels (1 for grayscale, 3 for RGB).
- Remember that both kernel weights and biases are learned via back‑propagation.
- Interpret high activations as strong feature detections, which can guide model debugging and visualization.
12. Frequently Asked Questions (FAQ)
- Can a CNN work with non‑image data? Yes. Any data that has a grid‑like topology (e.g., audio spectrograms, video frames, or even text embeddings) can be processed with convolutional layers.
- Why do we often add a bias term? Bias allows each filter to learn an offset, enabling the activation to fire even when the weighted sum is near zero, which improves model expressiveness.
- What happens if we omit padding? The output size shrinks with each convolution, which can be desirable for certain architectures but may lead to excessive loss of spatial information.
- How many filters should I use? It depends on the task complexity. Starting with 16–32 filters in early layers and gradually increasing depth in later layers is a common practice.
13. Further Reading and Resources
To deepen your understanding, explore the following resources:
- CS231n Convolutional Neural Networks Lecture
- Deep Learning (Goodfellow, Bengio, Courville)
- Very Deep Convolutional Networks for Large‑Scale Image Recognition (VGG)
- PyTorch CNN Tutorial
By mastering these core concepts, you are now equipped to design, implement, and troubleshoot CNN architectures for a wide range of computer vision problems.
