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