Image Classification On Fashion-MNIST dataset Using PyTorch

Afaf Athar
Analytics Vidhya
Published in
4 min readFeb 6, 2020

PyTorch is a famous open-source machine learning library for Python developed by Facebook’s AI research group. PyTorch believes in a dynamic graph, unlike TensorFlow that creates a static graph. PyTorch supports both CPU and GPU computations.

In this post, we will see the “HELLO WORLD” of the Machine Learning model with a very famous dataset available for image classification i.e. Fashion_MNIST dataset, in which we will for a given image if it’s a clothing item(differentiating between all sorts of top-wear bottom wear for both the sexes), footwear or a bag. Thus, determining its probabilities to which class it belongs.

About Dataset:

For this model, I am going to use the Fashion MNIST dataset that consists of Zalando’s article images which is a set of 28x28 greyscale images of clothes, a drop-in replacement for the MNIST dataset. Here, you’ll learn to build your neural network.

The image below shows the dataset about which you’ll learn in this post.

Fashion MNIST Dataset

Fashion MNIST dataset

Fashion MNIST dataset consists of 70,000 grayscale images and 10 classes. You can see here: https://github.com/zalandoresearch/fashion-mnist#labels

To start with, follow the steps below:

  1. Load the dataset through torchvision.
import torch
from torchvision import datasets, transforms
import helper
# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
#transforms.Lambda(lambda x: x.repeat(3,1,1)),
transforms.Normalize((0.5, ), (0.5,))])
# Download and load the training data
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
# Download and load the test data
testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True)

to load the image,

image, label = next(iter(trainloader))
helper.imshow(image[0,:]);

here we can see an image from the dataset.

Image of a single clothing item from the dataset

2. Building the network

As with MNIST, each image is 28x28 which is a total of 784 pixels, and there are 10 classes. You should include at least one hidden layer. We suggest you use ReLU activations for the layers and to return the logits or log-softmax from the forward pass. It’s up to you how many layers you add and the size of those layers.

We use the activation function (Relu, sigmoid, etc) to limit the output signal and restrict the output value to a certain finite value

from torch import nn, optim
import torch.nn.functional as F
#Nwtwork Architecture:class Classifier(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784,256)
self.fc2 = nn.Linear(256,128)
self.fc3 = nn.Linear(128,64)
self.fc4 = nn.Linear(64,10)

def forward(self,x):
x = x.view(x.shape[0],-1) ## flattening the Tensor

x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.log_softmax(self.fc4(x), dim = 1)

return x

3. Train the model

In this step of model training, we will define the criterion (like nn.CrossEntropy or nn.NLLLoss) and the optimizer (optim.Adam just like Gradient Descent to minimize the loss). Next, we will make a forward pass through the network to get the logits and calculate the loss. Further, perform a backward pass through the network with loss.backward() to calculate the gradients and with optimizer update the weights.

#Defining the criterion and optimizer
model = Classifier()
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.parameters(), lr = 0.005)
#Network Training
epochs = 5
for e in range(epochs):
running_loss=0
for images, labels in trainloader:
logps = model(images)
loss = criterion(logps, labels)

optimizer.zero_grad()
loss.backward()
optimizer.step()

running_loss += loss.item()

else:

print(f"Training loss:{running_loss}")

4. Plotting the images and probabilities

At last, we will calculate the probability using the softmax and plot.

It will give us the probability to which the class the image belongs

%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import helper #user define for library classification and labeldataiter = iter(testloader)
images,lables = dataiter.next()
img = images[2]
ps = torch.exp(model(img)) #Class probabilities (softmax)#Image and probabilities
helper.view_classify(img,ps,version = 'Fashion')

Here you have learned one of your Machine Learning models.!!

Image of the classified dress.

I know there are a lot of technical things thrown around in this post that might be daunting or out of the scope of anyone who has just forayed into machine learning. I’ll be posting successive articles about many of them in the future explaining them in detail.

To know more about Activation Functions, Optimization Techniques, and Loss Function click here

Hope this helps :)

Follow if you like my posts.

For more help, check my Github:- https://github.com/Afaf-Athar/FashionMNIST/blob/master/Fashion-%20MNIST.ipynb

--

--

Afaf Athar
Analytics Vidhya

I Do Data. I write what I wish I could have read when I was younger