How to Train and Use PyTorch Models on the CIFAR-10 Dataset

Oct 24, 2023 | Data Science

Are you eager to dive into the world of deep learning with PyTorch? One exciting project involves modifying TorchVision’s official implementation of popular Convolutional Neural Networks (CNNs) to train on the CIFAR-10 dataset. This blog will guide you through the process of training these models, loading pretrained weights, and interpreting the statistics of supported models, making it user-friendly and straightforward.

Getting Started with PyTorch and CIFAR-10

The CIFAR-10 dataset consists of 60,000 32×32 color images in 10 classes, with 6,000 images per class. The challenge is to classify these images correctly using CNNs. By modifying certain parameters in the TorchVision models, we can prepare them to work effectively on this dataset.

Modifying the TorchVision Implementation

In the modified code, several aspects were altered:

  • Number of classes
  • Filter size
  • Stride
  • Padding

Think of these modifications as customizing a recipe. Just as you might change the number of servings, ingredients, or cooking time for a dish, the same goes for preparing CNNs for different datasets such as CIFAR-10.

Statistics of Supported Models

Here’s a snapshot of the trained models along with their statistics:


No.      Model           Val. Acc.    No. Params    Size
------------------------------------------------------
1       vgg11_bn       92.39%       28.150 M      108 MB
2       vgg13_bn       94.22%       28.334 M      109 MB
3       vgg16_bn       94.00%       33.647 M      129 MB
4       vgg19_bn       93.95%       38.959 M      149 MB
5       resnet18       93.07%       11.174 M      43 MB
6       resnet34       93.34%       21.282 M      82 MB
7       resnet50       93.65%       23.521 M      91 MB
8       densenet121    94.06%       6.956 M       28 MB
9       densenet161    94.07%       26.483 M      103 MB
10      densenet169    94.05%       12.493 M      49 MB
11      mobilenet_v2   93.91%       2.237 M       9 MB
12      googlenet      92.85%       5.491 M       22 MB
13      inception_v3   93.74%       21.640 M      83 MB

How to Use Pretrained Models

To jumpstart your project, you can utilize pretrained models easily. Follow these steps:

  • Automatically download and extract the weights:
    • Run the command: python train.py –download_weights 1
  • Alternatively, download from Google Drive:

Load Model and Run

To load a model and run it, execute the following commands:


from cifar10_models.vgg import vgg11_bn, vgg13_bn, vgg16_bn, vgg19_bn

# Untrained model
my_model = vgg11_bn()

# Pretrained model
my_model = vgg11_bn(pretrained=True)
my_model.eval() # for evaluation

# Ensure your images are normalized and in range [0, 1]:
mean = [0.4914, 0.4822, 0.4465]
std = [0.2471, 0.2435, 0.2616]

Training Models from Scratch

If you’re inclined to train your own models from scratch, the hyper-parameters can be customized. Use the command below to reproduce similar accuracy:


python train.py --classifier resnet18

Testing Pretrained Models

To test pretrained models, utilize the command:


python train.py --test_phase 1 --pretrained 1 --classifier resnet18

After running the command, you should see an output similar to:


Output acctest: tensor(93.0689, device=cuda:0)

Troubleshooting

Encountering issues? Here are a few troubleshooting ideas:

  • Check the versions of your libraries. Ensure you’re using the following:
    • PyTorch: 1.7.0
    • For training and testing, include torchvision: 0.7.0, tensorboard: 2.2.1, and pytorch-lightning: 1.1.0.
  • If weights are not downloading, check your internet connection.
  • Verify your data normalization; ensure images are scaled to [0, 1].
  • If you have more specific errors, refer to the WandB Run Log for detailed tracking of hyper-parameters and training metrics.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Working with the CIFAR-10 dataset using modified PyTorch models can lead to excellent accuracy in image classification tasks. By following this guide, you’ll not only learn to train these models effectively but also understand their configurations and how to troubleshoot common issues. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox