Training an Image Classification Model in PyTorch
Training an image classification model is a great way to get started with model training using hub datasets.

How to Train an Image Classification Model in PyTorch using Activeloop Hub

This tutorial is also available as a Colab Notebook

The primary objective for Hub is to enable users to manage their data more easily so they can train better ML models. This tutorial shows you how to train a simple image classification model while streaming data from a Hub dataset stored in the cloud.

Data Preprocessing

The first step is to select a dataset for training. This tutorial uses the Fashion MNIST dataset that has already been converted into hub format. It is a simple image classification dataset that categorizes images by clothing type (trouser, shirt, etc.)
1
import hub
2
from PIL import Image
3
import numpy as np
4
import os, time
5
import torch
6
from torchvision import datasets, transforms, models
7
8
# Connect to the training and testing datasets
9
ds_train = hub.load('hub://activeloop/fashion-mnist-train')
10
ds_test = hub.load('hub://activeloop/fashion-mnist-test')
Copied!
The next step is to define a transformation function that will process the data and convert it into a format that can be passed into a deep learning model. In this particular example, torchvision.transforms is used as a part of the transformation pipeline that performs operations such as normalization and image augmentation (rotation).
1
tform = transforms.Compose([
2
transforms.ToPILImage(), # Must convert to PIL image for subsequent operations to run
3
transforms.RandomRotation(20), # Image augmentation
4
transforms.ToTensor(), # Must convert to pytorch tensor for subsequent operations to run
5
transforms.Normalize([0.5], [0.5]),
6
])
Copied!
You can now create a pytorch dataloader that connects the Hub dataset to the PyTorch model using the provided method ds.pytorch(). This method automatically applies the transformation function, takes care of random shuffling (if desired), and converts hub data to PyTorch tensors. The num_workers parameter can be used to parallelize data preprocessing, which is critical for ensuring that preprocessing does not bottleneck the overall training workflow.
The transform input is a dictionary where the key is the tensor name and the value is the transformation function that should be applied to that tensor. If a specific tensor's data does not need to be returned, it should be omitted from the keys. If a tensor's data does not need to be modified during preprocessing, the transformation function is set as None.
1
batch_size = 32
2
3
train_loader = ds_train.pytorch(num_workers = 0, shuffle = True, transform = {'images': tform, 'labels': None}, batch_size = batch_size)
4
test_loader = ds_test.pytorch(num_workers = 0, transform = {'images': tform, 'labels': None}, batch_size = batch_size)
Copied!

Model Definition

This tutorial uses a pre-trained ResNet18 neural network from the torchvision.models module, converted to a single-channel network for grayscale images.
Training is run on a GPU if possible. Otherwise, run on a CPU.
1
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
2
print(device)
Copied!
1
# Use a pre-trained ResNet18
2
model = models.resnet18(pretrained=True)
3
4
# Convert model to grayscale
5
model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
6
7
# Update the fully connected layer based on the number of classes in the dataset
8
model.fc = torch.nn.Linear(model.fc.in_features, len(ds_train.labels.info.class_names))
9
10
model.to(device)
11
12
# Specity the loss function and optimizer
13
criterion = torch.nn.CrossEntropyLoss()
14
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.1)
Copied!

Training the Model

Helper functions for training and testing the model are defined. Note that the output from Hub's PyTorch dataloader is fed into the model just like data from ordinary PyTorch dataloaders.
1
def train_one_epoch(model, optimizer, data_loader, device):
2
3
model.train()
4
5
# Zero the performance stats for each epoch
6
running_loss = 0.0
7
start_time = time.time()
8
total = 0
9
correct = 0
10
11
for i, data in enumerate(data_loader):
12
# get the inputs; data is a list of [inputs, labels]
13
inputs = data['images']
14
labels = torch.squeeze(data['labels'])
15
16
inputs = inputs.to(device)
17
labels = labels.to(device)
18
19
# zero the parameter gradients
20
optimizer.zero_grad()
21
22
# forward + backward + optimize
23
outputs = model(inputs.float())
24
loss = criterion(outputs, labels)
25
loss.backward()
26
optimizer.step()
27
28
_, predicted = torch.max(outputs.data, 1)
29
total += labels.size(0)
30
correct += (predicted == labels).sum().item()
31
accuracy = 100 * correct / total
32
33
# Print performance statistics
34
running_loss += loss.item()
35
if i % 10 == 0: # print every 10 batches
36
batch_time = time.time()
37
speed = (i+1)/(batch_time-start_time)
38
print('[%5d] loss: %.3f, speed: %.2f, accuracy: %.2f %%' %
39
(i, running_loss, speed, accuracy))
40
41
running_loss = 0.0
42
43
44
def test_model(model, data_loader):
45
46
model.eval()
47
48
start_time = time.time()
49
total = 0
50
correct = 0
51
with torch.no_grad():
52
for i, data in enumerate(data_loader):
53
# get the inputs; data is a list of [inputs, labels]
54
inputs = data['images']
55
labels = torch.squeeze(data['labels'])
56
57
inputs = inputs.to(device)
58
labels = labels.to(device)
59
60
# zero the parameter gradients
61
optimizer.zero_grad()
62
63
# forward + backward + optimize
64
outputs = model(inputs.float())
65
66
_, predicted = torch.max(outputs.data, 1)
67
total += labels.size(0)
68
correct += (predicted == labels).sum().item()
69
accuracy = 100 * correct / total
70
71
print('Finished Testing')
72
print('Testing accuracy: %.1f %%' %(accuracy))
Copied!
The model and data are ready for training🚀!
1
num_epochs = 1
2
for epoch in range(num_epochs): # loop over the dataset multiple times
3
print("------------------ Training Epoch {} ------------------".format(epoch+1))
4
train_one_epoch(model, optimizer, train_loader, device)
5
6
test_model(model, test_loader)
7
print('Finished Training')
Copied!
Congrats! You successfully trained an image classification model while streaming data directly from the cloud! 🎉