Use of CNN in Image Classification

(छवि वर्गीकरण में CNN का उपयोग)


🔶 1. Image Classification क्या है?

📌 परिभाषा:

Image Classification एक ऐसा task है जिसमें input image को एक predefined class में classify किया जाता है।

उदाहरण: एक model को बताना कि image में dog है या cat


🎯 2. CNN क्यों बेहतर है Image Classification के लिए?

CNN में मौजूद:

  • Convolution layers → local patterns और textures पहचानती हैं
  • Pooling layers → size reduce कर feature को concentrate करती हैं
  • Dense layers → final decision लेती हैं

👉 ये सब मिलकर CNN को image data पर बहुत सक्षम बना देती हैं।


🧠 3. Typical Image Classification Pipeline (Using CNN)

[Input Image]

Convolution Layers (Feature Extraction)

ReLU + Pooling Layers

Flatten Layer

Fully Connected (Dense) Layers

Softmax (Output → Class Probabilities)

📷 4. Real-world Examples:

DatasetClassesApplication
MNIST10 (digits)Handwritten digit recognition
CIFAR-1010 (animals, vehicles)Object classification
ImageNet1000+Large-scale classification

🔍 5. Feature Hierarchy in CNN:

Layer DepthLearns What
Shallow (1-2)Edges, corners, color blobs
Mid (3-4)Textures, patterns
Deep (5+)Objects, faces, full shapes

🔧 6. PyTorch Code Example: CNN for Image Classification (CIFAR-10)

import torch.nn as nn

class CIFAR10CNN(nn.Module):
def __init__(self):
super(CIFAR10CNN, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(3, 32, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(32, 64, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Flatten(),
nn.Linear(64 * 8 * 8, 128),
nn.ReLU(),
nn.Linear(128, 10) # 10 output classes for CIFAR-10
)

def forward(self, x):
return self.net(x)

📊 7. Output Layer (Softmax)

nn.Softmax(dim=1)
  • Converts raw outputs (logits) to probabilities
  • Highest probability class is the predicted label

📈 8. Training Process (Overview)

StepDescription
Data PreparationImage resize, normalization
Forward PassPrediction
Loss CalculationCrossEntropyLoss
Backward PassGradients calculate
OptimizationSGD, Adam, etc.
EvaluationAccuracy, Precision, Recall

✅ 9. Advantages of CNNs in Image Classification

BenefitExplanation
Local Feature ExtractionCaptures spatial hierarchy
Translation InvariancePosition of object doesn’t matter
Parameter EfficiencyFilters shared across image
End-to-End LearningNo need for manual feature extraction

📝 Practice Questions:

  1. CNN image classification में कैसे मदद करता है?
  2. एक simple CNN architecture लिखिए जो 10-class classification कर सके।
  3. Feature map क्या होता है?
  4. CNN में object recognition की hierarchy क्या होती है?
  5. PyTorch में prediction probabilities कैसे निकाली जाती हैं?

🎯 Summary:

ConceptUse in Classification
ConvolutionExtract features from images
PoolingDownsample and focus on important parts
Dense LayersFinal decision making
SoftmaxClass probability distribution
CNNEnd-to-end feature learning system

CNN Layers (Convolution, Pooling, Flatten, Dense)

(CNN में Layers कैसे काम करती हैं?)


🔶 1. Overview of CNN Architecture

CNN का उद्देश्य raw images से automatically important features निकालना और उन्हें classification, detection या segmentation के लिए इस्तेमाल करना होता है।

CNN typically निम्नलिखित layers में बँटा होता है:

[Input Image]

Convolution Layer

Activation (ReLU)

Pooling Layer

(Repeat Conv + Pool) ...

Flatten Layer

Dense Layer(s)

Output (e.g. Softmax)

🧩 2. Detailed Explanation of Each Layer


✅ A. Convolution Layer

  • Input image से features extract करता है
  • Multiple filters apply होते हैं
  • Output = Feature Maps

Math: y=w∗x+b

PyTorch:

nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)


✅ B. Activation Function (ReLU)

  • Non-linearity introduce करता है
  • Negative values को 0 कर देता है

f(x)=max⁡(0,x)

PyTorch:

nn.ReLU()

✅ C. Pooling Layer (MaxPooling or AvgPooling)

  • Feature map को compress करता है
  • Important features retain करता है
  • Overfitting reduce करता है

Types:

  • Max Pooling: Max value select करता है
  • Average Pooling: Average लेता है

PyTorch:

nn.MaxPool2d(kernel_size=2, stride=2)

✅ D. Flatten Layer

  • 2D feature maps को 1D vector में convert करता है
  • ताकि Dense Layer उसे process कर सके

Example:

Shape: (Batch, Channels, Height, Width) → (Batch, Features)

PyTorch:

x = x.view(x.size(0), -1)

✅ E. Fully Connected (Dense) Layer

  • Neural network के traditional layer
  • Classification या regression करता है

PyTorch:

nn.Linear(in_features=512, out_features=10)

📊 CNN Architecture Diagram (Example)

Input: 32x32x3 (RGB Image)

Conv (3x3, 16 filters) → 32x32x16

ReLU

MaxPool (2x2) → 16x16x16

Conv (3x3, 32 filters) → 16x16x32

ReLU

MaxPool (2x2) → 8x8x32

Flatten → 2048

Dense → 128

Output → 10 classes

🔧 3. PyTorch Code (Simple CNN Model)

import torch.nn as nn

class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv_layers = nn.Sequential(
nn.Conv2d(3, 16, 3, padding=1), # Conv1
nn.ReLU(),
nn.MaxPool2d(2, 2), # Pool1
nn.Conv2d(16, 32, 3, padding=1),# Conv2
nn.ReLU(),
nn.MaxPool2d(2, 2) # Pool2
)
self.fc_layers = nn.Sequential(
nn.Flatten(),
nn.Linear(32 * 8 * 8, 128), # Flatten to Dense
nn.ReLU(),
nn.Linear(128, 10) # Output
)

def forward(self, x):
x = self.conv_layers(x)
x = self.fc_layers(x)
return x

📈 Summary Table:

LayerPurposeOutput Shape
ConvFeatures extract(H, W, Filters)
ReLUNon-linearitySame as input
PoolingDownsample(H/2, W/2, Filters)
FlattenVector बनाना(1D features)
DensePredict classesOutput classes

📝 Practice Questions:

  1. Convolution layer में filter क्या करता है?
  2. MaxPooling और AveragePooling में क्या फर्क है?
  3. Flatten क्यों ज़रूरी होता है CNN में?
  4. Dense Layer का काम क्या होता है CNN में?
  5. PyTorch में CNN model define करने का तरीका बताइए।

What is Convolution

(कन्वोल्यूशन क्या है?)


🔶 1. Convolution क्या होता है?

Convolution एक mathematical operation है जो दो functions (या arrays) को combine करता है ताकि तीसरा function निकाला जा सके जो दोनों का अर्थपूर्ण interaction बताता है।

Deep Learning में, convolution का उपयोग image से features निकालने के लिए किया जाता है — जैसे edges, patterns, curves आदि।


🧮 Convolution in Math (1D):

जहाँ:

  • x = input
  • w = filter/kernel
  • ∗ = convolution operation

📸 2. Convolution in Images (2D Case)

✅ Image = 2D Matrix of Pixels

✅ Filter/Kernel = Small matrix (e.g. 3×3, 5×5)

Operation:

  • Filter को image के ऊपर slide किया जाता है (called stride)
  • हर जगह पर input और filter के corresponding values multiply और sum होते हैं
  • Result: एक नया feature map तैयार होता है

🧠 Example (3×3 Filter over 5×5 Image):

Input Image (5×5):
1 1 1 0 0
0 1 1 1 0
0 0 1 1 1
0 0 1 1 0
0 1 1 0 0

Filter (3×3):
1 0 1
0 1 0
1 0 1

Apply convolution → Output feature map (3×3)


🔁 Steps:

  1. Filter top-left पर रखें
  2. Overlapping values का element-wise product लें
  3. उनका sum लें → यह output feature का एक value बनेगा
  4. Filter को आगे move करें (stride से)
  5. Repeat until entire image covered

🔍 3. Why Convolution?

FeatureBenefit
LocalityNearby pixels के relationships capture होते हैं
Parameter SharingSame filter पूरे image पर use होता है → कम parameters
Translation InvarianceObject कहीं भी हो, features detect होते हैं

🖼 4. Visual Summary:

[Input Image]

[Filter/Kernel Slides]

[Feature Map Generated]

🔧 5. PyTorch Code Example:

import torch
import torch.nn as nn

conv = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3, stride=1, padding=0)

# Dummy input image: 1 channel, 1 image of size 5x5
input = torch.randn(1, 1, 5, 5)

output = conv(input)
print(output.shape) # → torch.Size([1, 1, 3, 3])

🎯 Summary:

TermMeaning
ConvolutionOperation to extract features
Kernel/FilterSmall matrix that slides over image
Feature MapOutput of convolution
StrideStep size of filter movement
PaddingExtra pixels added to retain size

📝 Practice Questions:

  1. Convolution operation image पर कैसे काम करता है?
  2. Kernel क्या होता है? इसका आकार क्यों छोटा रखा जाता है?
  3. Convolution में parameter sharing का क्या फायदा होता है?
  4. PyTorch में 2D convolution कैसे implement किया जाता है?
  5. Padding और stride का output shape पर क्या असर पड़ता है?

What is Batch Normalization

Batch Normalization (BatchNorm) एक technique है जो training को stabilize, accelerate और improve करने के लिए इस्तेमाल होती है।

यह हर layer के output (activations) को normalize कर देती है ताकि उनका distribution mean=0 और variance=1 के आस-पास रहे।

इससे gradients ज़्यादा smooth होते हैं और training तेज़ होती है।


🔁 2. क्यों ज़रूरी है?

Deep networks में, जैसे-जैसे layers बढ़ती हैं, activations का distribution shift होने लगता है — इस समस्या को कहते हैं:

📉 Internal Covariate Shift

BatchNorm इसका समाधान है — यह हर batch के output को rescale और re-center करता है।


🧮 3. Mathematical Explanation

मान लीजिए किसी layer का output x है।

Step 1: Mean और Variance निकालना

Step 2: Normalize

Step 3: Scale and Shift

यहाँ:

  • γ, β सीखने योग्य parameters हैं
  • ϵएक छोटा constant है stability के लिए

🔧 4. PyTorch Implementation

import torch.nn as nn

model = nn.Sequential(
nn.Linear(128, 64),
nn.BatchNorm1d(64), # BatchNorm for 1D input
nn.ReLU(),
nn.Linear(64, 10)
)

For images, use nn.BatchNorm2d(num_channels)


📈 5. Benefits of BatchNorm

BenefitExplanation
✅ Faster TrainingSmoother gradients → fast convergence
✅ Higher Learning RatesWithout instability
✅ Reduced Need for DropoutActs as light regularizer
✅ Mitigates Vanishing/Exploding GradientsKeeps activations in check
✅ Generalization ImprovesBetter test accuracy

🔍 6. Where to Apply?

TypeApply BatchNorm After
Linear (Dense)Linear → BatchNorm1d → Activation
Conv2D LayerConv2d → BatchNorm2d → Activation

⚠️ 7. Training vs Inference

  • During training → mean & variance per-batch
  • During inference → running average of mean & variance

PyTorch automatically handles this internally using .train() and .eval() modes.


🔁 With and Without BatchNorm (Effect on Accuracy):

EpochWithout BatchNormWith BatchNorm
562%79%
1071%87%
2076%91%

📝 Practice Questions:

  1. Batch Normalization का मुख्य उद्देश्य क्या है?
  2. Internal Covariate Shift किसे कहते हैं?
  3. PyTorch में BatchNorm1d और BatchNorm2d में क्या अंतर है?
  4. BatchNorm में γ और βका क्या role है?
  5. क्या BatchNorm dropout की तरह regularization भी करता है?

🎯 Summary:

FeatureBatchNorm Impact
Stability⬆️ Improves
Speed⬆️ Faster Training
Generalization✅ Helps prevent overfitting
Gradient Flow✅ Prevents vanishing/exploding


Weight Initialization Techniques

(वेट इनिशियलाइज़ेशन तकनीकें)


🔶 1. Weight Initialization क्या है?

📌 परिभाषा:

Weight Initialization का मतलब होता है — training शुरू करने से पहले neural network के weights को कुछ initial values देना।

अगर weights सही से initialize नहीं किए गए, तो training धीमी या पूरी तरह से fail हो सकती है — खासकर deep networks में।


🔁 2. क्यों ज़रूरी है सही initialization?

गलत Initializationसमस्या
सभी weights = 0Neurons same gradient सीखेंगे → symmetry break नहीं होगा
बहुत छोटे weightsGradient vanish होने लगेगा (Vanishing Gradient)
बहुत बड़े weightsGradient explode करने लगेगा (Exploding Gradient)

🔧 3. Common Weight Initialization Techniques


✅ A. Zero Initialization ❌ (Not Recommended)

nn.Linear(128, 64).weight.data.fill_(0)
  • Problem: All neurons learn the same thing → no learning
  • Symmetry नहीं टूटता

✅ B. Random Initialization (Normal/Uniform)

nn.init.normal_(layer.weight, mean=0.0, std=1.0)
nn.init.uniform_(layer.weight, a=-0.1, b=0.1)
  • Random values से symmetry टूटती है
  • लेकिन deep networks में gradient vanish/explode हो सकता है

✅ C. Xavier Initialization (Glorot Initializati

nn.init.xavier_uniform_(layer.weight)

✅ D. He Initialization (Kaiming Initialization)

  • Recommended for ReLU activation
  • Prevents vanishing gradients with ReLU
nn.init.kaiming_normal_(layer.weight, nonlinearity='relu')

📘 PyTorch Implementation

import torch.nn as nn

layer = nn.Linear(128, 64)

# Xavier Init
nn.init.xavier_uniform_(layer.weight)

# He Init (for ReLU)
nn.init.kaiming_normal_(layer.weight, nonlinearity='relu')

📈 Comparison Table:

MethodSuitable ForKeeps VarianceRecommended
ZeroNever
RandomShallow nets
XavierSigmoid/Tanh
HeReLU✅✅✅

🧠 Real-World Tip:

Deep networks trained with improper initialization often show:

  • No learning (loss flat रहता है)
  • NaN losses (gradient explode करता है)
  • Poor accuracy (early layers freeze हो जाते हैं)

📝 Practice Questions:

  1. Weight Initialization क्यों ज़रूरी है?
  2. Xavier Initialization किस प्रकार के activation functions के लिए उपयुक्त है?
  3. He Initialization में variance कैसे decide होता है?
  4. Zero initialization क्यों fail हो जाता है?
  5. PyTorch में He initialization कैसे implement करते हैं?

🎯 Summary:

ConceptExplanation
InitializationTraining से पहले weights की setting
XavierSigmoid/Tanh के लिए best
HeReLU के लिए best
ZeroUse नहीं करना चाहिए