2019 IFN680 – Week 08 Prac – CNN
In this week prac, you will build and train a CNN for the classification of handwritten digits by completing the code provided in the file cnn_prac.py.
Keras is a high-level neural networks API, written in Python and recently integrated into TensorFlow but Keras is also capable of running on top of other libraries. In this exercise, you will use Keras to create a CNN classifier. The documentation for Keras in TensorFlow can be found at https://www.tensorflow.org/guide/keras
Keras allows for easy and fast prototyping compared to TensorFlow (which offers a lower level API with more control). The simplest way to create a CNN in Keras is to use the Sequential model. A sequential model is a stack of layers that can be constructed by appending layers one by one to a list. For example, the following code creates a NN with an input of dimension 100, a hidden layer of 32 neurons and an output of 1 neuron:
model = Sequential()
model.add(Dense(32, activation=’relu’, input_dim=100)) model.add(Dense(1, activation=’sigmoid’))
To finish the construction of the model, the optimization algorithm, the loss function and a performance metric should be specified as parameters of the compile method of the model. For example,
model.compile(optimizer=’rmsprop’, loss=’binary_crossentropy’, metrics=[‘accuracy’]) The training is performed by calling the fit method of the model. For example,
model.fit(data, labels, epochs=10, batch_size=32)
The predict method, like with the sklearn library, allows you to make predictions with a trained model
on new data.
Exercise 1 – retrieving and saving the dataset
The MNIST database of handwritten digits is a dataset of 60,000 28×28 grayscale images of the 10 digits, along with a test set of 10,000 images. You first need to download and save this dataset by completing the function ex1. The function numpy.save can save an array to an npy file. The function numpy.savez allows you to save several arrays in a single file. Complete the function ex1 to download and save the dataset to a file named ‘mnist_dataset.npz’.
Details on the functions can be found at https://docs.scipy.org/doc/numpy/reference/routines.io.html Exercise 2 – loading the saved dataset
Complete the function ex2 to extract from your file ‘mnist_dataset.npz’ the arrays x_train, y_train, x_test and y_test. Print the shape and dtype of these arrays.
Exercise 3 – checking the dataset
Complete the function ex3 to display the training images indexed from 25 to 35 and their classes. Use the function plt.imshow and plt.show with the color map ‘gray’ to display the images, i.e., cmap=’gray’.
Details on the functions can be found at
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.html
Exercise 4 – creating the CNN classifier
Read and analyse the VGG-like convnet example at https://keras.io/getting-started/sequential-model-guide/
Then, complete the function ex4 to build, train and evaluate a CNN on the mnist dataset using Keras. Your CNN should have the following architecture;
• Layer 0 should be Conv2D with 32 filters and 3×3 kernel size. Use ‘relu’ for the activation option.
• Layer 1 should be another Conv2D with 64 filters and 3×3 kernel size. Use again ‘relu’ for the activation option.
• Layer 2 should be a MaxPooling2D with pool_size 2×2.
• Layer 3 should be a Flatten layer.
• Layer 4 should be a Dense layer with 128 neurons and ‘relu’ as the activation function.
• Layer 5 should be a Dense layer with 10 neurons and ‘softmax’ as the activation function.
The model should be compile with the options
• the loss parameter should be ‘categorical_crossentropy’
• the optimizer parameter should be keras.optimizers.Adadelta()
• the metrics parameter should be the list [‘accuracy’]
Train the model by calling the fit method of the model. Finally, evaluate the model on the test set.
Exercise 5 (quiz question on 1D conv)
• If there are n inputs to a 1-D Convolutional Neural Network, and there are two filters, the filter size is 2, the stride is 2 and the padding is 1 (i.e., add 1 zero at the beginning and end of the input), then how many units are in the first hidden layer? Assume n is even.
Exercise 6 (quiz question on max pool)
Max-pooling with pool_size 2×2 and stride-2 of the following matrix produces a matrix of what size?
12
20
30
0
8
12
2
0
34
70
37
4
112
100
25
12