🤖 AI/ML

CNN

Convolutional Neural Network

Convolutional neural network. Specialized for image recognition. Composed of convolution and pooling layers. ResNet and VGG are representative examples.

📖 Detailed Explanation

CNN (Convolutional Neural Network) is a deep learning architecture designed to process grid-structured data such as images, video, and audio. LeNet-5, developed by Yann LeCun in 1998 for handwritten digit recognition, is the origin of CNNs, and in 2012 AlexNet showed overwhelming performance at the ImageNet competition, triggering the deep learning revolution.

The core of a CNN is the convolution operation. A small filter (kernel) slides over the input image to extract features, and in this process it learns visual characteristics such as edges, textures, and patterns hierarchically. Pooling layers reduce the spatial size, which decreases computation and provides translation invariance.

Early layers learn low-level features such as edges and colors, while deeper layers learn high-level features such as eyes, noses, and car wheels. Thanks to this hierarchical feature extraction, CNNs show excellent performance in a variety of computer vision tasks such as image classification, object detection, and segmentation.

In practice, CNNs use pretrained models such as ResNet, VGG, and EfficientNet through transfer learning. Leveraging weights trained on ImageNet makes it possible to achieve high accuracy even with little data, so they are widely applied in medical image analysis, autonomous driving, manufacturing quality inspection, and more.

💻 Code Example

# PyTorch로 간단한 CNN 구현
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__()
        # 합성곱 레이어
        self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
        self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)

        # 풀링 레이어
        self.pool = nn.MaxPool2d(2, 2)

        # 완전 연결 레이어
        self.fc1 = nn.Linear(128 * 4 * 4, 512)
        self.fc2 = nn.Linear(512, num_classes)
        self.dropout = nn.Dropout(0.5)

    def forward(self, x):
        # Conv Block 1: 32x32 -> 16x16
        x = self.pool(F.relu(self.conv1(x)))
        # Conv Block 2: 16x16 -> 8x8
        x = self.pool(F.relu(self.conv2(x)))
        # Conv Block 3: 8x8 -> 4x4
        x = self.pool(F.relu(self.conv3(x)))

        # Flatten
        x = x.view(-1, 128 * 4 * 4)

        # Fully Connected
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)
        return x

# 모델 생성 및 추론
model = SimpleCNN(num_classes=10)
sample_input = torch.randn(1, 3, 32, 32)  # 배치, 채널, 높이, 너비
output = model(sample_input)
print(f"출력 shape: {output.shape}")  # torch.Size([1, 10])

🗣️ Examples of Practical Conversations

In a Technical Meeting

"For the defect detection model, use ResNet-50 as the backbone, replace only the last FC layer to match our number of classes, and fine-tune it. Using ImageNet pretrained weights cuts training time by more than 80%."

Interview Question

"The pooling layer in a CNN has three roles. First, it reduces the feature map size, which decreases computation. Second, it provides translation invariance, so an object is recognized as the same feature even if it shifts slightly. Third, it has a regularization effect that prevents overfitting."

In a code review

"Please add BatchNorm after the Conv layer. Training gets faster and convergence is more stable. Also, stacking small kernels (3x3) multiple times is more parameter-efficient than using one large kernel (7x7)."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Cautions

📊
Input Size Mismatch

Pretrained models expect a specific input size (224x224, 299x299, etc.). If you feed in an image of a different size, a dimension mismatch error occurs at the FC layer, so resizing is required.

💾
GPU Memory Management

A large batch size causes GPU out-of-memory (OOM) errors. When using high-resolution images or deep networks, reduce the batch size or apply gradient checkpointing.

🔄
Data Augmentation Is Essential

CNNs are sensitive to variations in the training data. Training without data augmentation such as rotation, flipping, cropping, and color transformation increases the likelihood of overfitting and degrades generalization performance.

🔗 Related Terms

📚 Learn More