You are on page 1of 12

CNN HAND’ S ON

1) CNN USING NUMPY

Pre-Requirements:

1. Open ide and select Install button under Run Option

2. Wait for the installation of packages

3. Run ------ Open Preview

Type the below code in CNN Jupyter Notebook

...................................................................................................................................................

Cell 1:

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

...................................................................................................................................................

Cell 2:

###Start code here

img = mpimg.imread("home.png")

data = img.reshape(1,img.shape[0], img.shape[1], img.shape[2])

###End code

print(type(img))

print("Image dimension ",img.shape)

print("Input data dimension ", data.shape)

....................................................................................................................................................
………………………………………………………………………………………………………

Cell 3:

plt.imshow(data[0,:,:,:])

plt.grid(False)

plt.axis("off")

...................................................................................................................................................

Cell 4:

def zero_pad(data, pad):

###Start code here

data_padded = np.pad(array = data, pad_width = ((0,0),(pad,pad), (pad,pad),(0,0)), mode =


'constant', constant_values = 0)

return data_padded

###End code

...................................................................................................................................................

Cell 5:

print("dimension before padding: ", data.shape)

img_pad = zero_pad(data, 10)

print("dimension after padding: ", img_pad.shape)

print(img_pad[0,8:12,8:12,1])

plt.imshow(img_pad[0,:,:,:], cmap = "gray")

plt.grid(False)

output1 = np.mean(img_pad)

...................................................................................................................................................
…………………………………………………………………………………………………

Cell 6:

def conv_single_step(data_slice, W, b):

###Start code

conv = np.multiply(data_slice, W)

Z = np.sum(conv) + b

###End code

return Z

....................................................................................................................................................

Cell 7:

def conv_forward(A_prev, W, b, hparams):

stride = hparams["stride"]

pad = hparams["pad"]

m, h_prev, w_prev, c_prev = A_prev.shape

f, f, c_prev, n_c = W.shape

n_h = int((h_prev - f + 2*pad)/stride) + 1

n_w = int((w_prev - f + 2*pad)/stride) + 1

Z = np.zeros((m, n_h, n_w, n_c))

A_prev_pad = zero_pad(A_prev, 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

Z[i,h,w,c] = conv_single_step(A_prev_pad[i, h_start:h_end,

w_start:w_end, :], W[:,:,:,c], b[:,:,:,c])

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}

output_ = conv_forward(input_, W, b, hparameters)

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

hparams ={"pad" : 0, "stride": 1}

b = np.zeros((1, 1, 1, 1))

Z = conv_forward(data, edge_detect, b, hparams)

plt.clf()

plt.imshow(Z[0,:,:,0], cmap='gray',vmin=0, vmax=1)

plt.grid(False)

print("dimension of image before convolution: ", data.shape)

print("dimension of image after convolution: ", Z.shape)

output2 = np.mean(Z[0,100:200,200:300,0])

....................................................................................................................................................
………………………………………………………………………………………………………

Cell 11:

def max_pool(input, hparam):

###start code

m, h_prev, w_prev, c_prev = input.shape

f = hparam["f"] ## f is the filter size to use for pooling

stride = hparam["stride"]

h_out = int(((h_prev - f)/stride) + 1)

w_out = int(((w_prev -f)/stride) + 1)

output = np.zeros((m, h_out, w_out, c_prev))

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)

assert output.shape == (m, h_out, w_out, c_prev)

###End code

return output

............................................................................................................................................................
………………………………………………………………………………………………………

Cell 12:

pool_params = {"stride" : 2, "f" : 2}

output_ = max_pool(input_, pool_params)

print(np.mean(output_))

............................................................................................................................................................

Cell 13:

###start code

hparams = {'stride':1, 'f':2}

Z_pool = max_pool(input_, hparams)

###End code

print("dimension before pooling :", Z.shape)

print("dimension after pooling :", Z_pool.shape)

plt.imshow(Z_pool[0,:,:,0], cmap = "gray")

with open("output.txt", "w+") as file:

file.write("output1 = %f" %output1)

file.write("\noutput2 = %f" %output2)

............................................................................................................................................................

OUTPUT: Run ------ Test


2) CNN USING TENSERFLOW

Pre-Requirements:

1. Open ide and select Install button under Run Option

2. Wait for the installation of packages (Minimum it will take 30-45 minutes)

3. Run ------ Open Preview

Type the below code in CNN Jupyter Notebook

………………………………………………………………………………………………………

CELL 1:

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

import numpy as np

import tensorflow as tf

...........................................................................................................................................................

CELL 2:

###Start code here

img = mpimg.imread("bird.png")

data = img.reshape(1,img.shape[0], img.shape[1], img.shape[2])

###End code

print(type(img))
print("Image dimension ",img.shape)

print(img.shape)

print("input data dimension ", data.shape)

.................................................................................................................................................

CELL 3:

plt.imshow(data[0,:,:,:])

...........................................................................................................................................

CELL 4:

graph = tf.Graph()

with graph.as_default():

tf.random.set_seed(1)

tinput_= tf.constant(data.astype(np.float32)) ##The input data is coverted into tensor of type


float32

###Start code here

W = tf.Variable(tf.random.normal([5,5,3,32]))

b = tf.Variable(tf.random.normal([32]))

conv = tf.nn.conv2d(tinput_,filters=W, strides=[1, 1, 1, 1], padding='SAME') + b

conv_bias = tf.nn.bias_add(conv, b)
conv_out = tf.nn.relu(conv_bias)

conv_pool = tf.nn.pool(input = conv_out , window_shape = [3,3], padding="VALID",


pooling_type="MAX")

###ENd code

.................................................................................................................................................

CELL 5:

with tf.compat.v1.Session(graph=graph) as sess:

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])

print("\n", after_pooling[0,100:105,200:205, 7])

with open("output.txt", "w+") as file:


file.write("mean1 = %f" %np.mean(conv_output))

file.write("\nmean2 = %f" %np.mean(after_pooling))

.................................................................................................................................................

CELL 6:

def show_conv_results(data, title):

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 = fig1.add_subplot(rows, cols, i + 1)

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 = fig2.add_subplot(rows, cols, i + 1)

ax2.imshow(img, interpolation='none')
ax2.axis('off')

show_weights(filters, title = "filters, "+"shape:" +str(filters.shape))

show_conv_results(conv_output, title = "after_convolution, "+ "shape:" +


str(conv_output.shape))

show_conv_results(after_pooling, title = "after_pooling, "+"shape:"+str(after_pooling.shape))

....................................................................................................................................................

You might also like