Download as pdf or txt
Download as pdf or txt
You are on page 1of 83

How to make the best use of Live Sessions

• Please login on time

• Please do a check on your network connection and audio before the class to have a smooth session

• All participants will be on mute, by default. You will be unmuted when requested or as needed

• Please use the “Questions” panel on your webinar tool to interact with the instructor at any point during the
class

• Ask and answer questions to make your learning interactive

• Please have the support phone number (US : 1855 818 0063 (toll free), India : +91 90191 17772) and raise
tickets from LMS in case of any issues with the tool

• Most often logging off or rejoining will help solve the tool related issues

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


AI & Deep Learning with TensorFlow
Module 5
Course Outline
Module Module
1 Introduction to Deep Learning 6 Recurrent Neural Networks

Module Understanding Neural Networks Module


2 7 RBM and Autoencoders
with Tensorflow

Module
Deep dive into Neural Networks Module
3 with Tensorflow 8 Keras

Module Master Deep Networks Module


TFlearn
4 9

Module Module
5 Convolutional Neural Networks 10 Hands-On Project

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Agenda
▪ Convolutional Neural Networks

▪ How CNN works?

▪ Convolutional Layer

▪ ReLU layer

▪ Pooling Layer

▪ Fully Connected Layer


Recap of Neural Networks
▪ As we saw in the previous module, Neural Networks receive an input (a single vector), and transform it
through a series of hidden layers. Each hidden layer is made up of a set of neurons, where each neuron is
fully connected to all neurons in the previous layer, and where neurons in a single layer function completely
independently and do not share any connections.
▪ The last fully-connected layer is called the “output layer” and in classification settings it represents the class
scores. Based on which classification output is decided.

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Input Image
▪ When computer takes an image as input, it will see an array of pixel values where the dimension of array
depends on the resolution and size of the image.
▪ For example:
01 55 23 11 32 12 42 12 21
21 36 21 22 92 09 37 13 17
52 25 21 27 95 55 62 13 51
91 42 21 52 42 92 08 13 86
48 53 21 72 93 81 01 13 11
27 35 21 05 12 73 82 13 19

what we see what computers see

▪ We can represent the above image using an array of dimension 32 X 32 X 3 (The 3 refers to RGB values)
▪ Each of these numbers is given a value from 0 to 255 which describes the pixel intensity at that point.
▪ These numbers, while meaningless to us when we perform image classification, are the only inputs available
to the computer.

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Why CNN?

This still looks


28x28x3 manageable

A single fully-connected neuron in a first


hidden layer of a regular Neural Network
would have 28*28*3 = 2352 weights.

2352 weights

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Why CNN?

How to
200x200x3 manage that?

An image of size, e.g. 200x200x3,


would lead to neurons that have
200*200*3 = 120,000 weights.

120,000 weights

Moreover, we would almost certainly want to have several such neurons, so the parameters would add up quickly! Clearly, this
full connectivity is wasteful and the huge number of parameters would quickly lead to overfitting.

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolutional Neural Network(CNN)

Now let’s discuss about a deep net that has completely dominated the machine vision space in
recent years

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


What is CNN?
Convolutional Neural Network (CNN, or ConvNet) is a type of feed-forward artificial neural network in which
the connectivity pattern between its neurons is inspired by the organization of the animal visual cortex.

Unlike regular neural networks where we have fully connected layers, in case of CNN the neuron in a layer will
only be connected to a small region of the layer before it, instead of all of the neurons in a fully-connected
manner.

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolutional Neural Network(CNN)
▪ Convolutional Neural Networks have wide application for image recognition and classification problems.

▪ This is one the top most method used worldwide for image recognition.

▪ There are three types of layers in a CNN:


– Convolutional Layer
– ReLU
– Pooling Layer

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolutional Neural Network(CNN)
▪ Let’s understand how CNN works using a simple example.

▪ We will create a small classifier which can classify whether a picture is of an X or an O.

CNN X or O
A two-dimensional
array of pixels

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolutional Neural Network(CNN)
▪ When we feed an image of X, it should classify it as X or O.

CNN X

CNN O
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
Trickier Cases
▪ Here, we will have some problems because X and O images won’t always have the same images, there can
be certain deformations such as shown below:

X
Translation Scaling Rotation Weight

O
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
How to Decide?
▪ We need a classifier which can be used with these images and correctly predict what it is.

?
=
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
How Computers See
▪ A computer understands an image using numbers at each pixels.

▪ In our example, we have considered that a black pixel will have a -1 value and a white will have a 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 -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 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


How Computers See
▪ Using normal techniques, computers compare these images as

-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 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 X -1 -1 -1 -1 X X -1

+ =
-1 -1 1 -1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 -1 X X -1 -1 X X -1 -1
-1 -1 -1 1 -1 1 -1 -1 -1 -1 -1 1 1 -1 1 -1 -1 -1 -1 -1 X 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 1 -1 1 -1 -1 -1 -1 -1 -1 1 -1 1 1 -1 -1 -1 -1 -1 1 -1 1 X -1 -1
-1 -1 1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 X X -1 -1 X X -1
-1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 X X -1 -1 -1 -1 X -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

As we can see this is not optimal, since these types of techniques requires exactly same images to
classify. This is not the case in real world, let’s see how CNN solves this problem.

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


How CNN Works?
▪ In CNN, we compare several features of images to classify them.

=
=
=
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
How CNN Works?
▪ CNNs compare images piece by piece. The pieces that it looks for are called features. By finding rough
feature matches, in roughly the same position in two images, CNNs get a lot better at seeing similarity than
whole-image matching schemes.

=
=
=
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
How CNN Works?
▪ Some examples of features used for classifying our input data are:

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

These are small pieces of the bigger image. If we choose a feature and put it on the input image, if it
matches then the image is classified correctly.

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


How CNN Works?
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 Now let’s see how these features are used for pattern matching

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolutional Layer

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolutional Layer
▪ The first layer in CNN is called convolutional layer.

▪ In convolutional layer, we perform convolution of a particular feature.

▪ Convolution is the process of trying every possible position, here we will move the feature to every possible
position.

Convolution symbol

The first step in convolution is called Filtering

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Filtering
▪ In this step we compare the features with a patch of the image and one by one the pixels are compared.

▪ Steps to follow:

1. Line up the feature and the image patch (by default image patch size is taken of 9 pixels).

2. Multiply each image pixel by the corresponding feature pixel.

3. Add them up.

4. Divide by the total number of pixels in the feature.

Let’s see how this is done graphically

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Filtering
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
-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
-1 1 -1 -1 -1 -1 -1 1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
Filtering

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
-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 1
-1 1 -1 -1 -1 -1 -1 1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Filtering

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
-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 1 1
-1 1 -1 -1 -1 -1 -1 1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Filtering

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
-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 1 1 1
-1 1 -1 -1 -1 -1 -1 1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Filtering

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
-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 1 1 1
-1 1 -1 -1 -1 -1 -1 1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 1

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Filtering

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
-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 1 1 1
-1 1 -1 -1 -1 -1 -1 1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 1 1

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Filtering

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
-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 1 1 1
-1 1 -1 -1 -1 -1 -1 1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Filtering

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
-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 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
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
Filtering

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
-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 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
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
Filtering

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
-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 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
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
Adding up the filter
▪ When we are done, we add the values of our filter and divide it by 9, which is the filter size.

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 -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
-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

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Map Creation
▪ Now to keep track of where that feature was, we create a map and put the value of the filter at that place.

-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 -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 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Filtering
▪ Now, using the same feature and move it to another location and perform the filtering again.

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
-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 1
-1 1 -1 -1 -1 -1 -1 1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Adding up the filter
▪ Here also we will add the whole values of the filter and divide it by 9

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
-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 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
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
Map Creation
▪ Now we will put the value of the filter at that position

-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 -1
-1 -1 -1 -1 1 -1 -1 -1 -1 .55
-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

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolution
▪ Similarly, we will move the feature to every other positions of the image and will see how the feature
matches that area
0.77 -0.11 0.11 0.33 0.55 -0.11 0.33
▪ Finally we will get an output as:
-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33


1 -1 -1
0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11 -1 1 -1
-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11 -1 -1 1
0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

From the map we can understand that our feature matches along the diagonal much better than
other areas

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolution
▪ Similarly, we will perform the same convolution with every other filters
0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33
-1 -1 -1 -1 -1 -1 -1 -1 -1
-1 1 -1 -1 -1 -1 -1 1 -1 -0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55
-1 -1 1 -1 -1 -1 1 -1 -1 1 -1 1 0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11

=
-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 -0.11 0.33 -0.77 1.00 -0.77 0.33 -0.11

0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11


-1
-1
-1
1
1
-1
-1
-1
-1
-1
-1
-1
1
-1
-1
1
-1
-1
1 -1 1 -0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55

0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33


-1 -1 -1 -1 -1 -1 -1 -1 -1

-1 -1 -1 -1 -1 -1 -1 -1 -1 0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

-1 1 -1 -1 -1 -1 -1 1 -1 -0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11


-1 -1 1 -1 -1 -1 1 -1 -1
-1 -1 1
=
0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11
-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 0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55


-1
-1
-1
1
1
-1
-1
-1
-1
-1
-1
-1
1
-1
-1
1
-1
-1
1 -1 -1 -0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11

-1 -1 -1 -1 -1 -1 -1 -1 -1 0.77 -0.11 0.11 0.33 0.55 -0.11 0.33

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolution Layer
▪ Now we will have a stack of filtered images, this is the output of a convolution layer
0.77 -0.11 0.11 0.33 0.55 -0.11 0.33

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11

1 -1 -1 0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

-1 1 -1 0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11

-1 -1 1 -0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

-1 -1 -1 -1 -1 -1 -1 -1 -1 0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33

-1 1 -1 -1 -1 -1 -1 1 -1 -0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55


-1
-1
-1
-1
1
-1
-1
1
-1
-1
-1
1
1
-1
-1
-1
-1
-1
1 -1 1 0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11

-1
-1
-1
-1
-1
-1
-1
1
1
-1
-1
1
-1
-1
-1
-1
-1
-1
-1 1 -1 -0.11 0.33 -0.77 1.00 -0.77 0.33 -0.11

0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11


-1
-1
-1
1
1
-1
-1
-1
-1
-1
-1
-1
1
-1
-1
1
-1
-1
1 -1 1 -0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55

0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33


-1 -1 -1 -1 -1 -1 -1 -1 -1

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

-1 -1 1 0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11

-1 1 -1 0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

1 -1 -1 -0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11

0.77 -0.11 0.11 0.33 0.55 -0.11 0.33

In convolution one image becomes a stack of filtered images, number of filtered images depends
on the filters

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


ReLU Layer

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


ReLU Layer
▪ In this layer we remove every negative values from the filtered images and replaces it with zero’s

▪ This is done to avoid the values from summing up to zero

▪ ReLU layer is represented using the symbol:

Let’s see how ReLU layer affects our filtered images

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


ReLU function - Recap
▪ Rectified Linear Unit (ReLU) transform function only activates a node if the input is above a certain quantity
Linear
▪ While the input is below zero, the output is zero, but when the input rises above a certain threshold, it has
a linear relationship with the dependent variable

Unit Step
Sigmoid x f(x)=x F(x)

-3 f(-3) = 0 0
Tanh

-5 f(-5) = 0 0

3 f(3) = 3 3

5 f(5) = 5 5

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


ReLU - Recap
▪ Question: When to use a ReLU transformation function?
Linear
▪ Answer: When we want to map input values to a value in the range ( input x to max(0,x)), i.e. it maps
negative inputs to 0 and positive inputs are output without any change, we apply tanh transformation
function
Sigmoid x f(x)=x F(x)

Unit Step -3 f(-3) = 0 0


Tanh

-5 f(-5) = 0 0

3 f(3) = 3 3

5 f(5) = 5 5

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


ReLU Layer
▪ Here we will translate through all the values of the filtered image and whenever we hit a negative value, it is
replaced by zero

0.77 -0.11 0.11 0.33 0.55 -0.11 0.33 0.77

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


ReLU Layer

0.77 -0.11 0.11 0.33 0.55 -0.11 0.33 0.77 0

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


ReLU Layer
▪ Finally we will get an output image like

0.77 -0.11 0.11 0.33 0.55 -0.11 0.33 0.77 0 0.11 0.33 0.55 0 0.33

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11 0 1.00 0 0.33 0 0.11 0

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55 0.11 0 1.00 0 0.11 0 0.55

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33 0.33 0.33 0 0.55 0 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11 0.55 0 0.11 0 1.00 0 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11 0 0.11 0 0.33 0 1.00 0

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77 0.33 0 0.55 0.33 0.11 0 0.77

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


ReLU Layer
▪ Similarly, for other filtered image also, we perform the same
0.77 0 0.11 0.33 0.55 0 0.33
0.77 -0.11 0.11 0.33 0.55 -0.11 0.33

0 1.00 0 0.33 0 0.11 0


-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55


0.11 0 1.00 0 0.11 0 0.55

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33 0.33 0.33 0 0.55 0 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11 0.55 0 0.11 0 1.00 0 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11 0 0.11 0 0.33 0 1.00 0

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77 0.33 0 0.55 0.33 0.11 0 0.77

0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33 0.33 0 0.11 0 0.11 0 0.33

-0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55 0 0.55 0 0.33 0 0.55 0

0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11 0.11 0 0.55 0 0.55 0 0.11

-0.11 0.33 -0.77 1.00 -0.77 0.33 -0.11 0 0.33 0 1.00 0 0.33 0

0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11 0.11 0 0.55 0 0.55 0 0.11

-0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55 0 0.55 0 0.33 0 0.55 0

0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33 0.33 0 0.11 0 0.11 0 0.33

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77 0.33 0 0.55 0.33 0.11 0 0.77

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11 0 0.11 0 0.33 0 1.00 0

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11 0.55 0 0.11 0 1.00 0 0.11

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33 0.33 0.33 0 0.55 0 0.33 0.33

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55 0.11 0 1.00 0 0.11 0 0.55

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11 0 1.00 0 0.33 0 0.11 0

0.77 -0.11 0.11 0.33 0.55 -0.11 0.33 0.77 0 0.11 0.33 0.55 0 0.33

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Pooling Layer

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Pooling Layer
▪ In this layer we shrink the image stack into a smaller size
Symbol:
▪ Steps to follow:

1. Pick a window size (usually 2 or 3).

2. Pick a stride (usually 2).

3. Walk your window across your filtered images.

4. From each window, take the maximum value.

Let’s perform pooling with a window size 2 and a stride 2

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Pooling Layer
▪ Let’s start with our first filtered image

▪ In our first window the maximum or highest value is 1, so we track that and move the window two strides

0.77 -0.11 0.11 0.33 0.55 -0.11 0.33


maximum

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11


1.00
0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Pooling Layer
▪ Similarly the process is repeated until all area of the image are covered

0.77 -0.11 0.11 0.33 0.55 -0.11 0.33


maximum

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11


1.00 0.33
0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Pooling Layer

0.77 -0.11 0.11 0.33 0.55 -0.11 0.33


maximum

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11


1.00 0.33 0.55
0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Pooling Layer

0.77 -0.11 0.11 0.33 0.55 -0.11 0.33


maximum

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11


1.00 0.33 0.55 0.33
0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Pooling Layer
▪ After all parts of the image are covered, we will get:
0.77 -0.11 0.11 0.33 0.55 -0.11 0.33

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11 1.00 0.33 0.55 0.33

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55


0.33 1.00 0.33 0.55
0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11


0.55 0.33 1.00 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11


0.33 0.55 0.11 0.77
0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

Here we can see that instead of 7 x 7 pixel image we get 4 x 4 one. Also the higher values are not affected.
This process is very helpful when we are working with images of 1000 x 1000 pixels and more.
Also we can see that even if a particular feature in image is slightly off from its place, the layer picks it up

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Pooling Layer
▪ Performing the same on other filtered images, we will get:

0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33


0.55 0.33 0.55 0.33
-0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55

0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11


0.33 1.00 0.55 0.11
-0.11 0.33 -0.77 1.00 -0.77 0.33 -0.11

0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11 0.55 0.55 0.55 0.11
-0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55
0.33 0.11 0.11 0.33
0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77


0.33 0.55 1.00 0.77
-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11


0.55 0.55 1.00 0.33
0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55 1.00 1.00 0.11 0.55
-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11
0.77 0.33 0.55 0.33
0.77 -0.11 0.11 0.33 0.55 -0.11 0.33

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Pooling Layer
▪ We can represent the process of pooling as
0.77 -0.11 0.11 0.33 0.55 -0.11 0.33
1.00 0.33 0.55 0.33
-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11

0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55 0.33 1.00 0.33 0.55
0.33 0.33 -0.33 0.55 -0.33 0.33 0.33

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11


0.55 0.33 1.00 0.11

-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11


0.33 0.55 0.11 0.77
0.33 -0.11 0.55 0.33 0.11 -0.11 0.77

0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33


0.55 0.33 0.55 0.33
-0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55

0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11 0.33 1.00 0.55 0.11
-0.11 0.33 -0.77 1.00 -0.77 0.33 -0.11
0.55 0.55 0.55 0.11
0.11 -0.55 0.55 -0.77 0.55 -0.55 0.11

-0.55 0.55 -0.55 0.33 -0.55 0.55 -0.55


0.33 0.11 0.11 0.33
0.33 -0.55 0.11 -0.11 0.11 -0.55 0.33

0.33 -0.11 0.55 0.33 0.11 -0.11 0.77


0.33 0.55 1.00 0.77
-0.11 0.11 -0.11 0.33 -0.11 1.00 -0.11

0.55 -0.11 0.11 -0.33 1.00 -0.11 0.11 0.55 0.55 1.00 0.33
0.33 0.33 -0.33 0.55 -0.33 0.33 0.33
1.00 1.00 0.11 0.55
0.11 -0.11 1.00 -0.33 0.11 -0.11 0.55

-0.11 1.00 -0.11 0.33 -0.11 0.11 -0.11 0.77 0.33 0.55 0.33
0.77 -0.11 0.11 0.33 0.55 -0.11 0.33

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Stacking up the Layers

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Stacking up the Layers
▪ As output of one layer is the input of other, we can stack up these three layers as:

1.00 0.33 0.55 0.33

0.33 1.00 0.33 0.55

Convolution
-1 -1 -1 -1 -1 -1 -1 -1 -1 0.55 0.33 1.00 0.11

Pooling
ReLU
-1 1 -1 -1 -1 -1 -1 1 -1 0.33 0.55 0.11 0.77

-1 -1 1 -1 -1 -1 1 -1 -1
0.55 0.33 0.55 0.33
-1 -1 -1 1 -1 1 -1 -1 -1
0.33 1.00 0.55 0.11
-1 -1 -1 -1 1 -1 -1 -1 -1
0.55 0.55 0.55 0.11
-1 -1 -1 1 -1 1 -1 -1 -1
0.33 0.11 0.11 0.33
-1 -1 1 -1 -1 -1 1 -1 -1
-1 1 -1 -1 -1 -1 -1 1 -1 0.33 0.55 1.00 0.77

-1 -1 -1 -1 -1 -1 -1 -1 -1 0.55 0.55 1.00 0.33

1.00 1.00 0.11 0.55

0.77 0.33 0.55 0.33

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Stacking up the Layers
▪ We can repeat this stack as much as we want

1.00 0.55

-1 -1 -1 -1 -1 -1 -1 -1 -1 0.55 1.00
-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.00 0.55
-1 -1 -1 -1 1 -1 -1 -1 -1
0.55 0.55
-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 0.55 1.00
-1 -1 -1 -1 -1 -1 -1 -1 -1
1.00 0.55

Each time the image gets more filtered as it goes through convolution layers and smaller as it goes
through pooling layers

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Fully connected Layer
▪ This is the final layer where the actual classification happens

▪ Here we take our filtered and shrinked images and put them into a single list
1.00

▪ In this layer a list of features becomes a list of votes 0.55

1.00 0.55 0.55

0.55 1.00 1.00

1.00

1.00 0.55 0.55


In this layer the classification is done using poling.
0.55 0.55 0.55
Here every value gets a vote
0.55
0.55 1.00
0.55
1.00 0.55
1.00

1.00

0.55

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Fully connected Layer
▪ For X value, we will have certain values which will be high

1.00

0.55

0.55

1.00

1.00
X
0.55

0.55
If these values of our image is high, then it is

O
0.55
classified as X
0.55

1.00

1.00

0.55

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Fully connected Layer
▪ Similarly, when we feed an image of an O, there will be certain values which will be high

0.55

1.00

1.00

0.55

0.55
X
0.55

0.55
If these values of our image is high, then it is

O
0.55
classified as O
1.00

0.55

0.55

1.00

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Prediction
▪ When we get a new input, after passing it through all other layers we will get an array as:

0.9

0.65

We will sum up all the points of X , and all the points


of O and compare them
0.45

0.87

0.96
X
0.73

0.23

O
0.63

0.44

0.89

0.94

0.53

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Prediction
▪ Here we can see that X has more votes, thus our input image is classified as X
0.9
0.9

0.65

X
0.65

0.45

0.87

0.96
X 0.45

0.87

0.96

0.73
0.73

0.23
0.23

O
0.63

O
0.63

0.44
0.44

0.89
0.89

0.94
0.94

0.53
0.53

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Final Output
▪ Now we can see that, when we feed and input image into a CNN, we will get a set of votes for each
categories

X
O
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
CNN Use-Case

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


CNN Use-Case
We need to create a model for image classification. This model should predict the difference between
10 categories, as shown below:

Airplane

Automobile

Bird

Cat

Deer

Dog

Frog

Horse

Ship

Truck

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


CNN Use-Case Model Architecture
Start

Training
Download the dataset Convolution Layer 1

Pooling Layer 1 Prediction


Split the data into training
and testing

Convolution Layer 2 Stop


TensorFlow data
structure for holding
features, labels etc.. Pooling Layer 2

Fully Connected
Define the Convolution Layer
Layers

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


CNN Use-Case Model Architecture

Airplane
Automobile
Bird
Cat
Deer
Dog
Frog
Horse
Input Convolutional Pooling Convolutional Pooling Ship
layer 1 1 layer 2 2 Truck

Dense Output

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Download Dataset
▪ Define the path where the data will be stored, specify the URL and use the method maybe_download
and extract() to download the data set and extract it.

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Dataset : Train and Test
▪ Split the data set into training and testing

▪ Here we are importing the functions distorted_inputs() and input() from cifar10_input file.

▪ def distorted_inputs(data_dir, batch_size): Construct distorted input for CIFAR training.

▪ def inputs(eval_data, data_dir, batch_size): Construct input for CIFAR evaluation

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Define Placeholders
▪ Declare the placeholder to store the images(feautres) and labels in image_holder and label_holder
respectively.

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Define Convolutional Layers
▪ Define the convolutional layer as defined below:

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Convolutional Layers (Continued…)
▪ Define the fully connected layer:

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Loss Function
▪ Define the loss function :

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Training
▪ Perform training on the dataset :

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Prediction
▪ Check the precision on the test dataset :

Copyright © 2017, edureka and/or its affiliates. All rights reserved.


Copyright © 2017, edureka and/or its affiliates. All rights reserved.
Copyright © 2017, edureka and/or its affiliates. All rights reserved.

You might also like