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 पर क्या असर पड़ता है?