0% found this document useful (0 votes)
4 views

LAB_Quiz2

This document outlines a practical lab for training a convolutional neural network (CNN) using the Fashion MNIST dataset, which consists of 70,000 grayscale images of clothing. It details the steps for data loading, preprocessing, model architecture design, training, evaluation, and visualization of predictions. The lab emphasizes understanding the dataset, model performance metrics, and techniques to prevent overfitting.

Uploaded by

Nathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

LAB_Quiz2

This document outlines a practical lab for training a convolutional neural network (CNN) using the Fashion MNIST dataset, which consists of 70,000 grayscale images of clothing. It details the steps for data loading, preprocessing, model architecture design, training, evaluation, and visualization of predictions. The lab emphasizes understanding the dataset, model performance metrics, and techniques to prevent overfitting.

Uploaded by

Nathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB 3: Deep Learning on Fashion MNIST

In this practical work, you will train a convolutional neural network model to classify images
of clothing, like sneakers and shirts. The database that we are going to use is the Fashion
MNIST dataset, which contains 70,000 grayscale images in 10 categories. The grayscale images
show individual articles of clothing at low resolution (28 by 28 pixels), as seen here:

Fashion MNIST is intended as a drop-in replacement for the classic MNIST dataset—often
used as the "Hello, World" of machine learning programs for computer vision. The MNIST
dataset contains images of handwritten digits (0, 1, 2, etc) in an identical format to the articles
of clothing we'll use here.

We will use 60,000 images to train the network and 10,000 images to evaluate how accurately
the network learned to classify images. You can access the Fashion MNIST directly from
TensorFlow.

1. Importing and loading the data:

fashion_mnist = tf.keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

2. Exploring the data

It is very important to explore the format of the dataset before we build and train the
model.
a. Simply inspect the shape of the train_images and test_images by executing:
train_images.shape and test_images.shape. Write down the shapes of both
datasets.

b. Then, inspect the length of the train_labels and test_labels by executing:


len(train_labels) and len(test_labels). Write down the length of both datasets.

c. Finally, just type train_labels in your console to view how class labels are stored.
You will get the following indicating that the label of classes is an integer between
0 and 9: array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)

d. Create the list class_names and store in it the names of the 10 classes.

The following table associates each integer label to its corresponding


class:

3. Build the architecture of the CNN such as it composed of 2 convolution layers and
2 fully-connected layers. We are going to use 32 filters of size 3x3 (feature
detectors) at the first convolution layer and 64 filters of size 3x3 at the second
convolution layer.

a. How many nodes de we have at the output layer?

b. What activation function should we use at this layer and why?

c. What optimization technique and what loss function are best suited for this
problem?

d. Schematize the architecture of the CNN and indicate the size of images as
well as the number of channels at the output of each convolution layer.
Specify the number of features at each fully connected layer (c.f. schema
provided in TensorFlow lab).

4. We need to perform some data preprocessing for several reasons:


a. First we need to reshape the train_images and the test_images as
follows because the CNN expects a 4-dim tensor as an input:
train_images = train_images.reshape(([-1, 28, 28, 1]))
test_images = test_images.reshape(([-1, 28, 28, 1]))
Can you tell what each dimension represents?

b. Then, we need to rescale images because the pixel values in the


train_images and test_images range between 0 and 255:
train_images=train_images.astype("float32")/255.0
test_images=test_images.astype("float32")/255.0
Can you say why rescaling is essential in this case?

c. Finally, we need to one-hot encode the train_labels and test_labels


arrays:
train_labels=keras.utils.to_categorical(train_labels,10)
test_labels=keras.utils.to_categorical(test_labels,10)
Can you describe what is one-hot encoding and why do we need to
do it in our case?

5. Train the model and take note of the accuracy


6. Test the model and take note of the accuracy. Is your model overfitting? If yes, what
modifications do you suggest to solve the problem?
7. Print out a classification report using the classification_report provided by sklearn:
from sklearn.metrics import classification_report
print("[INFO] evaluating network...")
print(classification_report(test_labels.argmax(axis=1), preds.argmax(axis=1),
target_names=class_names))

What do labels.argmax(axis=1) and pred.argmax(axis=1) return and why do we use


it?
8. Find the confusion matrix
9. Display some prediction results using the following helper functions:

def plot_image(i, predictions_array, true_label, img):


predictions_array, true_label, img = predictions_array[i],
true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])

plt.imshow(img, cmap=plt.cm.binary)

predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'

plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],


100*np.max(predictions_array),
class_names[true_label]),
color=color)

def plot_value_array(i, predictions_array, true_label):


predictions_array, true_label = predictions_array[i], true_label[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)

thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')

Then look at the predictions provided by the CNN for 0 th and 12th images of the test
dataset:

i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, np.argmax(test_labels,axis=1), test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, np.argmax(test_labels,axis=1))
plt.show()

i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, np.argmax(test_labels,axis=1), test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, np.argmax(test_labels,axis=1))
plt.show()

Are the two images correctly classified?

You might also like