VGG ও Inception
এই পাঠে যা শিখবেন
- VGG-এর "small kernel + deep" philosophy
- Inception block — multi-branch architecture
- $1 \times 1$ convolution — channel mixing ও reduction
- Auxiliary classifier — gradient flow সাহায্য
- VGG ও Inception PyTorch implement
১ · VGG — উপস্থিতির দর্শন
Karen Simonyan ও Andrew Zisserman (Visual Geometry Group, Oxford) — ImageNet ২০১৪ runner-up। GoogLeNet-এর তুলনায় architecture অনেক simple কিন্তু depth বেশি।
শুধু $3 \times 3$ conv (stride 1, padding 1) এবং $2 \times 2$ max pool (stride 2)। Depth: ১৬টি weight layer।
Block: (Conv 3×3) × n → MaxPool 2×2। Block-এর শেষে channel দ্বিগুণ।
VGG-16 detail:
- Block 1: 2 × Conv(64) → Pool ($224 \to 112$)
- Block 2: 2 × Conv(128) → Pool ($112 \to 56$)
- Block 3: 3 × Conv(256) → Pool ($56 \to 28$)
- Block 4: 3 × Conv(512) → Pool ($28 \to 14$)
- Block 5: 3 × Conv(512) → Pool ($14 \to 7$)
- FC: 4096 → 4096 → 1000
২ · কেন $3 \times 3$ stack-ই যথেষ্ট
দু'টো $3 \times 3$ stack-এর receptive field $5 \times 5$-এর সমান। তিনটি $3 \times 3$ — $7 \times 7$-এর সমান।
- Parameter: ২ × ($3 \times 3$) = ১৮ vs $5 \times 5$ = ২৫ — ২৮% কম।
- Non-linearity: দু'টো ReLU (vs একটি) — expressive power বেশি।
- Regularization: small kernel decompose — implicit constraint।
VGG-এর মূল contribution — "depth works" empirical proof। ১৬ layer test error AlexNet-এর চেয়ে অনেক ভাল।
৩ · VGG-16 — PyTorch
import torch.nn as nn
def vgg_block(in_ch, out_ch, n_conv):
layers = []
for i in range(n_conv):
layers += [
nn.Conv2d(in_ch if i == 0 else out_ch,
out_ch, 3, padding=1),
nn.ReLU(inplace=True),
]
layers.append(nn.MaxPool2d(2, 2))
return nn.Sequential(*layers)
class VGG16(nn.Module):
def __init__(self, num_classes=1000):
super().__init__()
self.features = nn.Sequential(
vgg_block(3, 64, 2),
vgg_block(64, 128, 2),
vgg_block(128, 256, 3),
vgg_block(256, 512, 3),
vgg_block(512, 512, 3),
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(512 * 7 * 7, 4096), nn.ReLU(True), nn.Dropout(0.5),
nn.Linear(4096, 4096), nn.ReLU(True), nn.Dropout(0.5),
nn.Linear(4096, num_classes),
)
def forward(self, x):
return self.classifier(self.features(x))
# torchvision থেকেও available:
# import torchvision.models as m
# vgg = m.vgg16(weights='IMAGENET1K_V1')
# Pretrained — transfer learning-এ excellent
৪ · Inception — Google-এর alternative
Christian Szegedy et al. (Google, ২০১৪) — ImageNet ২০১৪ winner। Architecture বিপরীত philosophy — "wide" with parallel branches।
Same input — চারটি parallel branch:
১) $1 \times 1$ conv
২) $1 \times 1$ → $3 \times 3$ conv
৩) $1 \times 1$ → $5 \times 5$ conv
৪) MaxPool → $1 \times 1$ conv
সব branch-এর output channel-এ concatenate।
Idea: different scale-এর pattern একসাথে capture। $1 \times 1$ small detail, $3 \times 3$ mid, $5 \times 5$ large।
৫ · $1 \times 1$ conv — bottleneck genius
Naive Inception — compute explode। Solution — $1 \times 1$ conv আগে, channel কমিয়ে।
- Input: $28 \times 28 \times 256$।
- Naive $5 \times 5 \times 256 \to 128$: $5 \times 5 \times 256 \times 128 = 819{,}200$ multiply।
- $1 \times 1 \times 256 \to 64$ (bottleneck) → $5 \times 5 \times 64 \to 128$: $256 \times 64 + 5 \times 5 \times 64 \times 128 = 220{,}800$ — ৭৩% কম।
$1 \times 1$ conv — spatial mixing করে না, শুধু channel-এ linear combination + non-linearity। পরে দেখা গেল এটি একটি pixel-wise MLP।
৬ · GoogLeNet — full network
২২ layer। ৯টি Inception block stack। মাত্র ৬M parameter (VGG-16-এর ৪%)। 7×7 stem + Inception blocks + GAP + FC।
import torch
import torch.nn as nn
class InceptionBlock(nn.Module):
def __init__(self, in_ch, b1, b2_red, b2, b3_red, b3, b4):
super().__init__()
self.b1 = nn.Sequential(
nn.Conv2d(in_ch, b1, 1), nn.ReLU(True))
self.b2 = nn.Sequential(
nn.Conv2d(in_ch, b2_red, 1), nn.ReLU(True),
nn.Conv2d(b2_red, b2, 3, padding=1), nn.ReLU(True))
self.b3 = nn.Sequential(
nn.Conv2d(in_ch, b3_red, 1), nn.ReLU(True),
nn.Conv2d(b3_red, b3, 5, padding=2), nn.ReLU(True))
self.b4 = nn.Sequential(
nn.MaxPool2d(3, 1, padding=1),
nn.Conv2d(in_ch, b4, 1), nn.ReLU(True))
def forward(self, x):
return torch.cat([
self.b1(x), self.b2(x), self.b3(x), self.b4(x)
], dim=1)
# Output channel: b1 + b2 + b3 + b4
block = InceptionBlock(192, 64, 96, 128, 16, 32, 32)
x = torch.randn(1, 192, 28, 28)
print("Output:", block(x).shape) # [1, 256, 28, 28]
৭ · Auxiliary classifier — gradient flow
GoogLeNet ২২ layer deep — gradient vanishing risk। Solution — middle layer থেকে auxiliary classifier (mini softmax)। Loss = main + 0.3 × aux। Train-এ gradient সাহায্য, inference-এ disable।
৮ · Inception evolution
- Inception v1 (GoogLeNet): উপরের block।
- Inception v2/v3 (২০১৫): $5 \times 5$ → দুটি $3 \times 3$, BN add।
- Inception v4 + Inception-ResNet (২০১৬): residual connection ও fuse।
- Xception (২০১৬): depthwise separable extreme।
৯ · VGG ও Inception — আজ কোথায়
- VGG: backbone হিসেবে obsolete (ResNet preferred), কিন্তু perceptual loss, style transfer-এ এখনো standard।
- Inception: production system-এ rare, ResNet/EfficientNet beat। কিন্তু $1 \times 1$ bottleneck idea — universal।
- Legacy: ResNet, MobileNet, EfficientNet — সব VGG ও Inception-এর descendant।
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ VGG vs Inception — দু'টো ImageNet ২০১৪ top finisher। আজকের পরিপ্রেক্ষিতে কোনটা "ভাল idea" ছিল? কেন একটা historical curiosity আর অন্যটা concept-wise alive?
Hindsight-এ VGG-এর depth uniformity আর Inception-এর multi-scale + bottleneck — দু'টোর গভীর architectural insight ভিন্ন legacy পেয়েছে।
VGG-এর হালচাল:
- Survives: perceptual loss (style transfer, super-resolution) — VGG feature semantic-rich।
- Pedagogical: CNN basics teach-এ ideal — clean।
- Obsolete: production CNN — ResNet faster, smaller, better।
- Memory heavy: 138M parameter — edge deployment impractical।
Inception-এর living legacy:
- $1 \times 1$ conv: bottleneck universal — ResNet, MobileNet, EfficientNet সবাই use।
- Multi-scale parallel: ASPP (segmentation), feature pyramid — Inception-inspired।
- Wide vs deep: EfficientNet — width scale Inception philosophy।
- Branching: attention-এর precursor।
কেন VGG architecture obsolete:
- Plain stack — vanishing gradient।
- FC layer — overfitting risk।
- No skip — deep training hard।
- No BN — training unstable।
কেন Inception alive:
- Multi-scale diverse pattern capture।
- $1 \times 1$ — channel computation efficient।
- Modular block — extensible।
- Compute/parameter efficient।
Modern Inception-style usage:
- Inception module: SqueezeNet fire module।
- ResNeXt: Cardinality (parallel groups)।
- Xception: Depthwise separable extreme।
- NAS architectures: Multi-branch search space।
VGG-এর niche survival:
- Perceptual loss: $L_{\text{VGG}} = \| \phi(x) - \phi(\hat{x}) \|^2$।
- Style transfer: Gram matrix on VGG features।
- GAN training: feature matching loss।
- Super-resolution: SRGAN VGG loss।
Why VGG features special:
- Hierarchical clean feature।
- Pre-trained millions images।
- Multi-scale capture।
- Standard reproducible।
Production reality check:
- VGG: perceptual loss, image features।
- Inception: GoogLeNet rare, but variants common।
- ResNet: dominant backbone।
- EfficientNet: efficient deployment।
Architectural contributions:
- VGG → "depth + small kernel" template।
- Inception → "multi-branch + bottleneck" template।
- ResNet → "skip + identity" template।
- Each enabled next generation।
Bangladesh use cases:
- Style transfer for art — VGG perceptual loss।
- Image classification — ResNet/EfficientNet।
- Edge device — MobileNet (Inception derivative)।
- Real-time — efficient architecture।
মূল উপলব্ধি: VGG simple but heavy — niche persistence। Inception complex but conceptually rich — multiple legacy। "Better idea" subjective — VGG architectural deadend, Inception concept evolution। Modern CNN — Inception thinking + ResNet skip + efficient design। Bangladesh practical — modern variant adopt, legacy understanding contextual।
প্র ০২ "$1 \times 1$ convolution = pixel-wise MLP" — এই insight কেন powerful? Modern architecture-এ এটা কীভাবে evolve করেছে?
$1 \times 1$ conv — Network in Network (Lin ২০১৪) এবং Inception উভয় paper-এ। DL architecture-এর গভীর insight।
$1 \times 1$ conv কী করে:
- Single pixel position-এ — channel-wise linear combination।
- Spatial mixing zero।
- Per-pixel transformation।
- Followed by activation = MLP per pixel।
Mathematical equivalence:
- Input: $H \times W \times C_{in}$।
- Each pixel — $C_{in}$-D vector।
- $1 \times 1$ conv = $C_{in} \times C_{out}$ matrix multiply।
- = Fully connected per pixel।
Three powerful uses:
- Channel reduction (bottleneck): $256 \to 64 \to 256$ — compute saver।
- Channel expansion: shallow network feature richness।
- Cross-channel mixing: different feature combine।
ResNet bottleneck block:
class Bottleneck(nn.Module):
def __init__(self, in_ch, out_ch):
super().__init__()
mid = out_ch // 4
self.conv1 = nn.Conv2d(in_ch, mid, 1) # 1x1 reduce
self.conv2 = nn.Conv2d(mid, mid, 3, padding=1)
self.conv3 = nn.Conv2d(mid, out_ch, 1) # 1x1 expand
MobileNet inverted bottleneck:
- $1 \times 1$ expand — শুরুতে।
- Depthwise $3 \times 3$ — middle।
- $1 \times 1$ project — শেষে।
- Mobile efficient।
Squeeze-and-Excitation (SE):
- GAP → $1 \times 1$ conv (squeeze) → $1 \times 1$ conv (excite)।
- Channel attention।
- Per-channel weighting learn।
Modern Transformer relevance:
- FFN in Transformer = $1 \times 1$ conv pair।
- Per-token MLP = per-pixel MLP equivalent।
- Linear projection — universal pattern।
Vision Transformer connection:
- Patch embedding = $16 \times 16$ stride 16 conv।
- FFN = $1 \times 1$ conv stack।
- Conv-based ViT possible।
ConvMixer (২০২২):
- Pure conv ViT-style।
- $1 \times 1$ + depthwise।
- Patch + mixing।
- Surprisingly competitive।
Computational cost:
- $1 \times 1$ conv — pure matrix multiply।
- Highly parallelizable।
- GPU-friendly।
- Cheap relative to spatial conv।
Practical implementation:
x_pre = x.permute(0, 2, 3, 1).contiguous() # NHWC
x_flat = x_pre.view(-1, C_in) # (NHW, C)
y_flat = x_flat @ W.T + b # equivalent
y = y_flat.view(N, H, W, C_out).permute(0, 3, 1, 2)
# = nn.Conv2d(C_in, C_out, kernel_size=1)(x)
Theoretical view:
- $1 \times 1$ conv — channel space basis change।
- Subspace projection।
- Feature recombination।
- Universal computation building block।
Bangladesh edge deployment:
- MobileNet — $1 \times 1$ + depthwise।
- Quantization-friendly।
- Mobile inference fast।
- On-device AI practical।
মূল উপলব্ধি: $1 \times 1$ conv — DL architecture-এর Swiss army knife। Channel reduction, expansion, mixing, attention — সব এই simple operation। ResNet bottleneck, MobileNet inverted, SE attention, Transformer FFN — সব আত্মীয়। Modern architecture-এ universal building block। Bangladesh efficient deployment-এ critical। "Channel-wise MLP" — surprising powerful insight।
প্র ০৩ VGG-এর "depth + uniformity" আর Inception-এর "multi-branch" — কোন idea ResNet-এ আরও powerful হয়ে এলো?
ResNet (২০১৫) — VGG ও Inception-এর synthesis + new idea (skip connection)। দু'টোর শক্তি integrate।
VGG থেকে ResNet নিয়েছে:
- Uniform $3 \times 3$ conv: kernel size standardized।
- Depth focus: 152 layer পর্যন্ত।
- Stage-wise design: blocks at multiple resolutions।
- Channel doubling: stride-এ।
Inception থেকে ResNet নিয়েছে:
- $1 \times 1$ bottleneck: ResNet-50+ এ critical।
- Modular block design: repeatable unit।
- Channel efficiency: bottleneck pattern।
- BN + ReLU placement: Inception v3 inspired।
ResNet-এর own innovation:
- Residual connection: $y = F(x) + x$।
- Identity mapping: gradient flow direct।
- 100+ layer practical: deep network train possible।
- BN built-in: training stable।
Why ResNet beat both:
- Deeper than VGG (8x)।
- Smaller than VGG (60% less)।
- Simpler than Inception।
- Better empirical performance।
ResNet-50 architecture:
- Stem: $7 \times 7$ conv stride 2 + maxpool।
- Stage 1: 3 bottleneck blocks (256 channel)।
- Stage 2: 4 blocks (512)।
- Stage 3: 6 blocks (1024)।
- Stage 4: 3 blocks (2048)।
- GAP + FC।
Bottleneck block (Inception heritage):
class Bottleneck(nn.Module):
def __init__(self, in_ch, out_ch, stride=1):
super().__init__()
mid = out_ch // 4
self.conv1 = nn.Conv2d(in_ch, mid, 1)
self.bn1 = nn.BatchNorm2d(mid)
self.conv2 = nn.Conv2d(mid, mid, 3,
stride=stride, padding=1)
self.bn2 = nn.BatchNorm2d(mid)
self.conv3 = nn.Conv2d(mid, out_ch, 1)
self.bn3 = nn.BatchNorm2d(out_ch)
self.shortcut = nn.Conv2d(in_ch, out_ch, 1,
stride=stride) if (in_ch != out_ch or stride != 1) else nn.Identity()
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = F.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
out = out + self.shortcut(x) # residual!
return F.relu(out)
Performance comparison:
- VGG-19: 19.6% top-5 error, 144M params।
- GoogLeNet: 6.67% top-5 error, 6M params।
- ResNet-50: 5.25% top-5 error, 25M params।
- ResNet-152: 4.49% top-5 error, 60M params।
Beyond ResNet:
- DenseNet: all-to-all connection।
- ResNeXt: + Inception cardinality।
- EfficientNet: scale all dimension।
- ConvNeXt: ResNet + Transformer ideas।
Modern lineage:
- VGG → ResNet (depth + uniformity)।
- Inception → ResNet ($1 \times 1$ + multi-branch)।
- ResNet → DenseNet, ResNeXt।
- ResNeXt → SwinTransformer (some ideas)।
The real winner — depth via skip:
- Pre-ResNet — 22 layer deep struggle।
- Post-ResNet — 1000 layer experiment।
- Skip connection enabled depth।
- VGG philosophy victory possible after ResNet।
Bangladesh implication:
- Modern CNN — ResNet derivative।
- Pre-trained widely available।
- Transfer learning standard।
- Edge deployment optimized variants।
Lesson for architects:
- Synthesis powerful than pure invention।
- Multiple ideas combine।
- Add new (skip)।
- Empirical iteration।
মূল উপলব্ধি: ResNet — VGG depth + Inception bottleneck + new skip connection। Synthesis-এর masterpiece। দু'টো previous philosophy outcome integrate। Modern CNN-এর foundation। Bangladesh-এ — ResNet/derivative production standard। Innovation = previous insight + new idea + careful integration। Architecture history continuous evolution।
প্র ০৪ Bangla calligraphy বা art image generation-এ VGG perceptual loss ব্যবহার — এর underlying mechanism কী? কেন GAN-এর সাথে যুক্ত করলে ভাল কাজ করে?
Perceptual loss — generative model-এর breakthrough idea। Pixel-wise loss-এর সীমা ছাড়িয়ে semantic similarity।
Pixel-wise loss-এর সমস্যা:
- L1/L2 loss — pixel difference average।
- Blurry output — smoothing tendency।
- Sharp edge miss।
- Texture detail lose।
VGG perceptual loss:
- Pre-trained VGG feature space-এ comparison।
- $L_{\text{perceptual}} = \| \phi(\hat{y}) - \phi(y) \|^2$।
- $\phi$ = VGG intermediate layer feature।
- Semantic similarity (not pixel)।
Why VGG features work:
- ImageNet trained — diverse pattern।
- Hierarchical feature — low to high level।
- Transferable features।
- Stable trained representation।
Layer choice matters:
- Early layers (conv1, conv2) — texture, edge।
- Mid layers (conv3, conv4) — pattern, parts।
- Late layers (conv5) — semantic, object।
- Multi-layer — comprehensive।
Implementation:
import torchvision.models as m
import torchvision.transforms as T
class VGGPerceptual(nn.Module):
def __init__(self):
super().__init__()
vgg = m.vgg19(weights='IMAGENET1K_V1').features
self.slices = nn.ModuleList([
vgg[:4], # relu1_2
vgg[4:9], # relu2_2
vgg[9:18], # relu3_4
vgg[18:27],# relu4_4
])
for p in self.parameters():
p.requires_grad = False
def forward(self, x):
feats = []
for slc in self.slices:
x = slc(x)
feats.append(x)
return feats
def perceptual_loss(pred, target, vgg):
pred_feats = vgg(pred)
target_feats = vgg(target)
return sum(F.l1_loss(p, t) for p, t in zip(pred_feats, target_feats))
GAN + perceptual combination:
- Pure GAN — unstable, mode collapse risk।
- Pure perceptual — slow convergence।
- Combined — stable + sharp।
- Best of both।
SRGAN (Super-Resolution GAN):
- $L_{\text{total}} = L_{\text{adv}} + \lambda L_{\text{perceptual}}$।
- Perceptual — feature similarity।
- Adversarial — realism।
- Sharp + plausible result।
Style transfer (Gatys et al. ২০১৫):
- Content loss — VGG mid-layer feature।
- Style loss — Gram matrix VGG features।
- Optimization in image space।
- Bangla calligraphy — target style।
Bangla art generation:
- Calligraphy style transfer: existing text → traditional script style।
- Generation: learn Bengali artistic style।
- Restoration: old manuscript enhance।
- Education: teach Bangla art digitally।
Practical Bangla pipeline:
- Collect calligraphy reference images।
- Train style transfer model।
- VGG feature extract — semantic preserve।
- Apply to user text।
Beyond VGG — newer perceptual:
- LPIPS: learned perceptual metric।
- DISTS: structure + texture similarity।
- CLIP-based: language-vision feature।
- DINO-v2: self-supervised feature।
Why VGG persists:
- Standard reproducible।
- Pre-trained widely available।
- Empirically strong।
- Computationally manageable।
Bangladesh creative AI:
- Bangla calligraphy generation app।
- Festival art automation (Pohela Boishakh)।
- Cultural preservation।
- Local artist tool।
Practical considerations:
- VGG feature extraction expensive (use mobile alternative)।
- Layer choice tune per task।
- Loss weight balance critical।
- Validation subjective (human evaluation)।
মূল উপলব্ধি: Perceptual loss — generative AI-এর breakthrough। Pixel similarity beyond — semantic comparison। VGG feature — universal representation। GAN + perceptual — sharp + stable। Bangladesh creative — calligraphy, art, restoration। Modern alternatives existing (LPIPS, CLIP) but VGG persistent। Cultural AI application — perceptual loss key tool।
অনুশীলন
-
Receptive field comparison: তিনটি stacked $3 \times 3$ conv (stride 1)-এর RF কত? কেন এটা একটি $7 \times 7$-এর সমান?
RF: $1 \to 3 \to 5 \to 7$. Three $3 \times 3$ stack — same RF as one $7 \times 7$, কিন্তু parameter ৪৫% কম এবং তিনটি ReLU।
-
Inception channel: আগের code-এর InceptionBlock(192, 64, 96, 128, 16, 32, 32) — total output channel কত?
$b_1 + b_2 + b_3 + b_4 = 64 + 128 + 32 + 32 = 256$।
-
Transfer learning: torchvision-এর VGG-16 load করে — শেষ FC layer-কে ১০ class-এর জন্য replace।
import torchvision.models as m vgg = m.vgg16(weights='IMAGENET1K_V1') # Freeze features for p in vgg.features.parameters(): p.requires_grad = False # Replace classifier head vgg.classifier[6] = nn.Linear(4096, 10) # Now train শুধু classifier
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ২২ · ResNet — skip connection পরবর্তী পাঠ VGG ও Inception-এর successor।
- পাঠ ২০ · LeNet ও AlexNet আগের পাঠ CNN-এর ancient roots।
- পাঠ ২৩ · Transfer learning এই module VGG/Inception pretrained — কীভাবে কাজে লাগাবেন।
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।