TensorFlow CNN
TensorFlow CNN
Over view
Layer 2
Layer 1
Layer N
…
Sounds like a classic neural network problem
Two Kinds of Layers in CNNs
Convolution Pooling
Convolution Pooling
28
= 784 pixels
Representing Images as Matrices
6
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
= 36 pixels
Representing Images
3
0 0 0 0 0 0
1 0 1
0.2 0.8 0 0.3 0.6 0
0 0 0 0.2 0.8 0
1 0 1
0 0 0 0.2 0.2 0
Matrix Kernel
Convolution
3
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Matrix Kernel
Convolution
4
0 0 0 0 0 0
0 0 0 0.2 0.8 0
x0 x1 x0
x1 x0 x1
1.0 1.8 2.0 1.8
0 0 0 0.2 0.2 0
0 x1 0x0 0 x1 0 0 0
0.2
x0 0.8x1 0x0 0.3 0.6 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 x1 0x0 0 x1 0 0 0
0.2
x0 0.8x1 0x0 0.3 0.6 0 1
0.2x1 0.9x0 0 x1 0.3 0.8 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 x1 0x0 0 x1 0 0
0.2 0.8
x0 0 x1 0.3
x0 0.6 0 1
0.2 0.9x1 0x0 0.3x1 0.8 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 x1 0x0 0 x1 0 0
0.2 0.8
x0 0 x1 0.3
x0 0.6 0 1 1.2
0.2 0.9x1 0x0 0.3x1 0.8 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 x1 0x0 0 x1 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 x1 0x0 0 x1 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 x1 0x0 0 x1
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 x1 0x0 0 x1
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0.2x1 0.8
x0 0 x1 0.3 0.6 0 1 1.2 1.1 0.9
0.2
x0 0.9x1 0x0 0.3 0.8 0
0.3x1 0.8
x0 0.7x1 0.8 0.9 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0.2x1 0.8
x0 0 x1 0.3 0.6 0 1 1.2 1.1 0.9
0.2
x0 0.9x1 0x0 0.3 0.8 0 1.9
0.3x1 0.8
x0 0.7x1 0.8 0.9 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
x0 x1 x1 x0
Negative corners, zero elsewhere ~ Edge enhance
x0 x0
…
Choice of Kernel Function
http://aishack.in/tutorials/image-convolution-examples/
Blur
Line Detection
Horizontal Lines
Edge Detection
Zero-padding, Stride Size
Narrow vs. Wide Convolution
Convolution result
Narrow vs. Wide Convolution
Little zero padding; output narrower than Lots of zero padding; output wider than
input input
Without Zero Padding
6
4
0 0 0 0 0 0
x0
x0
x1
x1
x0
4
x1 x0 x1
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
x0
x0
x1
x1
x0
0 0 0 0 0 0.2 0.8 0 0 0 x1 x0 x1
0 0 0 0 0 0.2 0.2 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0.2 0.8 0 0 0 0 x1 x0 x1
0 0 0 0 0 0 0.2 0.2 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
x0 x1 x1 x0
Can decide number of zero columns to pad with
0 x1 0x0 0 x1 0 0 0
0.2
x0 0.8x1 0x0 0.3 0.6 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Stride Size
0 0 x1 0x0 0 x1 0 0
0.2 0.8
x0 0 x1 0.3
x0 0.6 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Horizontal stride of 1
Stride Size
0 x1 0x0 0 x1 0 0 0
0.2
x0 0.8x1 0x0 0.3 0.6 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Stride Size
0 0 0 0 0 0
0.2
x0 0.9x1 0x0 0.3 0.8 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Vertical stride of 1
Stride Size
0 0 0 0 0 0
0.2
x0 0.9x1 0x0 0.3 0.8 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Layer 2
Layer 1
Layer N
…
Corpus of Layers in a neural network ML-based Classifier
Images
Neural Net works for Image Classification
Neurons
Horizontal Stride
Vertical Stride
Convolutional Layer
Feature Maps
Why filter?
Map m,
Column c,
Row r
Input Image Layer 1 Layer 2 Layer L
Neuron output depends on corresponding* neurons from
each preceding layer
(*corresponding: same receptive field and feature maps,
different layers)
Pooling Layers
Two Kinds of Layers in CNNs
Convolutional Pooling
x1 x0 x1
1.0 1.8 2.0 1.8
0 0 0 0.2 0.2 0
Convolutional Pooling
ReLU
ReLU
Convolutional Pooling Convolutional
ReLU
ReLU
Convolutional Pooling Convolutional
ReLU
ReLU
Convolutional Pooling Convolutional
ReLU
ReLU
Convolutional Pooling Convolutional
ReLU
ReLU
Convolutional Pooling Convolutional
ReLU
ReLU
Convolutional Pooling Convolutional Feed-forward Layers
CNN Layers
Fully Connected
Fully Connected
ReLU
ReLU
CNN Layers
Feed-forward Layers
Fully Connected
Fully Connected
Prediction
SoftMax
ReLU
ReLU
CNN Layers
Feed-forward Layers
Xi P(Y = False)
Wn
…
Xn b1 b2
SoftMax for Digit Classification
P(Y = 0)
P(Y = 1)
Softmax
Function …
P(Y = 9)
SoftMax for Image Classification
P(Y = “cat”)
P(Y = “bird”)
Softmax
Function …
P(Y = “car”)
Typical CNN Architecture
P(Y = 0)
Fully Connected
Fully Connected
P(Y = 1)
Prediction
SoftMax
ReLU
ReLU
CNN Layers
Convolutional
Typical CNN Architectures
At output end of CNN, regular feedforward NN
Dense Feed- stacked on
forward Layers
- Few fully connected layers