3.4.12 · HinglishConvolutional Neural Networks

Image classification pipeline

3,050 words14 min readRead in English

3.4.12 · AI-ML › Convolutional Neural Networks

Overview

Image classification pipeline ek end-to-end workflow hai jo raw pixel data ko predicted class labels mein transform karta hai, convolutional neural networks ka use karke. Is pipeline ko samajhna bahut zaroori hai kyunki yeh reveal karta hai ki CNNs systematically hierarchical features kaise extract karti hain aur decisions kaise leti hain—yeh computer vision systems ki foundation hai.

Figure — Image classification pipeline

[!intuition] Hume Pipeline Ki Zaroorat Kyun Hai?

Raw images sirf numbers ki arrays hoti hain (pixel values). Ek insaan turant "cat" ya "dog" dekh leta hai, lekin ek computer [[123, 45, 67], [89, 201, 34], ...] dekhta hai. Pipeline is gap ko in tareekon se bridge karti hai:

  1. Input ko standardize karna → Saari images same size, normalized values
  2. Features hierarchically extract karna → Edges → Textures → Parts → Objects
  3. Probabilistic decisions lena → "83% dog, 15% cat, 2% wolf"

Key insight yeh hai: Hum manually features engineer nahi karte (jaise "whiskers" ya "floppy ears"). CNN khud seekhti hai ki kaun se features matter karte hain, training data ke through.


[!definition] Pipeline Components

Image classification pipeline in cheezон se milti hai:

  1. Preprocessing Stage: Input images ko resize, normalize, aur augment karo
  2. Feature Extraction Stage: Convolutional + pooling layers hierarchical representations seekhti hain
  3. Classification Stage: Fully connected layers features ko class probabilities mein map karti hain
  4. Postprocessing Stage: Outputs interpret karo, thresholds apply karo, visualize karo

[!formula] Pipeline Mathematics

1. Preprocessing: Normalization

Normalize kyun karein? Neural networks tab zyada fast seekhti hain jab inputs similar scales par hoon (typically [0,1] ya [-1,1]).

First principles se derivation:

Raw pixel values se shuru karo .

Step 1: Mean subtract karo taaki data zero ke around center ho:

Yeh step kyun? Data ko center karta hai taaki positive/negative values balance hoon. Gradient descent ko faster converge karne mein help karta hai (origin ke around symmetric).

Step 2: Standard deviation se divide karo taaki variance scale ho:

Yeh step kyun? Saare features mein variance = 1 bana deta hai. Iske bina, bade values wale features loss function mein dominate karte hain.

Common practice: Transfer learning ke liye ImageNet statistics use karo (, RGB ke liye).


2. CNN Ke Through Forward Pass

Forward pass input ko layers ke through transform karta hai:

Flow ki derivation:

Layer 1-3 (Convolutional blocks): Local features extract karo

Har convolution:

Convolution kyun? Spatial locations ke across weights share karta hai → translation invariance. Edges (layer 1), textures (layer 2), parts (layer 3) seekhta hai.

Pooling: spatial dimensions ko 2× reduce karta hai.

Pooling kyun? (1) Parameters reduce karta hai → less overfitting. (2) Spatial invariance create karta hai → "top-left mein cat" same hai jaise "center mein cat".

Layer 4-5 (Fully connected): Global reasoning

Flatten kyun? Spatial feature maps se feature vector mein transition karta hai classification ke liye.

Output layer: Class logits mein map karo

jahan = number of classes.


3. Softmax: Logits se Probabilities Tak

Softmax function raw scores (logits) ko valid probabilities mein convert karta hai:

First principles se derivation:

Goal: Arbitrary real numbers ko probabilities mein transform karo aise ki:

Step 1: Sab positive banane ke liye exponentiate karo:

Yeh step kyun? sabhi ke liye. Aur, monotonic hai: zyada logit → zyada probability.

Step 2: Sum 1 ho iske liye normalize karo:

Yeh step kyun? Sum se divide karna force karta hai (probability axiom).

ko directly normalize kyun nahi karte? Kyunki softmax differences ko amplify karta hai: agar , toh (strong preference). Linear normalization deta (weak preference).


4. Loss Function: Cross-Entropy

Cross-entropy loss prediction error measure karta hai:

Information theory se derivation:

Step 1: Ek single example ke liye, true label hai (one-hot: , baaki = 0). Hum maximize karna chahte hain.

Kyun? Log probability maximize karna = probability maximize karna (log monotonic hai). Log maths ko aasaan banata hai (gradients mein product → sum).

Step 2: One-hot ke liye, sirf true class contribute karta hai:

Yeh step kyun? Agar , loss = (chhota). Agar , loss = (bada). Galat predictions ko heavily penalize karta hai.

Step 3: Saare examples par average karo:

Average kyun? Loss ko batch size ke relative scale-invariant banata hai.


[!example] Worked Example 1: Single Image Pipeline Ke Through

Problem: Ek 224×224 RGB dog image ko pre-trained ResNet-50 se classify karo.

Step 1 - Preprocessing:

Original image:

ImageNet stats se normalize karo:

Pixel (100, 100) par red channel ke liye:

Yeh step kyun? Us distribution se match karta hai jis par ResNet train hua tha → better transfer learning.

Step 2 - Feature Extraction:

conv1 ke baad (7×7, stride 2): layer1 ke baad (residual blocks): layer2 ke baad: layer3 ke baad: layer4 ke baad:

Dimensions kyun shrink hoti hain? Har pooling/stride spatial size ko 2 se divide karta hai. Depth badhti hai taaki zyada complex features capture ho sakein.

Global average pooling ke baad: → flatten hokar

Global average pooling kyun? 7×7 feature map ko single vector mein reduce karta hai (har channel ke liye ek number). Ab spatial location matter nahi karta—hum care karte hain "kya koi dog hai?" na ki "dog kahan hai?".

Step 3 - Classification:

Fully connected layer: , jahan (ImageNet mein 1000 classes hain).

Maano (logits).

Class 0 (dog) ke liye Softmax:

Agar denominator (sabse bada logit dominate karta hai), toh (8%).

Maano sabse zyada hai → (85% confidence).

Yeh step kyun? Softmax relative logit magnitudes ke according probability mass spread karta hai. Zyada logit → zyada probability.

Step 4 - Postprocessing:

Decision:

Confidence threshold: Agar max probability < 0.7 hai, toh "uncertain" output karo.

Threshold kyun? Out-of-distribution images par overconfident galat predictions rokta hai.


[!example] Worked Example 2: Batch Processing

Problem: 32 images ka ek batch pipeline ke through process karo. Batching computation ko kaise affect karta hai?

Step 1 - Batch Construction:

Individual images: for

Stacked batch:

Batching kyun? (1) GPU parallelism: Saari 32 convolutions simultaneously hoti hain. (2) Stable gradients: 32 examples par average karna noise reduce karta hai.

Step 2 - Forward Pass:

Batch par Convolution:

Yeh step kyun? Same weights saari 32 images par apply hote hain (shared parameters). Output: .

Saari layers ke baad: (32 logit vectors).

Step 3 - Loss Computation:

True label wali image ke liye:

Batch loss:

Yeh step kyun? Batch par average loss backpropagation ke liye stable gradient estimate provide karta hai.

Example numbers: Agar 28/32 images sahi classify hoon (probability > 0.9), 4 galat:

Yeh kyun matter karta hai? Kam loss (< 0.5) achhi performance indicate karta hai. Zyada loss (> 2) kharaab predictions indicate karta hai.


[!mistake] Common Mistakes

Mistake 1: Test Images Ko Normalize Karna Bhool Jaana

Galat approach: Normalized images se train karo, raw [0, 255] pixels se test karo.

Kyun sahi lagta hai: "Model ko koi bhi input handle karni chahiye."

Steel-man: Yeh instinct sahi hai ki models robust hone chahiye, lekin distribution shift learned features ko tod deta hai. Agar training images ka mean 0 aur variance 1 hai, lekin test images ka mean 127 aur variance 1000 hai, toh neurons ko unke training range se bahut bahar inputs milenge. ReLU activations jo 0.5 par fire karna seekhi hain, ab 500 jaise values dekhenge—completely saturate ya die ho jaenge.

Fix: Test time par hamesha same preprocessing pipeline lagao (resize, normalization, augmentation disabled). Training statistics (, ) save karo aur reuse karo.

Code example:

# Training
train_mean = train_data.mean()
train_std = train_data.std()
 
# Testing (CORRECT)
test_normalized = (test_data - train_mean) / train_std

Mistake 2: Cross-Entropy Loss Se Pehle Softmax Apply Karna

Galat approach:

logits = model(x)
probs = softmax(logits)
loss = cross_entropy(probs, y)  # WRONG

Kyun sahi lagta hai: "Cross-entropy ko probabilities chahiye, toh pehle softmax karna chahiye."

Steel-man: Confusion isliye hoti hai kyunki mathematically, cross-entropy probabilities par define hoti hai. Lekin numerically, bade ke liye compute karna overflow cause karta hai. Agar , toh float32 mein represent nahi ho sakta. Frameworks ke andar use hone wala "log-sum-exp" trick raw logits maangta hai.

Fix: cross_entropy_with_logits use karo (PyTorch: F.cross_entropy, TensorFlow: tf.nn.softmax_cross_entropy_with_logits). Yeh functions softmax + log + cross-entropy ko numerical stability ke saath combine karte hain.

Kyun kaam karta hai: Internally compute karta hai:

LogSumExp trick: overflow rokta hai.


Mistake 3: Multi-Label Classification Ke Liye Softmax Use Karna

Galat approach: Image mein "cat" aur "dog" dono hain (multi-label). Softmax use karo → force ho jaata hai.

Kyun sahi lagta hai: "Single-label ke liye softmax kaam kiya, toh multi-label ke liye bhi kaam karega."

Steel-man: Problem softmax ki normalization constraint hai. Agar image mein sach mein cat aur dog dono hain, toh hum chahte hain AUR (dono high). Lekin softmax unhe compete karne par majboor karta hai: agar , toh (kyunki unka sum 1 hona chahiye).

Fix: Har class ke liye independently sigmoid activation use karo:

Ab kuch bhi ho sakta hai (0 se tak). Binary cross-entropy loss use karo:

Example: Image mein cat (label 1) aur dog (label 1) hain. Sigmoid ke saath, , simultaneously predict kar sakte hain.


[!recall]- Twelve Saal Ke Bachche Ko Samjhao

Socho tum ek detective ho jo photos mein animals identify karne ki koshish kar rahe ho. Yeh hai tumhara process:

Step 1 - Photo taiyaar karo: Saari photos same size ki honi chahiye (jaise apne saare Pokémon cards ko same dimensions ka banana). Tum brightness bhi adjust karte ho taaki sab similar dikhein—na bahut dark, na bahut bright.

Step 2 - Clues (features) dhoondo: Pehle, tum simple cheezein notice karte ho jaise edges (koi round shape hai? pointy ears?). Phir tum unhe combine karte ho textures dekhne ke liye (fur? scales?). Phir body parts (chaar pair? ek tail?). Aakhir mein, tum sab jod lete ho: "Yeh ek dog hai!"

Cool baat yeh hai? Computer khud pata lagaata hai ki kaun se clues matter karte hain. Tum use nahi batate "whiskers dekh." Woh practice ke through discover karta hai ki whiskers matlab cat.

Step 3 - Apna guess karo: "Yeh dog hai!" 100% certainty ke saath kehne ki jagah, tum kehte ho "Mujhe 85% yaqeen hai yeh dog hai, 10% lagta hai wolf hai, 5% cat." Yahi softmax karta hai—tumhara confidence percentages mein convert karta hai jo 100% tak add up hote hain.

Step 4 - Galtiyon se seekho: Agar tumne kaha "90% cat" lekin woh actually dog tha, toh system ko bahut bura lagta hai (high loss). Agli baar, woh apni detective skills adjust karta hai us galti se bachne ke liye.

Poori pipeline ek factory assembly line jaisi hai: raw photo → cleaned photo → extracted clues → final answer. Har step ek kaam achhi tarah karta hai, aur milke woh puzzle solve karte hain!


[!mnemonic] PREP-SCORE

Preprocess: Resize aur normalize karo Run forward pass: Conv layers features extract karti hain Exponentiate: Softmax logits ko probabilities mein Predict: Argmax class choose karo

Softmax probabilities ke liye Cross-entropy loss ke liye Optimize backprop se Repeat har batch ke liye Evaluate test set par


Connections

  • Convolutional layers: Core feature extraction mechanism
  • Batch normalization: Deep pipelines mein training stabilize karta hai
  • Data augmentation: Generalization improve karne ke liye preprocessing expand karta hai
  • Transfer learning: Trained pipelines ko naye tasks ke liye reuse karta hai
  • Softmax activation: Logits ko probabilities mein convert karta hai
  • Cross-entropy loss: Classification error measure karta hai
  • Backpropagation: Pipeline weights update karne ke liye gradients compute karta hai
  • ResNet architecture: Skip connections wali ek specific pipeline
  • Multi-label classification: Multiple simultaneous classes ke liye modified pipeline
  • Object detection: Pipeline ko objects localize karne ke liye extend karta hai (sirf classify nahi)

#flashcards/ai-ml

Image classification pipeline ke chaar main stages kya hain? :: 1) Preprocessing (resize, normalize), 2) Feature extraction (conv+pooling layers), 3) Classification (FC layers), 4) Postprocessing (outputs interpret karo)

CNN mein feed karne se pehle images ko normalize kyun karte hain?
Data center karne ke liye (mean 0) aur variance scale karne ke liye (std 1) taaki gradients faster converge karein aur saare features loss mein equally contribute karein, bade raw values wale features ko dominate karne se rokta hai

Softmax function ko is requirement se derive karo ki outputs valid probabilities honi chahiye :: Chahiye P(y_i) ∈ [0,1] aur Σ P(y_i) = 1. Step 1: Positive banane ke liye logits exponentiate karo: ỹ_i = e^(z_i). Step 2: Sum se normalize karo: ŷ_i = e^(z_i) / Σ_j e^(z_j). Yeh dono constraints satisfy karta hai.

Ek single example ke liye cross-entropy loss kya hai aur yeh galat predictions ko heavily kyun penalize karta hai?
L = -log(ŷ_true). Agar ŷ_true = 0.9, L = 0.105 (chhota). Agar ŷ_true = 0.1, L = 2.303 (bada). Log function tezi se badhta hai jab probability 0 ke paas jaati hai, confident galat predictions ko heavily penalize karta hai.
CNN mein jitna deep jaate hain, spatial size kyun ghatti hai aur depth kyun badhti hai?
Har pooling ya strided convolution spatial dimensions ko 2 se divide karta hai, jabki depth (number of filters) badhti hai zyada complex features capture karne ke liye. Early layers: zyada pixels, simple features (edges). Deep layers: kam pixels, complex features (objects).
Classification layer se pehle global average pooling ka kya purpose hai?
7×7×2048 feature map ko ek single 2048-dimensional vector mein reduce karta hai har channel ko average karke. Yeh representation ko spatially invariant banata hai (location matter nahi karta) aur final FC layer ke liye parameters reduce karta hai.
Logits ko cross-entropy loss mein pass karne se pehle softmax kabhi apply kyun nahi karna chahiye?
Numerical stability. Bade z_i ke liye e^(z_i) compute karna overflow cause karta hai. Built-in functions log-sum-exp trick use karti hain: log(ŷ_i) = z_i - LSE(z), jahan LSE(z) = max(z) + log(Σ e^(z_j - max(z))) overflow rokta hai.
Multi-label classification mein softmax aur sigmoid mein kya fark hai?
Softmax Σ ŷ_i = 1 force karta hai (probabilities compete karti hain—agar cat high hai toh dog low hona chahiye). Sigmoid har class ko independently treat karta hai: ŷ_c = 1/(1+e^(-z_c)), multiple high probabilities simultaneously allow karta hai (image cat AUR dog dono ho sakti hai).
Image classification pipeline mein batch processing kyun faydемand hai?
1) GPU parallelism: Batch ki saari images simultaneously process hoti hain. 2) Gradient stability: Multiple examples par loss average karna gradient estimates mein noise reduce karta hai, smoother convergence ki taraf le jaata hai.
Agar training statistics se test images normalize karna bhool jao toh kya hota hai?
Distribution shift learned features ko tod deta hai. Agar training mean=0, std=1 par normalized ho lekin testing raw [0,255] pixels use kare, toh neurons out-of-range inputs receive karte hain (jaise 0.5 ki jagah 500), ReLU saturation ya death cause karta hai. Hamesha same preprocessing saved training μ aur σ ke saath apply karo.

Concept Map

input to

resize normalize augment

formula x minus mu over sigma

conv plus pooling layers

edges to textures to parts

fully connected layers

softmax

interpret and threshold

final output

gives

used in

CNN learns features from

Raw pixel data

Preprocessing stage

Normalized input X

Feature extraction stage

Hierarchical features

Classification stage

Class logits h L

Class probabilities

Postprocessing stage

Predicted class label

Weight sharing

Translation invariance

Training data