Train your image classifier model with PyTorch
Train your image classifier model with PyTorch
com/en-us/windows/ai/windows-ml/tutorials/pytorch-train-model
For greater functionality, PyTorch can also be used with DirectML on Windows.
In the previous stage of this tutorial, we acquired the dataset we'll use to train our image classifier with PyTorch. Now,
it's time to put that data to use.
To train the image classifier with PyTorch, you need to complete the following steps:
1. Load the data. If you've done the previous step of this tutorial, you've handled this already.
2. Define a Convolution Neural Network.
3. Define a loss function.
4. Train the model on the training data.
5. Test the network on the test data.
Here, you'll build a basic convolution neural network (CNN) to classify the images from the CIFAR10 dataset.
A CNN is a class of neural networks, defined as multilayered neural networks designed to detect complex features in
data. They're most commonly used in computer vision applications.
Conv -> BatchNorm -> ReLU -> Conv -> BatchNorm -> ReLU -> MaxPool -> Conv -> BatchNorm -> ReLU -> Conv ->
BatchNorm -> ReLU -> Linear.
The convolution layer is a main layer of CNN which helps us to detect features in images. Each of the layers has
number of channels to detect specific features in images, and a number of kernels to define the size of the detected
feature. Therefore, a convolution layer with 64 channels and kernel size of 3 x 3 would detect 64 distinct features,
each of size 3 x 3. When you define a convolution layer, you provide the number of in-channels, the number of out-
channels, and the kernel size. The number of out-channels in the layer serves as the number of in-channels to the next
layer.
For example: A Convolution layer with in-channels=3, out-channels=10, and kernel-size=6 will get the RGB image (3
channels) as an input, and it will apply 10 feature detectors to the images with the kernel size of 6x6. Smaller kernel
sizes will reduce computational time and weight sharing.
Other layers
The ReLU layer is an activation function to define all incoming features to be 0 or greater. When you apply this
layer, any number less than 0 is changed to zero, while others are kept the same.
the BatchNorm2d layer applies normalization on the inputs to have zero mean and unit variance and increase the
network accuracy.
The MaxPool layer will help us to ensure that the location of an object in an image will not affect the ability of the
neural network to detect its specific features.
The Linear layer is final layers in our network, which computes the scores of each of the classes. In the CIFAR10
dataset, there are ten classes of labels. The label with the highest score will be the one model predicts. In the
linear layer, you have to specify the number of input features and the number of output features which should
correspond to the number of classes.
A forward function computes the value of the loss function, and the backward function computes the gradients of the
learnable parameters. When you create our neural network with PyTorch, you only need to define the forward function.
The backward function will be automatically defined.
1. Copy the following code into the PyTorchTraining.py file in Visual Studio to define the CCN.
py Copy
import torch
import torch.nn as nn
import torchvision
import torch.nn.functional as F
return output
Note
Interested in learning more about neural network with PyTorch? Check out the PyTorch documentation
Loss value is different from model accuracy. Loss function gives us the understanding of how well a model behaves
after each iteration of optimization on the training set. The accuracy of the model is calculated on the test data and
shows the percentage of the right prediction.
In PyTorch, the neural network package contains various loss functions that form the building blocks of deep neural
networks. In this tutorial, you will use a Classification loss function based on Define the loss function with
Classification Cross-Entropy loss and an Adam Optimizer. Learning rate (lr) sets the control of how much you are
adjusting the weights of our network with respect the loss gradient. You will set it as 0.001. The lower it is, the slower
the training will be.
1. Copy the following code into the PyTorchTraining.py file in Visual Studio to define the loss function and an
optimizer.
py Copy
from torch.optim import Adam
https://learn.microsoft.com/en-us/windows/ai/windows-ml/tutorials/pytorch-train-model 2/6
2025/1/11 09:12 learn.microsoft.com/en-us/windows/ai/windows-ml/tutorials/pytorch-train-model
# Define the loss function with Classification Cross-Entropy loss and an optimizer with Adam optimizer
loss_fn = nn.CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr=0.001, weight_decay=0.0001)
py Copy
from torch.autograd import Variable
# Function to test the model with the test dataset and print the accuracy for the test images
def testAccuracy():
model.eval()
accuracy = 0.0
total = 0.0
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
with torch.no_grad():
for data in test_loader:
images, labels = data
# run the model on the test set to predict labels
outputs = model(images.to(device))
# the label with the highest energy will be our prediction
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
accuracy += (predicted == labels.to(device)).sum().item()
# Training function. We simply have to loop over our data iterator and feed the inputs to the network and optimize.
def train(num_epochs):
best_accuracy = 0.0
# Compute and print the average accuracy fo this epoch when tested over all 10000 test images
accuracy = testAccuracy()
print('For epoch', epoch+1,'the test accuracy over the whole test set is %d %%' % (accuracy))
py Copy
import matplotlib.pyplot as plt
import numpy as np
# Function to test the model with a batch of images and show the labels predictions
def testBatch():
# get batch of images from the test DataLoader
images, labels = next(iter(test_loader))
# Let's see what if the model identifiers the labels of those example
outputs = model(images)
# We got the probability for every 10 labels. The highest (max) probability should be correct label
_, predicted = torch.max(outputs, 1)
# Let's show the predicted labels on the screen to compare with the real ones
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
for j in range(batch_size)))
Finally, let’s add the main code. This will will initiate model training, save the model, and display the results on the
screen. We'll run only two iterations [train(2)] over the training set, so the training process won't take too long.
py Copy
if __name__ == "__main__":
# Let's load the model we just created and test the accuracy per label
model = Network()
path = "myFirstModel.pth"
model.load_state_dict(torch.load(path))
Let’s run the test! Make sure the dropdown menus in the top toolbar are set to Debug. Change the Solution Platform
to x64 to run the project on your local machine if your device is 64-bit, or x86 if it's 32-bit.
Choosing the epoch number (the number of complete passes through the training dataset) equal to two ([train(2)])
will result in iterating twice through the entire test dataset of 10,000 images. It will take around 20 minutes to complete
https://learn.microsoft.com/en-us/windows/ai/windows-ml/tutorials/pytorch-train-model 4/6
2025/1/11 09:12 learn.microsoft.com/en-us/windows/ai/windows-ml/tutorials/pytorch-train-model
the training on 8th Generation Intel CPU, and the model should achieve more or less 65% of success rate in the
classification of ten labels.
3. To run the project, click the Start Debugging button on the toolbar, or press F5.
The console window will pop up and will be able to see the process of training.
As you defined, the loss value will be printed every 1,000 batches of images or five times for every iteration over the
training set. You expect the loss value to decrease with every loop.
You'll also see the accuracy of the model after each iteration. Model accuracy is different from the loss value. Loss
function gives us the understanding of how well a model behaves after each iteration of optimization on the training
set. The accuracy of the model is calculated on the test data and shows the percentage of the right prediction. In our
case it will tell us how many images from the 10,000-image test set our model was able to classify correctly after each
training iteration.
Once the training is complete, you should expect to see the output similar to the below. Your numbers won't be exactly
the same - trianing depends on many factors, and won't always return identifical results - but they should look similar.
https://learn.microsoft.com/en-us/windows/ai/windows-ml/tutorials/pytorch-train-model 5/6
2025/1/11 09:12 learn.microsoft.com/en-us/windows/ai/windows-ml/tutorials/pytorch-train-model
After running just 5 epochs, the model success rate is 70%. This is a good result for a basic model trained for short
period of time!
Testing with the batch of images, the model got right 7 images from the batch of 10. Not bad at all and consistent with
the model success rate.
You can check which classes our model can predict the best. Simple add the run the code below:
4. Optional - add the following testClassess function into the PyTorchTraining.py file, add a call of this function -
testClassess() inside the main function - __name__ == "__main__".
py Copy
# Function to test what classes performed well
def testClassess():
class_correct = list(0. for i in range(number_of_labels))
class_total = list(0. for i in range(number_of_labels))
with torch.no_grad():
for data in test_loader:
images, labels = data
outputs = model(images)
_, predicted = torch.max(outputs, 1)
c = (predicted == labels).squeeze()
for i in range(batch_size):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
for i in range(number_of_labels):
print('Accuracy of %5s : %2d %%' % (
classes[i], 100 * class_correct[i] / class_total[i]))
Next Steps
Now that we have a classification model, the next step is to convert the model to the ONNX format
https://learn.microsoft.com/en-us/windows/ai/windows-ml/tutorials/pytorch-train-model 6/6