← View series: ibm ai engineering
~/blog
FashionMNISTProject v1
IBM 10TB Storage
Fashion-MNIST Project
Table of Contents
In this project, you will classify Fashion-MNIST dataset using convolutional neural networks.
Estimated Time Needed: 30 min
Preparation
Download the datasets you needed for this lab.
The following are the PyTorch modules you are going to need
# !pip install torch
# !pip install torchvision
# !pip install matplotlib# PyTorch Modules you need for this lab
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision.datasets as datasets
torch.manual_seed(0)Import Non-PyTorch Modules
# Other non-PyTorch Modules
from matplotlib.pyplot import imshow
import matplotlib.pylab as plt
from PIL import Imagedef show_data(data_sample):
plt.imshow(data_sample[0].numpy().reshape(IMAGE_SIZE, IMAGE_SIZE), cmap='gray')
plt.title('y = '+ str(data_sample[1]))Questions 1: Create a Dataset Class
In this section, you will load a Dataset object, but first you must transform the dataset. Use the Compose function to perform the following transforms:.
- Use the transforms object to
Resizeto resize the image. - Use the transforms object to
ToTensorto convert the image to a tensor.
You will then take a screen shot of your validation data.
Use the Compose function to compose the transforms
#Hint:
IMAGE_SIZE = 16
transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
transforms.ToTensor()#
composed = transforms.Compose([transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)), transforms.ToTensor()])Create two dataset objects for the Fashion MNIST dataset. One for training data called dataset_train and one for validation data dataset_val. You will be asked to take a screenshot of several samples.
Hint:
dsets.FashionMNIST(root= '.fashion/data', train=???, transform=composed, download=True)
# Enter your code here
dataset_train = datasets.FashionMNIST(root = './data', train = True, download = True, transform = composed)
dataset_val = datasets.FashionMNIST(root = './data', train = False, download = True, transform = composed)for n,data_sample in enumerate(dataset_val):
show_data(data_sample)
plt.show()
if n==2:
breakQuestions 2
Create a Convolutional Neural Network class using ONE of the following constructors. Train the network using the provided code then provide a screenshot of your training cost and accuracy with your validation data.Constructor using Batch Norm
class CNN_batch(nn.Module):
# Constructor
def __init__(self, out_1=16, out_2=32,number_of_classes=10):
super(CNN_batch, self).__init__()
# Layer stack 1
self.cnn1 = nn.Conv2d(in_channels=1, out_channels=out_1, kernel_size=5, padding=2)
self.conv1_bn = nn.BatchNorm2d(out_1)
self.maxpool1=nn.MaxPool2d(kernel_size=2)
# Layer stack 2
self.cnn2 = nn.Conv2d(in_channels=out_1, out_channels=out_2, kernel_size=5, stride=1, padding=2)
self.conv2_bn = nn.BatchNorm2d(out_2)
self.maxpool2=nn.MaxPool2d(kernel_size=2)
# Fully connected - Final layer
self.fc1 = nn.Linear(out_2 * 4 * 4, number_of_classes)
self.bn_fc1 = nn.BatchNorm1d(10)
# Prediction
def forward(self, x):
x = self.cnn1(x)
x=self.conv1_bn(x)
x = torch.relu(x)
x = self.maxpool1(x)
x = self.cnn2(x)
x=self.conv2_bn(x)
x = torch.relu(x)
x = self.maxpool2(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x=self.bn_fc1(x)
return xConstructor for regular Convolutional Neural Network
class CNN(nn.Module):
# Constructor
def __init__(self, out_1=16, out_2=32,number_of_classes=10):
super(CNN, self).__init__()
# Stack 1
self.cnn1 = nn.Conv2d(in_channels=1, out_channels=out_1, kernel_size=5, padding=2)
self.maxpool1=nn.MaxPool2d(kernel_size=2)
# Stack 2
self.cnn2 = nn.Conv2d(in_channels=out_1, out_channels=out_2, kernel_size=5, stride=1, padding=2)
self.maxpool2=nn.MaxPool2d(kernel_size=2)
# Fully connected - Final layer
self.fc1 = nn.Linear(out_2 * 4 * 4, number_of_classes)
# Prediction
def forward(self, x):
x = self.cnn1(x)
x = torch.relu(x)
x = self.maxpool1(x)
x = self.cnn2(x)
x = torch.relu(x)
x = self.maxpool2(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
return xtrain loader and validation loader
train_loader = torch.utils.data.DataLoader(dataset=dataset_train, batch_size=100 )
test_loader = torch.utils.data.DataLoader(dataset=dataset_val, batch_size=100 )Convolutional Neural Network object
#model = CNN(out_1=16, out_2=32,number_of_classes=10)
model =CNN_batch(out_1=16, out_2=32,number_of_classes=10)Create the objects for the criterion and the optimizer named criterion and optimizer. Make the optimizer use SGD with a learning rate of 0.1 and the optimizer use Cross Entropy Loss
# Enter your code here
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)Code used to train the model
import time
start_time = time.time()
cost_list=[]
accuracy_list=[]
N_test=len(dataset_val)
n_epochs=5
for epoch in range(n_epochs):
cost=0
model.train()
for x, y in train_loader:
optimizer.zero_grad()
z = model(x)
loss = criterion(z, y)
loss.backward()
optimizer.step()
cost+=loss.item()
correct=0
#perform a prediction on the validation data
model.eval()
for x_test, y_test in test_loader:
z = model(x_test)
_, yhat = torch.max(z.data, 1)
correct += (yhat == y_test).sum().item()
accuracy = correct / N_test
accuracy_list.append(accuracy)
cost_list.append(cost)You will use the following to plot the Cost and accuracy for each epoch for the training and testing data, respectively.
fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.plot(cost_list, color=color)
ax1.set_xlabel('epoch', color=color)
ax1.set_ylabel('Cost', color=color)
ax1.tick_params(axis='y', color=color)
ax2 = ax1.twinx()
color = 'tab:blue'
ax2.set_ylabel('accuracy', color=color)
ax2.set_xlabel('epoch', color=color)
ax2.plot( accuracy_list, color=color)
ax2.tick_params(axis='y', color=color)
fig.tight_layout()dataset: https://github.com/zalandoresearch/fashion-mnist
About the Authors:
Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.
Other contributors: Michelle Carey, Mavis Zhou