Key Word(s): Convolutional Neural Network, Neural Networks, CNNs, MPL
Title :¶
Pooling Mechanics
Description :¶
The aim of this exercise is to understand the tensorflow.keras implementation of:
- Max Pooling
- Average Pooling
Instructions :¶
First, implement Max Pooling by building a model with a single MaxPooling2D
layer. Print
the output of this layer by using model.predict()
to show the output.
Next, implement Average Pooling by building a model with a single AvgPooling2D
layer.
Print the output of this layer by using model.predict()
to show the output.
Hints:¶
tf.keras.layers.MaxPooling2D()Max pooling operation for 2D spatial data.
tf.keras.layers.AveragePooling2D()Average pooling operation for spatial data.
np.squeeze()Remove single-dimensional entries from the shape of an array.
np.expand_dims()Add single-dimensional entries from the shape of an array.
Example: np.expand_dims (img, axis=(0,3))
# Import necessary libraries
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import MaxPool2D,AveragePooling2D,Input
from helper import plot_pool
# Load the 7x7 mnist image
img = np.load('3.npy')
plt.imshow(img,cmap = 'bone', alpha=0.5);
plt.axis('off');
plt.title('MNIST image of 3',fontsize=20);
### edTest(test_chow1) ###
# Submit an answer choice as a string below (eg. if you choose option C, put 'C')
answer1 = '___'
Max Pooling¶
# Specify the variables for pooling
pool_size = ___
strides = ___
# Padding parameter can be 'valid', 'same', etc.
padding = '___'
# Build the model to perform maxpooling operation
model_1 = Sequential(name = 'MaxPool')
model_1.add(Input(shape = np.expand_dims(img,axis=2).shape))
model_1.add(MaxPool2D(pool_size = pool_size,strides=strides, padding=padding))
# Take a look at the summary to see the output shape
model_1.summary()
# Output the image using the model above
# Remember to use np.expand_dims to change input image dimensions
# to 4-d tensor because model_1.predict will not work on 2-d tensor
pooled_img = model_1.predict(___)
# Use the helper code to visualize the pooling operation
# np.squeeze() is used to bring the image to 2-dimension
# to use matplotlib to plot it
pooled_img = pooled_img.squeeze()
# plot_pool is a function that will return 3 plots to help visualize
# the pooling operation
plot_pool(img,pooled_img)
### edTest(test_chow2) ###
# Submit an answer choice as a string below
# (eg. if you choose option C, put 'C')
answer2 = '___'
Average Pooling¶
# Specify the variables for pooling
pool_size = ___
strides = ___
# Padding parameter can be 'valid', 'same', etc.
padding = '___'
# Build the model to perform average pooling operation
model_2 = Sequential(name = 'AveragePool')
model_2.add(Input(shape = np.expand_dims(img,axis=2).shape))
model_2.add(AveragePooling2D(pool_size = pool_size,strides=strides, padding=padding))
model_2.summary()
# Output the image using the model above
# Remember to use np.expand_dims to change input image dimensions
# to 4-d tensor because model_1.predict will not work on 2-d tensor
pooled_img = model_2.predict(___)
# Use the helper code to visualize the pooling operation
pooled_img = pooled_img.squeeze()
plot_pool(img,pooled_img)
### edTest(test_chow3) ###
# Submit an answer choice as a string below
# (eg. if you choose option A, put 'a')
answer3 = '___'