পাঠ ১২ · ৩৫-এর মধ্যে · মডিউল ২

VGG — গভীর কিন্তু সরল

VGG — deep but simple, all 3×3 convolutions
৭ মিনিট পড়া মাঝারি · Intermediate PyTorch কোডসহ

এই পাঠে যা শিখবেন

  • VGG-এর uniform architecture philosophy
  • 3×3 conv stacking-এর গাণিতিক যুক্তি
  • VGG-16 ও VGG-19 layer-by-layer
  • VGG-এর modern uses

১ · VGG — Oxford-এর contribution

Karen Simonyan ও Andrew Zisserman, Oxford University Visual Geometry Group (VGG)। ICLR 2015 paper। ILSVRC 2014-এ classification 2nd place (1st GoogLeNet)।

VGG-এর philosophy: "Just go deeper, with the simplest possible building block."

কেন্দ্রীয় ধারণা

Architecture diversity-র বদলে uniformity। প্রতিটি conv 3×3, প্রতিটি pool 2×2। Depth-ই key। সরলতাই beauty।

২ · 3×3 conv stacking-এর math

AlexNet 11×11 ও 5×5 use করেছিল। VGG বলল — না, পরপর 3×3 conv ব্যবহার, একই RF কিন্তু ভাল।

RF calculation:

  • একটি 7×7 conv: RF = 7।
  • তিনটি 3×3 conv: RF = $3 + 2 + 2 = 7$।
  • RF সমান।

Parameter comparison (single channel):

  • 7×7 conv: 49 params।
  • তিনটি 3×3: $3 \times 9 = 27$ params।
  • 45% কম।

Non-linearity:

  • 7×7 + ReLU: 1 ReLU।
  • তিনটি 3×3 + ReLU each: 3 ReLU।
  • Decision boundary বেশি expressive।

৩ · VGG-16 architecture

Input: 224×224×3।

  1. Block 1: Conv64 (3×3) × 2 → MaxPool (2×2) → 112×112×64।
  2. Block 2: Conv128 × 2 → Pool → 56×56×128।
  3. Block 3: Conv256 × 3 → Pool → 28×28×256।
  4. Block 4: Conv512 × 3 → Pool → 14×14×512।
  5. Block 5: Conv512 × 3 → Pool → 7×7×512।
  6. FC layers: 4096 → 4096 → 1000।

Pattern: spatial halve every block, channel double (64 → 128 → 256 → 512 → 512)।

VGG-19: Block 3, 4, 5-এ চারটি conv (extra one)। ~144M parameters।

৪ · কেন এই uniformity?

  • Reasoning simplicity: একটা rule — সব ক্ষেত্রে apply।
  • Search space: hyperparameter কমানো — kernel size fix।
  • Hardware efficiency: 3×3 conv well-optimized in cuDNN।
  • Reproducibility: অন্য researcher easy implement।
VGG = "Lego block" approach — একই block বারবার stack করে tower বানানো। AlexNet ছিল mixed (11×11, 5×5, 3×3)। VGG-র uniform approach পরে ResNet, EfficientNet-এ inherit।

৫ · VGG-এর সমস্যা

  • Parameter heavy: 138M — bulk মূলত FC layer-এ।
  • Memory: training-এ activation 12-14 GB।
  • Slow: deep + dense FC।
  • Vanishing gradient: 19+ layer-এ train difficult — ResNet-এর জন্ম এই সমস্যা থেকে।
  • Single-stream sequential: কোনো branching বা skip — limited representational power।
VGG-16 — uniform 3×3 conv blocks Input 224×224×3 Block1 2×Conv 64 + Pool 112²×64 Block2 2×Conv 128 + Pool 56²×128 Block3 3×Conv 256 + Pool 28²×256 Block4 3×Conv 512 + Pool 14²×512 Block5 3×Conv 512 + Pool 7²×512 FC1 4096 102M FC2 4096 17M FC3 1000 4M VGG-16 — ১৩ conv + ৩ FC = ১৬ layer · ~138M params channels: 64 → 128 → 256 → 512 → 512 (double after each pool) simplicity-এর শক্তি — uniform 3×3 stack এর philosophy
VGG-16 — ৫ Conv block + ৩ FC। Uniform 3×3 conv ও 2×2 pool — সরলতার মধ্যে গভীরতা।

৬ · PyTorch-এ VGG

Python · PyTorch
import torch
from torchvision.models import vgg16, VGG16_Weights

# Pretrained VGG-16
model = vgg16(weights=VGG16_Weights.IMAGENET1K_V1)
model.eval()

x = torch.randn(1, 3, 224, 224)
with torch.no_grad():
    out = model(x)
print("Output:", out.shape)            # (1, 1000)

# Parameter
n = sum(p.numel() for p in model.parameters())
print(f"Parameters: {n:,}")            # ~138M

# Feature extractor (without classifier)
features = model.features              # Sequential conv blocks
with torch.no_grad():
    feat = features(x)
print("Feature map:", feat.shape)      # (1, 512, 7, 7)

    
VGG-এর features অংশ — ImageNet-এ pretrained backbone। Style transfer, perceptual loss, ও ছোট dataset transfer learning-এ এই part-ই use হয়।

৭ · VGG-এর modern legacy

  • Perceptual loss: super-resolution, image generation-এ — VGG feature distance loss।
  • Style transfer: Gatys et al. (২০১৫) — VGG-19-এর Gram matrix।
  • SSIM/LPIPS metric: VGG-feature-based perceptual similarity।
  • Pre-trained backbone: ছোট dataset detection-এ Faster R-CNN VGG-16।
  • Pedagogical: "first deep CNN" tutorial-এ।

৮ · VGG vs পরবর্তী

  • VGG vs ResNet-50: RN-50 25M params (5x কম), 76% top-1 (3% better), 4x faster।
  • VGG vs EfficientNet-B0: EN-B0 5M params, 77% top-1, mobile-runnable।
  • VGG vs ConvNeXt: CN-T 28M, 82%, modern design।

৯ · VGG-এর শিক্ষা

  • Simplicity scales: uniform pattern + depth = good performance।
  • Small kernel preferred: 3×3 modern CNN-এর default।
  • Double channels at downsample: ResNet, EfficientNet-এ continue।
  • Pre-trained backbone: transfer learning-এর philosophy।
VGG আজ "old" — কিন্তু its philosophy ResNet, ConvNeXt, MobileNet-এ embedded। 3×3 conv-এর dominance VGG থেকে।

ভাবনার প্রশ্ন

প্র ০১ VGG-19 (১৯ layer) VGG-16-এর তুলনায় সামান্য better, কিন্তু VGG-22, 26 — সরাসরি deeper করলে accuracy কমে। কেন? এই observation-ই কি ResNet-এর জন্ম?

এটি DL-এর সবচেয়ে impactful empirical observation।

Degradation problem:

  • VGG-16: 71.5% top-1।
  • VGG-19: 71.8%।
  • Hypothetical VGG-30: 70% (worse!)।
  • Counter-intuitive — deeper should be better (more capacity)।

Cause analysis:

  • Vanishing gradient: backprop chain rule — deep layer-এ tiny gradient।
  • Optimization difficulty: deep network-এর loss landscape rugged।
  • Identity mapping hard: network যদি last layer "pass-through" শেখাতে চায়, weight tune করতে কঠিন।

Critical insight (He et al., 2015):

  • "যদি deep network-এর extra layer identity হত — তাহলে accuracy অন্তত equal থাকতো।"
  • "যেহেতু worse, optimization fail করছে identity শেখাতে।"
  • "সমাধান: identity-কে easy default বানানো।"

ResNet-এর solution:

$$y = F(x) + x$$

  • Skip connection — input directly add।
  • $F(x) = 0$ → identity (default)।
  • Network only learns "residual"।
  • Gradient skip-এ flow — vanishing solve।

Result:

  • ResNet-152 (১৫২ layer) — train possible।
  • VGG-style 152 layer — diverge।
  • ImageNet 2015 winner: ResNet 3.57% error (vs VGG 7.3%)।

Subsequent insights:

  • BatchNorm — independent fix to vanishing gradient।
  • Highway Networks (gated skip) — slightly earlier।
  • DenseNet — every layer connected to all previous।

Modern best practice:

  • Deep network-এ skip connection essential।
  • Transformer-এ residual connection — same idea।
  • EfficientNet, ConvNeXt — সব use করে।

মূল উপলব্ধি: VGG-র degradation-ই DL-এর সবচেয়ে impactful empirical paradox। সমাধান (skip connection) — architecture design-এর central principle।

প্র ০২ VGG-এর FC layer-এ 102M parameters। FC remove করে GAP দিয়ে replace করলে accuracy কতটা impacted? Modern ResNet তো এটাই করে।

এটি network-in-network paper (Lin et al., 2013) থেকে শুরু architecture trend।

VGG FC vs GAP:

  • VGG FC1: 7×7×512=25088 → 4096 = 102M params।
  • GAP-equivalent: 7×7 spatial average → 512 vector → 1000 = 0.5M params।
  • 200x reduction।

Accuracy impact:

  • "VGG-GAP" — 71.5% → 70% (1.5% drop)।
  • Negligible loss compared to enormous parameter saving।
  • Modern architectures — GAP standard।

কেন GAP কাজ করে:

  • Final feature map already abstract — spatial average meaningful।
  • Implicit regularization — each class probability spatial average।
  • "Class activation map" naturally — interpretable।

FC-এর role যা GAP-এ লস:

  • Spatial position-aware classification — যদি object position matter।
  • Complex feature combination।
  • আজ — এই extra capacity রক্ষায় conv depth বাড়ানো better।

Variants:

  • GAP + linear: ResNet, MobileNet।
  • GAP + small FC (256-512): EfficientNet।
  • GMP (max): some networks।
  • GAP + GMP concat: detail + summary।
  • Adaptive pool: variable input size।

Input size flexibility:

  • VGG: fixed 224×224 — FC requires fixed dim।
  • GAP: any size — adaptive avg pool spatial dim collapse।
  • Detection/segmentation-এ huge benefit।

Modern hybrid:

  • ConvNeXt: GAP + LayerNorm + linear।
  • Swin Transformer: similar।
  • ViT: CLS token (different paradigm)।

মূল উপলব্ধি: "Big FC = bad" আজকের consensus। VGG-এর historical importance — এটি দেখাল FC-এর problem। Modern minimalist heads industry standard।

প্র ০৩ VGG-19-এর Gram matrix style transfer-এর foundation। Gram matrix ঠিক কী, কেন style capture করে?

Gatys, Ecker & Bethge (২০১৫) — neural style transfer। Internet-এ viral। VGG-এর role essential।

Gram matrix definition:

  • Feature map $F \in \mathbb{R}^{C \times HW}$ (channel × spatial flatten)।
  • Gram matrix $G = F F^T \in \mathbb{R}^{C \times C}$।
  • Element $G_{ij} = \sum_k F_{ik} F_{jk}$ — channel $i$ ও $j$-এর correlation।

Spatial information removal:

  • Gram = inner product across spatial location।
  • Feature কোথায় (where) discard, কী (what) preserve।
  • Texture-এ "where" matter করে না — "what pattern" matters।

Style = correlation pattern:

  • Brushstroke — local texture pattern।
  • VGG layer-এ — channel-i "edge" detect, channel-j "color blob" detect।
  • Their co-occurrence (correlation) = style signature।
  • Van Gogh-এর painting-এ specific pattern co-activation।

Style transfer algorithm:

  1. Content image $C$, style image $S$, output $O$।
  2. Content loss: $\| F^L(O) - F^L(C) \|^2$ — high-level VGG feature match।
  3. Style loss: $\sum_l \| G^l(O) - G^l(S) \|^2$ — Gram matrix match across layer।
  4. $O$ initialize random, optimize via gradient descent।

VGG-এর role:

  • Pretrained on ImageNet — diverse natural pattern।
  • Hierarchical features — low-level texture থেকে high-level object।
  • Layer-wise style match → multi-scale texture transfer।

Modern alternative:

  • Fast NST (Johnson et al.): feedforward network train — realtime।
  • AdaIN (Huang & Belongie): adaptive instance normalization।
  • Stable Diffusion: text-guided style transfer।
  • VGG-এর role-ও সরে যাচ্ছে — কিন্তু perceptual loss-এ এখনো standard।

Bangladesh-এ application:

  • Local artist (S.M. Sultan, Zainul Abedin) style transfer app।
  • Cultural heritage digitization।
  • Educational tool — art history visualize।

মূল উপলব্ধি: Mathematical elegance — Gram matrix-এর simplicity-এ texture-এর semantic meaning। CV-র creative application-এর gateway।

প্র ০৪ VGG-16 ২০১৪-এর। ১২ বছর পর — কোনো production system এ এখনো VGG ব্যবহার করার valid reason আছে?

Pragmatically — কিছু niche-এ VGG এখনো relevant।

Cases যেখানে VGG পছন্দ:

  • Perceptual loss: super-resolution, image-to-image translation। VGG feature matching standard।
  • Style transfer: Gatys-style — VGG-19 still gold।
  • SSIM/LPIPS: perceptual quality metric — VGG-based।
  • Legacy systems: pre-2018 production — migration cost।
  • Educational: simplicity teaches CNN concepts।

Cases যেখানে VGG NOT okay:

  • Inference latency critical: mobile, edge — MobileNet, EfficientNet better।
  • Memory limited: 138M parameters massive।
  • Modern accuracy bar: VGG 71% vs ConvNeXt 84% — 13% gap।
  • Detection/segmentation: ResNet/EfficientNet backbone better।

Specific applications still using VGG:

  • SRGAN, ESRGAN — super-resolution generator+discriminator perceptual loss।
  • Pix2Pix, CycleGAN — feature matching loss।
  • GauGAN, StyleGAN — analysis network।
  • Few medical research codebase — historical।

Modern alternative for similar role:

  • Perceptual loss: EfficientNet, CLIP feature also work।
  • LPIPS update: AlexNet/VGG/SqueezeNet variants।
  • DINO feature: self-supervised, often better।

Bangladesh context:

  • Limited GPU access — VGG inference slow।
  • EfficientNet-B0 better starting point।
  • Research tutorial-এ VGG OK for clarity।

Migration recommendation:

  • Start: ResNet-50 (general)।
  • Mobile: MobileNetV3, EfficientNet-Lite।
  • Accuracy: ConvNeXt, EfficientNetV2।
  • Foundation: CLIP, DINOv2 features।

মূল উপলব্ধি: Architecture aging — like programming language। VGG-এর design principle alive (3×3, deep, simple)। Specific weights-এর use case narrow। Engineer-এর judgment crucial।

অনুশীলন

  1. Param count: VGG-16 Block 1-এর প্রথম conv (3 → 64, 3×3, padding=1)। Param কত?

    $3 \times 64 \times 3 \times 3 + 64 = 1{,}792$।

  2. RF check: VGG Block 5-এর শেষ conv-এর approximate receptive field?

    Each pool RF doubles। 5 pool 32x downsample। Conv-এর accumulated effect — RF প্রায় ২১২×২১২ — ছবির বেশিরভাগ। তাই global context captured।

  3. ভাবুন: VGG-16 vs MobileNet-V3 — accuracy কাছাকাছি কিন্তু MobileNet 25x ছোট। Mobile inference-এ কোনটা?

    অবশ্যই MobileNet-V3। 5M params, ARM-friendly depthwise separable conv, optimized squeeze-and-excitation। Real device-এ 10-20x faster।

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

কোড রানার কাজ না করলে? ব্রাউজারে কাজ না করলে Google Colab ব্যবহার করুন — Google-এর ফ্রি অনলাইন Python পরিবেশ, শুধু Gmail অ্যাকাউন্ট লাগে।
পূর্ববর্তী পাঠ
পাঠ ১১ · AlexNet