1 of 28

Lecture 5 Smaller Network: CNN

  • We know it is good to learn a small model.
  • From this fully connected model, do we really need all the edges?
  • Can some of these be shared?

2 of 28

Consider learning an image:

  • Some patterns are much smaller than the whole image

beak” detector

Can represent a small region with fewer parameters

3 of 28

Same pattern appears in different places:�They can be compressed!�What about training a lot of such “small” detectors�and each detector must “move around”.

“upper-left beak” detector

“middle beak” detector

They can be compressed

to the same parameters.

4 of 28

A convolutional layer

A filter

A CNN is a neural network with some convolutional layers

(and some other layers). A convolutional layer has a number

of filters that does convolutional operation.

Beak detector

5 of 28

Convolution

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

6 x 6 image

1

-1

-1

-1

1

-1

-1

-1

1

Filter 1

-1

1

-1

-1

1

-1

-1

1

-1

Filter 2

……

These are the network parameters to be learned.

Each filter detects a small pattern (3 x 3).

6 of 28

Convolution

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

6 x 6 image

1

-1

-1

-1

1

-1

-1

-1

1

Filter 1

3

-1

stride=1

Dot

product

7 of 28

Convolution

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

6 x 6 image

1

-1

-1

-1

1

-1

-1

-1

1

Filter 1

3

-3

If stride=2

8 of 28

Convolution

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

6 x 6 image

1

-1

-1

-1

1

-1

-1

-1

1

Filter 1

3

-1

-3

-1

-3

1

0

-3

-3

-3

0

1

3

-2

-2

-1

stride=1

9 of 28

Convolution

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

6 x 6 image

3

-1

-3

-1

-3

1

0

-3

-3

-3

0

1

3

-2

-2

-1

-1

1

-1

-1

1

-1

-1

1

-1

Filter 2

-1

-1

-1

-1

-1

-1

-2

1

-1

-1

-2

1

-1

0

-4

3

Repeat this for each filter

stride=1

Two 4 x 4 images

Forming 2 x 4 x 4 matrix

Feature

Map

10 of 28

Color image: RGB 3 channels

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

1

-1

-1

-1

1

-1

-1

-1

1

Filter 1

-1

1

-1

-1

1

-1

-1

1

-1

Filter 2

1

-1

-1

-1

1

-1

-1

-1

1

1

-1

-1

-1

1

-1

-1

-1

1

-1

1

-1

-1

1

-1

-1

1

-1

-1

1

-1

-1

1

-1

-1

1

-1

Color image

11 of 28

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

image

convolution

-1

1

-1

-1

1

-1

-1

1

-1

1

-1

-1

-1

1

-1

-1

-1

1

……

……

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

Convolution v.s. Fully Connected

Fully-connected

12 of 28

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

6 x 6 image

1

-1

-1

-1

1

-1

-1

-1

1

Filter 1

1

2

3

8

9

13

14

15

Only connect to 9 inputs, not fully connected

4:

10:

16

1

0

0

0

0

1

0

0

0

0

1

1

3

fewer parameters!

13 of 28

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

1

-1

-1

-1

1

-1

-1

-1

1

Filter 1

1:

2:

3:

7:

8:

9:

13:

14:

15:

4:

10:

16:

1

0

0

0

0

1

0

0

0

0

1

1

3

-1

Shared weights

6 x 6 image

Fewer parameters

Even fewer parameters

14 of 28

The whole CNN

Fully Connected Feedforward network

cat dog ……

Convolution

Max Pooling

Convolution

Max Pooling

Flattened

Can repeat many times

15 of 28

Max Pooling

3

-1

-3

-1

-3

1

0

-3

-3

-3

0

1

3

-2

-2

-1

-1

1

-1

-1

1

-1

-1

1

-1

Filter 2

-1

-1

-1

-1

-1

-1

-2

1

-1

-1

-2

1

-1

0

-4

3

1

-1

-1

-1

1

-1

-1

-1

1

Filter 1

16 of 28

Why Pooling

  • Subsampling pixels will not change the object

Subsampling

bird

bird

We can subsample the pixels to make image smaller

fewer parameters to characterize the image

17 of 28

A CNN compresses a fully connected network in two ways:

  • Reducing number of connections
  • Shared weights on the edges
  • Max pooling further reduces the complexity

18 of 28

Max Pooling

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

1

0

0

1

0

0

0

1

0

0

1

0

0

1

0

0

0

1

0

1

0

6 x 6 image

3

0

1

3

-1

1

3

0

2 x 2 image

Each filter

is a channel

New image

but smaller

Conv

Max

Pooling

19 of 28

The whole CNN

Convolution

Max Pooling

Convolution

Max Pooling

Can repeat many times

A new image

The number of channels is the number of filters

Smaller than the original image

3

0

1

3

-1

1

3

0

20 of 28

The whole CNN

Fully Connected Feedforward network

cat dog ……

Convolution

Max Pooling

Convolution

Max Pooling

Flattened

A new image

A new image

21 of 28

Flattening

3

0

1

3

-1

1

3

0

Flattened

3

0

1

3

-1

1

0

3

Fully Connected Feedforward network

22 of 28

Only modified the network structure and input format (vector -> 3-D tensor)

CNN in Keras

Convolution

Max Pooling

Convolution

Max Pooling

input

1

-1

-1

-1

1

-1

-1

-1

1

-1

1

-1

-1

1

-1

-1

1

-1

There are 25 3x3 filters.

……

Input_shape = ( 28 , 28 , 1)

1: black/white, 3: RGB

28 x 28 pixels

3

-1

-3

1

3

23 of 28

Only modified the network structure and input format (vector -> 3-D array)

CNN in Keras

Convolution

Max Pooling

Convolution

Max Pooling

Input

1 x 28 x 28

25 x 26 x 26

25 x 13 x 13

50 x 11 x 11

50 x 5 x 5

How many parameters for each filter?

How many parameters

for each filter?

9

225=

25x9

24 of 28

Only modified the network structure and input format (vector -> 3-D array)

CNN in Keras

Convolution

Max Pooling

Convolution

Max Pooling

Input

1 x 28 x 28

25 x 26 x 26

25 x 13 x 13

50 x 11 x 11

50 x 5 x 5

Flattened

1250

Fully connected feedforward network

Output

25 of 28

AlphaGo

Neural

Network

(19 x 19 positions)

Next move

19 x 19 matrix

Black: 1

white: -1

none: 0

Fully-connected feedforward network can be used

But CNN performs much better

26 of 28

AlphaGo’s policy network

Note: AlphaGo does not use Max Pooling.

The following is quotation from their Nature article:

27 of 28

CNN in speech recognition

Time

Frequency

Spectrogram

CNN

Image

The filters move in the frequency direction.

28 of 28

CNN in text classification

Source of image: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.703.6858&rep=rep1&type=pdf

?