Lecture 02 · NN Fundamentals

المحاضرة 02 · أساسيات الشبكات العصبية

Pooling and the CNN feature hierarchy

التجميع (pooling) وتسلسل الخصائص في CNN

Slides 21–26الشرائح 21–26 P2 SHOULD

Pooling shrinks every activation map, leaves the channel count alone, and has nothing to learn. Two hyperparameters, one formula, one 4 x 4 hand calculation, and you have the whole topic.

Pooling يصغّر كل خريطة تنشيط، ويترك عدد القنوات كما هو، وليس فيه أي شيء يُتعلَّم. معاملان، ومعادلة واحدة، وحساب يدوي واحد على شبكة 4 x 4، وبهذا تكون قد غطّيت الموضوع كله.

What pooling does

ماذا يفعل التجميع (pooling)

Slide 21: pooling "makes the representations smaller and more manageable" and "operates over each activation map independently". A 224 x 224 x 64 volume pooled with a 2 x 2 window and stride 2 becomes 112 x 112 x 64. Height and width halve; the 64 channels pass straight through.

الشريحة 21: pooling "يجعل التمثيلات أصغر وأسهل في التعامل"، و"يعمل على كل خريطة تنشيط بشكل مستقل". حجم 224 x 224 x 64 بعد pooling بنافذة 2 x 2 وstride يساوي 2 يصبح 112 x 112 x 64. الطول والعرض ينتصفان، والقنوات الـ 64 تمر كما هي.

The arithmetic

الحساب

Slide 22: input W1 x H1 x C, two hyperparameters (spatial extent F and stride S), output W2 x H2 x C.

الشريحة 22: الدخل W1 x H1 x C، ومعاملان (spatial extent F وstride S)، والخرج W2 x H2 x C.

W2 = (W1 − F)/S + 1     H2 = (H1 − F)/S + 1
channels out = C     parameters = 0

It is the conv output formula with no padding. There are no weights and no bias: max and mean are fixed functions, so the parameter count is exactly zero and nothing is stored in flash for a pooling layer.

هي معادلة خرج conv نفسها بدون padding. لا توجد أوزان ولا bias: فالـ max والمتوسط دالتان ثابتتان، ولذلك عدد parameters صفر بالضبط ولا يُخزَّن شيء في flash لطبقة pooling.

Workedمثال محلول
slide 21:  224 x 224 x 64,  F = 2, S = 2
  W2 = (224 - 2)/2 + 1 = 111 + 1 = 112
  out = 112 x 112 x 64

activations before = 224 x 224 x 64 = 3,211,264
activations after  = 112 x 112 x 64 =   802,816
kept 1 of every 4 values: 75% of activations dropped

AlexNet overlapping pool:  55 x 55 x 96,  F = 3, S = 2
  W2 = (55 - 3)/2 + 1 = 26 + 1 = 27
  out = 27 x 27 x 96

Slide 22 by hand: the 4 x 4 slice

الشريحة 22 باليد: الشريحة 4 x 4

One depth slice, F = 2, S = 2. The four 2 x 2 windows tile the input without overlap.

شريحة عمق واحدة، F = 2 وS = 2. النوافذ الأربع 2 x 2 تغطي الدخل بدون تداخل.

1124 5678 3210 1234 SINGLE DEPTH SLICE MAX 2x2, S = 2 MEAN 2x2, S = 2 68 34 3.255.25 22 keeps the strongest response per window blends all four values per window
Slide 22. Each colour is one window. The output is 2 x 2 because (4 − 2)/2 + 1 = 2.
الشريحة 22. كل لون نافذة واحدة. الخرج 2 x 2 لأن (4 − 2)/2 + 1 = 2.
Workedمثال محلول
input slice
  1 1 | 2 4
  5 6 | 7 8
  ----+----
  3 2 | 1 0
  1 2 | 3 4

window        values       max   mean
top-left      1 1 5 6       6    13/4 = 3.25
top-right     2 4 7 8       8    21/4 = 5.25
bottom-left   3 2 1 2       3     8/4 = 2
bottom-right  1 0 3 4       4     8/4 = 2

max  = [[6, 8], [3, 4]]
mean = [[3.25, 5.25], [2, 2]]

Max vs average, and what pooling buys

Max مقابل المتوسط، وماذا يكسبنا التجميع

Slide 21 says max pooling "introduces spatial invariance". If the strongest response moves by one pixel but stays inside the same window, the max is unchanged. That is an approximate invariance to small shifts only; a shift that crosses a window boundary changes the output.

الشريحة 21 تقول إن max pooling "يُدخل spatial invariance". فإذا تحركت أقوى استجابة بكسلاً واحداً لكنها بقيت داخل النافذة نفسها، تبقى قيمة max كما هي. وهذه ثباتية تقريبية أمام الإزاحات الصغيرة فقط، فالإزاحة التي تعبر حدود النافذة تغيّر الخرج.

The slide also claims max pooling "performs a lot better" than average because it discards noisy activations. Treat that as a common heuristic, not a law. Many modern networks replace pooling in the trunk with strided convolution, and use global average pooling at the end (for example 7 x 7 x 512 to 1 x 1 x 512, still zero parameters) instead of a huge flatten-plus-FC head.

الشريحة تقول أيضاً إن max pooling "أفضل بكثير" من المتوسط لأنه يتخلص من التنشيطات المشوّشة. اعتبر ذلك قاعدة عملية شائعة لا قانوناً. فكثير من الشبكات الحديثة تستبدل pooling داخل الجسم بـ strided convolution، وتستخدم global average pooling في النهاية (مثلاً من 7 x 7 x 512 إلى 1 x 1 x 512، وأيضاً بصفر parameters) بدلاً من رأس ضخم من flatten ثم FC.

Max poolingالتجميع الأقصى (max pooling)Average poolingالتجميع المتوسط (average pooling)
Output per windowالخرج لكل نافذةlargest valueأكبر قيمةmean valueالقيمة المتوسطة
Parametersالمعاملات00
Channelsالقنواتunchangedدون تغييرunchangedدون تغيير
Typical placeالموضع المعتادinside the trunk after a conv blockداخل جسم الشبكة بعد كتلة convglobal average pooling as the classifier headglobal average pooling رأساً للمصنّف

الجدول يقارن النوعين: كلاهما بصفر parameters ولا يغيّر عدد القنوات. الفرق في القيمة الخارجة من كل نافذة (الأكبر مقابل المتوسط) وفي المكان المعتاد داخل الشبكة.

What CNN filters learn: the feature hierarchy

ماذا تتعلم مرشحات CNN: تسلسل الخصائص

Slides 23 and 24: early conv layers learn low-level features (edges, colour blobs, oriented strokes), the middle layers learn mid-level features (object parts and shapes, such as eyes, wheels), and deep layers learn high-level features (whole objects, such as faces or cars). The early filters look almost the same whatever the dataset; the deep ones are specific to the classes trained on.

الشريحتان 23 و24: طبقات conv الأولى تتعلم ميزات منخفضة المستوى (حواف، بقع لونية، خطوط موجّهة)، والطبقات الوسطى تتعلم ميزات متوسطة المستوى (أجزاء أشكال، مثل العيون والعجلات)، والطبقات العميقة تتعلم ميزات عالية المستوى (أجسام كاملة، مثل الوجوه أو السيارات). الـ filters الأولى تبدو متشابهة تقريباً مهما كانت البيانات، أما العميقة فخاصة بالفئات التي دُرّبت عليها.

Why the hierarchy forms: each conv and pool step enlarges the receptive field, the patch of the input image one activation depends on. Pooling grows it fast, because every later 3 x 3 kernel now covers twice the input distance. A neuron that sees more of the image can respond to larger structures.

لماذا يتكون هذا التدرج: كل خطوة conv وpool تكبّر receptive field، أي الجزء من صورة الدخل الذي يعتمد عليه تنشيط واحد. والـ pooling يكبّره بسرعة، لأن كل kernel بحجم 3 x 3 بعده يغطي ضعف المسافة في الدخل. والخلية التي ترى جزءاً أكبر من الصورة تستطيع أن تستجيب لبُنى أكبر.

Slide 24 shows the full pipeline: CONV, RELU, CONV, RELU, POOL, repeated three times, then FC producing class scores; "car" scores highest, so the image is classified as a car.

الشريحة 24 تعرض المسار كاملاً: CONV, RELU, CONV, RELU, POOL مكررة ثلاث مرات، ثم FC يُخرج درجات الفئات، وفئة "car" هي الأعلى، فتُصنَّف الصورة على أنها سيارة.

The two demos, as numbers

العرضان التوضيحيان بالأرقام

ConvNetJS on CIFAR-10 (slide 25)

ConvNetJS على CIFAR-10 (الشريحة 25)

Input 32 x 32 x 3. First conv: 16 filters of 5 x 5 x 3, stride 1, output 32 x 32 x 16 (padding 2 keeps the size).

الدخل 32 x 32 x 3. أول conv: 16 filters بحجم 5 x 5 x 3، وstride يساوي 1، والخرج 32 x 32 x 16 (padding يساوي 2 يحافظ على الحجم).

Parameters = 16 x 5 x 5 x 3 + 16 = 1,200 + 16 = 1,216, the number printed on the slide.

عدد parameters = 16 x 5 x 5 x 3 + 16 = 1,200 + 16 = 1,216، وهو الرقم المطبوع على الشريحة.

CNN Explainer (slide 26)

CNN Explainer (الشريحة 26)

Input 64 x 64 x 3, 3 x 3 convs without padding (each removes 2), 2 x 2 max pools (each halves), 10 channels throughout:

الدخل 64 x 64 x 3، وconvs بحجم 3 x 3 بدون padding (كل واحد ينقص 2)، وmax pools بحجم 2 x 2 (كل واحد ينصّف)، و10 قنوات طوال الطريق:

64 → 62 → 60 → 30 → 28 → 26 → 13, then 10 class scores.

64 → 62 → 60 → 30 → 28 → 26 → 13، ثم 10 درجات للفئات.

Trapفخ

Pooling does not change the channel count. It works on each activation map independently. 224 x 224 x 64 in means 64 channels out, whatever F and S are.

Pooling لا يغيّر عدد القنوات. فهو يعمل على كل خريطة تنشيط بشكل مستقل. دخل 224 x 224 x 64 يعني خرجاً بـ 64 قناة، مهما كانت F وS.

Pooling has zero parameters. F and S are hyperparameters you choose, not weights the network learns. When counting a model's parameters, pooling rows contribute 0.

Pooling عدد parameters فيه صفر. F وS هما hyperparameters تختارهما أنت، لا أوزان تتعلمها الشبكة. وعند عدّ parameters النموذج، صفوف pooling تساهم بصفر.

2 x 2 with stride 2 drops 75 percent of activations, not 50 percent: both height and width halve, so 1/2 x 1/2 = 1/4 is kept.

2 x 2 مع stride يساوي 2 يحذف 75% من التنشيطات، لا 50%: فالطول والعرض كلاهما ينتصفان، فيبقى 1/2 x 1/2 = 1/4.

Memory hookمفتاح للحفظ

Pooling shrinks the map, never the depth, and owns no weights. Edges, then parts, then objects, as the receptive field grows.

Pooling يصغّر الخريطة، لا العمق أبداً، ولا يملك أي أوزان. حواف، ثم أجزاء، ثم أجسام، كلما كبر receptive field.

Check yourself

اختبر نفسك

A 2 x 2, stride 2 max pool is applied to a 56 x 56 x 128 tensor. What is the output shape and how many parameters does the layer have?

يُطبَّق max pool بحجم 2 x 2 وstride يساوي 2 على tensor بحجم 56 x 56 x 128. ما شكل الخرج، وكم عدد parameters في الطبقة؟

(56 − 2)/2 + 1 = 28 in each spatial dimension, channels unchanged at 128, and pooling learns nothing. Option A halves the channels (pooling never does). Option B invents weights (4 x 128). Option D uses stride 1.

(56 − 2)/2 + 1 = 28 في كل بُعد مكاني، والقنوات تبقى 128، وpooling لا يتعلم شيئاً. الخيار A ينصّف القنوات (وpooling لا يفعل ذلك أبداً). الخيار B يخترع أوزاناً (4 x 128). والخيار D يستخدم stride يساوي 1.

Apply 2 x 2 average pooling with stride 2 to the slice [[2, 0, 1, 3], [4, 1, 5, 2], [0, 7, 2, 2], [3, 1, 0, 8]]. Result?

طبّق average pooling بحجم 2 x 2 وstride يساوي 2 على الشريحة [[2, 0, 1, 3], [4, 1, 5, 2], [0, 7, 2, 2], [3, 1, 0, 8]]. ما النتيجة؟

Windows: top-left 2+0+4+1 = 7, top-right 1+3+5+2 = 11, bottom-left 0+7+3+1 = 11, bottom-right 2+2+0+8 = 12. Divide each by 4: 1.75, 2.75, 2.75, 3. Option A is max pooling. Option D forgets to divide (sum pooling). Option C drops the 8 in the last window.

النوافذ: أعلى اليسار 2+0+4+1 = 7، وأعلى اليمين 1+3+5+2 = 11، وأسفل اليسار 0+7+3+1 = 11، وأسفل اليمين 2+2+0+8 = 12. اقسم كلاً منها على 4: 1.75, 2.75, 2.75, 3. الخيار A هو max pooling. الخيار D ينسى القسمة. والخيار C يُسقط الرقم 8 من النافذة الأخيرة.

AlexNet pools a 55 x 55 x 96 map with F = 3, S = 2. What is the output width?

AlexNet يطبّق pooling على خريطة 55 x 55 x 96 بـ F = 3 وS = 2. ما عرض الخرج؟

(55 − 3)/2 + 1 = 26 + 1 = 27. Option B forgets the + 1. Option C divides by F instead of S. Option D is 55/2 rounded up, which ignores F. Windows overlap here because F > S.

(55 − 3)/2 + 1 = 26 + 1 = 27. الخيار B ينسى إضافة 1. الخيار C يقسم على F بدلاً من S. والخيار D هو 55/2 مقرّباً للأعلى، وهو يتجاهل F. النوافذ هنا متداخلة لأن F > S.

Which statement about the CNN feature hierarchy and pooling is correct?

أي عبارة عن تدرج الميزات في CNN وعن pooling صحيحة؟

Slide 23: low-level (edges) at the start, mid-level (parts) in the middle, high-level (objects) deep. Downsampling makes each later kernel cover more of the input, so the receptive field grows, not shrinks (C is backwards). Max pooling gives only approximate invariance to small shifts, not to any translation (B).

الشريحة 23: ميزات منخفضة المستوى (حواف) في البداية، ومتوسطة المستوى (أجزاء) في الوسط، وعالية المستوى (أجسام) في العمق. وتقليل الدقة يجعل كل kernel لاحق يغطي جزءاً أكبر من الدخل، فيكبر receptive field ولا يصغر (الخيار C معكوس). وmax pooling يعطي ثباتية تقريبية أمام الإزاحات الصغيرة فقط، لا أمام أي إزاحة (الخيار B).