Download as pdf or txt
Download as pdf or txt
You are on page 1of 78

EXERCISE - ADVANCED

CONVOLUTIONAL NEURAL NETWORK


Content

• Review Skip Connection


– Why do we need skip connection
– What are skip connection
– Variants of skip connection
• ResNet Architecture
• Densenet Architecture
• Unet Architecture
• Problem1: Implement ResNet34 Architecture:
– Analyze ResNet34 architecture and its components
– Implement step by step
Content
• Problem2: Implement Densenet121 Architecture:
– Analyze Densenet121 architecture and its components
– Implement step by step
• Problem3: Implement Unet Architecture:
– Analyze Unet architecture and its components
– Implement step by step
• Problem4: Weather Recognition with Skip Connection Model
– Prepare Data
– Recognize Weather with ResNet34 (without skip connection)
– Recognize Weather with ResNet34 (with skip connection)
– Recognize Weather with Densenet121 (without skip connection)
– Recognize Weather with Densenet121 (with skip connection)
– Compare results
Content

• Problem5: Super Resolution


– Review Super Resolution
– Image Super Resolution Using Unet Model (without skip connection)
– Image Super Resolution Using Unet Model (with skip connection)
– Compare results
Content

• Review Skip Connection


– Why do we need skip connection
– What are skip connection
– Variants of skip connection
• ResNet Architecture
• Densenet Architecture
• Unet Architecture
• Problem1: Implement ResNet34 Architecture:
– Analyze ResNet34 architecture and its components
– Implement step by step
Review Skip Connection

• Why do we need skip connection


– Human error rate: 5%
Review Skip Connection

• Why do we need skip connection


– Degradation problem: (Not Overfitting)
• Theory: Deep network có thể học complex function và hiệu quả hơn shallow network.
• Practice: Performance của network bị giảm khi add thêm nhiều layer hơn nữa
• Một phần nguyên nhân có thể là vanishing gradient, nhưng tác giả ResNet (He et al.) đã
khởi tạo weight và sử dụng BatchNormalization để đảm bảo có gradient tốt, nhưng
performance vẫn giảm

https://arxiv.org/abs/1512.03385v1
Review Skip Connection

• Why do we need skip connection


– Thử nghiệm 2 model: Shallow và deeper model

- Cùng train trên 1 tập

- Các layers màu xanh dương: khởi


tạo cùng giá trị weight

- Các layers xanh lá: set weight là


identity matrices

- Hypotheisis: khi training deeper


model perfomance tối thiểu bằng
shallow vì bản chất 2 model giống
nhau

https://www.analyticsvidhya.com/blog/2021/08/all-you-need-to-know-about-skip-connections/
Review Skip Connection

• Why do we need skip connection


– Thử nghiệm 2 model: Shallow và deeper model

- Thực tế performance của deeper model không tốt


bằng shallow model

- Deeper layer không thể học được identity mappings

- “not all systems are similarly easy to optimize”.

https://www.analyticsvidhya.com/blog/2021/08/all-you-need-to-know-about-skip-connections/
Review Skip Connection

• Why do we need skip connection


– Thử nghiệm 2 model: Shallow và deeper model

- Nguyên nhân: là weight thường được khởi tạo


với mean=0, và sử dụng các regularization

- Weight trong model khi học sẽ xung quanh 0


và không thể học được identity mapping

- Skip connections có thể giải quyết được vấn


đề này

https://www.analyticsvidhya.com/blog/2021/08/all-you-need-to-know-about-skip-connections/
Review Skip Connection

• What are skip connection


– Skip connection: output của 1 layer sẽ bỏ qua một số layer tiếp theo và tham
gia vào như là input của một layer khác cách đó nhiều layer trung gian
– Skip connection: có nhiều biến thể tùy thuộc vào kiến trúc sử dụng
• Resnet architecture: giải quyết vấn đề degradation
• Densenet architercture: các feature của layer trước đó đều được sử dụng lại

Skip
connection
Review Skip Connection

• What are skip connection


– Neural networks có thể học được các function phức tạp (high-dimensional)
với điều kiện là non-convex
– Visualize loss surface: cho thấy rằng skip connection giúp việc học mượt hơn
và hội tụ nhanh hơn

https://arxiv.org/pdf/1712.09913.pdf complex loss surfaces


Review Skip Connection

• Variants of skip connection


– Cách cài đặt skip connection được chia làm 2 loại: short và long skip
coonection

Skip connection
Short skip connection :
(ví dụ Renset)

Skip connection
Long skip connection :
(ví dụ Unet)
Review Skip Connection

• Variants of skip connection


– Có 2 biến thể chính: Addition và Concatenation

Skip
connection

ADDITION CONCATENATION
Review Skip Connection

• Variants of skip connection


– Addition: Cộng output layer và skip connection

ADDITION

- Advantage: Input size của layer tiếp


theo không tăng

- Disadvantage: Có thể tạo ra nhiễu


cho layer tiếp theo
Review Skip Connection

• Variants of skip connection


– Concatenation: Ghép output layer với skip connection

CONCATENATION

- Advantage: Các layer đều được sử


dụng (ví dụ: thông tin của layer 1 vẫn
được sử dụng ở layer 3 và không thay
đổi giá trị)

- Disadvantage: Kích thước tăng dần


khi qua mỗi layer
Review Skip Connection

• Variants of skip connection


– Highway Networks (Srivastava et al.)

https://www.tutorialexample.com/a-beginner-introduction-to-highway-networks-machine-learning-tutorial/
Variants of skip connection

• ResNet Architecture
– Concatenation:

( ) = ( ) +

https://arxiv.org/abs/1512.03385v1
Variants of skip connection

• ResNet Architecture https://arxiv.org/abs/1512.03385v1

– Concatenation:
Variants of skip connection

• ResNet Architecture

( ) = ( ) +

= = ( + 1) = +

- Khi không cần học ở nhóm layer này thì nó sẽ


được điều hướng và học như identity function

- Gradient qua các layer có thể sẽ nhỏ dần và


bằng 0, do đó skip connection giup thông tin truyền
ngược lại dễ hơn
Variants of skip connection

• ResNet Architecture

https://slideplayer.com/slide/14282282/
Variants of skip connection

• ResNet Architecture

Option A Option B

Skip connection: vẫn là Skip connection: lúc này


identity, phần diminesion bị được gọi là projection
thiếu ở input sẽ được zero- shortcut sử dụng
padding để thực hiện add convolutional layer với
với output kernel size = (1x1)

  ≠ ( )  
Variants of skip connection

• ResNet Architecture
Renset Option A Renset Option B Renset Option C

Sử dụng skip Sử dụng skip Sử dụng skip


connection connection connection
Option A cho Option B cho Option B cho
toàn bộ các block có toàn bộ
network input và output network
dimension khác
nhau, còn lại là
Option A

https://arxiv.org/abs/1512.03385v1
Variants of skip connection

• ResNet Architecture
- Tất cả option đều tốt hơn plain network

- Resnet option B tốt hơn Resnet option A vì phần skip


connection ở A thực tế là zero-pading nên sẽ không learn
như của B

- Resnet option ở C tốt hơn Resnet option B vì yếu tố


tương tự skip connection ở C được học hoàn toàn thay vì B

- Việc dùng projection shortcuts nhìn vào từ table có cải thiện


kết quả một ít. Để cân bằng về độ chính xác và số lượng
parameter option B được chọn làm model chính

https://arxiv.org/abs/1512.03385v1
Variants of skip connection

• ResNet Architecture

So sánh model không sử dụng skip connection và skip


connection với option A . Cả 2 model đều có cùng số
lượng parameter

https://arxiv.org/abs/1512.03385v1
Variants of skip connection

• ResNet Architecture

https://arxiv.org/abs/1512.03385v1
Variants of skip connection

• ResNet Architecture

Identity Shortcut

Model trong thực tế mỗi


Convolution layer sẽ có 1
BatchNormalization layer đi
cùng Projection Shortcut
Variants of skip connection
Original Identity , Bottleneck
• ResNet Architecture Projection Shortcuts Architecture

https://arxiv.org/abs/1512.03385v1
Variants of skip connection

• ResNet Architecture

- Model càng sâu thời gian training để hội


Deeper Bottleneck Architectures tụ là một vấn để lớn

- Bottleneck được thiết kế để làm giảm thời


gian training (giảm số lượng parameters)

- 1 stack bottleneck của 3 layer (conv(1x1),


(3x3 )) được dùng thay vì 2, ý nghĩa
conv(1x1) là làm giảm dimension, để
conv(3x3) học và conv(1x1) tiếp theo
restore lại dimension

https://arxiv.org/abs/1512.03385v1
Variants of skip connection

• ResNet Architecture

- Training on CIFAR-10.
- Dashed lines denote training error, and bold lines denote testing error.
- Left: plain networks. The error of plain-110 is higher than 60% and not displayed.
- Middle: ResNets.
- Right: ResNets with 110 and 1202 layers.
https://arxiv.org/abs/1512.03385v1
Variants of skip connection
• ResNet Architecture
( ) = ( ) +

= = ( + 1) = +

- Khi không cần học ở nhóm layer này thì nó sẽ được điều hướng và học như identity
function

- Gradient qua các layer có thể sẽ nhỏ dần và bằng 0, do đó skip connection giup thông
tin truyền ngược lại dễ hơn

- ResNet với kỹ thuật skip connection cho phép xây dựng mode sâu hơn và
performance vẫn tốt hơn so với shallow network.

- ResNet bản gốc được dùng cho image classification và sau này được sử dụng như
backbone họặc cơ chế skip connection được sử dụng rộng rãi trong image
segmentation, keypoint detection and object detection, ...
Variants of skip connection

• Densenet Architecture
- Làm giảm được vanishing-gradient problem

- Strengthen feature propagation

- Các feature sau sử dụng lại các feature trước

- Giảm lượng lớn parameter

- Skip Connection sử dụng concatenate

https://arxiv.org/pdf/1608.06993.pdf
Variants of skip connection
• Densenet Architecture
Resnet Densenet

https://www.youtube.com/watch?v=-W6y8xnd--U&t=527s
Variants of skip connection
• Densenet Architecture

Layer cuối cùng sẽ có tất cả thông tin về feature map của các layer trước đó

https://www.youtube.com/watch?v=-W6y8xnd--U&t=527s
Variants of skip connection
• Densenet Architecture

Càng tiến về sâu


hơn kích thước
channel sẽ tăng
rất nhiều. Tốn
nhiều tài nguyên
tính toán hơn

Sử dụng
convolution layer
1x1 để giảm kích
thước dimension

https://www.youtube.com/watch?v=-W6y8xnd--U&t=527s
Variants of skip connection

• Densenet Architecture

Transition Layer

https://www.youtube.com/watch?v=-W6y8xnd--U&t=527s
Variants of skip connection

• Densenet Architecture
– k là growth rate: quyết định số lượng channel

Dense block
Transition layer

Convolution block
4*k
channels 0,5*k
channels
k
channels

https://www.youtube.com/watch?v=-W6y8xnd--U&t=527s
Variants of skip connection

• Densenet Architecture

https://arxiv.org/pdf/1608.06993.pdf
Variants of skip connection

• Densenet Architecture

Strong Gradient Flow

https://www.youtube.com/watch?v=-W6y8xnd--U&t=527s
Variants of skip connection

• Densenet Architecture
Parameter and Computation Efficiency - l: là layer
index

- concatenate:
giúp cho việc
feature input
được đa dạng
hơn

https://www.youtube.com/watch?v=-W6y8xnd--U&t=527s
Variants of skip connection

• Densenet Architecture
Parameter and Computation Efficiency - Làm tăng
được độ mạnh
và phức tạp
của các
feautres

- Giúp cho việc


phân loại dễ
hơn, decision
boundary
smooth hơn

https://www.youtube.com/watch?v=-W6y8xnd--U&t=527s
Variants of skip connection

• Densenet Architecture
Parameter and Computation Efficiency

https://www.youtube.com/watch?v=-W6y8xnd--U&t=527s
Variants of skip connection

• Densenet Architecture

- Một số version của


Densenet gần tương đương
performance với resnet
nhưng số lượng parameter
gần ít hơn một nửa

https://arxiv.org/pdf/1608.06993.pdf
Variants of skip connection

• Unet Architecture

UNet có tác dụng bổ sung thêm


thông tin để xây dựng output (là một
hình) tốt và chi tiết hơn

https://arxiv.org/abs/1505.04597v1
Variants of skip connection

• Unet Architecture

Decoder: là phần
Encoder: Là phần khôi phục lại kích
hoạt động bình thước feature map,
thường như CNN kết hợp với skip
model (vd VGG19) connection để tạo
để extract feature output là image

https://arxiv.org/abs/1505.04597v1
Variants of skip connection

• Unet Architecture
– Các biến thể khác : Vnet

https://theaisummer.com/unet-architectures/
Variants of skip connection

• Unet Architecture
– Các biến thể khác : Unet++

https://theaisummer.com/unet-architectures/
Content

• Review Skip Connection


– Why do we need skip connection
– What are skip connection
– Variants of skip connection
• ResNet Architecture
• Densenet Architecture
• Unet Architecture
• Problem1: Implement ResNet34 Architecture
– Analyze ResNet34 architecture and its components
– Implement step by step
Problem1: Implement ResNet34 Architectures

• Yêu cầu đề bài: Xây dựng ResNet34


Problem1: Implement ResNet34 Architectures

• Yêu cầu đề bài: Xây dựng ResNet34

- Conv2D layers 7x7, stride=2, filter=64 + Max pooling 3x3,


stride=2

- Skip connecion block (thực hiện Add)(identity shortcut,


projection shortcut, convolutional layers và batchnorm layer).
Những layer có ký hiệu ’/2’ trong hình 1 là dùng stride bằng 2
để giảm kích thước featuremap đi 1/2

- Global Average pooling (* average pool hình 2)

- Fully connect layer cho việc phân loại

- Những block dùng identity shortcut (đường thẳng nối liền) và


những block dùng projection shortcut (conv2, kernel 1x1,
stride=2 là đường nét đứt, các chấm)

- Sau mỗi conv2D là 1 BatchNorm layer


Problem1: Implement ResNet34 Architectures

• Yêu cầu đề bài: Xây dựng ResNet34


Problem1: Implement ResNet34 Architectures

• Yêu cầu đề bài: Xây dựng ResNet34

Identity Shortcut

Projection Shortcut
Problem1: Implement ResNet34 Architectures

• Yêu cầu đề bài: Xây dựng ResNet34

Projection Shortcut
Content
• Problem2: Implement Densenet121 Architecture
– Analyze Densenet121 architecture and its components
– Implement step by step
• Problem3: Implement Unet Architecture:
– Analyze Unet architecture and its components
– Implement step by step
• Problem4: Weather Recognition with Skip Connection Model
– Prepare Data
– Recognize Weather with ResNet34 (without skip connection)
– Recognize Weather with ResNet34 (with skip connection)
– Recognize Weather with Densenet121 (without skip connection)
– Recognize Weather with Densenet121 (with skip connection)
– Compare results
Problem2: Implement Densenet121 Architecture

• Yêu cầu đề bài: Xây dựng Densenet121


Problem2: Implement Densenet121 Architecture

• Yêu cầu đề bài: Xây dựng Densenet121


- Conv2D layers 7x7, stride=2, filter=64, + BatchNorm
+ Max pooling 3x3, stride=2
- Dense Block: (thực hiện Concatenate) chứa các
conv_block, mỗi conv_block gồm các conv layer
(BatchNorm + Relu + Conv2D). Ví dụ: Conv(1x1) =
BatchNorm + Relu + Conv2D 1x1. Mỗi conv_block
gồm Conv(1x1) và Conv(3,3) và skip connection được
thực hiện bằng cách concatenate input với output sau
khi qua Conv(1x1) và Conv(3x3)
- Transition Layer: Conv(1x1) layer (tương tự như
Conv_layer trong dense block: BatchNorm+ Relu +
Conv2D 1x1 ) + Average Pooling layer 2x2 stride=2
- Luu ý: Trong Dense block, Conv(1x1) layer sẽ sử
dụng Conv2D có số lượng filters=4*k, Conv(3x3) layer
sẽ sử dụng Conv2D có số lượng filters=k, k=32. Còn
trong Transition Layer, Conv(1x1) layer sẽ sử dụng
Conv2D có số lượng filters= 1/2 channel (filters) của
input.
Problem2: Implement Densenet121 Architecture

• Densenet Architecture
– k là growth rate: quyết định số lượng channel

Dense block

Convolution block
4*k
channels

k
channels
Problem2: Implement Densenet121 Architecture

• Densenet Architecture
– k là growth rate: quyết định số lượng channel

Transition layer

0,5*k
channels
Content
• Problem2: Implement Densenet121 Architecture:
– Analyze Densenet121 architecture and its components
– Implement step by step
• Problem3: Implement Unet Architecture
– Analyze Unet architecture and its components
– Implement step by step
• Problem4: Weather Recognition with Skip Connection Model
– Prepare Data
– Recognize Weather with ResNet34 (without skip connection)
– Recognize Weather with ResNet34 (with skip connection)
– Recognize Weather with Densenet121 (without skip connection)
– Recognize Weather with Densenet121 (with skip connection)
– Compare results
Problem3: Implement Unet Architecture

• Yêu Cầu Đề Bài: Xây dựng Unet


Problem3: Implement Unet Architecture
• Yêu Cầu Đề Bài: Xây dựng Unet
Content
• Problem2: Implement Densenet121 Architecture:
– Analyze Densenet121 architecture and its components
– Implement step by step
• Problem3: Implement Unet Architecture:
– Analyze Unet architecture and its components
– Implement step by step
• Problem4: Weather Recognition with Skip Connection Model
– Prepare Data
– Recognize Weather with ResNet34 (without skip connection)
– Recognize Weather with ResNet34 (with skip connection)
– Recognize Weather with Densenet121 (without skip connection)
– Recognize Weather with Densenet121 (with skip connection)
– Compare results
Problem2: Comparing Downsample Techniques
- Resnet34 (no skip connection)
Model
• Yêu cầu đề bài Ảnh thời tiết 11 Classes trạng - Resnet34
thái thời tiết
- Densenet121 (no skip connection)
- Densenet121
Problem2: Comparing Downsample Techniques
• Results (Resnet34 no skip connection)
Problem2: Comparing Downsample Techniques
• Results (Resnet34)
Problem2: Comparing Downsample Techniques
• Results(Densenet121 no skip connection)
Problem2: Comparing Downsample Techniques
• Results(Densenet121)
Problem2: Comparing Downsample Techniques
• Compare Results

ResNet34 ResNet34
No Skip
Connection

Densenet121 Densenet121
No skip
connection
Problem2: Comparing Downsample Techniques
• Compare Results

ResNet34 ResNet34
No Skip
Connection

Densenet121 Densenet121
No skip
connection
Content

• Problem5: Super Resolution


– Review Super Resolution
– Image Super Resolution Using Unet Model (without skip connection)
– Image Super Resolution Using Unet Model (with skip connection)
– Compare results
Problem5: Super Resolution

• Yêu cầu đề bài Model


Ảnh size nhỏ Ảnh size lớn (4x)

- Cho 1 tập data ảnh không


có label
- Tự tạo tập data cho bài
toán Super Resolution
Problem5: Super Resolution
• Giới thiệu Super Resolution
Pre-Upsampling Post-Upsampling

Progressive Upsampling Iterative Up and Down Sampling

https://beyondminds.ai/blog/an-introduction-to-super-resolution-using-deep-learning/
Problem5: Super Resolution

Input 64x64, tạo model mới


gồm 1 resize layer để resize
ảnh thành 256x256 sau đó
cho ảnh này đi tiếp Unet
mode ở problem 3
Problem5: Super Resolution
• Results (Unet no skip connection)

Epoch
thứ nhất
Problem5: Super Resolution
• Results (Unet no skip connection)

Epoch
thứ 40
Problem5: Super Resolution
• Results (Unet)

Epoch
thứ nhất
Problem5: Super Resolution
• Results (Unet)

Epoch
thứ 40
Problem5: Super Resolution
Epoch thứ nhất unet (no skip connection )

Ở epoch thứ nhất đã thấy


được sự khác biệt rõ ràng
giữa ảnh predict.

- Unet (no skip connection)


không có hoặc rất ít thông
được background mà chỉ có
thông tin chính mà model học
được là cái xe (vùng đen gần
Epoch thứ nhất unet tâm ảnh)

- Unet nhờ có skip connection


nên hầu như thông tin
background cũng đã được
giữ lại trong epoch đầu tiên

You might also like