80
Deep Learning Bootcamp with PyTorch Gyungin Shin

Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Deep Learning Bootcamp with PyTorch

Gyungin Shin

Page 2: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

ContentsPart I. Classification Model

1. Multilayer perceptron

2. Convolutional neural networks

3. Residual networks

4. Densely connected convolutional networks

5. Attention modules - squeeze and excitation networks

6. Attention modules - gather-excite

7. Attention modules - convolutional block attention module

Page 3: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Contents

Part II. Time Series Data (will be updated)

8. Recurrent neural networks

9. Long short-term memory

10. seq2seq

11. seq2seq with attention

Page 4: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

ContentsPart III. Generative Model

12. Generative adversarial networks

13. Deep convolutional adversarial networks

14. Conditional generative adversarial networks

15. Image-to-image translation with conditional adversarial networks

16. Unpaired image-to-image translation using cycle-consistent adversarial networks

Page 5: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Appendix

A. Python Class

Contents

Page 6: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Introduction

Page 7: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Goal of lecture

Getting used to

• read a PyTorch code of interest,

• make a deep learning model using PyTorch.

Page 8: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Prerequisite

You need to have overall understanding of ‘class’ in Python. If you are not familiar with class, you may refer to Appendix A.

Page 9: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

What is PyTorch?

PyTorch is an open-source machine learning library for Python, based on Torch.

• Tensor computation with strong GPU acceleration*

• Deep neural networks built on a tape-based autograd system

Page 10: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Why PyTorch?

• A lot easier to learn than to learn TensorFlow.

• Purely Pythonic.

• Recently public codes for deep learning research papers are usually written with PyTorch.

Page 11: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Basics

Page 12: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

What is deep learning?

NeuronBrain

Cell body

Synapse

Neural NetworksModel

: Node: Weight

Page 13: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

What is deep learning?

Feature extraction Classification

Feature extraction + Classification

Traditional machine learning algorithm

Deep learning

Object Probability

E.g. classification

Page 14: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

How many buttons?

Machine learning researcher:

“Find things having a circular shape and three holes.”

Deep learning researcher:

“I don’t care how you count them. Anyway the answer is four. Train yourself why the answer is four.”

Page 15: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

How many buttons?Machine learning model: “Okay. Although they are circular, as they don’t have three holes, they are not button. The answer is 0.”

Deep learning model: “Hmm… they don’t exactly match what I saw previously, somehow they look like ‘button’. Maybe the answer is 5?”

Page 16: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Part I Classification Model

Page 17: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

1. Multilayer perceptron

Page 18: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

How does it work?

Forward path

Backward path (Gradient Back-propagation)

Penguin: 0.01

Penguin: 1.0

Difference measurement

(Loss)

… ∇θ L

Iteration

Input

Page 19: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Forward path

Page 20: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

W0, 0

W1, 0

W2, 0…

W1024, 0

Forward path1

32 pixels

32 pixels

X1

X2

X1024

Y0

Y1

Y2

Ym

Z0

Z1

Z2

Zm

Input layer 1st Hidden layer 1st Activation layer

Z = σ(Y )Lth Hidden layer

Y0(L)

Y1(L)

Y2(L)

YN-1(L)

…Z0(L)

Z1(L)

Z2(L)

ZN-1(L)

Z(L) = softmax(Y (L))Output layer

Flattening 32x32 -> 1024

Y = WX

Densely connected (fully connected)

Bias Activation1024

∑i=0

XiWi,0

Page 21: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Forward path

Y = -X + 0.5 -> X + Y - 0.5 -> X1 + X2 - 0.5

Linearly separable

(1, 1)(0, 1)

(0, 0) (1, 0)

1

X1

X2

Z

-0.5

1

1

0 if Z <= 0

1 if Z > 0

(0, 0) -> -0.5 -> 0 (0, 1) -> 0.5 -> 1 (1, 0) -> 0.5 -> 1 (1, 1) -> 1.5 -> 1

A perceptron

: 0

: 1

Decision line

X1

X2

Page 22: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Forward path

X1 + X2 - 1.5

X1 + X2 - 0.5

(0, 1) (1, 1)

(1, 0)(0, 0)

1

X1

X2

Z1

-0.5

1

1

0 if Z <= 0

1 if Z > 0

1

X1

X2

Z2

1.5

-1

-1

0 if Z <= 0

1 if Z > 0

X1

X2

Page 23: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

1

X1

X2

Z1

-0.5

1

1

1

X1

X2

Z2

1.5

-1

-1

1

X1

X2

Z1

-0.5

1

1Z2

1.5

-1

-1

Forward path

Page 24: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Forward path

(1, 1)

(1, 0)(0, 0)X1

X2

Z1

Z2

(0, 1)

Page 25: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Z1

Z2

Forward path

1

X1

X2

Z1

-0.5

1

1Z2

1.5

-1

-1

L

-1.51

1

1

Double layer

Page 26: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Forward path• Why do we need an activation function?

Without any activation function, it is infeasible to approximate higher degree function.

Y1 = WX1 + B

Y2 = W(WX1 + B) + B = W’X1 + B’ = WX1 + B

Y1 = WX1 + B

Z1 = f(Y1)

Y2 = WZ1 + B

where f(.) is non-linear activation function

Thus we add an activation function after a weight layer.

Page 27: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Activation functions

• ReLU (Rectified linear unit) :

• Leaky ReLU :

• Hyperbolic tangent (tanh):

• Logistic sigmoid :

ex − e−x

ex + e−x

11 + e−x

=ex

ex + 1=

12

+12

tanh(x2

)

max(0, x)

max(0, x) + negative slope × min(0, x)

Page 28: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

W0, 0

W1, 0

W2, 0…

W1024, 0

Forward path1

32 pixels

32 pixels

X1

X2

X1024

Y0

Y1

Y2

Ym

Z0

Z1

Z2

Zm

Input layer 1st Hidden layer 1st Activation layer

Z = σ(Y )Lth Hidden layer

Y0(L)

Y1(L)

Y2(L)

YN-1(L)

…Z0(L)

Z1(L)

Z2(L)

ZN-1(L)

Z(L) = softmax(Y (L))Output layer

Flattening 32x32 -> 1024

Y = WX

Densely connected (fully connected)

Bias Activation1024

∑i=0

XiWi,0

softmax(yi) =eyi

∑Cj=1 eyj

Page 29: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Backward path

Page 30: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Backward pathZ0(L)

Z1(L)

Z2(L)

ZN-1(L)

Cross-entropy loss

L = −N−1

∑k=0

tklogZk = − logZi

L

∂L∂Z (L)

0

∂L∂Z(L)

1

∂L∂Z (L)

2

∂L∂Z(L)

N−1

Y0(L)

Y1(L)

Y2(L)

YN-1(L)

∂Z (L)0

∂Y (L)0

∂Z(L)1

∂Y (L)1

∂Z (L)2

∂Y (L)2

∂Z(L)N−1

∂Y (L)N−1

1

X1

X2

X1024

Y0

Y1

Y2

Ym

W0, 0

W1, 0

W2, 0…

Z0

Z1

Z2

Zm…

W1024, 0…

Bias ∂Z0

∂Y0

∂Z1

∂Y1

∂Z2

∂Y2

∂ZM

∂YM

∂L∂W0, 0

=∂L∂Y0

∂Y0

∂W0, 0

W0, 0 = W0, 0 − ρ∂L

∂W0, 0

Page 31: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

• Gradient back-propagation updates(changes) weights to have lower loss in a way loss decreases most fast.

• However, this does not guarantee the loss converges to its global minimum.

Backward path

Page 32: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Practice

Page 33: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

MNIST database

Training set: 60,000 images and labels Test set: 10,000 images and labels

The MNIST database (Modified National Institute of Standards and Technology database) is a large database of handwritten digits that is commonly used for training various image processing systems.

Page 34: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

2. Convolutional neural networks

Page 35: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Drawbacks of fully-connected layer

A. Fully-connected (FC) layer does NOT consider “the shape of data.” It is worth noticing that we should make data flattened when we feed a MLP model.

32 pixels

32 pixels

X1

X2

X1024

Input layer

Flattening 32x32 -> 1024

Loss of spatial information!

Page 36: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Drawbacks of fully-connected layer

B. FC layers have a large number of weights which tend to cause overfitting.

* Overfitting refers to a situation where a model memorizes correct answers for training samples so that it loses general performance on test samples which are not shown to model during training. This is mainly due to the exceeding number of parameters (weights) required to train on a certain dataset.

-: Overfitted -: Regularized

-: Training -: Test

Page 37: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Convolution

1 4 2 11 3 1 10 2 1 34 1 1 0

2 0 11 1 20 2 3

17

1 4 2 11 3 1 10 2 1 34 1 1 0

1 4 2 11 3 1 10 2 1 34 1 1 0

1 4 2 11 3 1 10 2 1 34 1 1 0

2 0 11 1 20 2 3

2 0 11 1 20 2 3

2 0 11 1 20 2 3

17 26

17 26

12

17 26

12 18

=

=

=

=

Input feature map

Output feature map

Convolution filter (or kernel)

Page 38: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Convolution• What do kernels do?

0 0 0 1

0 0 1 0

0 1 0 0

1 0 0 0

0 0 01 1 10 0 0

0 0 10 1 01 0 0

1 0 00 1 00 0 1

0 1 00 1 00 1 0

0 3

3 0

1 1

1 1

0 1

1 0

1 1

1 1

=

=

=

=

Most meaningful kernel

Least meaningful kernel

Input feature map

Output feature map

Page 39: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Convolution

https://medium.com/@anuj_shah/through-the-eyes-of-gabor-filter-17d1fdb3ac97

Page 40: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Convolution

32 pixels

32 pixels

1x32x32 (ICxHxW)

Nx1x3x3 (OCxICxKHxKW)

……

… … ……

……

… … ……

……

… … ……

……

… … ……

……

… … ……

……

… … ……

Nx30x30 (OCxHxW)

Convolution

H: Height W: Width OC: Output Channel IC: Input Channel KH: Kernel Height KW: Kernel Width

. ..⊕

Nx1 (OCx1)

Channelwise Addition

=

Bias

N filters

Shape of data has changed!

Page 41: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Convolution

=

7x7 Input feature map

3x3 Kernel 5x5

Output feature map

o = i − k + 1

7 - 3 + 1 = 5

Page 42: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Convolution

=

(7 + 2 x p)x(7 + 2 x p) Input feature map

3x3 Kernel

7x7 Output feature map

⊛Padding=1

o = i − k + 2p + 1

7 - 3 + 2 * 1 + 1 = 7

Page 43: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Convolution

=

(7 + 2 x p)x(7 + 2 x p) Input feature map

3x3 Kernel

4x4 Output feature map

⊛Padding=1 Stride=2

o = ⌊i − k + 2p

s⌋ + 1

(7 - 3 + 2 * 1) // 2 + 1 = 4

Page 44: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Convolutional neural network

32 pixels

32 pixels

CONV: CONVolution FC: Fully Connected layer ReLU: Rectified Linear Unit SM: SoftMax activation layer

CONV

ReLU

FC

SM

CONV

ReLU

CONV

ReLU

… …

Prediction

Page 45: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Practice

Page 46: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

MNIST database

Training set: 60,000 images and labels Test set: 10,000 images and labels

The MNIST database (Modified National Institute of Standards and Technology database) is a large database of handwritten digits that is commonly used for training various image processing systems.

Page 47: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

3. Residual Networks

Page 48: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

When feature map size changes, 1x1 convolution with stride 2 is applied such that the shortcut connection can be presented every two convolution layers.

CON V

B N

R e L U

C O N V

B N

Element-wise summation

R e L U

Page 49: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

4. Densely Connected Networks

Page 50: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Dense Network

AVG: AVeraGe pooling BN: BatchNormalization layer CONV: Convolutional layer FC: Fully-Connected layer GAVG: Global AVeraGe pooling ReLU: Rectified Linear Unit

Dense Block

CON V

GAVG

FC

3x3

B N

CON V

R e L U

Dense Block

AVG

1x1

B N

CON V

R e L U

Dense Block

AVG

1x1

Page 51: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Dense Block

I N P U T

O P U T

B N

CON V

R e L U

Concatenation

B N

CON V

R e L U

B N

CON V

R e L U

OR

DenseNet-BC1x1 3x33x3

Page 52: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

5. Squeeze-and-Excitement Networks

Page 53: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Input feature map

GAVG

DENSE

Re LU

DENSE

S i gm o i d

Attention map

Excitation - Element-wise multiplication

Output feature map

Squeeze

Squeeze-and-Excitation Block

DENSE: Fully-connected layer GAVG: Global AVeraGe pooling ReLU: Rectified Linear Unit

Page 54: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Part II Time Series Data

Page 55: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Part III Generative Model

Page 56: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Various generative models

• Hidden Markov Model (HMM)

• Restricted Boltzmann Machine (RBM)

• Variational Auto-Encoder (VAE)

• Recurrent Neural Network (RNN)

• Generative Adversarial Network (GAN)

Page 57: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

GAN

Page 58: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

What is GAN?

Fake

Counterfeiter (Generator)

Police (Discriminator)

Introspection result

Page 59: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

How does GAN work?

. .. . .

.. .

. . ..

. ..

. ..

..

....

..

.

...

ReshapeDifference

measurement by Discriminator

Generated image

Target image

Forward

Backward ∇θ L

L

Random vector

E.g. image generation model

Generator

Page 60: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

How does GAN work?. .

.

. ..

. ..

..

....

..

.

...

or . ..

. ... .

.

Sigmoid [0, 1]

…Flatten

Discriminator

Page 61: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Practice

Page 62: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

CIFAR10airplane

automobile

bird

cat

deer

dog

frog

horse

ship

truck

The CIFAR-10 dataset consists of 60,000 32x32 color images in 10 classes, with 6,000 images per class. There are 50,000 training images and 10,000 test images.

Credit. Learning Multiple Layers of Features from Tiny Images, Alex Krizhevsky, 2009.

Page 63: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Conditional GAN

Page 64: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

GeneratorLATENT

LABEL

FC

FC

CONCAT

FC

DROPOUT

S I GMOI D

100 200

10 1000

1200 784 (28*28)

FC: Fully Connected layer Note. This model is for MNIST dataset. LABEL is one-hot encoded label. Dropout rate is 0.5.

Page 65: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

DiscriminatorI NPUT

LABEL

MAXOUT

MAXOUT

CONCAT

784 (28*28) 240

10 50

290

MAXOUT

240

DROPOUT

S I GMOI D

FC

1

FC: Fully Connected layerNote. This model is for MNIST dataset. LABEL is one-hot encoded label. Dropout rate is 0.5. MAXOUT layer includes dropout layer implicitly with rate 0.5. MAXOUT parameter k is set to 5 except for the last MAXOUT set to be 4.

Page 66: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

DCGAN

Page 67: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Generator

LATENT

FC

RESHAPE

BN

ReLU

TCONV

BN

ReLU

TCONV

BN

ReLU

TCONV

BN

ReLU

TCONV

TANH

100 16384 (=4*4*1024)

1024x4x4 (CxHxW) 512x8x8 256x16x16 128x32x32 3x64x64Dim

BN: Batch Normalization C: Channel FC: Fully Connected layer H: Height ReLU: Rectified Linear Unit TCONV: 5x5 Transposed CONVolution (stride 2) W: Width

Note. This is for the case of LSUN dataset. The number of TCONV layer can be varied with your target dataset.

Page 68: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Discriminator

CONV

ReLU

I NPUT

CONV

BN

ReLU

CONV

BN

ReLU

CONV

BN

ReLU

CONV

S IG

BN: Batch Normalization C: Channel H: Height ReLU: Rectified Linear Unit CONV: 5x5 CONVolution (stride 2) W: Width

3x64x64 128x32x32 256x16x16 512x8x8 1024x4x4 1x1x1 (4x4 CONV with stride 1)

Page 69: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

pix2pix

Page 70: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

What is pix2pix?

Page 71: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

GeneratorContent image

Stylized image

Discriminator

Target image

What is pix2pix?

Page 72: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Generator

1x1 2x2

I PUT

LReLU

CONV

BN

CONC

ReLU

TCONV

BN

CONV

BN

LReLU

CONV

BN

CONC

ReLU

TCONV

BN

CONC

ReLU

TCONV

TANH

… …

OPUT

2x2

4x4

BN: Batch Normalization CONC: CONCatenation CONV: 4x4 CONVolution (stride 2) DO: DropOut (p = 0.5) IPUT: InPUT LReLU: Leaky ReLU with slope 0.2 OPUT: OutPUT ReLU: Rectified Linear Unit TCONV: 4x4 Transposed CONV (stride 2)

LReLU

CONV

ReLU

TCON V

BN

DO

DO

Note. Dropout is applied where feature map size is 2x2, 4x4, and 8x8 in the decoder part.

Page 73: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

DiscriminatorI PUT

CONV 2

CONV 2

BN

LReLU

LReLU

CONV 2

BN

LReLU

CONV 1

BN

LReLU

CONV 1

S I G

1 channel output

BN: Batch Normalization CONV1: 4x4 CONVolution (stride 1) CONV2: 4x4 CONVolution (stride 2) IPUT: InPUT LReLU: Leaky ReLU with slope 0.2 OPUT: OutPUT

OPUT

Note. This is for the case of 70x70 receptive field. Layers should be varied to change receptive field size.

Page 74: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Practice

Page 75: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

CycleGAN

Page 76: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Appendix

Page 77: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

A. Python class

I assume you know about Python function. Class is usually used for storing related data or handling a certain type of data using functions defined in the class. For example, calculator has four basic functions - addition, subtraction, multiplication, and division. By making a class equipped with above functions, we can calculate numbers easily.

Page 78: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

class Calculator: def __init__(self): # __init__ function is called "constructor" as every time you make an instance of Calculator, # codes written in the constructor will be implemented. self.value = 0

def add(self, n): self.value += n print("After addition:", self.value)

def subtract(self, n): self.value -= n print("After subtraction:", self.value)

def multiply(self, n): self.value *= n print("After multiplication:", self.value)

def divide(self, n): if n == 0: print("You can't divide with 0.") return

else: self.value /= n print("After division", self.value)

def reset(self): self.value = 0 print("reset value to", self.value)

print("Calculator a...")a = Calculator() # Make an instance of Calculator class. You need parenthesis after class name to do this.

a.add(5) # Call add function defined in Calculator class.a.divide(0)a.divide(5)

print()print("Calculator b...")b = Calculator() # Make another instance of Calculator class. This does not share value with calculator a.

b.add(1)b.multiply(100)b.reset()

Page 79: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

CPU vs. GPU

credit. NVIDIAcredit. hothardware.com

A central processing unit (CPU), also called a central processor or main processor, is the electronic circuitry within a computer that carries out the instructions of a computer program by performing the basic arithmetic, logic, controlling, and input/output operations specified by the instructions.

A graphics processing unit (GPU) is a specialized electronic circuit designed to rapidly manipulate and alter memory to accelerate the creation of images in a frame buffer intended for output to a display device.

Page 80: Deep Learning Bootcamp with PyTorchgyungin/pdf/DeepLearning... · 2020. 9. 30. · Deep Learning Bootcamp with PyTorch Gyungin Shin. Contents Part I. Classification Model 1. Multilayer

Loss functions

• Binary cross entropy loss (BCE) :

• Cross entropy loss (CE) :

• Mean squared error loss (MSE) :

• Mean absolute error loss (MAE) :

𝔼p(y|x)[−log ̂p(y |x)] = −C

∑c=1

p(yc |x)log ̂p(yc |x) = − log ̂p(yc |x)

−log ̂p(yc |x) −C

∑k=1, k≠c

log[1 − ̂p(yk |x)]

1MN

M−1

∑i=0

N−1

∑j=0

(xi, j − ̂xi, j)2

1MN

M−1

∑i=0

N−1

∑j=0

|xi, j − ̂xi, j |