convnets in tensorflow - stanford university · convnets in tensorflow cs 20si: tensorflow for deep...

26
Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1

Upload: others

Post on 07-Jul-2020

32 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Convnets in TensorFlowCS 20SI:

TensorFlow for Deep Learning ResearchLecture 72/3/2017

1

Page 2: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

2

Page 3: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Agenda

Playing with convolutions

Convolution support in TF

More MNIST!!!

Autoencoder

(Half) guest workshop by Nishith Khandwala

3

Page 4: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Understanding convolutions

4

Page 5: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Convolutions in maths and physics

5

a function derived from two given functions by integration that expresses how the shape of one is modified by the other

Page 6: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Convolutions in maths and physics

6

Page 7: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Convolutions in neural networks

7

a function derived from two given functions by element-wise multiplication that expresses how the value and shape of one is

modified by the other

Page 8: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Convolutions in neural networks

8

We can use one single convolutional layer to modify a certain image

Page 9: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Convolutions in neural networks

9

We can use one single convolutional layer to modify a certain image

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)

Page 10: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Convolutions without training

10

0.0625 0.125 0.0625

0.125 0.25 0.125

0.0625 0.125 0.0625

tf.nn.conv2d

input

Kernel for blurring

output

Page 11: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Some basic kernels

11

input blur sharpen edge top sobel emboss

See kernels.py and 07_basic_filters.py on the class GitHub!!!

Page 12: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Convolutions in neural networks

12

In training, we don’t specify kernels. We learn kernels!

Page 13: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Getting dimensions right

13

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)

Input: Batch size x Height x Width x Channels

Filter: Height x Width x Input Channels x Output Channels(e.g. [5, 5, 3, 64])Strides: 4 element 1-D tensor, strides in each direction(often [1, 1, 1, 1] or [1, 2, 2, 1])

Padding: ‘SAME’ or ‘VALID’

Data_format: default to NHWC

Page 14: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Convnet with MNIST

14

Page 15: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Getting dimensions right

15

Original Image28 x 28 x 1

Conv1Filter: 5 x 5 x 1 x 32Stride: 1, 1, 1, 1Out: 28 x 28 x 32ReluMaxpool (2 x 2 x 1)Out: 14 x 14 x 32

Conv2Filter: 5 x 5 x 32 x 64Stride: 1, 1, 1, 1Out: 14 x 14 x 64ReluMaxpool (2 x 2 x 1)Out: 7 x 7 x 64

Fully connectedW: 7*7*64 x 1024Out: 1 x 1024

ReluOut: 1 x 1024

SoftmaxW: 1024 x 10Out: 1 x 10

Softmax1 x 10

Page 16: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Getting dimensions right

16

Original Image28 x 28 x 1

Conv1Filter: 5 x 5 x 1 x 32Stride: 1, 1, 1, 1Out: 28 x 28 x 32ReluMaxpool (2 x 2 x 1)Out: 14 x 14 x 32

Conv2Filter: 5 x 5 x 32 x 64Stride: 1, 1, 1, 1Out: 14 x 14 x 64ReluMaxpool (2 x 2 x 1)Out: 7 x 7 x 64

Fully connectedW: 7*7*64 x 1024Out: 1 x 1024

ReluOut: 1 x 1024

SoftmaxW: 1024 x 10Out: 1 x 10

Softmax1 x 10

(W−F+2P)/S+ 1W: input widthF: filter width

P: paddingS: stride

More exciting math in the lecture note!

Page 17: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

TensorFlow support

17

Convolutiontf.nn.conv2d

Relutf.nn.relu

Maxpooltf.nn.max_pool

Fully connectedtf.nn.relu

Softmaxtf.nn.softmax_cross_enptry_with_logits

Page 18: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Variable scope

18

with tf.variable_scope('conv1') as scope: w = tf.get_variable('weights', [5, 5, 1, 32]) b = tf.get_variable('biases', [32], initializer=tf.random_normal_initializer()) conv = tf.nn.conv2d(images, w, strides=[1, 1, 1, 1], padding='SAME') conv1 = tf.nn.relu(conv + b, name=scope.name)

Page 19: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Interactive coding

19

Download 07_convnet_mnist_starter.py from GitHub!

Page 20: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

MNIST Covnet graph

20

Page 21: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Accuracy

21

Epochs Accuracy

1 0.9111

2 0.9401

3 0.9494

5 0.9549

10 0.9692

25 0.9736

40 0.9793

50 0.9804

Page 22: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Autoencoder

22

Page 23: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Autoencoder

Reconstructed InputInput Autoencoder

Page 24: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Autoencoder

● Input and Output dimensions should match.

● Input and Output range should be same.

Page 25: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Autoencoder

25

Live codingSee autoencoder folder on GitHub

Page 26: Convnets in TensorFlow - Stanford University · Convnets in TensorFlow CS 20SI: TensorFlow for Deep Learning Research Lecture 7 2/3/2017 1. 2. Agenda Playing with convolutions Convolution

Next class

Guest lecture by Jon Shlens

Convnet

Deep Dream

Feedback: [email protected]

Thanks!

26