Fashion Forward is a new AI-based e-commerce clothing retailer. They want to use image classification to automatically categorize new product listings, making it easier for customers to find what they're looking for. It will also assist in inventory management by quickly sorting items.
As a data scientist tasked with implementing a garment classifier, your primary objective is to develop a machine learning model capable of accurately categorizing images of clothing items into distinct garment types such as shirts, trousers, shoes, etc.
!pip install torchmetrics
!pip install torchvision
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchmetrics import Accuracy, Precision, Recall
# Load datasets
from torchvision import datasets
import torchvision.transforms as transforms
train_data = datasets.FashionMNIST(root='./data', train=True, download=True, transform=transforms.ToTensor())
test_data = datasets.FashionMNIST(root='./data', train=False, download=True, transform=transforms.ToTensor())
We need to know important characteristics about the data including the number of classes, and the size of the images.
len(train_data.classes)
image_size = train_data[0][0].shape[1]
print(image_size)
We start by creating the Convolutional Neural Network's Architecture including the feature extractor layer and the classifier. The model takes in one input and output's 16. The image size is reduced as it passed through the model by 2. So the classifier takes in the number of output channels and the image now in size 14 by 14, outputs the number of classes. We then define the forward layer and pass x through the feature extractor, and the classifier.
class ImageClassifier(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.feature_extractor = nn.Sequential(
nn.Conv2d(1, 16, kernel_size = 3, padding = 1, stride = 1),
nn.ReLU(),
nn.MaxPool2d(kernel_size = 2, stride = 2),
nn.Flatten()
)
self.classifier = nn.Linear(16*14*14, num_classes)
def forward(self, x):
x = self.feature_extractor(x)
x = self.classifier(x)
return x
We would like to process the images in batches, and we directly use the DataLoader function to create batches of 10, shuffling for the Train set, but not the test set.
# DataLoader for batching
train_loader = torch.utils.data.DataLoader(train_data, batch_size=10, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=10, shuffle=False)
The function to train the model is then created. The function takes as params, the optimizer, model and the numnber of epochs. Criteria is set for CrossEntropyLoss, as this is a multiclass classifier model. The running loss is calculated for each batch and the entire train average loss is computed for each epoch
def train_model(optimizer,model,num_epochs):
num_processed = 0
criterion = nn.CrossEntropyLoss()
for epoch in range(num_epochs):
running_loss = 0
num_processed = 0
for images, labels in train_loader:
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
num_processed += len(labels)
print(f'epoch{epoch}, loss:{running_loss/num_processed}')
train_loss = running_loss/ len(train_loader)
The train function is used to train the model on the dataset used in this project. The optimizer used is Adam, and it runs for 3 epochs.