CNN Numpy 1st Handson
CNN Numpy 1st Handson
https://docs-secure-cdn.fresco.me/system/attachments/files/022/435/617/original/be27314b657521a589c09cf5feed350c47d3d1ef/CNN_practice … 1/5
4/19/2021 CNN_numpy_1st_handson
In [ ]:
print(type(img))
print("Image dimension ",img.shape)
print("Input data dimension ", data.shape)
<class 'numpy.ndarray'>
Image dimension (252, 362, 3)
Input data dimension (1, 252, 362, 3)
Run the below cell to view the image from the data
plt.imshow(data[0,:,:,:])
plt.grid(False)
plt.axis("off")
(-0.5, 361.5, 251.5, -0.5)
zero padding
Define method named zero_pad that performs specified number of zero padding on the inpu
t data.
parameters:
data: the data on which padding is performed
pad: the amount of padding around the data
returns:
data_padded: the nd-array after padding
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
Run the below cell to add zero zero padding using the method define above.
Expected output:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
https://docs-secure-cdn.fresco.me/system/attachments/files/022/435/617/original/be27314b657521a589c09cf5feed350c47d3d1ef/CNN_practice … 2/5
4/19/2021 CNN_numpy_1st_handson
[0. 0. 1. 1.]
[0. 0. 1. 1.]]
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)
dimension before padding: (1, 252, 362, 3)
dimension after padding: (1, 272, 382, 3)
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 1. 1.]
[0. 0. 1. 1.]]
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 ##(convolved output)
The below cell defines the test data for input as well as filter.
Run the cell to perfom the convolution operation using the method defined above.
Expected output: 0.145
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}
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])
##below are the filters for vetical as well as horizontal edge detection, try these fil
ters once you have completed this handson.
##vertical_filter = np.array([[-1,2,-1],[-1,2,-1],[-1,2,-1]]).reshape(3,3,1,1)
##horizontal_filter = np.array([[-1,-1,-1],[2,2,2],[-1,-1,-1]]).reshape((3,3,1,1))
dimension of image before convolution: (1, 252, 362, 3)
dimension of image after convolution: (1, 250, 360, 1)
Max pooling
Define method max_pool to perform max pooling on the input data.
Parameters:
returns:
###End code
return output
Run the below cell to test the method you define above.
Expected output: 1.075
pool_params = {"stride" : 2, "f" : 2}
output_ = max_pool(input_, pool_params)
print(np.mean(output_))
(10, 2, 2, 3)
1.0753012177728354
Define pooling parameters "stride" and filter size "f" as a dictionary named hparams wi
th stride = 1 and f = 2
call the method max_pool with parameters Z (the convolved output) and hparams
###start code
hparams ={'stride':1, 'f':2}
Z_pool = max_pool(input_, hparams)
###End code
https://docs-secure-cdn.fresco.me/system/attachments/files/022/435/617/original/be27314b657521a589c09cf5feed350c47d3d1ef/CNN_practice … 5/5