Lecture 02 · NN Fundamentals
المحاضرة 02 · أساسيات الشبكات العصبية
Padding, stride and receptive field
الحشو (padding) والخطوة (stride) ومجال الاستقبال (receptive field)
P1 MUSTThree knobs that decide how big the output map is and how much of the image each output can see. None of them adds a single parameter.
ثلاثة مفاتيح تحدد حجم خريطة المخرج، ومقدار ما يراه كل مخرج من الصورة. ولا يضيف أي منها parameter واحداً.
The one output-size formula
صيغة حجم الخرج الوحيدة
Slide 14 gave ho = hi - kh + 1. Slide 16 adds padding p on each side, and slide 18 adds stride s. Put together:
أعطت الشريحة 14 الصيغة ho = hi - kh + 1. ثم تضيف الشريحة 16 الـ padding p على كل جانب، وتضيف الشريحة 18 الـ stride s. وبجمعها معاً:
ho = ⌊ hi + 2p − khs ⌋ + 1
Read it as: the padded input is hi + 2p wide; the kernel's first position uses up kh of that; every further position needs s more cells. The floor says a partial last step is dropped. The same formula holds for width with w and kw. Slide 18 prints it without the floor brackets, but every framework (PyTorch included) floors, and the exam can pick numbers that do not divide.
اقرأها هكذا: المدخل بعد padding عرضه hi + 2p؛ الموضع الأول للـ kernel يستهلك kh منه؛ وكل موضع إضافي يحتاج s خلايا أخرى. والـ floor يعني أن الخطوة الأخيرة الناقصة تُهمَل. والصيغة نفسها تنطبق على العرض باستخدام w وkw. الشريحة 18 تكتبها بدون أقواس floor، لكن كل framework (ومنها PyTorch) يطبّق floor، وقد يختار الامتحان أرقاماً لا تقبل القسمة.
p = 0, s = 1
p = 0, s = 1
hi − k + 1
Slide 14: 4 - 3 + 1 = 2.
الشريحة 14: 4 - 3 + 1 = 2.
padding p, s = 1
حشو p، وs = 1
hi + 2p − k + 1
Slide 16: 5 + 2·1 - 3 + 1 = 5.
الشريحة 16: 5 + 2·1 - 3 + 1 = 5.
padding p, stride s
حشو p، وخطوة s
⌊(hi + 2p − k)/s⌋ + 1
Slide 15: (4 - 2)/2 + 1 = 2.
الشريحة 15: (4 - 2)/2 + 1 = 2.
Padding: keep the map from shrinking
الحشو (padding): يمنع الخريطة من الانكماش
Slide 16: "Padding can be used to keep the output feature map size the same as input feature map size." At stride 1, setting ho = hi in hi + 2p - k + 1 gives 2p = k - 1:
الشريحة 16: "Padding can be used to keep the output feature map size the same as input feature map size." عند stride = 1، إذا وضعنا ho = hi في hi + 2p - k + 1 نحصل على 2p = k - 1:
"same" padding at s = 1: p = k − 12 k = 3 → p = 1, k = 5 → p = 2, k = 7 → p = 3
This is one reason kernels are odd-sized: an even k would need a half pixel of padding on each side, so the padding could not be symmetric.
وهذا أحد أسباب أن أحجام kernels فردية: الـ k الزوجي يحتاج نصف بكسل من padding على كل جانب، فلا يمكن أن يكون padding متماثلاً.
Padding modes
أنماط الحشو
The slide lists zero padding (pads the boundary with 0, the PyTorch default) and three others: reflection, replication and constant. They differ only in which numbers go into the border cells:
تذكر الشريحة zero padding (يملأ الحدود بالصفر، وهو الافتراضي في PyTorch) وثلاثة أنواع أخرى: reflection وreplication وconstant. والفرق بينها فقط في الأرقام التي توضع في خلايا الحدود:
"Padding is a frame, not a filter." It only writes border cells of the input. The kernel, the weights and the bias are untouched, so every padding mode costs zero parameters. What it changes is the output size, and with it the activation count and the MACs.
"الـ padding إطار وليس filter." هو يكتب فقط في خلايا حدود المدخل. لا يمس الـ kernel ولا الأوزان ولا الـ bias، ولذلك كل أنواع padding تكلف صفراً من parameters. ما يغيّره هو حجم المخرج، ومعه عدد activations وعدد MACs.
Stride: downsample inside the network
الخطوة (stride): تقليل الأبعاد داخل الشبكة
Stride s is how many cells the kernel jumps between positions. s = 2 roughly halves each spatial dimension, so it cuts the output area, the activations and the MACs by about 4. The weights are the same weights; only the positions where they are applied change.
الـ stride s هو عدد الخلايا التي يقفزها الـ kernel بين موضع وآخر. s = 2 يقسم كل بعد مكاني على 2 تقريباً، فيخفض مساحة المخرج وactivations وMACs بنحو 4 مرات. الأوزان هي نفسها، والذي يتغير فقط هو المواضع التي تُطبَّق فيها.
h_o = floor((h_i + 2p - k) / s) + 1 (a) 32, k=5, p=2, s=1 (32 + 4 - 5)/1 + 1 = 31 + 1 = 32 same size (b) 4, k=2, p=0, s=2 (4 - 2)/2 + 1 = 1 + 1 = 2 slide 15 (c) 7, k=3, p=0, s=2 (7 - 3)/2 + 1 = 2 + 1 = 3 (d) 6, k=3, p=0, s=2 (6 - 3)/2 + 1 = floor(1.5) + 1 = 2 FLOOR CASE (e) 224, k=7, p=3, s=2 (224 + 6 - 7)/2 + 1 = floor(111.5) + 1 = 112 (f) 227, k=11, p=0, s=4 (227 - 11)/4 + 1 = 54 + 1 = 55 AlexNet conv1
In (d) the kernel fits at columns 1-3 and 3-5. A third position would need columns 5-7, but there is no column 7, so column 6 is never read at all. Writing 2.5 or rounding up to 3 are both wrong.
في الحالة (d) يتسع الـ kernel عند الأعمدة 1-3 و3-5. الموضع الثالث يحتاج الأعمدة 5-7، لكن لا يوجد عمود 7، فيبقى العمود 6 دون أن تقرأه أي نافذة. كتابة 2.5 أو التقريب إلى 3 كلاهما خطأ.
Case (e) is the ResNet stem: 224 → 112. Case (f) is why AlexNet's first map is 55 × 55.
الحالة (e) هي بداية ResNet: 224 → 112. والحالة (f) تفسّر لماذا أول خريطة في AlexNet حجمها 55 × 55.
Conv 3 → 16 channels, 3 × 3 kernel, p = 1, on a 32 × 32 input.
طبقة conv من 3 إلى 16 قناة، kernel بحجم 3 × 3، وp = 1، على مدخل 32 × 32.
| Outputالخرج | Parametersالمعاملات | Activationsالتنشيطات | MACs | |
|---|---|---|---|---|
| s = 1 | 32 × 32 × 16 | 448 | 16,384 | 442,368 |
| s = 2 | 16 × 16 × 16 | 448 | 4,096 | 110,592 |
يقارن هذا الجدول الطبقة نفسها عند stride = 1 وstride = 2. عدد parameters هو 16 × 3 × 9 + 16 = 448 في الحالتين دون أي تغيير. أما المخرج فينكمش من 32 × 32 إلى 16 × 16، فتنخفض activations من 16,384 إلى 4,096، وتنخفض MACs من 442,368 إلى 110,592، أي إلى الربع بالضبط.
params = 16 x 3 x 3 x 3 + 16 = 432 + 16 = 448 (both rows)
s = 2: h_o = floor((32 + 2 - 3)/2) + 1 = floor(15.5) + 1 = 16
MACs = c_o x h_o x w_o x c_i x k x k
s=1: 16 x 32 x 32 x 27 = 442,368
s=2: 16 x 16 x 16 x 27 = 110,592 (exactly 1/4)
Receptive field
مجال الاستقبال (receptive field)
Slide 17: each output element depends on a kh × kw window of its input. Stack another layer and each output now depends on a k-wide window of things that were each k wide, overlapping, so the window on the original image grows. Each successive stride-1 convolution adds k - 1.
الشريحة 17: كل عنصر في المخرج يعتمد على نافذة kh × kw من مدخله. أضف طبقة أخرى فيصبح كل مخرج معتمداً على نافذة عرضها k من عناصر كان كل منها يرى k، مع تداخل، فتكبر النافذة على الصورة الأصلية. كل convolution إضافية بـ stride = 1 تضيف k - 1.
RF (L layers, stride 1) = L · (k − 1) + 1 k = 3: L = 2 → 5, L = 3 → 7
Slide 18 shows what stride does: with stride 2 in the first layer, L = 2 and k = 3 give a receptive field of 7, the same as three stride-1 layers. Neighbouring cells of a strided map sit s input pixels apart, so the next layer's k - 1 extra cells each cover s pixels. The general recurrence:
تعرض الشريحة 18 أثر stride: مع stride = 2 في الطبقة الأولى، تعطي L = 2 وk = 3 مجالاً استقبالياً (receptive field) قدره 7، وهو نفسه ما تعطيه ثلاث طبقات بـ stride = 1. الخلايا المتجاورة في خريطة ذات stride تبعد عن بعضها s بكسل في المدخل، فكل خلية من الخلايا الإضافية k - 1 في الطبقة التالية تغطي s بكسل. والعلاقة العامة:
RFL = RFL−1 + (kL − 1) × ∏j<L sj RF0 = 1
All kernels 3 × 3. Product = product of the strides of all earlier layers.
كل kernels بحجم 3 × 3. والحاصل هو حاصل ضرب strides لكل الطبقات السابقة.
| Strides (L1, L2, L3)الخطوات (L1, L2, L3) | After L1بعد L1 | After L2بعد L2 | After L3بعد L3 |
|---|---|---|---|
| 1, 1, 1 | 1 + 2·1 = 3 | 3 + 2·1 = 5 | 5 + 2·1 = 7 |
| 2, 1, 1 | 1 + 2·1 = 3 | 3 + 2·2 = 7 | 7 + 2·2 = 11 |
| 2, 2, 1 | 3 | 3 + 2·2 = 7 | 7 + 2·4 = 15 |
| 1, 2, 1 | 3 | 3 + 2·1 = 5 | 5 + 2·2 = 9 |
يحسب هذا الجدول receptive field بعد كل طبقة لأربعة ترتيبات من strides. الخلية المظللة هي مثال الشريحة 18: stride = 2 في الطبقة الأولى يرفع RF بعد طبقتين من 5 إلى 7. لاحظ أن stride الطبقة نفسها لا يؤثر على RF الخاص بها، بل على كل الطبقات التي بعدها، ولهذا الصف الأخير (1, 2, 1) يبقى 5 بعد الطبقة الثانية ثم يقفز في الثالثة.
A layer's own stride never changes its own receptive field; it multiplies the growth of every layer after it. That is why the slide's stride-2 example puts the stride in the first layer.
الـ stride الخاص بطبقة ما لا يغيّر أبداً receptive field تلك الطبقة نفسها، بل يضاعف نمو كل طبقة بعدها. ولهذا يضع مثال الشريحة stride = 2 في الطبقة الأولى.
Slide 17 ends on the motivation: "Problem: for large images, we need many layers for each output to 'see' the whole image. Solution: downsample inside the neural network." With stride 1 the RF grows linearly, 2 pixels per 3 × 3 layer, so covering a 224-pixel image would take about 112 layers. Strided convs and pooling make it grow geometrically instead.
تنتهي الشريحة 17 بالدافع: "Problem: for large images, we need many layers for each output to 'see' the whole image. Solution: downsample inside the neural network." مع stride = 1 ينمو RF خطياً، بمقدار 2 بكسل لكل طبقة 3 × 3، فتغطية صورة عرضها 224 بكسل تحتاج نحو 112 طبقة. أما strided convs وpooling فتجعل النمو هندسياً بدلاً من ذلك.
Forgetting the floor. (6 - 3)/2 + 1 is 2, not 2.5 and not 3. Output sizes are integers, and the partial step is dropped.
نسيان floor. (6 - 3)/2 + 1 تساوي 2، وليست 2.5 ولا 3. أحجام المخرج أعداد صحيحة، والخطوة الناقصة تُهمَل.
Using p instead of 2p. p is padding per side; the input grows by 2p. With p = 1 a 5 × 5 input becomes 7 × 7, not 6 × 6.
استخدام p بدلاً من 2p. الـ p هو padding لكل جانب، فيزيد المدخل بمقدار 2p. مع p = 1 يصبح المدخل 5 × 5 بحجم 7 × 7، وليس 6 × 6.
Adding k instead of k - 1. Two 3 × 3 layers see 5, not 6. The first layer gives k; each further layer adds k - 1 (times the earlier strides).
إضافة k بدلاً من k - 1. طبقتان 3 × 3 تريان 5 وليس 6. الطبقة الأولى تعطي k، وكل طبقة إضافية تضيف k - 1 (مضروبة في strides السابقة).
Thinking stride or padding changes the parameter count. Both change only the output size, so they change activations and MACs. Parameters stay co ci kh kw + co.
الظن بأن stride أو padding يغيّر عدد parameters. كلاهما يغيّر حجم المخرج فقط، فيغيّر activations وMACs. أما parameters فتبقى co ci kh kw + co.
Check yourself
اختبر نفسك
A 15 × 15 input goes through a 3 × 3 conv with padding 1 and stride 2. What is the output size?
مدخل 15 × 15 يمر عبر conv 3 × 3 مع padding = 1 وstride = 2. ما حجم المخرج؟
C. (15 + 2 - 3)/2 + 1 = 14/2 + 1 = 8. This one divides exactly. A forgets the +1. B is not a size. D would need stride 1.
C. (15 + 2 - 3)/2 + 1 = 14/2 + 1 = 8. القسمة هنا تامة. الخيار A ينسى +1. والخيار B ليس حجماً أصلاً. والخيار D يحتاج stride 1.
A 10 × 10 input goes through a 3 × 3 conv with no padding and stride 2. What is the output size?
مدخل 10 × 10 يمر عبر conv 3 × 3 بدون padding ومع stride = 2. ما حجم المخرج؟
A. (10 - 3)/2 = 3.5, floor to 3, plus 1 = 4. C is what you get by rounding 3.5 up. D is the stride-1 answer, 10 - 3 + 1.
A. (10 - 3)/2 = 3.5، نأخذ الجزء الصحيح (floor) فيصبح 3، ثم نضيف 1 فيكون 4. الخيار C هو ما تحصل عليه بتقريب 3.5 للأعلى. والخيار D هو إجابة stride 1، أي 10 - 3 + 1.
Which padding keeps a 5 × 5 kernel's output the same size as its input at stride 1, and how many parameters does that padding add?
ما الـ padding الذي يُبقي مخرج kernel بحجم 5 × 5 بنفس حجم المدخل عند stride = 1، وكم parameter يضيف؟
B. p = (k - 1)/2 = (5 - 1)/2 = 2. Padding only fills border cells of the input, so it adds no parameters in any mode (zero, reflection, replication, constant). A confuses 2p = 4 with p.
B. p = (k - 1)/2 = (5 - 1)/2 = 2. الحشو يملأ خلايا حافة الدخل فقط، لذا لا يضيف أي معاملات في أي نمط (zero أو reflection أو replication أو constant). الخيار A يخلط بين 2p = 4 وp.
Two 3 × 3 conv layers are stacked, and the first one has stride 2. What is the receptive field of one output of the second layer?
طبقتا conv 3 × 3 فوق بعضهما، والأولى لها stride = 2. ما receptive field لمخرج واحد من الطبقة الثانية؟
D. RF1 = 3. RF2 = 3 + (3 - 1) × 2 = 7, the slide 18 result. A is the stride-1 answer, L(k - 1) + 1 = 5. B adds k instead of k - 1. C would come from three layers with the stride in the middle (3, 5, 9) or from multiplying by the stride twice.
D. RF1 = 3. RF2 = 3 + (3 - 1) × 2 = 7، وهي نتيجة الشريحة 18. الخيار A هو إجابة stride 1، أي L(k - 1) + 1 = 5. والخيار B يضيف k بدلاً من k - 1. والخيار C يأتي من ثلاث طبقات مع stride في الوسطى (3, 5, 9) أو من الضرب في stride مرتين.
Changing a conv layer's stride from 1 to 2 (padding 1, 3 × 3 kernel, 32 × 32 input) does what to parameters and MACs?
تغيير stride طبقة conv من 1 إلى 2 (مع padding = 1 وkernel 3 × 3 ومدخل 32 × 32) ماذا يفعل بـ parameters وMACs؟
A. The output goes from 32 × 32 to 16 × 16, a quarter of the positions, and each position costs the same MACs, so MACs drop to 1/4. The kernel is the same kernel, so parameters do not move. C halves only one dimension in your head; stride applies to both height and width.
A. ينتقل الخرج من 32 × 32 إلى 16 × 16، أي ربع المواضع، وكل موضع يكلف نفس MACs، فتنخفض MACs إلى 1/4. النواة هي النواة نفسها، فلا تتغير المعاملات. أما الخيار C فينصّف بُعداً واحداً فقط في ذهنك، بينما تُطبَّق stride على الارتفاع والعرض معاً.