In This Hands-On You Will Be Performing CNN Operations Using Tensorflow Package
In This Hands-On You Will Be Performing CNN Operations Using Tensorflow Package
In [2]:
Read the image file 'bird.png'(in current directory) using mpimg.imread("file_path") function provided
by matplotlib.image module. This function reads the image and returns the pixel intensities in numpy
format. Assign this result to variable img.
The dimension of img will now be nH x nw x nc
reshape img to dimension m x nH x nw x nc and assign it to variable data. The dimension m will be
one since we are dealing with one image data. (use numpy's reshape())
In [3]:
print(type(img))
print("Image dimension ",img.shape)
print(img.shape)
print("input data dimension ", data.shape)
<class 'numpy.ndarray'>
Image dimension (194, 259, 3)
(194, 259, 3)
input data dimension (1, 194, 259, 3)
https://docs-secure-cdn.fresco.me/system/attachments/files/022/435/618/original/0376df74c3a23d463c48fc9584f1debcf8878f91/CNN_tensorflow … 1/6
4/19/2021 CNN_tensorflow
In [4]:
plt.imshow(data[0,:,:,:])
Out[4]:
<matplotlib.image.AxesImage at 0x7fe7f3805e48>
https://docs-secure-cdn.fresco.me/system/attachments/files/022/435/618/original/0376df74c3a23d463c48fc9584f1debcf8878f91/CNN_tensorflow … 2/6
4/19/2021 CNN_tensorflow
In [12]:
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 te
nsor of type float32
###Start code here
W = tf.Variable(tf.random.normal([5,5,3,32]))
b = tf.Variable(tf.random.normal([32]))
Run the below cell to run the tensorflow graph defined in the above steps
Expected output
Tensor("Relu:0", shape=(1, 194, 259, 32), dtype=float32)
https://docs-secure-cdn.fresco.me/system/attachments/files/022/435/618/original/0376df74c3a23d463c48fc9584f1debcf8878f91/CNN_tensorflow … 3/6
4/19/2021 CNN_tensorflow
In [13]:
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])
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
Run the below cell to visualize the actual filters and plot the convolution output.
https://docs-secure-cdn.fresco.me/system/attachments/files/022/435/618/original/0376df74c3a23d463c48fc9584f1debcf8878f91/CNN_tensorflow … 4/6
4/19/2021 CNN_tensorflow
In [15]:
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')
https://docs-secure-cdn.fresco.me/system/attachments/files/022/435/618/original/0376df74c3a23d463c48fc9584f1debcf8878f91/CNN_tensorflow … 5/6
4/19/2021 CNN_tensorflow
In [ ]:
https://docs-secure-cdn.fresco.me/system/attachments/files/022/435/618/original/0376df74c3a23d463c48fc9584f1debcf8878f91/CNN_tensorflow … 6/6