Professional Documents
Culture Documents
CNN Hands On
CNN Hands On
Pre-Requirements:
...................................................................................................................................................
Cell 1:
import numpy as np
...................................................................................................................................................
Cell 2:
img = mpimg.imread("home.png")
###End code
print(type(img))
....................................................................................................................................................
………………………………………………………………………………………………………
Cell 3:
plt.imshow(data[0,:,:,:])
plt.grid(False)
plt.axis("off")
...................................................................................................................................................
Cell 4:
return data_padded
###End code
...................................................................................................................................................
Cell 5:
print(img_pad[0,8:12,8:12,1])
plt.grid(False)
output1 = np.mean(img_pad)
...................................................................................................................................................
…………………………………………………………………………………………………
Cell 6:
###Start code
conv = np.multiply(data_slice, W)
Z = np.sum(conv) + b
###End code
return Z
....................................................................................................................................................
Cell 7:
stride = hparams["stride"]
pad = hparams["pad"]
for i in range(m):
for h in range(n_h):
for w in range(n_w):
for c in range(n_c):
w_start = w * stride
w_end = w_start + f
h_start = h * stride
h_end = h_start + f
return Z
....................................................................................................................................................
Cell 8:
np.random.seed(1)
input_ = np.random.randn(10, 4, 4, 3)
W = np.random.randn(2, 2, 3, 8)
b = np.random.randn(1, 1, 1, 8)
hparameters = {"pad" : 1,
"stride": 1}
print(np.mean(output_))
....................................................................................................................................................
………………………………………………………………………………………………
Cell 9:
edge_detect = np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]).reshape((3,3,1,1))
...................................................................................................................................................
Cell 10:
###Start code
b = np.zeros((1, 1, 1, 1))
plt.clf()
plt.grid(False)
output2 = np.mean(Z[0,100:200,200:300,0])
....................................................................................................................................................
………………………………………………………………………………………………………
Cell 11:
###start code
stride = hparam["stride"]
for i in range(m):
for c in range(c_prev):
for h in range(h_out):
for w in range(w_out):
w_start = w * stride
w_end = w_start + f
h_start = h * stride
h_end = h_start + f
output[i, h, w, c] = np.max(input[i,h_start:h_end,
w_start:w_end, c])
print(output.shape)
###End code
return output
............................................................................................................................................................
………………………………………………………………………………………………………
Cell 12:
print(np.mean(output_))
............................................................................................................................................................
Cell 13:
###start code
###End code
............................................................................................................................................................
Pre-Requirements:
2. Wait for the installation of packages (Minimum it will take 30-45 minutes)
………………………………………………………………………………………………………
CELL 1:
import numpy as np
import tensorflow as tf
...........................................................................................................................................................
CELL 2:
img = mpimg.imread("bird.png")
###End code
print(type(img))
print("Image dimension ",img.shape)
print(img.shape)
.................................................................................................................................................
CELL 3:
plt.imshow(data[0,:,:,:])
...........................................................................................................................................
CELL 4:
graph = tf.Graph()
with graph.as_default():
tf.random.set_seed(1)
W = tf.Variable(tf.random.normal([5,5,3,32]))
b = tf.Variable(tf.random.normal([32]))
conv_bias = tf.nn.bias_add(conv, b)
conv_out = tf.nn.relu(conv_bias)
###ENd code
.................................................................................................................................................
CELL 5:
sess.run(tf.compat.v1.global_variables_initializer())
filters = sess.run(W)
conv_output = sess.run(conv_out)
after_pooling = sess.run(conv_pool)
###sanity check
print(conv_out)
print(conv_pool)
print(conv_output[0,100:105,200:205, 7])
.................................................................................................................................................
CELL 6:
fig1 = plt.figure()
fig1.suptitle(title, fontsize=30)
rows, cols = 4, 8
for i in range(np.shape(data)[3]):
img = data[0, :, :, i]
ax1.imshow(img, interpolation='none')
ax1.axis('off')
def show_weights(W,title):
fig2 = plt.figure()
fig2.suptitle(title, fontsize=30)
rows, cols = 4, 8
for i in range(np.shape(W)[3]):
img = W[:, :, 0, i]
ax2.imshow(img, interpolation='none')
ax2.axis('off')
....................................................................................................................................................