Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/C4 - Convolutional Neural Networks/Week 1/Convolution_model_Application_v1a.ipynb
Views: 4802
Convolutional Neural Networks: Application
Welcome to Course 4's second assignment! In this notebook, you will:
Implement helper functions that you will use when implementing a TensorFlow model
Implement a fully functioning ConvNet using TensorFlow
After this assignment you will be able to:
Build and train a ConvNet in TensorFlow for a classification problem
We assume here that you are already familiar with TensorFlow. If you are not, please refer the TensorFlow Tutorial of the third week of Course 2 ("Improving deep neural networks").
Updates to Assignment
If you were working on a previous version
The current notebook filename is version "1a".
You can find your work in the file directory as version "1".
To view the file directory, go to the menu "File->Open", and this will open a new tab that shows the file directory.
List of Updates
initialize_parameters
: added details about tf.get_variable,eval
. Clarified test case.Added explanations for the kernel (filter) stride values, max pooling, and flatten functions.
Added details about softmax cross entropy with logits.
Added instructions for creating the Adam Optimizer.
Added explanation of how to evaluate tensors (optimizer and cost).
forward_propagation
: clarified instructions, use "F" to store "flatten" layer.Updated print statements and 'expected output' for easier visual comparisons.
Many thanks to Kevin P. Brown (mentor for the deep learning specialization) for his suggestions on the assignments in this course!
1.0 - TensorFlow model
In the previous assignment, you built helper functions using numpy to understand the mechanics behind convolutional neural networks. Most practical applications of deep learning today are built using programming frameworks, which have many built-in functions you can simply call.
As usual, we will start by loading in the packages.
Run the next cell to load the "SIGNS" dataset you are going to use.
As a reminder, the SIGNS dataset is a collection of 6 signs representing numbers from 0 to 5.
The next cell will show you an example of a labelled image in the dataset. Feel free to change the value of index
below and re-run to see different examples.
In Course 2, you had built a fully-connected network for this dataset. But since this is an image dataset, it is more natural to apply a ConvNet to it.
To get started, let's examine the shapes of your data.
1.1 - Create placeholders
TensorFlow requires that you create placeholders for the input data that will be fed into the model when running the session.
Exercise: Implement the function below to create placeholders for the input image X and the output Y. You should not define the number of training examples for the moment. To do so, you could use "None" as the batch size, it will give you the flexibility to choose it later. Hence X should be of dimension [None, n_H0, n_W0, n_C0] and Y should be of dimension [None, n_y]. Hint: search for the tf.placeholder documentation".
Expected Output
X = Tensor("Placeholder:0", shape=(?, 64, 64, 3), dtype=float32) |
1.2 - Initialize parameters
You will initialize weights/filters and using tf.contrib.layers.xavier_initializer(seed = 0)
. You don't need to worry about bias variables as you will soon see that TensorFlow functions take care of the bias. Note also that you will only initialize the weights/filters for the conv2d functions. TensorFlow initializes the layers for the fully connected part automatically. We will talk more about that later in this assignment.
Exercise: Implement initialize_parameters(). The dimensions for each group of filters are provided below. Reminder - to initialize a parameter of shape [1,2,3,4] in Tensorflow, use:
tf.get_variable()
Search for the tf.get_variable documentation. Notice that the documentation says:
So we can use this function to create a tensorflow variable with the specified name, but if the variables already exist, it will get the existing variable with that same name.
** Expected Output:**
1.3 - Forward propagation
In TensorFlow, there are built-in functions that implement the convolution steps for you.
tf.nn.conv2d(X,W, strides = [1,s,s,1], padding = 'SAME'): given an input and a group of filters , this function convolves 's filters on X. The third parameter ([1,s,s,1]) represents the strides for each dimension of the input (m, n_H_prev, n_W_prev, n_C_prev). Normally, you'll choose a stride of 1 for the number of examples (the first value) and for the channels (the fourth value), which is why we wrote the value as
[1,s,s,1]
. You can read the full documentation on conv2d.tf.nn.max_pool(A, ksize = [1,f,f,1], strides = [1,s,s,1], padding = 'SAME'): given an input A, this function uses a window of size (f, f) and strides of size (s, s) to carry out max pooling over each window. For max pooling, we usually operate on a single example at a time and a single channel at a time. So the first and fourth value in
[1,f,f,1]
are both 1. You can read the full documentation on max_pool.tf.nn.relu(Z): computes the elementwise ReLU of Z (which can be any shape). You can read the full documentation on relu.
tf.contrib.layers.flatten(P): given a tensor "P", this function takes each training (or test) example in the batch and flattens it into a 1D vector.
If a tensor P has the shape (m,h,w,c), where m is the number of examples (the batch size), it returns a flattened tensor with shape (batch_size, k), where . "k" equals the product of all the dimension sizes other than the first dimension.
For example, given a tensor with dimensions [100,2,3,4], it flattens the tensor to be of shape [100, 24], where 24 = 2 * 3 * 4. You can read the full documentation on flatten.
tf.contrib.layers.fully_connected(F, num_outputs): given the flattened input F, it returns the output computed using a fully connected layer. You can read the full documentation on full_connected.
In the last function above (tf.contrib.layers.fully_connected
), the fully connected layer automatically initializes weights in the graph and keeps on training them as you train the model. Hence, you did not need to initialize those weights when initializing the parameters.
Window, kernel, filter
The words "window", "kernel", and "filter" are used to refer to the same thing. This is why the parameter ksize
refers to "kernel size", and we use (f,f)
to refer to the filter size. Both "kernel" and "filter" refer to the "window."
Exercise
Implement the forward_propagation
function below to build the following model: CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED
. You should use the functions above.
In detail, we will use the following parameters for all the steps:
Conv2D: stride 1, padding is "SAME"
ReLU
Max pool: Use an 8 by 8 filter size and an 8 by 8 stride, padding is "SAME"
Conv2D: stride 1, padding is "SAME"
ReLU
Max pool: Use a 4 by 4 filter size and a 4 by 4 stride, padding is "SAME"
Flatten the previous output.
FULLYCONNECTED (FC) layer: Apply a fully connected layer without an non-linear activation function. Do not call the softmax here. This will result in 6 neurons in the output layer, which then get passed later to a softmax. In TensorFlow, the softmax and cost function are lumped together into a single function, which you'll call in a different function when computing the cost.
Expected Output:
1.4 - Compute cost
Implement the compute cost function below. Remember that the cost function helps the neural network see how much the model's predictions differ from the correct labels. By adjusting the weights of the network to reduce the cost, the neural network can improve its predictions.
You might find these two functions helpful:
tf.nn.softmax_cross_entropy_with_logits(logits = Z, labels = Y): computes the softmax entropy loss. This function both computes the softmax activation function as well as the resulting loss. You can check the full documentation softmax_cross_entropy_with_logits.
tf.reduce_mean: computes the mean of elements across dimensions of a tensor. Use this to calculate the sum of the losses over all the examples to get the overall cost. You can check the full documentation reduce_mean.
Details on softmax_cross_entropy_with_logits (optional reading)
Softmax is used to format outputs so that they can be used for classification. It assigns a value between 0 and 1 for each category, where the sum of all prediction values (across all possible categories) equals 1.
Cross Entropy is compares the model's predicted classifications with the actual labels and results in a numerical value representing the "loss" of the model's predictions.
"Logits" are the result of multiplying the weights and adding the biases. Logits are passed through an activation function (such as a relu), and the result is called the "activation."
The function is named
softmax_cross_entropy_with_logits
takes logits as input (and not activations); then uses the model to predict using softmax, and then compares the predictions with the true labels using cross entropy. These are done with a single function to optimize the calculations.
** Exercise**: Compute the cost below using the function above.
Expected Output:
1.5 Model
Finally you will merge the helper functions you implemented above to build a model. You will train it on the SIGNS dataset.
Exercise: Complete the function below.
The model below should:
create placeholders
initialize parameters
forward propagate
compute the cost
create an optimizer
Finally you will create a session and run a for loop for num_epochs, get the mini-batches, and then for each mini-batch you will optimize the function. Hint for initializing the variables
Adam Optimizer
You can use tf.train.AdamOptimizer(learning_rate = ...)
to create the optimizer. The optimizer has a minimize(loss=...)
function that you'll call to set the cost function that the optimizer will minimize.
For details, check out the documentation for Adam Optimizer
Random mini batches
If you took course 2 of the deep learning specialization, you implemented random_mini_batches()
in the "Optimization" programming assignment. This function returns a list of mini-batches. It is already implemented in the cnn_utils.py
file and imported here, so you can call it like this:
(You will want to choose the correct variable names when you use it in your code).
Evaluating the optimizer and cost
Within a loop, for each mini-batch, you'll use the tf.Session
object (named sess
) to feed a mini-batch of inputs and labels into the neural network and evaluate the tensors for the optimizer as well as the cost. Remember that we built a graph data structure and need to feed it inputs and labels and use sess.run()
in order to get values for the optimizer and cost.
You'll use this kind of syntax:
Notice that
sess.run
takes its first argumentfetches
as a list of objects that you want it to evaluate (in this case, we want to evaluate the optimizer and the cost).It also takes a dictionary for the
feed_dict
parameter.The keys are the
tf.placeholder
variables that we created in thecreate_placeholders
function above.The values are the variables holding the actual numpy arrays for each mini-batch.
The sess.run outputs a tuple of the evaluated tensors, in the same order as the list given to
fetches
.
For more information on how to use sess.run, see the documentation tf.Sesssion#run documentation.
Run the following cell to train your model for 100 epochs. Check if your cost after epoch 0 and 5 matches our output. If not, stop the cell and go back to your code!
Expected output: although it may not match perfectly, your expected output should be close to ours and your cost value should decrease.
**Cost after epoch 0 =** |
Congratulations! You have finished the assignment and built a model that recognizes SIGN language with almost 80% accuracy on the test set. If you wish, feel free to play around with this dataset further. You can actually improve its accuracy by spending more time tuning the hyperparameters, or using regularization (as this model clearly has a high variance).
Once again, here's a thumbs up for your work!