Lecture7 (deep_learning_part3_1.pdf, 30 หน้า) · 2603520 Deep Learning

Convolutional Neural Networks: Convolution, Padding, Stride, Pooling และการนับ Parameter

Lesson 1–6 ใช้ network แบบ dense (fully connected) ทั้งหมด: ทุก node ต่อกับทุก node ของ layer ก่อนหน้า. บทนี้เปลี่ยนชนิดของ layer สำหรับข้อมูลภาพ. ส่วนที่ต้องคำนวณมือได้มีสามเรื่อง:

  1. ค่าใน feature map จากการเลื่อน filter ทับภาพ (หน้า 7–9)
  2. ขนาด output ของ convolution/pooling จากสูตร \(\lfloor (n+2p-f)/s \rfloor + 1\) (หน้า 12–20)
  3. จำนวน parameter ของแต่ละ layer ในตารางหน้า 21
อ่านเพิ่มก่อนเริ่ม (แนะนำ)
CS231n — Convolutional Neural Networks (สูตรขนาด output และการนับ parameter แบบเดียวกับสไลด์) · Keras — Conv2D และ MaxPooling2D (สไลด์หน้า 28 อ้างถึง) · Géron, Hands-On Machine Learning บทที่ 14 (ที่มาของรูปหน้า 5, 10, 19, 25).
สัญลักษณ์ที่ใช้ทั้งบท
ต้องรู้ก่อน — ภาพคือ matrix ของตัวเลข

ภาพ grayscale ขนาด 28×28 คือ matrix 28 แถว 28 คอลัมน์ แต่ละช่องเก็บความสว่างของ pixel (0 = ดำ, 255 = ขาว). ภาพสีเก็บ 3 matrix ซ้อนกัน (Red, Green, Blue) จึงมี shape (สูง, กว้าง, 3). มิติที่สามเรียกว่า channel หรือ depth.

Dense layer ของ Lesson 5 รับได้เฉพาะ vector จึงต้อง flatten ภาพ 28×28 เป็น vector ยาว 784 ก่อน. CNN รับ matrix เข้าไปตรงๆ โดยไม่ flatten.

1. Computer Vision และเหตุผลที่ Dense Network ไม่เหมาะกับภาพ

Slide: The Area of Computer Vision. Computer vision deals with how to give machines the ability to understand visual input such as images and videos as humans understand. Applications: image classification, image captioning, object detection, semantic segmentation, autonomous systems, creating new things such as art works. Diagram comparing human vision system (eye, brain) with computer vision system (sensing device, interpreting device)
Slide: The Area of Computer Vision — นิยามและงานประยุกต์ (Lecture7, p.2)

Computer vision คือสาขาที่ทำให้เครื่องมีความสามารถ understand visual input เช่นภาพและวิดีโอ ในแบบที่มนุษย์เข้าใจ. ระบบของมนุษย์มีตา (sensing device) ส่งสัญญาณให้สมอง (interpreting device). ระบบของคอมพิวเตอร์มีกล้องเป็น sensing device และโมเดลเป็น interpreting device ซึ่งให้ output เดียวกัน เช่น "bowl, oranges, bananas, lemons, peaches".

Applications of Computer Vision ที่สไลด์ระบุมี 4 กลุ่ม:

Slide: Deep Learning for Computer Vision. A large motorcycle image is fed to a densely connected network that outputs h(x), labelled Motorcycle?. Right side shows a grey scale image as a grid of pixel values and a color image split into red, green and blue channels, with pixel (11, 102, 35) producing forest green
Slide: Deep Learning for Computer Vision — จำแนกภาพด้วย dense network (Lecture7, p.3)

Deep learning has been applied in various fields of computer vision. ตัวอย่างในสไลด์คือการจำแนกว่าภาพเป็น Motorcycle หรือไม่ (binary classification, output เดียวผ่าน sigmoid). วิธีแรกที่ทำได้ด้วยความรู้เดิมคือ Image Classification using Densely Connected Neural Networks: flatten ภาพเป็น vector แล้วป้อนเข้า dense layer.

คอมพิวเตอร์เห็นภาพ grayscale เป็นตารางตัวเลข 1 ชั้น. ภาพสีเป็นตาราง 3 ชั้น: pixel ที่มีค่า (R, G, B) = (11, 102, 35) คือสี forest green. จำนวน input feature จึงเท่ากับ สูง × กว้าง × จำนวน channel.

Slide: Why not Densely Connected Neural Networks? Two problems: computational complexity and not scale; loss of spatial information of images. Three 4 by 4 grids each contain a small square drawn at a different location; the first flattens to the input vector 1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0. The network will have no idea that these are the shapes of squares
Slide: Why not Densely Connected Neural Networks? — สองปัญหา (Lecture7, p.4)

สไลด์ให้เหตุผลสองข้อที่ dense network ใช้กับภาพไม่ดี.

ข้อ 1: Computational Complexity & Not Scale. จำนวน weight ของ dense layer แรก = (จำนวน input) × (จำนวน node) และจำนวน input โตตามพื้นที่ภาพ.

คำนวณ: จำนวน parameter ของ dense layer แรก เมื่อภาพใหญ่ขึ้น
ให้ dense layer แรกมี 1,000 node (แต่ละ node มี bias 1 ตัว). สูตรจาก Lesson 5: \(\#\text{param} = n_{in} \times n_{out} + n_{out}\)
  1. ขั้นที่ 1 — ภาพสี 32×32
    $$\begin{gathered}n_{in} = 32 \times 32 \times 3 = 3{,}072 \\[0.35em] \Rightarrow\; 3{,}072 \times 1{,}000 + 1{,}000 = 3{,}073{,}000\end{gathered}$$
  2. ขั้นที่ 2 — ภาพสี 1000×1000
    $$\begin{gathered}n_{in} = 1000 \times 1000 \times 3 = 3{,}000{,}000 \\[0.35em] \Rightarrow\; 3{,}000{,}000 \times 1{,}000 + 1{,}000 = 3{,}000{,}001{,}000\end{gathered}$$
  3. ขั้นที่ 3 — เทียบกับ convolution layer ที่มี 10 filter ขนาด 3×3 บนภาพสี (สูตรใน §5)
    $$(3 \times 3 \times 3 + 1) \times 10 = 280 \quad\text{(เท่ากันทุกขนาดภาพ)}$$

ด้านยาวขึ้นราว 31 เท่า parameter ของ dense layer แรกเพิ่มจาก 3 ล้านเป็น 3 พันล้าน. จำนวน parameter ของ convolution layer ไม่ขึ้นกับขนาดภาพ ขึ้นกับขนาด filter, จำนวน channel และจำนวน filter เท่านั้น.

ข้อ 2: Loss Spatial Information of Images. การ flatten ทำลายข้อมูลว่า pixel ไหนอยู่ติดกัน. ตารางแรกในสไลด์เป็นภาพ 4×4 ที่มีสี่เหลี่ยมอยู่มุมซ้ายบน (pixel ค่า 1 ที่ตำแหน่ง (1,1), (1,2), (2,1), (2,2)). เมื่อ flatten ทีละแถวได้

$$\text{Input vector} = [1, 1, 0, 0,\; 1, 1, 0, 0,\; 0, 0, 0, 0,\; 0, 0, 0, 0]$$

ค่า 1 สองตัวของแถวบน (ตำแหน่ง 1–2) กับสองตัวของแถวล่าง (ตำแหน่ง 5–6) ซึ่งติดกันในภาพ ถูกแยกห่างกัน 4 ตำแหน่งใน vector. Dense network เรียน weight แยกต่อตำแหน่ง จึงเรียนได้แค่ว่า "ตำแหน่ง 1, 2, 5, 6 เป็น 1". ถ้าสี่เหลี่ยมเดียวกันย้ายไปมุมขวาล่าง ค่า 1 จะไปอยู่ตำแหน่ง 11, 12, 15, 16 ซึ่งใช้ weight คนละชุดที่ไม่เคยถูกเทรน. สไลด์สรุปว่า network จะ no idea ว่ารูปเหล่านี้คือสี่เหลี่ยม เพราะไม่ได้เรียนรูปทรงสี่เหลี่ยมเป็น feature.

ออกสอบสูง — ข้อความที่อาจารย์ทำสี

2. CNN: ที่มาและโครงสร้างระดับบน

Slide: Convolutional Neural Networks (CNNs). CNNs, aka ConvNets (1980s), emerged from the study of the brain's visual cortex. Many neurons in visual cortex have a small local receptive field (Hubel and Wiesel, 1968). Higher-level neurons are based on neighboring lower-level neurons' outputs. Diagrams of the cat experiment and local receptive fields of five neurons
Slide: Convolutional Neural Networks (CNNs) — ที่มาจาก visual cortex (Lecture7, p.5)

CNN (อีกชื่อคือ ConvNets) เกิดในทศวรรษ 1980 จากการศึกษา visual cortex ของสมอง. ข้อค้นพบของ Hubel & Wiesel (1968) ที่สไลด์อ้างมีสองข้อ และทั้งสองข้อกลายเป็นหลักออกแบบ CNN:

  1. neuron จำนวนมากใน visual cortex มี receptive field ขนาดเล็กเฉพาะที่ คือตอบสนองต่อสิ่งเร้าในบริเวณจำกัดของลานสายตาเท่านั้น ⇒ ใน CNN neuron หนึ่งตัวต่อกับ pixel กลุ่มเล็ก ไม่ต่อทั้งภาพ
  2. neuron ระดับสูงทำงานจาก output ของ neuron ระดับล่างที่อยู่ใกล้กัน ⇒ ใน CNN layer ลึกรวม feature ง่าย (เส้น, ขอบ) เป็น feature ซับซ้อน (รูปทรง, วัตถุ)
Slide: CNNs Architecture consists of input layer (raw image matrix without flattening), convolutional layers (extract features from images, generating feature maps, later flattened; neurons connected only to pixels in their receptive fields), fully connected layers (classification task), output layers (prediction task). Diagram: digit 3 image, convolutional layers producing feature maps, flatten, fully connected layers, prediction. Below: low-level, mid-level and high-level features
Slide: CNNs Architecture — สี่ส่วนของ CNN (Lecture7, p.6)

CNNs Architecture ประกอบด้วยสี่ส่วนเรียงกัน:

ส่วนข้อความในสไลด์หน้าที่
Input layerRaw image matrix as an input (without flatten the image)รับภาพเป็น matrix เช่น (28, 28, 1)
Convolutional layersextract features จากภาพ ได้ภาพที่ถูกแปลงเรียกว่า feature maps ซึ่งภายหลังถูก flatten เป็น vector ของ feature ที่เรียนได้feature extraction
Fully connected layersClassification taskรับ vector ของ feature มาจำแนก
Output layersPrediction taskให้ความน่าจะเป็นของแต่ละ class

หมายเหตุในสไลด์: neuron ใน convolutional layer ต่อเฉพาะกับ pixel ใน receptive fields. ของตัวเอง ต่างจาก dense layer ที่ต่อกับ input ทุกตัว. รูปล่างของสไลด์แสดงลำดับชั้นของ feature: layer ต้นเรียน low-level feature (ขอบ, เส้น), layer กลางเรียนชิ้นส่วน (ตา, จมูก), layer ลึกเรียน high-level feature (ใบหน้าทั้งหน้า).

ออกสอบสูง — ข้อความที่อาจารย์ทำสี

3. Convolution Operation และ Filter

Slide: Convolution Operation in Convolution Layers. A 6 by 6 input image is convolved with a 3 by 3 filter with columns 1, 0, -1. The first output is (1x2)+(0x3)+(-1x1)+(1x5)+(0x4)+(-1x3)+(1x6)+(0x4)+(-1x4)=5. Feature map dimension is (n-f+1) by (n-f+1)
Slide: Convolution Operation in Convolution Layers — ตัวอย่าง 6×6 กับ filter 3×3 (Lecture7, p.7)

Convolution layer ใช้ convolution kernels (อีกชื่อคือ filters) สกัด feature จากภาพ. ตัวอย่างในสไลด์คือ edge detection. วิธีคำนวณ: filter เลื่อนไปบนภาพทีละตำแหน่ง ที่แต่ละตำแหน่งคูณค่าที่ซ้อนกันทีละคู่ (element-wise multiplication) แล้วบวกทั้งหมด ได้ตัวเลข 1 ตัว. ตัวเลขทั้งหมดเรียงกันเป็น feature map (หรือ activation map).

คำนวณตามสไลด์: feature map แถวแรก (input 6×6, filter 3×3, ไม่มี padding, stride 1)
ให้ input สามแถวแรก: \([2,3,1,2,2,3]\), \([5,4,3,9,8,5]\), \([6,4,4,3,5,8]\) และ filter ทุกแถวเป็น \([1, 0, -1]\)
  1. ขั้นที่ 1 — ลดรูป filter

    คอลัมน์ซ้ายของ filter คูณ 1, คอลัมน์กลางคูณ 0, คอลัมน์ขวาคูณ −1. ผลลัพธ์จึงเท่ากับ

    $$\text{output} = \textstyle\sum \text{คอลัมน์ซ้าย} - \sum \text{คอลัมน์ขวา}$$
  2. ขั้นที่ 2 — ตำแหน่ง (1,1): หน้าต่างคลุมคอลัมน์ 1–3
    $$\begin{aligned}&(1{\times}2)+(0{\times}3)+(-1{\times}1)\\ +\,&(1{\times}5)+(0{\times}4)+(-1{\times}3)\\ +\,&(1{\times}6)+(0{\times}4)+(-1{\times}4) = 13 - 8 = 5\end{aligned}$$
  3. ขั้นที่ 3 — เลื่อนขวา 1 ช่อง: คอลัมน์ 2–4
    $$(3+4+4) - (2+9+3) = 11 - 14 = -3$$
  4. ขั้นที่ 4 — คอลัมน์ 3–5 และ 4–6
    $$\begin{gathered}(1+3+4) - (2+8+5) = 8 - 15 = -7\\ (2+9+3) - (3+5+8) = 14 - 16 = -2\end{gathered}$$
  5. ขั้นที่ 5 — ขนาด feature map
    $$n - f + 1 = 6 - 3 + 1 = 4 \quad\Rightarrow\quad 4 \times 4$$

แถวแรกของ feature map คือ \([5, -3, -7, -2]\). ทำซ้ำอีก 3 แถวได้ feature map เต็ม (ตรงกับที่สไลด์หน้า 12 และ 23 พิมพ์ไว้):

$$\begin{bmatrix} 5 & -3 & -7 & -2\\ 4 & -6 & -5 & -1\\ 0 & -2 & 5 & 1\\ 2 & -5 & 0 & 9 \end{bmatrix}$$
Slide: Why is This Doing Edge Detection? Vertical edge detection. A 6 by 6 image whose left three columns are 80 and right three columns are 0, convolved with the filter with columns 1, 0, -1, gives a 4 by 4 feature map whose columns are 0, 240, 240, 0
Slide: Why is This Doing Edge Detection? — vertical edge (Lecture7, p.8)

ภาพหน้า 8 ถูกทำให้ง่าย: ครึ่งซ้ายสว่าง (80) ครึ่งขวาดำ (0) จึงมีขอบแนวตั้งหนึ่งเส้นตรงกลาง. ใช้กฎ "คอลัมน์ซ้าย − คอลัมน์ขวา" ของ filter เดิม:

Feature map จึงเป็น \([0, 240, 240, 0]\) ทุกแถว. บริเวณสีเรียบให้ 0 ไม่ว่าจะสว่างหรือมืด. ค่าสูงเกิดเฉพาะตำแหน่งที่หน้าต่างคร่อมขอบ. filter นี้จึงเป็นตัวตรวจ vertical edge และ feature map "amplifying the edges".

Slide: Why is This Doing Edge Detection? (2) Horizontal edge detection. A 6 by 6 image with 80 in the top-left and bottom-right quadrants and 0 elsewhere, convolved with a filter whose rows are 1,1,1 / 0,0,0 / -1,-1,-1, gives rows 0,0,0,0 / 240,80,-80,-240 / 240,80,-80,-240 / 0,0,0,0. Other filters can be applied to detect different types of features
Slide: Why is This Doing Edge Detection? (2) — horizontal edge (Lecture7, p.9)

หมุน filter 90° ให้แถวบนเป็น 1, แถวกลางเป็น 0, แถวล่างเป็น −1 จะได้ output = (ผลรวมแถวบน) − (ผลรวมแถวล่าง) ซึ่งตรวจ horizontal edge. ภาพหน้า 9 มีมุมซ้ายบนและขวาล่างสว่าง (80).

คำนวณตามสไลด์: แถวที่ 2 ของ feature map หน้า 9
ให้ หน้าต่างคลุมแถว 2–4 ของภาพ. แถว 2 = \([80,80,80,0,0,0]\) (แถวบนของหน้าต่าง), แถว 4 = \([0,0,0,80,80,80]\) (แถวล่างของหน้าต่าง)
  1. ขั้นที่ 1 — คอลัมน์ 1–3
    $$(80+80+80) - (0+0+0) = 240$$
  2. ขั้นที่ 2 — คอลัมน์ 2–4
    $$(80+80+0) - (0+0+80) = 160 - 80 = 80$$
  3. ขั้นที่ 3 — คอลัมน์ 3–5
    $$(80+0+0) - (0+80+80) = 80 - 160 = -80$$
  4. ขั้นที่ 4 — คอลัมน์ 4–6
    $$(0+0+0) - (80+80+80) = -240$$

แถวที่ 2 (และแถวที่ 3) = \([240, 80, -80, -240]\). แถวที่ 1 และ 4 เป็น 0 เพราะหน้าต่างคลุมแถวบนและแถวล่างที่เหมือนกัน. เครื่องหมายบอกทิศของขอบ: บวก = สว่างอยู่บน มืดอยู่ล่าง; ลบ = มืดอยู่บน สว่างอยู่ล่าง. ค่า 80 และ −80 คือช่วงเปลี่ยนผ่านตรงกลางภาพที่ขอบทั้งสองทิศปนกัน.

สไลด์ปิดหน้า 9 ว่า filter แบบอื่นใช้ตรวจ feature ชนิดอื่นได้ (blur, sharpen, ขอบแนวทแยง).

Slide: Filters. Filter element: each element in a filter is a weight; they are randomly initialized and trainable to be learned from backpropagation. Filter size: smaller filters capture fine image details, larger capture broader image details. Example: a vertical 7 by 7 filter and a horizontal 7 by 7 filter applied to a building image produce two different feature maps
Slide: Filters — แต่ละช่องคือ weight ที่เรียนได้ (Lecture7, p.10)

ค่า 1, 0, −1 ที่ใช้มาเป็นค่าที่คนตั้งเองเพื่ออธิบาย. ใน CNN จริง:

ตัวอย่างในสไลด์ใช้ filter 7×7 สองตัวกับภาพอาคาร. (1) Vertical filter: เป็น 0 ทั้งหมดยกเว้นคอลัมน์กลางเป็น 1 ⇒ Feature Map 1 เส้นแนวตั้งชัดขึ้น ส่วนอื่นเบลอ. (2) Horizontal filter: เป็น 0 ทั้งหมดยกเว้นแถวกลางเป็น 1 ⇒ Feature Map 2 เส้นแนวนอนชัดขึ้น. input เดียวกัน filter ต่างกันให้ feature map ต่างกัน.

คำถาม 1Convolution
ภาพ 4×4 มีแถว [1,2,0,1], [3,1,1,0], [0,2,4,1], [2,0,1,3] ทำ convolution กับ filter 3×3 ที่ทุกแถวเป็น [1,0,−1] (ไม่มี padding, stride 1) ค่าที่ตำแหน่งซ้ายบนของ feature map คือเท่าใด?
คำถาม 2Edge detection
ภาพ 6×6 มี 3 คอลัมน์ซ้ายเป็น 0 และ 3 คอลัมน์ขวาเป็น 50 ทำ convolution กับ filter 3×3 ที่ทุกแถวเป็น [1,0,−1] (ไม่มี padding, stride 1) แต่ละแถวของ feature map คือข้อใด?
ออกสอบสูง — ข้อความที่อาจารย์ทำสี

4. Padding และ Stride

Slide: Convolution Operator Problems. Two downsides of basic convolution: image shrinks down every time detecting edge (in deep networks the image gets really small, nothing left for feature extraction), and throws away a lot of information from the edge (pixels on the corners or edges are used much less in the output). A 5 by 5 input with a 3 by 3 window produces a 3 by 3 output
Slide: Convolution Operator Problems — สองข้อเสีย (Lecture7, p.11)

Two downside of basic convolutional:

  1. Image shrink down ทุกครั้งที่ทำ convolution. ภาพ 5×5 กับ filter 3×3 ในสไลด์ให้ output \(5-3+1 = 3\) คือ 3×3. network ลึกจะทำให้ภาพเล็กลงจนไม่เหลือให้สกัด feature: ภาพ 32×32 ผ่าน filter 3×3 แต่ละครั้งด้านลดลง 2 จึงเหลือ 2×2 หลัง 15 layer
  2. Throw away a lot of information from edge. pixel มุมถูกหน้าต่างคลุมเพียง 1 ครั้ง ส่วน pixel กลางภาพ 5×5 ถูกคลุม 9 ครั้ง ข้อมูลที่ขอบจึงมีน้ำหนักใน output น้อยกว่ามาก
Slide: Padding pads the image with additional border (often zeros). SAME uses zero padding so the output has the same dimension as the input; VALID means no padding (default in Keras). The 6 by 6 image with a zero border is convolved with the 3 by 3 filter; the top-left output is (1x0)+(0x0)+(-1x0)+(1x0)+(0x2)+(-1x3)+(1x0)+(0x5)+(-1x4) = -7. New dimension = (n+2p-f+1) by (n+2p-f+1)
Slide: Padding — เติมขอบด้วยศูนย์ (Lecture7, p.12)

Padding คือการเติม additional border รอบภาพ (ส่วนใหญ่เติม zeros) เพื่อรักษาขนาดเชิงพื้นที่ ให้ความกว้างและสูงของ output เท่ากับ input. Keras มีสองค่า:

$$\text{New dimension} = (n + 2p - f + 1) \times (n + 2p - f + 1)$$

เติมขอบข้างละ p ทำให้ด้านยาวขึ้น 2p ก่อนเข้าสูตรเดิม n − f + 1

คำนวณตามสไลด์: padding 1 ชั้นกับภาพ 6×6 และ filter 3×3
ให้ \(n = 6,\ f = 3,\ p = 1\), stride 1, ภาพและ filter ชุดเดิมจากหน้า 7
  1. ขั้นที่ 1 — ขนาด output
    $$\begin{gathered}n + 2p - f + 1 = 6 + 2(1) - 3 + 1 = 6 \\[0.35em] \Rightarrow\; 6 \times 6 \text{ (เท่า input)}\end{gathered}$$
  2. ขั้นที่ 2 — ค่าที่มุมซ้ายบน: หน้าต่างคลุมขอบศูนย์ 1 แถว 1 คอลัมน์ กับ pixel 2,3 / 5,4
    $$\begin{aligned}&(1{\times}0)+(0{\times}0)+(-1{\times}0)\\ +\,&(1{\times}0)+(0{\times}2)+(-1{\times}3)\\ +\,&(1{\times}0)+(0{\times}5)+(-1{\times}4) = -7\end{aligned}$$
  3. ขั้นที่ 3 — หาค่า p ที่ทำให้ขนาดคงเดิม (SAME, stride 1)
    $$n + 2p - f + 1 = n \quad\Rightarrow\quad p = \frac{f-1}{2}$$

    \(f=3 \Rightarrow p=1\); \(f=5 \Rightarrow p=2\); \(f=7 \Rightarrow p=3\). filter จึงนิยมใช้ขนาดคี่

Output เป็น 6×6. ส่วน 4×4 ตรงกลางคือ feature map เดิมของหน้า 7 (\(5, -3, -7, -2, \ldots\)) และมีขอบใหม่รอบนอก 1 ชั้นซึ่งมุมซ้ายบนเท่ากับ −7. pixel มุมของภาพถูกใช้ในหลายหน้าต่างขึ้น ข้อเสียทั้งสองข้อของหน้า 11 จึงลดลง.

Slide: Stride is the step size of the filter as it moves over an image. Larger stride may skip valuable regions, leading to lesser overlap and smaller output; larger strides increase speed, lesser computation and memory; reduced resolution can have a regularizing side. A 7 by 7 input with stride 2 and the 3 by 3 filter gives a 3 by 3 feature map 5,-7,2 / 0,5,-5 / 0,-5,6. New dimension = (n+2p-f)/s+1, floor if not an integer
Slide: Stride Convolution — ก้าวทีละ s ช่อง (Lecture7, p.13)

Stride คือ step size ของ filter ขณะเลื่อนไปบนภาพ. stride 1 เลื่อนทีละ 1 ช่อง; stride 2 ข้ามทีละ 2 ช่องทั้งแนวนอนและแนวตั้ง. ผลของ stride ที่ใหญ่ขึ้นตามสไลด์:

$$\text{New dimension} = \left\lfloor \frac{n + 2p - f}{s} \right\rfloor + 1$$

ถ้าหารไม่ลงตัวให้ปัดลง: ตำแหน่งที่หน้าต่างล้นออกนอกภาพ (รวม padding แล้ว) ไม่ถูกคำนวณ

คำนวณตามสไลด์: stride 2 กับภาพ 7×7
ให้ \(n = 7,\ f = 3,\ p = 0,\ s = 2\). สามแถวแรกของภาพ: \([2,3,1,2,2,3,2]\), \([5,4,3,9,8,5,5]\), \([6,4,4,3,5,8,6]\)
  1. ขั้นที่ 1 — ขนาด output
    $$\left\lfloor \frac{7 + 0 - 3}{2} \right\rfloor + 1 = 2 + 1 = 3 \quad\Rightarrow\quad 3 \times 3$$
  2. ขั้นที่ 2 — หน้าต่างเริ่มที่คอลัมน์ 1, 3, 5 (ข้ามทีละ 2)
    $$\begin{aligned} \text{คอลัมน์ 1–3:}\quad &(2+5+6) - (1+3+4) = 13 - 8 = 5\\ \text{คอลัมน์ 3–5:}\quad &(1+3+4) - (2+8+5) = 8 - 15 = -7\\ \text{คอลัมน์ 5–7:}\quad &(2+8+5) - (2+5+6) = 15 - 13 = 2 \end{aligned}$$
  3. ขั้นที่ 3 — กรณีหารไม่ลงตัว: ภาพ 6×6 เดิม, \(f=3,\ s=2\)
    $$\left\lfloor \frac{6-3}{2} \right\rfloor + 1 = \lfloor 1.5 \rfloor + 1 = 2$$

    หน้าต่างที่สามจะต้องเริ่มที่คอลัมน์ 5 และคลุมคอลัมน์ 5–7 แต่ภาพมีแค่ 6 คอลัมน์ จึงไม่คำนวณ

แถวแรกของ feature map = \([5, -7, 2]\) ตรงกับสไลด์ (feature map เต็ม: \([5,-7,2]\), \([0,5,-5]\), \([0,-5,6]\)). เทียบกับ stride 1 ที่ให้ 5×5 = 25 ค่า stride 2 ให้ 3×3 = 9 ค่า.

กลไกที่ต้องอธิบายได้
ทำไมต้องมี
Padding แก้ปัญหาภาพหดและข้อมูลขอบหาย. Stride เป็นวิธีย่อขนาด feature map โดยเจตนาเพื่อลดการคำนวณ.
เปลี่ยนแล้วเกิดอะไร
เพิ่ม \(p\) ⇒ output ใหญ่ขึ้น. เพิ่ม \(s\) ⇒ output เล็กลงโดยประมาณ \(1/s\) ต่อด้าน. เพิ่ม \(f\) ⇒ output เล็กลงและ parameter เพิ่ม. padding="same" กับ stride 2 ให้ output \(\lceil n/2 \rceil\) ไม่ใช่ \(n\).
เชื่อมกับ
สูตรเดียวกันใช้กับ pooling (§6) โดยตั้ง \(p = 0\). ตารางหน้า 21 (§7) ใช้สูตรนี้ทุกแถว.
คำถาม 3Output size
Input ขนาด 28×28 ผ่าน convolution ที่ใช้ filter 5×5, padding 2, stride 1 ขนาด output (กว้าง×สูง) คือข้อใด?
คำถาม 4Output size
Input ขนาด 64×64 ผ่าน convolution ที่ใช้ filter 7×7, padding 0, stride 2 ขนาด output (กว้าง×สูง) คือข้อใด?
ออกสอบสูง — ข้อความที่อาจารย์ทำสี

5. Convolution บน Volume, หลาย Filter และ Convolution Layer เต็มรูป

Slide: Convolution on Volume. Given an input with certain height, width and number of channels, the applied filters have potentially different weights but the same number of channels (one for each channel). To perform convolution we sum up terms for all channels. Input nw x nh x nc, filter f x f x nc (image nc = filter nc), feature map (nw+2p-f)/s+1 by (nh+2p-f)/s+1
Slide: Convolution on Volume — filter ต้องมี channel เท่า input (Lecture7, p.14)

ภาพสีมี 3 channel. filter ที่ใช้กับ input หลาย channel ต้องมี the same number of channels กับ input (หนึ่งชั้นต่อหนึ่ง channel) โดยแต่ละชั้นมี weight ต่างกันได้. filter จึงมีขนาด \(f \times f \times n_C\). การคำนวณหนึ่งตำแหน่งคือคูณและบวกทุกช่องของทุก channel รวมกันเป็นตัวเลขเดียว.

Slide: Multiple Filters. A convolutional layer can simultaneously apply multiple trainable filters to its inputs, making it capable of detecting multiple features anywhere in its inputs. Two filters of size f x f x nc each produce one feature map; total feature map is (nw+2p-f)/s+1 by (nh+2p-f)/s+1 by nc' (number of filters)
Slide: Multiple Filters — จำนวน filter คือจำนวน channel ของ output (Lecture7, p.15)

Convolutional layer ใช้ multiple trainable filters พร้อมกันได้ จึง detecting multiple features anywhere ใน input. filter แต่ละตัวให้ feature map 1 แผ่น. เมื่อนำมาซ้อนกันจะได้ output

$$\begin{gathered}\left(\left\lfloor\frac{n_W + 2p - f}{s}\right\rfloor + 1\right) \times \left(\left\lfloor\frac{n_H + 2p - f}{s}\right\rfloor + 1\right)\\ \times\; n_C'\end{gathered}$$

กว้างและสูงมาจากสูตรเดิม ส่วน channel ของ output เท่ากับจำนวน filter ไม่เกี่ยวกับ channel ของ input

Slide: A Convolution Layer. What is X, theta, Z, activation function, a? The input image or input feature map (nW x nH x nC) plays the role of X; each filter (f x f x nC) plays the role of theta; the convolution result plus bias theta0 plays the role of Z; ReLU is applied; the stacked feature map plays the role of a, the image to the next layer
Slide: A Convolution Layer — จับคู่กับ X, θ, Z, a ของ dense layer (Lecture7, p.16)

Convolution layer มีส่วนประกอบเดียวกับ dense layer ของ Lesson 2 ทุกตัว:

บทบาทDense layerConvolution layer
\(X\) (หรือ \(a^{[l-1]}\))vector ของ inputภาพ หรือ feature map ของ layer ก่อนหน้า (\(n_W \times n_H \times n_C\))
\(\theta\)weight matrixfilter แต่ละตัว (\(f \times f \times n_C\))
\(\theta_0\)bias 1 ตัวต่อ nodebias 1 ตัวต่อ filter
\(Z\)\(\theta X + \theta_0\)ผล convolution + bias \(\theta_0\)
\(a\)\(g(Z)\)\(\text{ReLU}(Z)\) ⇒ feature map ที่เป็น "image to the next layer"
คำนวณ: จำนวน parameter ของ convolution layer
ให้ layer มี \(n_C'\) filter ขนาด \(f \times f\) รับ input ที่มี \(n_C\) channel
  1. ขั้นที่ 1 — weight ของ filter หนึ่งตัว
    $$f \times f \times n_C$$
  2. ขั้นที่ 2 — บวก bias 1 ตัวต่อ filter แล้วคูณจำนวน filter
    $$\#\text{param} = (f \times f \times n_C + 1) \times n_C'$$
  3. ขั้นที่ 3 — ตัวอย่าง: 10 filter ขนาด 3×3 บนภาพสี
    $$(3 \times 3 \times 3 + 1) \times 10 = 28 \times 10 = 280$$
  4. ขั้นที่ 4 — ตัวอย่าง: 20 filter ขนาด 5×5 บน input 10 channel
    $$(5 \times 5 \times 10 + 1) \times 20 = 251 \times 20 = 5{,}020$$

สูตรไม่มี \(n_W, n_H, p, s\). ขนาดภาพ padding และ stride เปลี่ยนขนาด output แต่ไม่เปลี่ยนจำนวน parameter. ตัวที่ลืมบ่อยคือ \(n_C\) ของ input (ไม่ใช่ของภาพต้นฉบับ) และ +1 ของ bias.

คำถาม 5Parameters
Convolution layer มี 16 filter ขนาด 3×3 รับ input ขนาด 14×14×8 และใช้ padding แบบ same จำนวน parameter ที่เรียนได้ของ layer นี้คือเท่าใด?
คำถาม 6Output shape
Input ขนาด 32×32×3 ผ่าน convolution layer ที่มี 8 filter ขนาด 5×5, padding 0, stride 1 shape ของ output คือข้อใด?
ออกสอบสูง — ข้อความที่อาจารย์ทำสี

6. Pooling Layer และ Translation Invariance

Slide: Pooling Layer. Pooling layer reduces the size of images or feature maps (not affecting channels), reduces spatial dimensions, computation and number of parameters in subsequent layers, limiting risk of overfitting. Common types: max pooling (most common, keeps most important features; hyperparameters filter size and stride, no parameters to learn) and average pooling. Example 6 by 6 input, f=3, s=3, first output 6. Output ((nW-f)/s+1) by ((nH-f)/s+1) by nC, floor
Slide: Pooling Layer — ย่อ feature map โดยไม่มี parameter (Lecture7, p.18)

Pooling layer ทำหน้าที่ reduce the size of images or feature maps:

Common types of polling strategy มีสองแบบ:

$$\begin{gathered}\text{Output} = \left(\left\lfloor\frac{n_W - f}{s}\right\rfloor + 1\right) \times \left(\left\lfloor\frac{n_H - f}{s}\right\rfloor + 1\right)\\ \times\; n_C\end{gathered}$$
คำนวณตามสไลด์: max pooling และ average pooling (f = 3, s = 3) บนภาพ 6×6
ให้ ภาพ 6×6 ชุดเดิม แบ่งเป็น 4 บล็อก 3×3 ที่ไม่ซ้อนกัน. บล็อกซ้ายบน \(\{2,3,1,5,4,3,6,4,4\}\), ขวาบน \(\{2,2,3,9,8,5,3,5,8\}\), ซ้ายล่าง \(\{6,3,6,7,8,9,7,4,3\}\), ขวาล่าง \(\{5,5,5,9,4,3,6,9,3\}\)
  1. ขั้นที่ 1 — ขนาด output
    $$\left\lfloor \frac{6-3}{3} \right\rfloor + 1 = 2 \quad\Rightarrow\quad 2 \times 2$$
  2. ขั้นที่ 2 — Max pooling: เลือกค่ามากสุดของแต่ละบล็อก
    $$\begin{bmatrix} \max = 6 & \max = 9\\ \max = 9 & \max = 9 \end{bmatrix}$$
  3. ขั้นที่ 3 — Average pooling: ผลรวมของแต่ละบล็อกหาร 9
    $$\begin{bmatrix} 32/9 & 45/9\\ 53/9 & 49/9 \end{bmatrix} \approx \begin{bmatrix} 3.56 & 5.00\\ 5.89 & 5.44 \end{bmatrix}$$

สไลด์เติมไว้ช่องเดียวคือ 6 (ซ้ายบน). สามช่องที่เหลือของ max pooling เป็น 9 ทั้งหมด. ข้อมูล 36 ค่าเหลือ 4 ค่า และไม่มี weight ให้เรียนเลย.

Slide: Max Pooling Introduces Translation Invariance. Shifting an image a few pixels should result in similar features (invariance to small translations). Images A, B, C pass through max pooling with a 2 by 2 kernel and stride 2; B and C are A shifted right by one and two pixels. Outputs for A and B are identical; C's output is shifted by one pixel (still 75% invariance)
Slide: Max Pooling Introduce Translation Invariant — ภาพ A, B, C (Lecture7, p.19)

ภาพที่เลื่อนไปไม่กี่ pixel ควรให้ feature ใกล้เคียงเดิม เรียกว่า invariance to small translations. ตัวอย่างในสไลด์: ภาพ A, B, C ผ่าน max pooling ที่ใช้ kernel 2×2 stride 2. B คือ A เลื่อนขวา 1 pixel, C คือ A เลื่อนขวา 2 pixel.

ข้อสรุปของสไลด์: ใส่ max pooling ทุกสองสาม layer ทำให้ได้ translation invariance ระดับหนึ่งในสเกลที่ใหญ่ขึ้น. invariance นี้มีประโยชน์กับงานที่คำตอบไม่ควรขึ้นกับตำแหน่งละเอียด เช่น classification.

คำถาม 7Pooling
Feature map ขนาด 4×4 มีแถว [1,3,2,0], [4,2,1,5], [0,1,7,2], [3,6,1,4] ผ่าน max pooling ที่ใช้ pool size 2 และ stride 2 ผลลัพธ์คือข้อใด?
คำถาม 8Pooling
Feature map ขนาด 15×15×20 ผ่าน max pooling ที่ใช้ pool size 2 และ stride 2 shape ของ output และจำนวน parameter ที่เรียนได้ของ layer นี้คือข้อใด?
ออกสอบสูง — ข้อความที่อาจารย์ทำสี

7. ประกอบเป็น CNN และตาราง Parameters Summary

Slide: Put Together: Convolution and Densely Connected Layer. A 32x32x3 dog image passes through a convolution with f=3, 10 filters, p=0, s=1 and a second convolution with f=5, 20 filters, p=0, s=2, then into fully connected layers ending in h(x). Formula dim = (n+2p-f)/s + 1
Slide: Put Together: Convolution & Densely Connected Layer — ยังไม่มี pooling (Lecture7, p.17)

หน้า 17 ต่อ convolution สองชั้นเข้ากับ dense layer โดยยังไม่มี pooling. ภาพ 32×32×3 ผ่านชั้นแรก (f = 3, 10 filter, p = 0, s = 1) ได้ \(32 - 3 + 1 = 30\) คือ 30×30×10. ชั้นที่สอง (f = 5, 20 filter, p = 0, s = 2) ได้ \(\lfloor (30-5)/2 \rfloor + 1 = \lfloor 12.5 \rfloor + 1 = 13\) คือ 13×13×20. เมื่อ flatten จะได้ vector ยาว \(13 \times 13 \times 20 = 3{,}380\).

Slide: Convolution Neural Network. The 32x32x3 image passes through Layer 1 (convolution f=3, 10 filters, p=0, s=1, then pooling f=2, s=2) and Layer 2 (convolution f=5, 20 filters, p=0, s=2, then pooling f=2, s=2), then fully connected Layer 3, Layer 4 up to Layer L and output h(x)
Slide: Convolution Neural Network — เพิ่ม pooling หลัง convolution แต่ละชั้น (Lecture7, p.20)

หน้า 20 เพิ่ม max pooling (f = 2, s = 2) หลัง convolution แต่ละชั้น. สไลด์นับ convolution + pooling คู่หนึ่งเป็น 1 layer (Layer 1, Layer 2) เพราะ pooling ไม่มี parameter. dense layer จึงเริ่มที่ Layer 3.

Slide: Parameters Summary table. Input 32,32,3 size 3,072 params 0. Layer 1 Conv (f=3, 10 filters, s=1) 30,30,10 size 9,000 params 280. Layer 1 Pooling (f=2, s=2) 15,15,10 size 2,250 params 0. Layer 2 Conv (f=5, 20 filters, s=2) 6,6,20 size 720 params 5020. Layer 2 Pooling 3,3,20 size 180 params 0. Layer 3 fully connected 100 nodes params 18,100. Layer 4 fully connected 100 nodes params 10,100. Sigmoid 1 node params 101. As the CNN goes deeper the height and width decrease whereas the number of channels increase
Slide: Parameters Summary — ตารางที่ต้องคำนวณได้ทุกช่อง (Lecture7, p.21)
คำนวณตามสไลด์: ทุกแถวของตาราง Parameters Summary
ให้ input 32×32×3 และ network ตามหน้า 20. Activation size = ผลคูณของ activation shape
  1. ขั้นที่ 1 — Input
    $$32 \times 32 \times 3 = 3{,}072 \text{ ค่า},\quad 0 \text{ parameter}$$
  2. ขั้นที่ 2 — Layer 1 Conv (f = 3, 10 filter, p = 0, s = 1)
    $$\begin{gathered}\frac{32 + 0 - 3}{1} + 1 = 30 \;\Rightarrow\; 30 \times 30 \times 10 = 9{,}000 \\[0.35em] \#\text{param} = (3 \cdot 3 \cdot 3 + 1) \cdot 10 = 280\end{gathered}$$
  3. ขั้นที่ 3 — Layer 1 Pooling (f = 2, s = 2)
    $$\begin{gathered}\frac{30 - 2}{2} + 1 = 15 \;\Rightarrow\; 15 \times 15 \times 10 = 2{,}250 \\[0.35em] \#\text{param} = 0\end{gathered}$$
  4. ขั้นที่ 4 — Layer 2 Conv (f = 5, 20 filter, p = 0, s = 2): input มี 10 channel
    $$\begin{gathered}\frac{15 - 5}{2} + 1 = 6 \;\Rightarrow\; 6 \times 6 \times 20 = 720 \\[0.35em] \#\text{param} = (5 \cdot 5 \cdot 10 + 1) \cdot 20 = 5{,}020\end{gathered}$$
  5. ขั้นที่ 5 — Layer 2 Pooling (f = 2, s = 2)
    $$\frac{6 - 2}{2} + 1 = 3 \;\Rightarrow\; 3 \times 3 \times 20 = 180 \qquad \#\text{param} = 0$$
  6. ขั้นที่ 6 — Flatten แล้วเข้า Layer 3 (Fully Connected 100 node)
    $$180 \times 100 + 100 = 18{,}100$$
  7. ขั้นที่ 7 — Layer 4 (Fully Connected 100 node) และ output sigmoid 1 node
    $$100 \times 100 + 100 = 10{,}100 \qquad 100 \times 1 + 1 = 101$$
  8. ขั้นที่ 8 — รวม
    $$280 + 0 + 5{,}020 + 0 + 18{,}100 + 10{,}100 + 101 = 33{,}601$$

ทุกค่าตรงกับตารางในสไลด์ และผลรวม 33,601 ตรงกับ Total params ของ model.summary() หน้า 27. ชั้น convolution ทั้งสองมี parameter รวม 5,300 ตัว (16%) ส่วน dense layer มี 28,301 ตัว (84%).

หมายเหตุใต้ตาราง: เมื่อ CNN ลึกขึ้น height and width จะ decrease ส่วน number of channels จะ increase. ในตารางนี้ด้านกว้างเดินเป็น 32 → 30 → 15 → 6 → 3 และ channel เดินเป็น 3 → 10 → 20.

คำนวณ: pooling ลด parameter ของ dense layer ได้เท่าไร (หน้า 17 เทียบหน้า 20)
ให้ network สองแบบ ต่างกันเฉพาะการมี pooling. Dense layer แรกมี 100 node
  1. ขั้นที่ 1 — ไม่มี pooling (หน้า 17): flatten ได้ 13×13×20 = 3,380
    $$3{,}380 \times 100 + 100 = 338{,}100$$
  2. ขั้นที่ 2 — มี pooling (หน้า 20): flatten ได้ 3×3×20 = 180
    $$180 \times 100 + 100 = 18{,}100$$
  3. ขั้นที่ 3 — อัตราส่วน
    $$\frac{338{,}100}{18{,}100} \approx 18.7$$

Pooling ไม่มี parameter ของตัวเอง แต่ลด parameter ของ layer ถัดไปราว 18.7 เท่า ตรงกับข้อความหน้า 18 ที่ว่า pooling ลด "number of parameters in subsequent layers".

คำถาม 9Parameter table
ภาพ 28×28×1 ผ่าน Conv (6 filter, f=5, p=0, s=1) → MaxPool (f=2, s=2) → Conv (16 filter, f=5, p=0, s=1) → MaxPool (f=2, s=2) → Flatten จำนวนค่าใน vector หลัง flatten คือเท่าใด?
คำถาม 10Parameter table
จาก network เดียวกัน (ภาพ 28×28×1 → Conv 6 filter f=5 → MaxPool → Conv 16 filter f=5 → MaxPool) convolution layer ชั้นที่สองมี parameter ที่เรียนได้กี่ตัว?
ออกสอบสูง — ข้อความที่อาจารย์ทำสี

8. ทำไมต้อง ConvNets

Slide: Why ConvNets? CNNs can learn spatial features of images. The network looks at the image as a set of pixels that when close to each other are heavily related, independently from the location in the image itself. Three 4 by 4 grids show the same square shape at three locations; a cat diagram shows local features combining into a cat
Slide: Why ConvNets? — เรียน spatial features (Lecture7, p.22)

CNNs can learn spatial features of images. network มองภาพเป็นกลุ่ม pixel ที่ถ้าอยู่ใกล้กันจะสัมพันธ์กันมาก โดยไม่ขึ้นกับตำแหน่งในภาพ. ตารางสามรูปเดิมจากหน้า 4 กลับมาอีกครั้ง: filter ตัวเดียวที่ตรวจมุมของสี่เหลี่ยมถูกเลื่อนไปทั่วภาพ จึงพบสี่เหลี่ยมได้ทั้งมุมซ้ายบน กลางภาพ และมุมขวาล่าง. ปัญหาข้อ 2 ของ dense network (§1) จึงหมดไป.

Slide: Why ConvNets? (2) CNNs are composed of locally connected layers called convolutional layers, which use fewer parameters to be trained compared to a fully connected layer, thus less computation and less prone to overfitting. All neurons in a feature map share the same parameters (weight sharing). In the 6 by 6 example the parameter count is only 3x3+1 = 10 per filter, and the value 5 depends only on the green 3 by 3 area
Slide: Why ConvNets? (2) — locally connected และ weight sharing (Lecture7, p.23)

CNN ประกอบด้วย locally connected layers ซึ่งใช้ fewer parameters เมื่อเทียบกับ fully connected layer จึงคำนวณน้อยกว่าและ overfit ยากกว่า. มีสองกลไกที่ทำให้ parameter น้อย:

คำนวณ: dense เทียบกับ convolution สำหรับ output 16 ค่าจากภาพ 6×6
ให้ input 6×6 = 36 ค่า ต้องการ output 16 ค่า (เท่ากับ feature map 4×4)
  1. ขั้นที่ 1 — Dense layer 16 node
    $$36 \times 16 + 16 = 592$$
  2. ขั้นที่ 2 — Locally connected แต่ไม่แชร์ weight (16 node, node ละ 9 weight + 1 bias)
    $$16 \times (9 + 1) = 160$$
  3. ขั้นที่ 3 — Convolution (แชร์ weight): filter 3×3 หนึ่งตัว
    $$3 \times 3 + 1 = 10$$

592 → 160 จาก local connectivity และ 160 → 10 จาก weight sharing. รวมแล้วลดลง 59 เท่าในตัวอย่างเล็กนี้.

ออกสอบสูง — ข้อความที่อาจารย์ทำสี

9. Design Patterns และ Hyperparameter ของ CNN

Slide: Classical Design Patterns in CNN Architectures. Convnets are composed of a feature extraction part (stack a few convolutional layers each followed by ReLU, then a pooling layer, repeated; often one to three conv layers per pooling layer) and a classification part (a few fully-connected layers with ReLU, final layer outputs prediction). The image gets smaller and smaller but deeper and deeper; the number of filters grows, usually doubling. Example: 28x28 input, convolution 24x24, pooling 12x12, convolution 8x8, pooling 4x4, flatten, fully connected, softmax
Slide: Classical Design Patterns in CNN Architectures (Lecture7, p.24)

Convnet แบบ classical มีสองส่วน:

  1. ส่วน feature extraction: ซ้อน convolutional layer สองสามชั้น (แต่ละชั้นตามด้วย ReLU) แล้ว pooling layer แล้ว convolutional layer อีกชุด แล้ว pooling อีก. นิยมจัดกลุ่ม convolutional layer 1–3 ชั้นต่อ pooling 1 ชั้น
  2. ส่วน classification: fully-connected layer จำนวนไม่มาก (+ReLU) และ layer สุดท้ายให้ผลทำนาย

ระหว่างเดินผ่าน network image gets smaller and smaller แต่ deeper and deeper (มี feature map มากขึ้น). จำนวน filter โตตามความลึก และมักเพิ่มเป็นสองเท่า (เช่น 32 → 64 → 128).

ตัวอย่างในรูปของสไลด์ตรวจด้วยสูตรได้ทั้งหมด: input 28×28 ผ่าน convolution kernel 5×5 ได้ \(28-5+1 = 24\); max pooling 2×2 ได้ 12; convolution 5×5 ได้ \(12-5+1 = 8\); max pooling 2×2 ได้ 4; จากนั้น flatten เข้า fully connected และ softmax 10 class.

Slide: Modern Design Patterns in CNN Architectures. Classical CNN: Conv/Pool then Flatten then Dense. Modern CNN: often Conv blocks then Global Average Pooling then classifier. Diagram of ResNet-34 with a residual unit (two 3x3 convolutions with batch norm and ReLU plus a skip connection), global average pool, fully connected 1000 units and softmax. Note: most recent CNN architectures use pooling minimally or not at all
Slide: Modern Design Patterns in CNN Architectures — ResNet-34 (Lecture7, p.25)
รุ่นลำดับตามสไลด์
Classical CNNConv/Pool → Flatten → Dense
Modern CNNมักเป็น Conv blocks → Global Average Pooling → classifier

รูป ResNet-34 ในสไลด์เริ่มด้วย convolution 7×7 (64 filter, stride 2) และ max pool 3×3 (stride 2) หนึ่งครั้ง จากนั้นเป็น convolution 3×3 ต่อกันยาว. จุดที่จำนวน filter เพิ่มจาก 64 เป็น 128 ใช้ convolution stride 2 ย่อขนาดแทน pooling. ท้าย network คือ global average pool → fully connected 1000 units → softmax. หมายเหตุของสไลด์: CNN รุ่นใหม่ (เช่น ResNets) use pooling minimally or not at all. รายละเอียด residual unit อยู่ใน Lesson 8.

คำนวณ: Flatten เทียบกับ Global Average Pooling
ให้ feature map สุดท้ายขนาด 7×7×512 ต่อกับ dense layer 1,000 node
  1. ขั้นที่ 1 — Flatten: ทุกค่ากลายเป็น input
    $$\begin{gathered}7 \times 7 \times 512 = 25{,}088 \\[0.35em] \Rightarrow\; 25{,}088 \times 1{,}000 + 1{,}000 = 25{,}089{,}000\end{gathered}$$
  2. ขั้นที่ 2 — Global Average Pooling: เฉลี่ย 49 ค่าของแต่ละ channel เหลือ 1 ค่า
    $$512 \quad\Rightarrow\quad 512 \times 1{,}000 + 1{,}000 = 513{,}000$$

Global Average Pooling ลด parameter ของ classifier ลง 49 เท่า (เท่ากับ 7×7) และตัว GAP เองไม่มี parameter.

Slide: CNNs HyperParameter. The CONV layer attributes: filters = number of kernel filters in each layer (depth of the hidden layer); kernel_size = size of the filter, commonly 3x3; stride commonly 1, use 2 when downsampling; padding = add zeros around the border, same or valid; activation = ReLU is a common baseline. Dropout layer: dropout rate 0.3 to 0.5 is a good start, increase if overfitting, commonly used in the classification part
Slide: CNNs HyperParameter — อาร์กิวเมนต์ของ Conv2D และ Dropout (Lecture7, p.26)

นอกจาก hyperparameter ทั่วไปของ neural network (Lesson 6) CNN มีของตัวเอง:

Hyperparameterความหมายตามสไลด์ค่าที่ใช้บ่อย
filtersจำนวน kernel filter ของ layer (= depth ของ hidden layer)32, 64, 128 (เพิ่มสองเท่าตามความลึก)
kernel_sizeขนาดของ filter3×3
stridesก้าวของ filter1; ใช้ 2 เมื่อต้องการ downsampling
paddingเติมแถวและคอลัมน์ศูนย์รอบขอบเพื่อรักษาขนาดภาพใน layer ถัดไป"same" หรือ "valid"
activationactivation หลัง convolutionReLU เป็น baseline
Dropout rateสัดส่วน neuron ที่ถูกสุ่มปิดเริ่มที่ 0.3 ถึง 0.5; ถ้าเห็น overfitting ให้เพิ่ม. นิยมใช้ในส่วน classification
คำถาม 11Design pattern
ข้อใดอธิบายการเปลี่ยนแปลงของ feature map เมื่อเดินลึกเข้าไปใน CNN แบบ classical ได้ถูกต้อง?
คำถาม 12Design pattern
Feature map สุดท้ายของ CNN มีขนาด 5×5×256 ถ้าเปลี่ยนจาก Flatten เป็น Global Average Pooling ก่อน dense layer จำนวน input ของ dense layer จะเปลี่ยนจากเท่าใดเป็นเท่าใด?
ออกสอบสูง — ข้อความที่อาจารย์ทำสี

10. เขียน CNN ด้วย Keras

Slide: Example with Keras API. Code builds a Sequential model with Conv2D(filters=10, kernel_size=3, activation='relu', input_shape=(32,32,3)), MaxPooling2D(pool_size=2, strides=2), Conv2D(filters=20, kernel_size=5, strides=2, activation='relu'), MaxPooling2D(pool_size=2, strides=2), Flatten, Dense(100, relu), Dense(100, relu), Dense(1, sigmoid) and model.summary showing params 280, 0, 5020, 0, 0, 18100, 10100, 101, total 33,601
Slide: Example with Keras API — โค้ดของ network ในตารางหน้า 21 (Lecture7, p.27)

โค้ดหน้า 27 คือ network ของหน้า 20–21 ทุกชั้น. model.summary() ให้ตัวเลขชุดเดียวกับที่คำนวณมือใน §7.

model = models.Sequential()
model.add(Conv2D(filters=10,kernel_size=3,activation='relu',input_shape=(32,32,3)))
model.add(MaxPooling2D(pool_size=2,strides=2))
model.add(Conv2D(filters=20,kernel_size=5,strides=2, activation='relu'))
model.add(MaxPooling2D(pool_size=2,strides=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
โค้ดทำอะไร
Conv2D(filters=10, kernel_size=3, activation='relu', input_shape=(32,32,3))10 filter ขนาด 3×3. ไม่ระบุ strides ⇒ 1; ไม่ระบุ padding ⇒ 'valid'. input_shape คือ (สูง, กว้าง, channel) ไม่รวม batch. Output (None, 30, 30, 10), 280 parameter
MaxPooling2D(pool_size=2, strides=2)หน้าต่าง 2×2 ก้าวทีละ 2. Output (None, 15, 15, 10), 0 parameter. ถ้าไม่ระบุ strides Keras ใช้ค่าเท่ากับ pool_size
Conv2D(filters=20, kernel_size=5, strides=2, activation='relu')20 filter ขนาด 5×5 stride 2. Output (None, 6, 6, 20), 5,020 parameter
MaxPooling2D(pool_size=2, strides=2)Output (None, 3, 3, 20), 0 parameter
Flatten()แปลง 3×3×20 เป็น vector 180 ค่า. Output (None, 180), 0 parameter
Dense(100, activation='relu')180·100 + 100 = 18,100 parameter
Dense(100, activation='relu')100·100 + 100 = 10,100 parameter
Dense(1, activation='sigmoid')output สำหรับ binary classification, 101 parameter
model.summary()พิมพ์ Output Shape และ Param # ของทุกชั้น. None ตัวแรกคือ batch size ที่ยังไม่กำหนด. Total params: 33,601
Slide: Exercise: MNIST Dataset. Build a CNN with 3 CONV + 3 POOL + 1 FC (output layer): CONV1 32 filters, f=3, padding same, relu; POOL1 pool_size 2; CONV2 64 filters, f=3, padding same, relu; POOL2 pool_size 2; CONV3 128 filters, f=3, padding same, relu; POOL pool_size 2; FC 10 nodes softmax
Slide: Exercise: MNIST Dataset — 3 CONV + 3 POOL + 1 FC (Lecture7, p.28)

แบบฝึกหัดหน้า 28 กำหนดสถาปัตยกรรม 3 CONV + 3 POOL + 1 FC สำหรับ MNIST (ภาพ 28×28×1, 10 class). ทุก CONV ใช้ f = 3, padding = same, ReLU และจำนวน filter เพิ่มสองเท่า 32 → 64 → 128. (สไลด์พิมพ์ POOL ตัวที่สามว่า "POOL2" ซึ่งหมายถึง POOL3.) โค้ดที่ตรงกับ spec:

model = models.Sequential()
model.add(Conv2D(32, kernel_size=3, activation='relu', padding='SAME', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=3, activation='relu', padding='SAME'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, kernel_size=3, activation='relu', padding='SAME'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.summary()
โค้ดทำอะไร
X = X.reshape(60000, 28, 28, 1)(ขั้นเตรียมข้อมูลก่อนสร้างโมเดล) CNN ต้องการ input 4 มิติ (batch, สูง, กว้าง, channel). MNIST โหลดมาเป็น (60000, 28, 28) จึงต้องเพิ่มมิติ channel = 1. ต่างจาก Lesson 5 ที่ reshape เป็น (60000, 784)
Conv2D(32, kernel_size=3, activation='relu', padding='SAME', input_shape=(28, 28, 1))อาร์กิวเมนต์ตัวแรกคือ filters. padding same ⇒ ขนาดคงเดิม 28×28×32
MaxPooling2D(pool_size=(2, 2))ไม่ระบุ strides ⇒ เท่ากับ pool_size = 2. ขนาดลดครึ่ง
Flatten()แปลง feature map สุดท้ายเป็น vector ก่อนเข้า dense
Dense(10, activation='softmax')10 class ⇒ 10 node + softmax. ใช้คู่กับ loss sparse_categorical_crossentropy เมื่อ label เป็นจำนวนเต็ม 0–9
คำนวณ: shape และ parameter ของ CNN ในแบบฝึกหัด MNIST
ให้ input 28×28×1. CONV ทุกชั้น f = 3, padding same, stride 1. POOL ทุกชั้น pool size 2, stride 2
  1. ขั้นที่ 1 — CONV1 (32 filter) และ POOL1
    $$\begin{gathered}28 \times 28 \times 32,\quad (3 \cdot 3 \cdot 1 + 1) \cdot 32 = 320 \\[0.35em] \Rightarrow\; \lfloor (28-2)/2 \rfloor + 1 = 14 \;\Rightarrow\; 14 \times 14 \times 32\end{gathered}$$
  2. ขั้นที่ 2 — CONV2 (64 filter) และ POOL2
    $$\begin{gathered}14 \times 14 \times 64,\quad (3 \cdot 3 \cdot 32 + 1) \cdot 64 = 18{,}496 \\[0.35em] \Rightarrow\; 7 \times 7 \times 64\end{gathered}$$
  3. ขั้นที่ 3 — CONV3 (128 filter) และ POOL3
    $$\begin{gathered}7 \times 7 \times 128,\quad (3 \cdot 3 \cdot 64 + 1) \cdot 128 = 73{,}856 \\[0.35em] \Rightarrow\; \lfloor (7-2)/2 \rfloor + 1 = 3 \;\Rightarrow\; 3 \times 3 \times 128\end{gathered}$$
  4. ขั้นที่ 4 — Flatten และ FC 10 node
    $$3 \times 3 \times 128 = 1{,}152 \qquad 1{,}152 \times 10 + 10 = 11{,}530$$
  5. ขั้นที่ 5 — รวม
    $$320 + 18{,}496 + 73{,}856 + 11{,}530 = 104{,}202$$

Total params = 104,202 ตรงกับ model.summary() ของโมเดลนี้. จุดที่ต้องระวังคือ POOL3: \((7-2)/2 = 2.5\) ปัดลงเป็น 2 แล้วบวก 1 ได้ 3 ไม่ใช่ 3.5 หรือ 4. เทียบกับ dense network 784 → 16 → 16 → 10 ของ Lesson 5 ที่มี 13,002 parameter โมเดลนี้ใหญ่กว่า 8 เท่า และให้ test accuracy ราว 0.99.

คำถาม 13Keras code
โค้ด Conv2D(64, kernel_size=3, activation='relu') รับ input ขนาด (None, 20, 20, 32) โดยไม่ระบุ padding และ strides output shape คือข้อใด?
คำถาม 14Keras code
ต้องการเติมโค้ดให้ layer ย่อ feature map จาก 14×14×32 เป็น 7×7×32 โดยไม่มี parameter ที่เรียนได้ ข้อใดถูกต้อง?

11. Data Augmentation

Slide: Data Augmentation generates more training data from existing training examples by augmenting the examples via a number of random transformations. It does not permanently create and store a new dataset; it usually generates transformations on the fly as training batches are processed. The images are still heavily intercorrelated with original images, so data augmentation alone cannot completely get rid of overfitting. It does not always increase accuracy but frequently does, especially with small datasets
Slide: Data Augmentation — นิยามและข้อจำกัด (Lecture7, p.29)

Data augmentation สร้าง training data เพิ่มจากตัวอย่างที่มีอยู่ด้วยการ augmenting the examples via a number of random transformations (หมุน, เลื่อน, ซูม, กลับด้าน). แนวคิดนี้ปรากฏครั้งแรกใน Lesson 4 ในฐานะวิธีลด overfitting. สไลด์ระบุสามข้อ:

  1. มัน not permanently create and store a new dataset. การแปลงเกิด on the fly ขณะประมวลผลแต่ละ training batch. ภาพเดิมจึงถูกแปลงต่างกันทุก epoch และขนาด dataset บนดิสก์ไม่เปลี่ยน
  2. ภาพที่โมเดลเห็นยัง still heavily intercorrelated with original images เพราะมาจากภาพต้นฉบับชุดเดิม. data augmentation อย่างเดียวจึง cannot completely get rid of overfitting
  3. ไม่ได้เพิ่ม accuracy ของ CNN เสมอไป แต่เพิ่มบ่อย โดยเฉพาะกับ dataset ขนาดเล็ก
Slide: Data Augmentation in Keras. Keras performs data augmentation using preprocessing layers. Code creates data_augmentation = keras.Sequential with RandomRotation(0.1), RandomTranslation(0.2, 0.2), RandomZoom(0.2), RandomFlip('horizontal'), then a model with keras.Input(shape=(32,32,3)), data_augmentation, Conv2D(32, 3, padding='same', activation='relu'). Four augmented dog images are shown
Slide: Data Augmentation in Keras — preprocessing layers (Lecture7, p.30)

Keras ทำ data augmentation ด้วย preprocessing layers ซึ่งวางเป็น layer แรกของโมเดล. ภาพสุนัขสี่ภาพในสไลด์คือภาพต้นฉบับภาพเดียวที่ผ่าน pipeline สี่ครั้ง ได้การหมุน เลื่อน และซูมต่างกันทุกครั้ง.

# Create a data augmentation pipeline
data_augmentation = keras.Sequential([
    # Randomly rotate images by up to ±10% of 360° (±36°)
    keras.layers.RandomRotation(0.1),
    # Randomly shift images vertically and horizontally by up to ±20%
    keras.layers.RandomTranslation(0.2, 0.2),
    # Randomly zoom images by up to ±20%
    keras.layers.RandomZoom(0.2),
    # Randomly flip images horizontally
    keras.layers.RandomFlip("horizontal")
])

model = keras.Sequential([
    keras.Input(shape=(32, 32, 3)),
    data_augmentation,
    keras.layers.Conv2D(32, 3, padding="same", activation="relu"),
    ...
])
โค้ดทำอะไร
data_augmentation = keras.Sequential([...])รวม preprocessing layer หลายตัวเป็น pipeline เดียวที่ใช้เหมือน layer หนึ่งชั้น
keras.layers.RandomRotation(0.1)หมุนแบบสุ่มไม่เกิน ±10% ของ 360° = ±36°. ค่าเป็นสัดส่วนของรอบเต็ม ไม่ใช่องศา
keras.layers.RandomTranslation(0.2, 0.2)เลื่อนแนวตั้งและแนวนอนแบบสุ่มไม่เกิน ±20% ของความสูงและความกว้าง. ภาพ 32×32 เลื่อนได้ไม่เกิน 0.2 × 32 = 6.4 pixel
keras.layers.RandomZoom(0.2)ซูมเข้าหรือออกแบบสุ่มไม่เกิน ±20%
keras.layers.RandomFlip("horizontal")สุ่มกลับภาพซ้าย-ขวา
keras.Input(shape=(32, 32, 3))ประกาศ shape ของ input (แบบที่ Keras 3 แนะนำแทน input_shape= ใน layer แรก)
data_augmentation,วาง pipeline ก่อน Conv2D ชั้นแรก. layer กลุ่มนี้ทำงานเฉพาะตอน training; ตอน evaluate/predict ภาพผ่านไปโดยไม่ถูกแปลง
keras.layers.Conv2D(32, 3, padding="same", activation="relu")ส่วน CNN ตามปกติ: 32 filter, kernel 3×3. Output 32×32×32, parameter (3·3·3+1)·32 = 896
กลไกที่ต้องอธิบายได้
ทำไมต้องมี
โมเดล overfit เมื่อเห็นตัวอย่างเดิมซ้ำทุก epoch จนจำได้. การแปลงแบบสุ่มทำให้ไม่มี epoch ใดเห็นภาพเดิมทุก pixel และสอนให้โมเดลทนต่อการหมุน/เลื่อน/ซูม.
เปลี่ยนแล้วเกิดอะไร
เพิ่มค่า factor ⇒ ภาพหลากหลายขึ้นแต่ถ้ามากเกินวัตถุอาจหลุดเฟรมหรือความหมายเปลี่ยน (กลับเลข 6 แนวตั้งกลายเป็นคล้าย 9). จำนวนตัวอย่างต่อ epoch ไม่เพิ่ม: dataset 2,000 ภาพยังคง 2,000 ภาพต่อ epoch.
เชื่อมกับ
Dropout และ L2 (Lesson 4) เป็น regularization ที่ตัวโมเดล; data augmentation เป็น regularization ที่ตัวข้อมูล. ใช้ร่วมกันได้ และใช้ร่วมกับ transfer learning ใน Lesson 8.
คำถาม 15Data augmentation
ข้อใดอธิบายการทำงานของ data augmentation ด้วย preprocessing layers ของ Keras ได้ถูกต้อง?
คำถาม 16Data augmentation
keras.layers.RandomRotation(0.05) หมุนภาพแบบสุ่มได้มากที่สุดกี่องศาในแต่ละทิศ?
ออกสอบสูง — ข้อความที่อาจารย์ทำสี


ชัยชนะของบทเรียนนี้
คุณควร (1) คำนวณค่าใน feature map จาก input และ filter ที่กำหนด และอธิบายว่าทำไม filter [1, 0, −1] ตรวจ vertical edge; (2) ใช้สูตร \(\lfloor (n+2p-f)/s \rfloor + 1\) หาขนาด output ของ convolution และ pooling รวมกรณีปัดลง; (3) นับ parameter ของ convolution layer ด้วย \((f \cdot f \cdot n_C + 1) \cdot n_C'\) และสร้างตาราง Parameters Summary ได้ทั้งตาราง (280 / 5,020 / 18,100 / 10,100 / 101 = 33,601); (4) คำนวณ max/average pooling และอธิบาย translation invariance; (5) อธิบาย local connectivity กับ weight sharing ด้วยตัวเลข (592 → 160 → 10); (6) เขียนโค้ด Keras ของ CNN (Conv2D, MaxPooling2D, Flatten, Dense) และ pipeline ของ data augmentation ได้.

ภาคผนวก: Slide ทั้งหมดใน deep_learning_part3_1.pdf (30 หน้า) — เช็คว่าอ่านครบ

หน้าเนื้อหาสถานะ
1ปก Part 3 — Convolutional Neural Network & Transfer Learningetc — สไลด์ปก (ชื่อวิชา, ภาคเรียน, เวลาเรียน) ไม่มีเนื้อหาวิชา
2The Area of Computer Vision✓ หัวข้อ 1 (รูป + นิยาม + applications)
3Deep Learning for Computer Vision✓ หัวข้อ 1 (รูป + ภาพเป็นตัวเลข)
4Why not Densely Connected Neural Networks?✓ หัวข้อ 1 (รูป + คำนวณ parameter + input vector)
5Convolutional Neural Networks (CNNs)✓ หัวข้อ 2 (รูป + receptive field)
6CNNs Architecture✓ หัวข้อ 2 (รูป + ตาราง 4 ส่วน)
7Convolution Operation in Convolution Layers✓ หัวข้อ 3 (รูป + คำนวณ feature map)
8Why is This Doing Edge Detection? — vertical✓ หัวข้อ 3 (รูป + คำนวณ 0/240)
9Why is This Doing Edge Detection? (2) — horizontal✓ หัวข้อ 3 (รูป + คำนวณ 240/80/−80/−240)
10Filters✓ หัวข้อ 3 (รูป + filter element/size)
11Convolution Operator Problems✓ หัวข้อ 4 (รูป + สองข้อเสีย)
12Padding✓ หัวข้อ 4 (รูป + คำนวณ −7 และ p = (f−1)/2)
13Stride Convolution✓ หัวข้อ 4 (รูป + คำนวณ stride 2)
14Convolution on Volume✓ หัวข้อ 5 (รูป + channel ของ filter)
15Multiple Filters✓ หัวข้อ 5 (รูป + สูตร output volume)
16A Convolution Layer✓ หัวข้อ 5 (รูป + ตารางบทบาท + สูตร parameter)
17Put Together: Convolution & Densely Connected Layer✓ หัวข้อ 7 (รูป + คำนวณ 30/13/3,380)
18Pooling Layer✓ หัวข้อ 6 (รูป + คำนวณ max/average)
19Max Pooling Introduce Translation Invariant✓ หัวข้อ 6 (รูป + ภาพ A/B/C)
20Convolution Neural Network✓ หัวข้อ 7 (รูป + การนับ layer)
21Parameters Summary✓ หัวข้อ 7 (รูป + คำนวณทุกแถว)
22Why ConvNets?✓ หัวข้อ 8 (รูป + spatial features)
23Why ConvNets? (2)✓ หัวข้อ 8 (รูป + คำนวณ 592/160/10)
24Classical Design Patterns in CNN Architectures✓ หัวข้อ 9 (รูป + ตรวจ 28→24→12→8→4)
25Modern Design Patterns in CNN Architectures✓ หัวข้อ 9 (รูป + คำนวณ GAP)
26CNNs HyperParameter✓ หัวข้อ 9 (รูป + ตาราง hyperparameter)
27Example with Keras API✓ หัวข้อ 10 (รูป + โค้ด + summary)
28Exercise: MNIST Dataset✓ หัวข้อ 10 (รูป + โค้ด + คำนวณ 104,202)
29Data Augmentation✓ หัวข้อ 11 (รูป + สามข้อจำกัด)
30Data Augmentation in Keras✓ หัวข้อ 11 (รูป + โค้ด)

สงสัยตรงไหน ถามได้เลยในแชท — ผมเป็นผู้สอนของคุณ ถามซ้ำได้ ถามลึกได้ ไม่ต้องเกรงใจ.