\n",
+ " \n",
+ " \n",
+ " **x.shape**:\n",
+ " | \n",
+ " \n",
+ " (4, 3, 3, 2)\n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " **x_pad.shape**:\n",
+ " | \n",
+ " \n",
+ " (4, 7, 7, 2)\n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " **x[1,1]**:\n",
+ " | \n",
+ " \n",
+ " [[ 0.90085595 -0.68372786]\n",
+ " [-0.12289023 -0.93576943]\n",
+ " [-0.26788808 0.53035547]]\n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " **x_pad[1,1]**:\n",
+ " | \n",
+ " \n",
+ " [[ 0. 0.]\n",
+ " [ 0. 0.]\n",
+ " [ 0. 0.]\n",
+ " [ 0. 0.]\n",
+ " [ 0. 0.]\n",
+ " [ 0. 0.]\n",
+ " [ 0. 0.]]\n",
+ " | \n",
+ "
\n",
+ "\n",
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 3.2 - Single step of convolution \n",
+ "\n",
+ "In this part, implement a single step of convolution, in which you apply the filter to a single position of the input. This will be used to build a convolutional unit, which: \n",
+ "\n",
+ "- Takes an input volume \n",
+ "- Applies a filter at every position of the input\n",
+ "- Outputs another volume (usually of different size)\n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ " **Z's mean**\n",
+ " | \n",
+ " \n",
+ " 0.0489952035289\n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " **Z[3,2,1]**\n",
+ " | \n",
+ " \n",
+ " [-0.61490741 -6.7439236 -2.55153897 1.75698377 3.56208902 0.53036437\n",
+ " 5.18531798 8.75898442]\n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " **cache_conv[0][1][2][3]**\n",
+ " | \n",
+ " \n",
+ " [-0.20075807 0.18656139 0.41005165]\n",
+ " | \n",
+ "
\n",
+ "\n",
+ "
\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Finally, CONV layer should also contain an activation, in which case we would add the following line of code:\n",
+ "\n",
+ "```python\n",
+ "# Convolve the window to get back one output neuron\n",
+ "Z[i, h, w, c] = ...\n",
+ "# Apply activation\n",
+ "A[i, h, w, c] = activation(Z[i, h, w, c])\n",
+ "```\n",
+ "\n",
+ "You don't need to do it here. \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 4 - Pooling layer \n",
+ "\n",
+ "The pooling (POOL) layer reduces the height and width of the input. It helps reduce computation, as well as helps make feature detectors more invariant to its position in the input. The two types of pooling layers are: \n",
+ "\n",
+ "- Max-pooling layer: slides an ($f, f$) window over the input and stores the max value of the window in the output.\n",
+ "\n",
+ "- Average-pooling layer: slides an ($f, f$) window over the input and stores the average value of the window in the output.\n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ " **dA_mean**\n",
+ " | \n",
+ " \n",
+ " 1.45243777754\n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " **dW_mean**\n",
+ " | \n",
+ " \n",
+ " 1.72699145831\n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " **db_mean**\n",
+ " | \n",
+ " \n",
+ " 7.83923256462\n",
+ " | \n",
+ "
\n",
+ "\n",
+ "
\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 5.2 Pooling layer - backward pass\n",
+ "\n",
+ "Next, let's implement the backward pass for the pooling layer, starting with the MAX-POOL layer. Even though a pooling layer has no parameters for backprop to update, you still need to backpropagation the gradient through the pooling layer in order to compute gradients for layers that came before the pooling layer. \n",
+ "\n",
+ "### 5.2.1 Max pooling - backward pass \n",
+ "\n",
+ "Before jumping into the backpropagation of the pooling layer, you are going to build a helper function called `create_mask_from_window()` which does the following: \n",
+ "\n",
+ "$$ X = \\begin{bmatrix}\n",
+ "1 && 3 \\\\\n",
+ "4 && 2\n",
+ "\\end{bmatrix} \\quad \\rightarrow \\quad M =\\begin{bmatrix}\n",
+ "0 && 0 \\\\\n",
+ "1 && 0\n",
+ "\\end{bmatrix}\\tag{4}$$\n",
+ "\n",
+ "As you can see, this function creates a \"mask\" matrix which keeps track of where the maximum of the matrix is. True (1) indicates the position of the maximum in X, the other entries are False (0). You'll see later that the backward pass for average pooling will be similar to this but using a different mask. \n",
+ "\n",
+ "**Exercise**: Implement `create_mask_from_window()`. This function will be helpful for pooling backward. \n",
+ "Hints:\n",
+ "- [np.max()]() may be helpful. It computes the maximum of an array.\n",
+ "- If you have a matrix X and a scalar x: `A = (X == x)` will return a matrix A of the same size as X such that:\n",
+ "```\n",
+ "A[i,j] = True if X[i,j] = x\n",
+ "A[i,j] = False if X[i,j] != x\n",
+ "```\n",
+ "- Here, you don't need to consider cases where there are several maxima in a matrix."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 82,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def create_mask_from_window(x):\n",
+ " \"\"\"\n",
+ " Creates a mask from an input matrix x, to identify the max entry of x.\n",
+ " \n",
+ " Arguments:\n",
+ " x -- Array of shape (f, f)\n",
+ " \n",
+ " Returns:\n",
+ " mask -- Array of the same shape as window, contains a True at the position corresponding to the max entry of x.\n",
+ " \"\"\"\n",
+ " \n",
+ " ### START CODE HERE ### (≈1 line)\n",
+ " mask = (x==np.max(x))\n",
+ " ### END CODE HERE ###\n",
+ " \n",
+ " return mask"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 83,
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "x = [[ 1.62434536 -0.61175641 -0.52817175]\n",
+ " [-1.07296862 0.86540763 -2.3015387 ]]\n",
+ "mask = [[ True False False]\n",
+ " [False False False]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "np.random.seed(1)\n",
+ "x = np.random.randn(2,3)\n",
+ "mask = create_mask_from_window(x)\n",
+ "print('x = ', x)\n",
+ "print(\"mask = \", mask)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "**Expected Output:** \n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ "\n",
+ "**x =**\n",
+ " | \n",
+ "\n",
+ "\n",
+ "\n",
+ "[[ 1.62434536 -0.61175641 -0.52817175] \n",
+ " [-1.07296862 0.86540763 -2.3015387 ]]\n",
+ "\n",
+ " | \n",
+ "
\n",
+ "\n",
+ " \n",
+ "\n",
+ "**mask =**\n",
+ " | \n",
+ "\n",
+ "[[ True False False] \n",
+ " [False False False]]\n",
+ " | \n",
+ "
\n",
+ "\n",
+ "\n",
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Why do we keep track of the position of the max? It's because this is the input value that ultimately influenced the output, and therefore the cost. Backprop is computing gradients with respect to the cost, so anything that influences the ultimate cost should have a non-zero gradient. So, backprop will \"propagate\" the gradient back to this particular input value that had influenced the cost. "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 5.2.2 - Average pooling - backward pass \n",
+ "\n",
+ "In max pooling, for each input window, all the \"influence\" on the output came from a single input value--the max. In average pooling, every element of the input window has equal influence on the output. So to implement backprop, you will now implement a helper function that reflects this.\n",
+ "\n",
+ "For example if we did average pooling in the forward pass using a 2x2 filter, then the mask you'll use for the backward pass will look like: \n",
+ "$$ dZ = 1 \\quad \\rightarrow \\quad dZ =\\begin{bmatrix}\n",
+ "1/4 && 1/4 \\\\\n",
+ "1/4 && 1/4\n",
+ "\\end{bmatrix}\\tag{5}$$\n",
+ "\n",
+ "This implies that each position in the $dZ$ matrix contributes equally to output because in the forward pass, we took an average. \n",
+ "\n",
+ "**Exercise**: Implement the function below to equally distribute a value dz through a matrix of dimension shape. [Hint](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ones.html)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 84,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def distribute_value(dz, shape):\n",
+ " \"\"\"\n",
+ " Distributes the input value in the matrix of dimension shape\n",
+ " \n",
+ " Arguments:\n",
+ " dz -- input scalar\n",
+ " shape -- the shape (n_H, n_W) of the output matrix for which we want to distribute the value of dz\n",
+ " \n",
+ " Returns:\n",
+ " a -- Array of size (n_H, n_W) for which we distributed the value of dz\n",
+ " \"\"\"\n",
+ " \n",
+ " ### START CODE HERE ###\n",
+ " # Retrieve dimensions from shape (≈1 line)\n",
+ " (n_H, n_W) = shape\n",
+ " \n",
+ " # Compute the value to distribute on the matrix (≈1 line)\n",
+ " average = dz/(n_H+n_W)\n",
+ " \n",
+ " # Create a matrix where every entry is the \"average\" value (≈1 line)\n",
+ " a = np.zeros(shape)+average\n",
+ " ### END CODE HERE ###\n",
+ " \n",
+ " return a"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 85,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "distributed value = [[0.5 0.5]\n",
+ " [0.5 0.5]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "a = distribute_value(2, (2,2))\n",
+ "print('distributed value =', a)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**Expected Output**: \n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ "\n",
+ "**mean of dA =**\n",
+ " | \n",
+ "\n",
+ "\n",
+ "\n",
+ "0.145713902729\n",
+ "\n",
+ " | \n",
+ "
\n",
+ "\n",
+ " \n",
+ "\n",
+ "**dA_prev[1,1] =** \n",
+ " | \n",
+ "\n",
+ "[[ 0. 0. ] \n",
+ " [ 5.05844394 -1.68282702] \n",
+ " [ 0. 0. ]]\n",
+ " | \n",
+ "
\n",
+ "
\n",
+ "\n",
+ "mode = average\n",
+ " \n",
+ " \n",
+ "\n",
+ "\n",
+ "**mean of dA =**\n",
+ " | \n",
+ "\n",
+ "\n",
+ "\n",
+ "0.145713902729\n",
+ "\n",
+ " | \n",
+ "
\n",
+ "\n",
+ " \n",
+ "\n",
+ "**dA_prev[1,1] =** \n",
+ " | \n",
+ "\n",
+ "[[ 0.08485462 0.2787552 ] \n",
+ " [ 1.26461098 -0.25749373] \n",
+ " [ 1.17975636 -0.53624893]]\n",
+ " | \n",
+ "
\n",
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Congratulations !\n",
+ "\n",
+ "Congratulation on completing this assignment. You now understand how convolutional neural networks work. You have implemented all the building blocks of a neural network. In the next assignment you will implement a ConvNet using TensorFlow."
+ ]
+ }
+ ],
+ "metadata": {
+ "coursera": {
+ "course_slug": "convolutional-neural-networks",
+ "graded_item_id": "qO8ng",
+ "launcher_item_id": "7XDi8"
+ },
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.7.1"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 1
+}
diff --git a/cnn/cnn_report.pdf b/cnn/cnn_report.pdf
new file mode 100644
index 0000000..082457b
Binary files /dev/null and b/cnn/cnn_report.pdf differ
diff --git a/cnn/cnn_utils.py b/cnn/cnn_utils.py
new file mode 100644
index 0000000..4bdf418
--- /dev/null
+++ b/cnn/cnn_utils.py
@@ -0,0 +1,155 @@
+import math
+import numpy as np
+import h5py
+import matplotlib.pyplot as plt
+import tensorflow as tf
+from tensorflow.python.framework import ops
+
+def load_dataset():
+ train_dataset = h5py.File('datasets/train_signs.h5', "r")
+ train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
+ train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
+
+ test_dataset = h5py.File('datasets/test_signs.h5', "r")
+ test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
+ test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
+
+ classes = np.array(test_dataset["list_classes"][:]) # the list of classes
+
+ train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
+ test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
+
+ return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
+
+
+def random_mini_batches(X, Y, mini_batch_size = 64, seed = 0):
+ """
+ Creates a list of random minibatches from (X, Y)
+
+ Arguments:
+ X -- input data, of shape (input size, number of examples) (m, Hi, Wi, Ci)
+ Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples) (m, n_y)
+ mini_batch_size - size of the mini-batches, integer
+ seed -- this is only for the purpose of grading, so that you're "random minibatches are the same as ours.
+
+ Returns:
+ mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y)
+ """
+
+ m = X.shape[0] # number of training examples
+ mini_batches = []
+ np.random.seed(seed)
+
+ # Step 1: Shuffle (X, Y)
+ permutation = list(np.random.permutation(m))
+ shuffled_X = X[permutation,:,:,:]
+ shuffled_Y = Y[permutation,:]
+
+ # Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.
+ num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionning
+ for k in range(0, num_complete_minibatches):
+ mini_batch_X = shuffled_X[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:,:,:]
+ mini_batch_Y = shuffled_Y[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:]
+ mini_batch = (mini_batch_X, mini_batch_Y)
+ mini_batches.append(mini_batch)
+
+ # Handling the end case (last mini-batch < mini_batch_size)
+ if m % mini_batch_size != 0:
+ mini_batch_X = shuffled_X[num_complete_minibatches * mini_batch_size : m,:,:,:]
+ mini_batch_Y = shuffled_Y[num_complete_minibatches * mini_batch_size : m,:]
+ mini_batch = (mini_batch_X, mini_batch_Y)
+ mini_batches.append(mini_batch)
+
+ return mini_batches
+
+
+def convert_to_one_hot(Y, C):
+ Y = np.eye(C)[Y.reshape(-1)].T
+ return Y
+
+
+def forward_propagation_for_predict(X, parameters):
+ """
+ Implements the forward propagation for the model: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX
+
+ Arguments:
+ X -- input dataset placeholder, of shape (input size, number of examples)
+ parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3"
+ the shapes are given in initialize_parameters
+
+ Returns:
+ Z3 -- the output of the last LINEAR unit
+ """
+
+ # Retrieve the parameters from the dictionary "parameters"
+ W1 = parameters['W1']
+ b1 = parameters['b1']
+ W2 = parameters['W2']
+ b2 = parameters['b2']
+ W3 = parameters['W3']
+ b3 = parameters['b3']
+ # Numpy Equivalents:
+ Z1 = tf.add(tf.matmul(W1, X), b1) # Z1 = np.dot(W1, X) + b1
+ A1 = tf.nn.relu(Z1) # A1 = relu(Z1)
+ Z2 = tf.add(tf.matmul(W2, A1), b2) # Z2 = np.dot(W2, a1) + b2
+ A2 = tf.nn.relu(Z2) # A2 = relu(Z2)
+ Z3 = tf.add(tf.matmul(W3, A2), b3) # Z3 = np.dot(W3,Z2) + b3
+
+ return Z3
+
+def predict(X, parameters):
+
+ W1 = tf.convert_to_tensor(parameters["W1"])
+ b1 = tf.convert_to_tensor(parameters["b1"])
+ W2 = tf.convert_to_tensor(parameters["W2"])
+ b2 = tf.convert_to_tensor(parameters["b2"])
+ W3 = tf.convert_to_tensor(parameters["W3"])
+ b3 = tf.convert_to_tensor(parameters["b3"])
+
+ params = {"W1": W1,
+ "b1": b1,
+ "W2": W2,
+ "b2": b2,
+ "W3": W3,
+ "b3": b3}
+
+ x = tf.placeholder("float", [12288, 1])
+
+ z3 = forward_propagation_for_predict(x, params)
+ p = tf.argmax(z3)
+
+ sess = tf.Session()
+ prediction = sess.run(p, feed_dict = {x: X})
+
+ return prediction
+
+#def predict(X, parameters):
+#
+# W1 = tf.convert_to_tensor(parameters["W1"])
+# b1 = tf.convert_to_tensor(parameters["b1"])
+# W2 = tf.convert_to_tensor(parameters["W2"])
+# b2 = tf.convert_to_tensor(parameters["b2"])
+## W3 = tf.convert_to_tensor(parameters["W3"])
+## b3 = tf.convert_to_tensor(parameters["b3"])
+#
+## params = {"W1": W1,
+## "b1": b1,
+## "W2": W2,
+## "b2": b2,
+## "W3": W3,
+## "b3": b3}
+#
+# params = {"W1": W1,
+# "b1": b1,
+# "W2": W2,
+# "b2": b2}
+#
+# x = tf.placeholder("float", [12288, 1])
+#
+# z3 = forward_propagation(x, params)
+# p = tf.argmax(z3)
+#
+# with tf.Session() as sess:
+# prediction = sess.run(p, feed_dict = {x: X})
+#
+# return prediction
\ No newline at end of file
diff --git a/cnn/datasets/test_signs.h5 b/cnn/datasets/test_signs.h5
new file mode 100644
index 0000000..ac34131
Binary files /dev/null and b/cnn/datasets/test_signs.h5 differ
diff --git a/cnn/datasets/train_signs.h5 b/cnn/datasets/train_signs.h5
new file mode 100644
index 0000000..15904fb
Binary files /dev/null and b/cnn/datasets/train_signs.h5 differ
diff --git a/cnn/images/Convolution_schematic.gif b/cnn/images/Convolution_schematic.gif
new file mode 100644
index 0000000..d8c73dc
Binary files /dev/null and b/cnn/images/Convolution_schematic.gif differ
diff --git a/cnn/images/PAD.png b/cnn/images/PAD.png
new file mode 100644
index 0000000..883ad2d
Binary files /dev/null and b/cnn/images/PAD.png differ
diff --git a/cnn/images/SIGNS.png b/cnn/images/SIGNS.png
new file mode 100644
index 0000000..abba7c4
Binary files /dev/null and b/cnn/images/SIGNS.png differ
diff --git a/cnn/images/a_pool.png b/cnn/images/a_pool.png
new file mode 100644
index 0000000..93c2f33
Binary files /dev/null and b/cnn/images/a_pool.png differ
diff --git a/cnn/images/ave-pool.png b/cnn/images/ave-pool.png
new file mode 100644
index 0000000..eb2704e
Binary files /dev/null and b/cnn/images/ave-pool.png differ
diff --git a/cnn/images/ave_pool1.png b/cnn/images/ave_pool1.png
new file mode 100644
index 0000000..5e36230
Binary files /dev/null and b/cnn/images/ave_pool1.png differ
diff --git a/cnn/images/average_pool.png b/cnn/images/average_pool.png
new file mode 100644
index 0000000..d60c6aa
Binary files /dev/null and b/cnn/images/average_pool.png differ
diff --git a/cnn/images/conv.png b/cnn/images/conv.png
new file mode 100644
index 0000000..0ae8eef
Binary files /dev/null and b/cnn/images/conv.png differ
diff --git a/cnn/images/conv1.png b/cnn/images/conv1.png
new file mode 100644
index 0000000..fd0e1a8
Binary files /dev/null and b/cnn/images/conv1.png differ
diff --git a/cnn/images/conv_kiank.mp4 b/cnn/images/conv_kiank.mp4
new file mode 100644
index 0000000..c550f75
Binary files /dev/null and b/cnn/images/conv_kiank.mp4 differ
diff --git a/cnn/images/conv_nn.png b/cnn/images/conv_nn.png
new file mode 100644
index 0000000..e90a9de
Binary files /dev/null and b/cnn/images/conv_nn.png differ
diff --git a/cnn/images/max_pool.png b/cnn/images/max_pool.png
new file mode 100644
index 0000000..17c82ba
Binary files /dev/null and b/cnn/images/max_pool.png differ
diff --git a/cnn/images/max_pool1.png b/cnn/images/max_pool1.png
new file mode 100644
index 0000000..36acdb5
Binary files /dev/null and b/cnn/images/max_pool1.png differ
diff --git a/cnn/images/model.png b/cnn/images/model.png
new file mode 100644
index 0000000..84dc475
Binary files /dev/null and b/cnn/images/model.png differ
diff --git a/cnn/images/thumbs_up.jpg b/cnn/images/thumbs_up.jpg
new file mode 100644
index 0000000..64ab7dc
Binary files /dev/null and b/cnn/images/thumbs_up.jpg differ
diff --git a/cnn/images/vert_horiz_kiank.png b/cnn/images/vert_horiz_kiank.png
new file mode 100644
index 0000000..15e28bb
Binary files /dev/null and b/cnn/images/vert_horiz_kiank.png differ
diff --git a/cnn/my_signs_tf.ipynb b/cnn/my_signs_tf.ipynb
new file mode 100644
index 0000000..02a4994
--- /dev/null
+++ b/cnn/my_signs_tf.ipynb
@@ -0,0 +1,271 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import numpy as np\n",
+ "import tensorflow as tf\n",
+ "import h5py\n",
+ "import matplotlib.pyplot as plt\n",
+ "from tensorflow.python.framework import ops"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "train_data=h5py.File('datasets/train_signs.h5', 'r')\n",
+ "test_data=h5py.File('datasets/test_signs.h5', 'r')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "x_train=np.array(train_data['train_set_x'])/255\n",
+ "y_train=np.array(train_data['train_set_y'])\n",
+ "\n",
+ "x_test=np.array(test_data['test_set_x'])/255\n",
+ "y_test=np.array(test_data['test_set_y'])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "classes= np.array(train_data['list_classes'])\n",
+ "M=np.eye(classes.shape[0])\n",
+ "def one_hot(Y,classes=classes):\n",
+ " Y_hot=np.zeros((Y.shape[0], classes.shape[0]))\n",
+ " for i in range(Y.shape[0]):\n",
+ " Y_hot[i]=M[Y[i]]\n",
+ " return Y_hot"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "y_train=one_hot(y_train, classes)\n",
+ "y_test=one_hot(y_test, classes)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "y_train.shape"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def forward_prop(X,parameters):\n",
+ " W1=parameters['W1']\n",
+ " W2=parameters['W2']\n",
+ " Z1=tf.nn.conv2d(X, filter=W1, strides=[1,1,1,1], padding='SAME')\n",
+ " A1=tf.nn.relu(Z1)\n",
+ " A1=tf.nn.max_pool(A1,ksize=[1,8,8,1],strides=[1,8,8,1], padding='SAME')\n",
+ " Z2=tf.nn.conv2d(A1, filter=W2, strides=[1,1,1,1], padding='SAME')\n",
+ " A2=tf.nn.relu(Z2)\n",
+ " A2=tf.nn.max_pool(A2, ksize=[1,4,4,1], strides=[1,4,4,1], padding='SAME')\n",
+ "\n",
+ " F=tf.contrib.layers.flatten(A2)\n",
+ " Z3=tf.contrib.layers.fully_connected(F,6, activation_fn=None)\n",
+ " return Z3"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "ops.reset_default_graph()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def cost(Z3,Y):\n",
+ " return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z3, labels=Y)) "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "\n",
+ "\n",
+ "X=tf.placeholder(dtype=tf.float32, shape=[None,64,64,3])\n",
+ "Y=tf.placeholder(dtype=tf.float32, shape=[None,6])\n",
+ "\n",
+ "tf.set_random_seed(1)\n",
+ "W1=tf.get_variable('W1', shape=[4,4,3,8], initializer=tf.contrib.layers.xavier_initializer(seed=0))\n",
+ "W2=tf.get_variable('W2', shape=[2,2,8,16], initializer=tf.contrib.layers.xavier_initializer(seed=0))\n",
+ "parameters={'W1':W1, 'W2':W2}\n",
+ "\n",
+ "Z3=forward_prop(X,parameters)\n",
+ "cost=cost(Z3,Y)\n",
+ "optimizer=tf.train.AdamOptimizer(learning_rate=0.009).minimize(cost)\n",
+ "init=tf.global_variables_initializer()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "