CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: amanchadha/coursera-deep-learning-specialization
Path: blob/master/C4 - Convolutional Neural Networks/Week 2/ResNets/resnets_utils.py
Views: 4818
1
import os
2
import numpy as np
3
import tensorflow as tf
4
import h5py
5
import math
6
7
8
def load_dataset():
9
train_dataset = h5py.File('datasets/train_signs.h5', "r")
10
# your train set features
11
train_set_x_orig = np.array(train_dataset["train_set_x"][:])
12
train_set_y_orig = np.array(
13
train_dataset["train_set_y"][:]) # your train set labels
14
15
test_dataset = h5py.File('datasets/test_signs.h5', "r")
16
# your test set features
17
test_set_x_orig = np.array(test_dataset["test_set_x"][:])
18
test_set_y_orig = np.array(
19
test_dataset["test_set_y"][:]) # your test set labels
20
21
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
22
23
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
24
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
25
26
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
27
28
29
def random_mini_batches(X, Y, mini_batch_size=64, seed=0):
30
"""
31
Creates a list of random minibatches from (X, Y)
32
33
Arguments:
34
X -- input data, of shape (input size, number of examples) (m, Hi, Wi, Ci)
35
Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples) (m, n_y)
36
mini_batch_size - size of the mini-batches, integer
37
seed -- this is only for the purpose of grading, so that you're "random minibatches are the same as ours.
38
39
Returns:
40
mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y)
41
"""
42
43
m = X.shape[0] # number of training examples
44
mini_batches = []
45
np.random.seed(seed)
46
47
# Step 1: Shuffle (X, Y)
48
permutation = list(np.random.permutation(m))
49
shuffled_X = X[permutation, :, :, :]
50
shuffled_Y = Y[permutation, :]
51
52
# Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.
53
# number of mini batches of size mini_batch_size in your partitionning
54
num_complete_minibatches = math.floor(m / mini_batch_size)
55
for k in range(0, num_complete_minibatches):
56
mini_batch_X = shuffled_X[k * mini_batch_size: k *
57
mini_batch_size + mini_batch_size, :, :, :]
58
mini_batch_Y = shuffled_Y[k * mini_batch_size: k *
59
mini_batch_size + mini_batch_size, :]
60
mini_batch = (mini_batch_X, mini_batch_Y)
61
mini_batches.append(mini_batch)
62
63
# Handling the end case (last mini-batch < mini_batch_size)
64
if m % mini_batch_size != 0:
65
mini_batch_X = shuffled_X[num_complete_minibatches *
66
mini_batch_size: m, :, :, :]
67
mini_batch_Y = shuffled_Y[num_complete_minibatches *
68
mini_batch_size: m, :]
69
mini_batch = (mini_batch_X, mini_batch_Y)
70
mini_batches.append(mini_batch)
71
72
return mini_batches
73
74
75
def convert_to_one_hot(Y, C):
76
Y = np.eye(C)[Y.reshape(-1)].T
77
return Y
78
79
80
def forward_propagation_for_predict(X, parameters):
81
"""
82
Implements the forward propagation for the model: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX
83
84
Arguments:
85
X -- input dataset placeholder, of shape (input size, number of examples)
86
parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3"
87
the shapes are given in initialize_parameters
88
89
Returns:
90
Z3 -- the output of the last LINEAR unit
91
"""
92
93
# Retrieve the parameters from the dictionary "parameters"
94
W1 = parameters['W1']
95
b1 = parameters['b1']
96
W2 = parameters['W2']
97
b2 = parameters['b2']
98
W3 = parameters['W3']
99
b3 = parameters['b3']
100
# Numpy Equivalents:
101
# Z1 = np.dot(W1, X) + b1
102
Z1 = tf.add(tf.matmul(W1, X), b1)
103
A1 = tf.nn.relu(Z1) # A1 = relu(Z1)
104
# Z2 = np.dot(W2, a1) + b2
105
Z2 = tf.add(tf.matmul(W2, A1), b2)
106
A2 = tf.nn.relu(Z2) # A2 = relu(Z2)
107
# Z3 = np.dot(W3,Z2) + b3
108
Z3 = tf.add(tf.matmul(W3, A2), b3)
109
110
return Z3
111
112
113
def predict(X, parameters):
114
115
W1 = tf.convert_to_tensor(parameters["W1"])
116
b1 = tf.convert_to_tensor(parameters["b1"])
117
W2 = tf.convert_to_tensor(parameters["W2"])
118
b2 = tf.convert_to_tensor(parameters["b2"])
119
W3 = tf.convert_to_tensor(parameters["W3"])
120
b3 = tf.convert_to_tensor(parameters["b3"])
121
122
params = {"W1": W1,
123
"b1": b1,
124
"W2": W2,
125
"b2": b2,
126
"W3": W3,
127
"b3": b3}
128
129
x = tf.placeholder("float", [12288, 1])
130
131
z3 = forward_propagation_for_predict(x, params)
132
p = tf.argmax(z3)
133
134
sess = tf.Session()
135
prediction = sess.run(p, feed_dict={x: X})
136
137
return prediction
138
139