0% found this document useful (0 votes)
130 views5 pages

Assignment 2.4.1 Multiclass Classification

The document provides code for a multiclass classification model using the Sign Language MNIST dataset. It reads in the training and test CSV files, preprocesses the images and labels, defines a convolutional neural network model with data augmentation, trains the model for 2 epochs, evaluates the model on the test set, and plots the accuracy and loss curves for both training and validation.

Uploaded by

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

Assignment 2.4.1 Multiclass Classification

The document provides code for a multiclass classification model using the Sign Language MNIST dataset. It reads in the training and test CSV files, preprocesses the images and labels, defines a convolutional neural network model with data augmentation, trains the model for 2 epochs, evaluates the model on the test set, and plots the accuracy and loss curves for both training and validation.

Uploaded by

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

Assignment 2.4.

1 Multiclass Classification

# ATTENTION: Please do not alte


# ATTENTION: Please do not add
# ATTENTION: Please use the pro
import csv
import numpy as np

# ATTENTION: Please do not alter any of the provided code in the exercise. Only add your own code where
indicated
# ATTENTION: Please do not add or remove any cells in the exercise. The grader will check specific cells
based on the cell position.
# ATTENTION: Please use the provided epoch values when training.

import csv
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from os import getcwd
In [12]:

def get_data(filename):
# You will need to write code that will read the file passed
# into this function. The first line contains the column headers
# so you should ignore it
# Each successive line contians 785 comma separated values between 0 and 255
# The first value is the label
# The rest are the pixel values for that picture
# The function will return 2 np.array types. One with all the labels
# One with all the images
#
# Tips:
# If you read a full line (as 'row') then row[0] has the label
# and row[1:785] has the 784 pixel values
# Take a look at np.array_split to turn the 784 pixels into 28x28
# You are reading in strings, but need the values to be floats
# Check out np.array().astype for a conversion
with open(filename) as training_file:
# Your code starts here
first_line = True
temp_labels = []
temp_images = []
csv_reader = csv.reader(training_file, delimiter = ',')
for row in csv_reader:
if first_line:
first_line = False
else:
temp_labels.append(row[0])
temp_images.append(np.array_split(row[1:],28))

labels = np.array(temp_labels).astype('float')
images = np.array(temp_images).astype('float')

# Your code ends here


return images, labels

path_sign_mnist_train = f"{getcwd()}/../tmp2/sign_mnist_train.csv"
path_sign_mnist_test = f"{getcwd()}/../tmp2/sign_mnist_test.csv"
training_images, training_labels = get_data(path_sign_mnist_train)
testing_images, testing_labels = get_data(path_sign_mnist_test)

# Keep these
print(training_images.shape)
print(training_labels.shape)
print(testing_images.shape)
print(testing_labels.shape)

# Their output should be:


# (27455, 28, 28)
# (27455,)
# (7172, 28, 28)
# (7172,)
(27455, 28, 28)
(27455,)
(7172, 28, 28)
(7172,)
In [13]:

# In this section you will have to add another dimension to the data
# So, for example, if your array is (10000, 28, 28)
# You will need to make it (10000, 28, 28, 1)
# Hint: np.expand_dims

training_images = np.expand_dims(training_images,axis=3) # axis refer to location , if 0 means beginning,


Your Code Here
testing_images = np.expand_dims(testing_images,axis=3) # Your Code Here

# Create an ImageDataGenerator and do Image Augmentation


train_datagen = ImageDataGenerator(
# Your Code Here
rescale=1.0/255.0,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)

validation_datagen = ImageDataGenerator(rescale = 1./255)


train_generator = train_datagen.flow(training_images,
training_labels,
batch_size = 20)

validation_generator = validation_datagen.flow(testing_images,
testing_labels,
batch_size = 20)

# Your Code Here)

# Keep These
print(training_images.shape)
print(testing_images.shape)

# Their output should be:


# (27455, 28, 28, 1)
# (7172, 28, 28, 1)
(27455, 28, 28, 1)
(7172, 28, 28, 1)
In [17]:

# Define the model


# Use no more than 2 Conv2D and 2 MaxPooling2D
model = tf.keras.models.Sequential([
# Your Code Here
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(26, activation='softmax')
])

# Compile Model.
model.compile(loss = 'sparse_categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])# Your Code
Here)

# Train the Model


history = model.fit_generator(train_generator,
validation_data = validation_generator,
steps_per_epoch = len(training_images)/20,
epochs = 2,
validation_steps = len(testing_images)/20,
verbose = 2)
# Your Code Here (set 'epochs' = 2))

model.evaluate(testing_images, testing_labels, verbose=0)


Epoch 1/2
1373/1372 - 93s - loss: 2.5203 - accuracy: 0.2268 - val_loss: 2.0626 -
val_accuracy: 0.3334
Epoch 2/2
1373/1372 - 90s - loss: 1.6340 - accuracy: 0.4771 - val_loss: 0.8598 -
val_accuracy: 0.7030
Out[17]:
[256.28286111707206, 0.45287228]
In [21]:

# Plot the chart for accuracy and loss on both training and validation
%matplotlib inline
import matplotlib.pyplot as plt
acc = history.history['accuracy']# Your Code Here
val_acc = history.history['val_accuracy'] # Your Code Here
loss = history.history['loss'] # Your Code Here
val_loss = history.history['val_loss']# Your Code Here

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', label='Training accuracy')


plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()

plt.plot(epochs, loss, 'r', label='Training Loss')


plt.plot(epochs, val_loss, 'b', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()
Submission Instructions
In [ ]:

# Now click the 'Submit Assignment' button above.

When you're done or would like to take a break,


please run the two cells below to save your work and
close the Notebook. This will free up resources for
your fellow learners.
In [18]:

%%javascript
<!-- Save the notebook -->
IPython.notebook.save_checkpoint();
In [ ]:

%%javascript
IPython.notebook.session.delete();
window.onbeforeunload = null
setTimeout(function() { window.close(); }, 1000);

You might also like