Problem 2: Fashion MNIST Classification
Link to Colab Notebook: colabl ink
Import Necessary Modules
from tensorflow . keras . datasets import fashion_mnist
import matplotlib . pyplot as plt
import numpy as np
from tensorflow . keras . utils import to_categ orical
from tensorflow . keras . layers import Input , Flatten , Dense , Conv2D ,
MaxPooling2D
from tensorflow . keras . models import Model
from tensorflow . keras . optimizers import RMSprop
Load and Investigate Data
( trainX , trainY ) , ( testX , testY ) = fashion_mnist . load_data ()
print ( f ’ trainX . shape : { trainX . shape } , trainY . shape : { trainY . shape } ,
testX . shape : { testX . shape } , testY . shape : { testY . shape } ’)
print ( f ’ trainX . dtype : { trainX . dtype } , trainY . dtype : { trainY . dtype } ,
testX . dtype : { testX . dtype } , testY . dtype : { testY . dtype } ’)
print ( f ’ trainX . Range : { trainX . max () } - { trainX . min () } , testX . Range :
{ testX . max () } - { testX . min () } ’)
Display Some Images
def display_img ( img_set , title_set ) :
n = len ( title_set )
for i in range ( n ) :
plt . subplot (3 , 3 , i + 1)
plt . imshow ( img_set [ i ] , cmap = ’ gray ’)
plt . title ( title_set [ i ])
plt . show ()
display_img ( trainX [:9] , trainY [:9])
1
Prepare Dataset
# Reshape the data to add a channel dimension for CNN
trainX = np . expand_dims ( trainX , axis = -1)
testX = np . expand_dims ( testX , axis = -1)
# Normalize the data to the range [0 , 1]
trainX = trainX / 255.0
testX = testX / 255.0
# Convert labels to one - hot encoding
trainY = to_cate gorical ( trainY , num_classes =10)
testY = to_ca tegoric al ( testY , num_classes =10)
# Investigate the updated shapes
print ( f ’ trainX . shape : { trainX . shape } , trainY . shape : { trainY . shape } ,
testX . shape : { testX . shape } , testY . shape : { testY . shape } ’)
Fully Connected Neural Network (FCNN)
numClasses = 10
inputs = Input ((28 , 28 , 1) , name = " InputLayer " )
x = Flatten () ( inputs )
x = Dense (64 , activation = ’ sigmoid ’) ( x )
x = Dense (128 , activation = ’ sigmoid ’) ( x )
x = Dense (128 , activation = ’ sigmoid ’) ( x )
x = Dense (64 , activation = ’ sigmoid ’) ( x )
outputs = Dense ( numClasses , activation = ’ softmax ’ , name = ’ OutputLayer
’) ( x )
model = Model ( inputs , outputs , name = ’ FCNN ’)
model . summary ()
model . compile ( loss = ’ c a t e g o r i c a l _ c r o s s e n t r o p y ’ , optimizer = RMSprop () ,
metrics =[ ’ accuracy ’ ])
model . fit ( trainX , trainY , epochs =10 , batch_size =128)
test_loss , fcnn_test_acc = model . evaluate ( testX , testY )
print ( f ’ Test Accuracy : { fcnn_test_acc } ’)
Convolutional Neural Network (CNN)
numClasses = 10
inputs = Input ((28 , 28 , 1) )
x = Conv2D (32 , (3 , 3) , activation = ’ relu ’ , padding = ’ same ’) ( inputs )
x = MaxPooling2D ((2 , 2) ) ( x )
x = Conv2D (64 , (3 , 3) , activation = ’ relu ’ , padding = ’ same ’) ( x )
2
x = MaxPooling2D ((2 , 2) ) ( x )
x = Conv2D (128 , (3 , 3) , activation = ’ relu ’ , padding = ’ same ’) ( x )
x = MaxPooling2D ((2 , 2) ) ( x )
x = Flatten () ( x )
x = Dense (64 , activation = ’ sigmoid ’) ( x )
x = Dense (128 , activation = ’ sigmoid ’) ( x )
x = Dense (128 , activation = ’ sigmoid ’) ( x )
x = Dense (64 , activation = ’ sigmoid ’) ( x )
outputs = Dense ( numClasses , activation = ’ softmax ’ , name = ’ OutputLayer
’) ( x )
model = Model ( inputs , outputs , name = ’ CNN ’)
model . summary ()
model . compile ( loss = ’ c a t e g o r i c a l _ c r o s s e n t r o p y ’ , optimizer = RMSprop () ,
metrics =[ ’ accuracy ’ ])
model . fit ( trainX , trainY , epochs =10 , batch_size =32 ,
v a l i d a t i o n _ s p li t =0.1)
test_loss , cnn_test_acc = model . evaluate ( testX , testY )
print ( f ’ Test Accuracy : { cnn_test_acc } ’)