Skip to content

Commit

Permalink
feat: adding a plot figure of expected output
Browse files Browse the repository at this point in the history
  • Loading branch information
awbrown90 committed Feb 18, 2017
1 parent 476c6d5 commit ad28fde
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions Traffic_Sign_Classifier.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,20 @@
"\n",
"## Step 4: Visualize the Neural Network's State with Test Images\n",
"\n",
" This Section is not required to complete but acts as an additional excersise for understaning the output of a neural network's weights. While neural networks can be a great learning device they are often referred to as a black box. We can understand what the weights of a neural network look like better by plotting their feature maps. After successfully training your neural network you can see what it's feature maps look like by plotting the output of the network's weight layers in response to a test stimuli image. From these plotted feature maps, it's possible to see what characteristics of an image the network finds interesting. For a sign, maybe the inner network feature maps react with high activation to the sign's boundary outline or to the contrast in the sign's painted symbol.\n",
" This Section is not required to complete but acts as an additional excersise for understaning the output of a neural network's weights. While neural networks can be a great learning device they are often referred to as a black box. We can understand what the weights of a neural network look like better by plotting their feature maps. After successfully training your neural network you can see what it's feature maps look like by plotting the output of the network's weight layers in response to a test stimuli image. From these plotted feature maps, it's possible to see what characteristics of an image the network finds interesting. For a sign, maybe the inner network feature maps react with high activation to the sign's boundary outline or to the contrast in the sign's painted symbol.\n",
"\n",
" Provided for you below is the function code that allows you to get the visualization output of any tensorflow weight layer you want. The inputs to the function should be a stimuli image, one used during training or a new one you provided, and then the tensorflow variable name that represents the layer's state during the training process, for instance if you wanted to see what the [LeNet lab's](https://classroom.udacity.com/nanodegrees/nd013/parts/fbf77062-5703-404e-b60c-95b78b2f3f9e/modules/6df7ae49-c61c-4bb2-a23e-6527e69209ec/lessons/601ae704-1035-4287-8b11-e2c2716217ad/concepts/d4aca031-508f-4e0b-b493-e7b706120f81) feature maps looked like for it's second convolutional layer you could enter conv2 as the tf_activation variable.\n",
"\n",
"For an example of what feature map outputs look like, check out NVIDIA's results in their paper [End-to-End Deep Learning for Self-Driving Cars](https://devblogs.nvidia.com/parallelforall/deep-learning-self-driving-cars/) in the section Visualization of internal CNN State. NVIDIA was able to show that their network's inner weights had high activations to road boundary lines by comparing feature maps from an image with a clear path to one without. Try experimenting with a similar test to show that your trained network's weights are looking for interesting features, whether it's looking at differences in feature maps from images with or without a sign, or even what feature maps look like in a trained network vs a completely untrained one on the same sign image."
"For an example of what feature map outputs look like, check out NVIDIA's results in their paper [End-to-End Deep Learning for Self-Driving Cars](https://devblogs.nvidia.com/parallelforall/deep-learning-self-driving-cars/) in the section Visualization of internal CNN State. NVIDIA was able to show that their network's inner weights had high activations to road boundary lines by comparing feature maps from an image with a clear path to one without. Try experimenting with a similar test to show that your trained network's weights are looking for interesting features, whether it's looking at differences in feature maps from images with or without a sign, or even what feature maps look like in a trained network vs a completely untrained one on the same sign image.\n",
"\n",
"<figure>\n",
" <img src=\"visualize_cnn.png\" width=\"380\" alt=\"Combined Image\" />\n",
" <figcaption>\n",
" <p></p> \n",
" <p style=\"text-align: center;\"> Your output should look something like this (above)</p> \n",
" </figcaption>\n",
"</figure>\n",
" <p></p> \n"
]
},
{
Expand All @@ -453,32 +462,32 @@
"### Visualize your network's feature maps here.\n",
"### Feel free to use as many code cells as needed.\n",
"\n",
"# image_input: the test image being fed into the network to produce the feature maps\n",
"# image_input: the test image being fed into the network to produce the feature maps\n",
"# tf_activation: should be a tf variable name used during your training procedure that represents the calculated state of a specific weight layer\n",
"# activation_min/max: can be used to view the activation contrast in more detail, by default matplot sets min and max to the actual min and max values of the output\n",
"# plt_num: used to plot out multiple different weight feature map sets on the same block, just extend the plt number for each new feature map entry\n",
"\n",
"def outputFeatureMap(image_input, tf_activation, activation_min=-1, activation_max=-1 ,plt_num=1):\n",
" # Here make sure to preprocess your image_input in a way your network expects\n",
" # with size, normalization, ect if needed\n",
" # image_input =\n",
" # Note: x should be the same name as your network's tensorflow data placeholder variable\n",
" # If you get an error tf_activation is not defined it maybe having trouble accessing the variable from inside a function\n",
" activation = tf_activation.eval(session=sess,feed_dict={x : image_input})\n",
" featuremaps = activation.shape[3]\n",
" plt.figure(plt_num, figsize=(15,15))\n",
" for featuremap in range(featuremaps):\n",
" plt.subplot(6,8, featuremap+1) # sets the number of feature maps to show on each row and column\n",
" # Here make sure to preprocess your image_input in a way your network expects\n",
" # with size, normalization, ect if needed\n",
" # image_input =\n",
" # Note: x should be the same name as your network's tensorflow data placeholder variable\n",
" # If you get an error tf_activation is not defined it maybe having trouble accessing the variable from inside a function\n",
" activation = tf_activation.eval(session=sess,feed_dict={x : image_input})\n",
" featuremaps = activation.shape[3]\n",
" plt.figure(plt_num, figsize=(15,15))\n",
" for featuremap in range(featuremaps):\n",
" plt.subplot(6,8, featuremap+1) # sets the number of feature maps to show on each row and column\n",
" plt.title('FeatureMap ' + str(featuremap)) # displays the feature map number\n",
" if activation_min != -1 & activation_max != -1:\n",
" plt.imshow(activation[0,:,:, featuremap], interpolation=\"nearest\", vmin =activation_min, vmax=activation_max, cmap=\"gray\")\n",
" elif activation_max != -1:\n",
" plt.imshow(activation[0,:,:, featuremap], interpolation=\"nearest\", vmax=activation_max, cmap=\"gray\")\n",
" elif activation_min !=-1:\n",
" plt.imshow(activation[0,:,:, featuremap], interpolation=\"nearest\", vmin=activation_min, cmap=\"gray\")\n",
" else:\n",
" plt.imshow(activation[0,:,:, featuremap], interpolation=\"nearest\", cmap=\"gray\")"
]
" elif activation_max != -1:\n",
" plt.imshow(activation[0,:,:, featuremap], interpolation=\"nearest\", vmax=activation_max, cmap=\"gray\")\n",
" elif activation_min !=-1:\n",
" plt.imshow(activation[0,:,:, featuremap], interpolation=\"nearest\", vmin=activation_min, cmap=\"gray\")\n",
" else:\n",
" plt.imshow(activation[0,:,:, featuremap], interpolation=\"nearest\", cmap=\"gray\")"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -517,7 +526,7 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
Expand Down
Binary file added visualize_cnn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ad28fde

Please sign in to comment.