CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

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