Convolutional Neural Network (CNN) presentation from theory to code in Theano

87
M&S Convolutional Neural Network from Theory to Code Seongwon Hwang

Transcript of Convolutional Neural Network (CNN) presentation from theory to code in Theano

Page 1: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Convolutional Neural Network from Theory

to Code

Seongwon Hwang

Page 2: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Preliminaries

Page 3: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Tensor :

(Index Notation)

ky klijy

ijklA

kja

ijA iA

kijjB

Page 4: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Order (Rank)

Page 5: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Range

[𝑎 𝑏𝑐 𝑑]

[𝑎 𝑏 𝑐𝑑 𝑒 𝑓𝑔 h 𝑖 ] [ 𝑎 𝑏

𝑒 𝑓𝑐 𝑑𝑔 h

𝑖 𝑗𝑚 𝑛

𝑘 𝑙𝑜 𝑝 ]

𝐴𝑖𝑗

i = 2, j = 2

i = 3, j = 3 i = 4, j = 4

[𝑎 𝑏𝑐 𝑑𝑒 𝑓 ]

i = 3, j = 2

Page 6: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Free index : unrepeated index

+

+

+, k = 1,2

2

1iikik xay

) ,( 21 yyy

Page 7: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Dummy index : repeated index

1,2kixay ikik ,

Free index Dummy index

2

1iikik xay

+

+

Page 8: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Example

iji BA jj BABA 2211 =

212111 ,1 BABAj

222121 ,2 BABAj

) ,( 222121212111 BABABABA iji BA =

i, j = 1,2

Page 9: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Example

ijjA 332211 iii AAA

) , ,( 333322311233222211133122111 AAAAAAAAA

133122111 ,1 AAAi

233222211 ,2 AAAi

333322311 ,3 AAAi

=i, j = 1,2,3

Page 10: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

e-Permutation

ijke

Page 11: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Example – Determinant

bcaddcba

aaaa

A detdetdet2221

1211

bcadaaaa

aaeaaeaaeaae

aae jiij

00 21122211

221222211221221112211111

21

Page 12: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Example – Determinant

) ( ...det 21... matrixNNaaaeA Nkjikij

Page 13: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Kronecker delta

Page 14: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Derivatives

kjjkjj xxyy

xyy

2

,, ,

1,2,3jφxφ

xφφgrad jj

) , ,( ,

Example – Gradient

Page 15: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

CNN Tensor notation in Theano

- Input Images -

4D tensor

1D tensor

[number of feature maps at layer m, number of feature maps at layer m-1, filter height, filter width]

ijklx

opqrW

mb

[ i, j, k, l ] =

[ o, p, q, r ] =

[ m ] =

- Weight -

- Bias -

[n’th feature map number]

[mini-batch size, number of input feature maps, image height, image width]

Page 16: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Convolutional Neural Network Theory I

Page 17: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Convolution?

daatwax

twxty

)()(

))(()(

a

anwaxny ][][][

- Continuous Variables -

- Discrete Variables -

Page 18: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Convolution?

a

anwaxny ][][][

- Discrete Variables -

][][ awax ][][ awax

)]([][ nawax n ste

p move

Y-axis transformation

Page 19: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Y축

X축

Convolution?

X축

X축

Page 20: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Cross-Correlation?

a

nawaxnwxny ][][])[ (][

- Discrete Variables (In real number) -

][][ awax ][][ nawax n step move

Page 21: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Convolution VS. Cross-Correlation

Cross-CorrelationConvolution

Page 22: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Cross-Correlation in 2D

Output (y) Kernel (w) Input (x)

n m

nmwjnimx

jiwxjiy

],[],[

],)[(],[

Page 23: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Intuition for Cross-Correlation

wx

Page 24: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

wx

Intuition for Cross-Correlation

Page 25: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

wx

Intuition for Cross-Correlation

Page 26: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

wx

Intuition for Cross-Correlation

Page 27: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

wx

Intuition for Cross-Correlation

Input

) (

mapFreatureneuronHidden

fieldrecptiveLocal

filteror kernel

Page 28: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Convolutional Neural Network Theory II

Page 29: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

CNN overall procedures

Input Image Convolutional Layer Pooling MLP Softmax Output

Page 30: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

CNN overall procedures

Input Image

Convolutional Layer

Pooling

MLP

Softmax Output

Page 31: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Input Image

Input Image

1D - Input neurons

2D - Input neurons

Page 32: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Convolutional Layer

Input Image

Convolutional Layer

Input neurons

Hidden neuron

Page 33: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Traditional Neural Network

Input layer Hidden layer Output layer

Page 34: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

CNN - Sparse Connectivity

Input layer Hidden layer(Feature map)

Output layer

Page 35: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

CNN – Dimension shrinkage ( + Pooling)

Input layer Output layerHidden layer(Feature map)

Page 36: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Cross-Correlation Input (or Hidden) layer with weight

Input layer Output layer

1W2W

3W

'3W'2W'1W

Hidden layer(Feature map)(Receptive field)

Page 37: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Shared Weight representation

Input layer Output layer

1W2W

3W

'3W'2W'1W

Hidden layer(Feature map)

Page 38: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Shared Weight representation

Input layer Output layer

1W2W

3W

'3W'2W'1W

Hidden layer(Feature map)

Page 39: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Shared Weight representation

Input layer Output layer

1W2W

3W

'3W'2W'1W

Hidden layer(Feature map)

Page 40: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Shared Weight representation

Input layer Output layer

1W2W

3W

'3W'2W'1W

Hidden layer(Feature map)

Page 41: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Shared Weight representation

Input layer Output layer

1W2W

3W

'3W'2W'1W

Hidden layer(Feature map)

Page 42: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Shared Weight representation

Input layer Output layer

1W2W

3W

'3W'2W'1W

Hidden layer(Feature map)

Page 43: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Multiple Feature maps

Input Image

Convolutional Layer

Input neurons First hidden layer

Page 44: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Max Pooling

Input Image

Convolutional Layer

PoolingMax pooling with 2x2 filters and stride 2

Page 45: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Why pooling?

Input Image

Convolutional Layer

Pooling

1. Low computation 2. Translation invariance

3. Transformation invariance

4. Scaling invariance

Page 46: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Several types of Pooling

Input Image

Convolutional Layer

Pooling

Page 47: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Transform data dimension before MLP

Input Image

Convolutional Layer

Pooling

MLP

1D - Output neurons

2D - Output neurons

Page 48: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Multilayer Perceptron (MLP)

Input Image

Convolutional Layer

Pooling

MLP

Input layer Hidden layer 1 Hidden layer 2

Page 49: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Softmax Output

Input Image

Convolutional Layer

Pooling

MLP

Softmax Output

Hidden layer 2 Output layer

Page 50: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Several types of CNN

Page 51: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Intuition for CNN

Input Image

Convolutional Layer

Pooling

MLP

Softmax Output

Page 52: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Convolutional Neural Network

Code in Theono

Page 53: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

CNN overall procedures

Input Image

Convolutional Layer

Pooling

MLP

Softmax Output

Page 54: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Input Image

Input Image

- Input Images -

4D tensor

[mini-batch size, number of input feature maps, image height, image width]ij

klx

5...

28

28

500

7

[ i, j, k, l ] =

Mini batch 1

5...

28

28

500

8

Mini batch 100

. . .

50,000 images in the training data

Page 55: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Weight tensor

Input Image

Convolutional Layer

4D tensor

[number of feature maps at layer m, number of feature maps at layer m-1, filter height, filter width]

opqrW [ o, p, q, r ] =

- Weight -

Page 56: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Exercise for Input and Weight tensor

1111x

1111W

Input layer Convolutional layer 1 Convolutional layer 2

[ 1, 1, 1, 1 ]

[ 1, 1, 1, 1 ]

Page 57: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Code for Convolutional Layer

28

28 8

def evaluate_lenet5(learning_rate=0.1, n_epochs=2, dataset=‘minist.pkl.gz’, nkerns=[20, 50], batch_size=500):

LeNetConvPoolLayer

image_shape=(batch_size, 1, 28, 28)filter_shape=(nkerns[0], 1, 5, 5)poolsize=(2, 2)

image_shape=(batch_size, nkerns[0], 12, 12)filter_shape=(nkerns[1], nkerns[0], 5, 5)poolsize=(2, 2)

Layer0 – Convolutional layer 1

Layer1 – Convolutional layer 2

5

5

20

24

24

20

5

5

12

12

8

8

44

20 50 50 50

28 – 5 + 1 = 24Convolution

24 / 2 = 12Pooling

12 – 5 + 1 = 8Convolution

8 / 2 = 4Pooling

Class

Page 58: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding Input layer Output layerHidden layer(Feature map)

Input Image

Convolutional Layer

Pooling

Page 59: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding Input layer Output layerHidden layer(Feature map)

0

0

Page 60: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding Input layer Output layerHidden layer(Feature map)

0

0

Page 61: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding Input layer Output layerHidden layer(Feature map)

0

0

Page 62: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding Input layer Output layerHidden layer(Feature map)

0

0

Page 63: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding Input layer Output layerHidden layer(Feature map)

0

0

0

0

Page 64: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding Input layer Output layerHidden layer(Feature map)

0

0

0

0

Page 65: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding

0

0

Page 66: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding

0

0

Page 67: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding

0

0

Page 68: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding

0

0

0

0

Page 69: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding

0

0

0

0

Page 70: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding

0

0

0

0

Page 71: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Zero-Padding

No zero-padding Zero-padding 1 Zero-padding 2

Dimension Reduction Dimension Equality Dimension Increase

Zero-pad-ding in Theano

Default in Theano

Page 72: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Code for Zero-Padding setting in Theano Library

conv.py (In Library File)

def conv2d(input, filters, image_shape=None, filter_shape=None, border_mode=‘valid’, subsample=(1, 1), **kargs):

‘valid’ ‘full’

We should modify dimension changes in code by ourselves!

Image_shape + filter_shape – 1Image_shape - filter_shape + 1

28 + 5 – 1 = 3228 - 5 + 1 = 24

Page 73: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

1. Border in Pooling

No problem in Border Ingore_border = False Ingore_border = True

Page 74: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

1. Border in Pooling (Code)

No problem in Border

Ingore_border = False Ingore_border = True

pooled_out = downsample.max_pool_2d(input=conv_out, ds=poolsize, ignore_border=True)

LeNetConvPoolLayerClass

Default in Theano library is False!

Page 75: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

2. Stride in Pooling

Default in Theano stride size = poolsize

Unduplicated!

Page 76: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

2. Stride in Pooling2

Default in Theano stride size = poolsize

Unduplicated!

Page 77: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

2. Stride in Pooling2

Default in Theano stride size = poolsize = (2,2)

Unduplicated!

Page 78: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

2. Stride in Pooling

2

Default in Theano stride size = poolsize = (2,2)

Unduplicated!

Page 79: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

2. Stride in Pooling

2

Default in Theano stride size = poolsize = (2,2)

Unduplicated!

Page 80: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

2. Stride in Pooling

stride size = (1, 1)

6

Page 81: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

2. Stride in Pooling1

76

Page 82: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

2. Stride in Pooling (Code)

76 8

6 7 8

3 3 4

pooled_out = downsample.max_pool_2d(input=conv_out, ds=poolsize, ignore_border=True)

pooled_out = downsample.max_pool_2d(input=conv_out, ds=poolsize, ignore_border=True, st = (1,1))

Page 83: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Activation Function in Convolutional Layer

self.output=T.tanh(pooled_out + self-.b.dimshuffle(‘x’,0,‘x’,‘x’)

Convolution Pooling Activation

LeNetConvPoolLayerClass

Page 84: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Dimension Reduction 2D 1D

Input Image

Convolutional Layer

Pooling

MLP

layer2_input = layer1_input.output.flatten(2) 1D - Output neurons

2D - Output neurons

Page 85: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Code for MLP in Theano

Input Image

Convolutional Layer

Pooling

MLP layer2 = HiddenLayer( rng, input=layer2_input, n_in-nkerns[1] * 4 * 4, n_out=500, activation = T.tanh)

HiddenLayerClass

Last output size for C+P

Number of node at Hidden layer

Activation function at Hidden layer

***In order to extend the number of Hidden Layer in MLP, We need to make layer3 by copying this code***

Page 86: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

Code for Softmax Output in Theano

Input Image

Convolutional Layer

Pooling

MLP

Softmax Output

layer3 = LogisticRegression( input=layer2.output, n_in=500, n_out=10)

LogisticRegressionClass

cost = layer3.negative_log_likelihood(y)

Number of node at previous Hidden layer

Final Output sizeEx) 0, 1, 2, ,,, 9

Softmax activation function

Page 87: Convolutional Neural Network (CNN) presentation from theory to code in Theano

M&S

CNN application in bioinformatics problem