{
  "source_file": "0930",
  "source_platform": "技术平台",
  "author": "Satya Mallick / LearnOpenCV",
  "title_listed": "Image Classification with PyTorch",
  "keywords": "图像分类,PyTorch,迁移学习",
  "domain": "图像处理",
  "content_type": "教程",
  "is_paid_flag": "否",
  "url": "https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/",
  "publish_date": "2021-03",
  "corpus_stage": "预训练/SFT",
  "notes": "PyTorch图像分类迁移学习",
  "id": "e1dddcdf5067",
  "collected_at": "2026-10-02T17:29:43",
  "title": "Pre Trained Models for Image Classification - PyTorch",
  "body_markdown": "We use essential cookies to make our site work. With your consent, we may also use non-essential cookies to improve user experience and analyze website traffic. By clicking “Accept,” you agree to our website's cookie use as described in our Cookie Policy. You can change your cookie settings at any time by clicking “Preferences.” \nAccept\n[ ![](../images/000.png) ](https://learnopencv.com)\n  * [Getting Started](https://learnopencv.com/category/install/)\n    * [Subscribe](https://learnopencv.com/)\n    * [Installation](https://learnopencv.com/category/install/)\n    * [Getting Started with OpenCV](https://learnopencv.com/getting-started-with-opencv/)\n    * [PyTorch](https://learnopencv.com/getting-started-with-pytorch/)\n    * [Tensorflow & Keras](https://learnopencv.com/getting-started-with-tensorflow-keras/)\n  * [Free Courses](https://opencv.org/university/free-courses/?utm_source=lopcv&utm_medium=menu&utm_campaign=fcpage)\n    * [VLM Bootcamp](https://opencv.org/university/vision-language-model-bootcamp/?utm_source=lopcv&utm_medium=menu&utm_campaign=vlm)\n    * [PyTorch Bootcamp](https://opencv.org/university/free-pytorch-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=ptbc)\n    * [TensorFlow Keras Course](https://opencv.org/university/free-tensorflow-keras-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=tbc)\n    * [OpenCV Course](https://opencv.org/university/free-opencv-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=obc)\n    * [Python for Beginners](https://opencv.org/university/python-for-beginners/?utm_source=lopcv&utm_medium=menu&utm_campaign=pbc)\n  * [AI Consulting](https://bigvision.ai/expert-ai-solution-builders/#talk?utm_source=locv&utm_medium=menu)\n  * [About](https://learnopencv.com/about/)\n\n\n  * [Getting Started](https://learnopencv.com/category/install/)\n    * [Subscribe](https://learnopencv.com/)\n    * [Installation](https://learnopencv.com/category/install/)\n    * [Getting Started with OpenCV](https://learnopencv.com/getting-started-with-opencv/)\n    * [PyTorch](https://learnopencv.com/getting-started-with-pytorch/)\n    * [Tensorflow & Keras](https://learnopencv.com/getting-started-with-tensorflow-keras/)\n  * [Free Courses](https://opencv.org/university/free-courses/?utm_source=lopcv&utm_medium=menu&utm_campaign=fcpage)\n    * [VLM Bootcamp](https://opencv.org/university/vision-language-model-bootcamp/?utm_source=lopcv&utm_medium=menu&utm_campaign=vlm)\n    * [PyTorch Bootcamp](https://opencv.org/university/free-pytorch-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=ptbc)\n    * [TensorFlow Keras Course](https://opencv.org/university/free-tensorflow-keras-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=tbc)\n    * [OpenCV Course](https://opencv.org/university/free-opencv-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=obc)\n    * [Python for Beginners](https://opencv.org/university/python-for-beginners/?utm_source=lopcv&utm_medium=menu&utm_campaign=pbc)\n  * [AI Consulting](https://bigvision.ai/expert-ai-solution-builders/#talk?utm_source=locv&utm_medium=menu)\n  * [About](https://learnopencv.com/about/)\n\n\nSearch ...\nResults\nSee all results\n  * [Home](https://learnopencv.com \"Home\")\n  * >\n  * [Deep Learning](https://learnopencv.com/category/deep-learning/ \"Deep Learning\")\n  * >\n  * Pre Trained Models for Image Classification – PyTorch for Beginners\n\n\n[Vishwesh Shrimali](https://learnopencv.com/author/vishwesh/)\n  * on  June 3, 2019\n\n\n# Pre Trained Models for Image Classification – PyTorch for Beginners\nHi! This post is part of our PyTorch series. In the previous post, Pytorch Tutorial for beginners, we discussed PyTorch, it’s strengths and why you should learn it. We also had a brief look at Tensors – the core data structure used in PyTorch. In this article, we will jump\n  * [Deep Learning](https://learnopencv.com/category/deep-learning/), [Getting Started with PyTorch](https://learnopencv.com/category/getting-started-with-pytorch/), [Image Classification](https://learnopencv.com/category/image-classification/), [PyTorch](https://learnopencv.com/category/pytorch/), [Tutorial](https://learnopencv.com/category/tutorial/)\n\n\n![](../images/006.png)\n  *   *   * [ ](https://bigvision.ai/expert-ai-solution-builders?utm_source=lopcv&utm_medium=post-icon)\n\n\nHi! This post is part of our PyTorch series.  \n| PyTorch for Beginners |  \n| --- |  \n| [PyTorch for Beginners: Basics](https://learnopencv.com/pytorch-for-beginners-basics/) |  \n| [PyTorch for Beginners: Image Classification using Pre-trained models](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/) |  \n| [Image Classification using Transfer Learning in PyTorch](https://learnopencv.com/image-classification-using-transfer-learning-in-pytorch/) |  \n| [PyTorch Model Inference using ONNX and Caffe2](https://learnopencv.com/pytorch-model-inference-using-onnx-and-caffe2/) |  \n| [PyTorch for Beginners: Semantic Segmentation using torchvision](https://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/) |  \n|  [Object Detection](https://learnopencv.com/faster-r-cnn-object-detection-with-pytorch)  \n |  \n|  [Instance Segmentation](https://learnopencv.com/mask-r-cnn-instance-segmentation-with-pytorch/)  \n |  \nIn the previous post, [Pytorch Tutorial for beginners](https://learnopencv.com/pytorch-for-beginners-basics/), we discussed PyTorch, it’s strengths and why you should learn it. We also had a brief look at Tensors – the core data structure used in PyTorch. In this article, we will jump into some hands-on examples of using pre-trained networks present in **TorchVision** module – pre trained models for **Image Classification**.\n**Torchvision** package consists of popular datasets, model architectures, and common image transformations for computer vision. Basically, if you are into Computer Vision and using PyTorch, Torchvision will be of great help.!\n## 1. Pre trained Models for Image Classification\nPre-trained models are Neural Network models trained on large benchmark datasets like ImageNet. The Deep Learning community has greatly benefitted from these open-source models. Also, the pre-trained models are a major factor for rapid advances in Computer Vision research. Other researchers and practitioners can use these state-of-the-art models instead of re-inventing everything from scratch.\nGiven below is a rough timeline of how the state-of-the-art models have improved over time. We have included only those models which are present in the Torchvision package.\n[![State of the art Model timeline - pre trained models for Image Classification](../images/010.png)](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095456/Model_Timeline.png)\nBefore we jump into the details of how we can use pre-trained models for image classification, let’s see what the various pre-trained models available are. We will discuss **AlexNet** and **ResNet101** as the two main examples here. Both the networks have been trained on **ImageNet dataset**.\nImageNet dataset has over **14 million images** maintained by **Stanford University**. It is extensively used for a large variety of Image related deep learning projects. The images belong to various **classes** or **labels**. Even though we can use both terms interchangeably, we will stick to **classes**. The aim of the pre-trained models like AlexNet and ResNet101 is to take an image as an input and predict it’s class.\nThe word pre-trained here means that the deep learning architectures **AlexNet** and **ResNet101** , for instance, have been already trained on some (huge) dataset and thus carry the resultant weights and biases with them. This difference between **architecture** and **weights and biases** should be very clear because as we will see in the next section, TorchVision has both the architectures and the pre-trained models.\n### 1.1. Model Inference Process\nSince we will focus on how to use the pre-trained models for predicting the class (label) of input, let’s also discuss the process involved in this. This process is referred to as **Model Inference**. The entire process consists of the following main steps.\n  1. Reading the input image\n  2. Performing transformations on the image. For example – resize, center crop, normalization, etc.\n  3. Forward Pass: Use the pre-trained weights to find out the output vector. Each element in this output vector describes the **confidence** with which the model predicts the input image belongs to a particular class.\n  4. Based on the scores obtained (elements of the output vector mentioned in step 3), display the predictions.\n\n\n### 1.2. Loading Pre-Trained Network using TorchVision\nNow that we are equipped with the knowledge of model inference and know what a pre-trained model means, let’s see how we can use them with the help of **TorchVision** module.\nFirst, let’s install the **TorchVision** module using the command given below.  \n| 1  |  `pip ``install` `torchvision`  |  \n| --- | --- |  \nNext, let’s import **models** from **torchvision** module and see the different models and architectures available to us.  \n|  1 2 3 4  |  `from torchvision import models` `import torch` `dir(models)`  |  \n| --- | --- |  \nNot so fast! Carefully observe the output that we got above.  \n|  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22  |  `['AlexNet',` `'DenseNet',` `'GoogLeNet',` `'Inception3',` `'MobileNetV2',` `'ResNet',` `'ShuffleNetV2',` `'SqueezeNet',` `'VGG',` `...` `'alexnet',` `'densenet',` `'densenet121',` `'densenet161',` `'densenet169',` `'densenet201',` `'detection',` `'googlenet',` `'inception',` `'inception_v3',` `...` `]`  |  \n| --- | --- |  \nNotice that there is one entry called **AlexNet** and one called **alexnet**. The capitalized name refers to the Python class (**AlexNet**) whereas **alexnet** is a convenience function that returns the model instantiated from the **AlexNet** class. It’s also possible for these convenience functions to have different parameter sets. For example, **densenet121** , **densenet161** , **densenet169** , **densenet201,** are all instances of **DenseNet** class but with a different number of layers – 121,161,169 and 201, respectively.\n### 1.3. Using AlexNet for Image Classification\nLet’s first start with **AlexNet**. It is one of the early breakthrough networks in Image Recognition. If you are interested in learning about AlexNet’s architecture, you can check out our post on **[Understanding AlexNet](https://learnopencv.com/understanding-alexnet/).**\n![AlexNet Architecture](../images/012.png)AlexNet Architecture\n#### **Step 1: Load the pre-trained model**\nIn the first step, we will create an instance of the network. We’ll also pass an argument so the function download the weights of the model.  \n|  1 2 3 4  |  `alexnet ``=` `models.alexnet(pretrained``=``True``)` `# You will see a similar output as below` `# Downloading: \"https://download.pytorch.org/models/alexnet-owt-[](https://download.pytorch.org/models/alexnet-owt-) 4df8aa71.pth\" to /home/hp/.cache/torch/checkpoints/alexnet-owt-4df8aa71.pth`  |  \n| --- | --- |  \nNote that usually the PyTorch models have an extension of **.pt** or **.pth**\nOnce the weights have been downloaded, we can proceed with the other steps. We can also check out some details of the network’s architecture as follows.  \n| 1  |  `print``(alexnet)`  |  \n| --- | --- |  \nDon’t worry about the overflowing output. This basically states the various operations and layers in the AlexNet architecture.\n#### **Step 2: Specify image transformations**\nOnce we have the model with us, the next step is to transform the input image so that they have the right shape and other characteristics like mean and standard deviation. These values should be similar to those used while training the model. This makes sure that the network will produce meaningful answers.\nWe can pre-process the input image with the help of **transforms** present in **TochVision** module. In this case, we can use the following transforms for **both** AlexNet and ResNet.\n**Download Code** To easily follow along this tutorial, please download code by clicking on the button below. It's FREE!\n[Download Code](javascript:void\\(0\\))  \n|  1 2 3 4 5 6 7 8 9  |  `from` `torchvision ``import` `transforms` `transform ``=` `transforms.Compose([            ``#[1]` `transforms.Resize(``256``),                    ``#[2]` `transforms.CenterCrop(``224``),                ``#[3]` `transforms.ToTensor(),                     ``#[4]` `transforms.Normalize(                      ``#[5]` `mean``=``[``0.485``, ``0.456``, ``0.406``],                ``#[6]` `std``=``[``0.229``, ``0.224``, ``0.225``]                  ``#[7]` `)])`  |  \n| --- | --- |  \nLet’s try to understand what happened in the above code snippet.\n**Line [1]** : Here we are defining a variable **transform** which is a combination of all the image transformations to be carried out on the input image.\n**Line [2]:** Resize the image to 256×256 pixels.\n**Line [3]** : Crop the image to 224×224 pixels about the center.\n**Line [4]** : Convert the image to PyTorch Tensor data type.\n**Line [5-7]:** Normalize the image by setting its mean and standard deviation to the specified values.\n#### **Step 3: Load the input image and pre-process it**\nNext, let’s load the input image and carry out the image transformations specified above. Note that we will use **Pillow** (PIL) module extensively with TorchVision as it’s the default image backend supported by TorchVision.  \n|  1 2 3  |  `# Import Pillow` `from` `PIL ``import` `Image` `img ``=` `Image.``open``(``\"dog.jpg\"``)`  |  \n| --- | --- |  \n![Sample test image](../images/015.jpg)A Yellow Labrador (Source: Wikipedia)\nNext, pre-process the image and prepare a batch to be passed through the network.  \n|  1 2  |  `img_t ``=` `transform(img)` `batch_t ``=` `torch.unsqueeze(img_t, ``0``)`  |  \n| --- | --- |  \n#### **Step 4: Model Inference**\nFinally, it’s time to use the pre-trained model to see what the model thinks the image is.\nFirst, we need to put our model in **eval** mode.  \n| 1  |  `alexnet.``eval``()`  |  \n| --- | --- |  \nNext, let’s carry out the inference.  \n|  1 2  |  `out ``=` `alexnet(batch_t)` `print``(out.shape)`  |  \n| --- | --- |  \nThis is all good but what do we do with this output vector **out** with **1000 elements**? We still haven’t got the **image’s class (or label)**. For this, we will first read and store the labels from a text file having a list of all the 1000 labels. Note that the line number specified the class number, so it’s very important to ensure that you don’t change that order.  \n|  1 2  |  `with open('imagenet_classes.txt') as f:` `classes = [line.strip() for line in f.readlines()]`  |  \n| --- | --- |  \nSince AlexNet and ResNet have been trained on the same ImageNet dataset, we can use the same **classes** list for both models.\nNow, we need to find the index where the maximum score in output vector **out** occurs. We will use this index to find out the prediction.  \n|  1 2 3 4 5  |  `_, index ``=` `torch.``max``(out, ``1``)` `percentage ``=` `torch.nn.functional.softmax(out, dim``=``1``)[``0``] ``*` `100` `print``(labels[index[``0``]], percentage[index[``0``]].item())`  |  \n| --- | --- |  \nThere we go! The model predicts the image to be of a **Labrador Retriever** with a **41.58% confidence.**\nBut that sounds too low. Let’s see what other classes the model thought the image belonged to.  \n|  1 2  |  `_, indices ``=` `torch.sort(out, descending``=``True``)` `[(labels[idx], percentage[idx].item()) ``for` `idx ``in` `indices[``0``][:``5``]]`  |  \n| --- | --- |  \nAnd here are the outputs:  \n|  1 2 3 4 5  |  `[('Labrador retriever', 41.585166931152344),` `('golden retriever', 16.59166145324707),` `('Saluki, gazelle hound', 16.286880493164062),` `('whippet', 2.8539133071899414),` `('Ibizan hound, Ibizan Podenco', 2.3924720287323)]`  |  \n| --- | --- |  \nIf you are not aware, all these are dog breeds. So the model managed to predict that it was a dog with a fairly high confidence but it was not very sure about the breed of the dog.\nLet’s try the same thing for an image of strawberries and a car and see the ouputs we get.\n![](../images/017.jpg)Strawberries\nHere is the output obtained for the above image of strawberries. As we can see, the class with highest score was “Strawberries” with a score of close to 99.99%.  \n|  1 2 3 4 5  |  `[('strawberry', 99.99365997314453),` `('custard apple', 0.001047826954163611),` `('banana', 0.0008201944874599576),` `('orange', 0.0007371827960014343),` `('confectionery, confectionary, candy store', 0.0005758354091085494)]`  |  \n| --- | --- |  \n![](../images/019.jpg)Passenger car\nSimilarly, for the the image of the car given above, the output is as follows.  \n|  1 2 3 4 5 6  |  `[('cab, hack, taxi, taxicab', 33.30569839477539),` `('sports car, sport car', 14.424001693725586),` `('racer, race car, racing car', 10.685123443603516),` `('beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon',` `7.846532821655273),` `('passenger car, coach, carriage', 6.985556125640869)]`  |  \n| --- | --- |  \nThat’s it! All it takes is these 4 steps to carry out image classification using pre-trained models.\nHow about we try the same with ResNet?\n### 1.4. Using ResNet for Image Classification\nWe will use **resnet101** – a 101 layer Convolutional Neural Network. resnet101 has about **44.5 million parameters** tuned during the training process. That’s huge!\nLet’s quickly go through the steps required to use resnet101 for image classification.  \n|  1 2 3 4 5 6 7 8 9 10 11 12 13  |  `# First, load the model` `resnet ``=` `models.resnet101(pretrained``=``True``)` `# Second, put the network in eval mode` `resnet.``eval``()` `# Third, carry out model inference` `out ``=` `resnet(batch_t)` `# Forth, print the top 5 classes predicted by the model` `_, indices ``=` `torch.sort(out, descending``=``True``)` `percentage ``=` `torch.nn.functional.softmax(out, dim``=``1``)[``0``] ``*` `100` `[(labels[idx], percentage[idx].item()) ``for` `idx ``in` `indices[``0``][:``5``]]`  |  \n| --- | --- |  \nAnd here is what **resnet101** predicted.  \n|  1 2 3 4 5  |  `[('Labrador retriever', 48.25556945800781),` `('dingo, warrigal, warragal, Canis dingo', 7.900787353515625),` `('golden retriever', 6.916920185089111),` `('Eskimo dog, husky', 3.6434383392333984),` `('bull mastiff', 3.0461232662200928)]`  |  \n| --- | --- |  \nJust like AlexNet, ResNet managed to predict that it was a dog with very high confidence and predicted that it was a **Labrador Retriever** with a **48.25%** confidence. That’s pretty good!\n## 2. Model Comparison\nSo far we have discussed how we can use pre-trained models to perform image classification but one question that we have yet to answer is how do we decide which model to choose for a particular task. In this section we will compare the pre-trained models based on the following criteria:\n  1. **Top-1 Error** : A top-1 error occurs if the class predicted by a model with the highest confidence is **not** the same as the true class.\n  2. **Top-5 Error** : A top-5 error occurs when the true class is not among the top 5 classes predicted by a model (sorted in terms of confidence).\n  3. **Inference Time on CPU** : Inference time is the time taken for the model inference step.\n  4. **Inference Time on GPU**\n  5. **Model size** : Here size stands for the physical space occupied by the **.pth** file of the pre-trained model supplied by PyTorch\n\n\nA **good** model will have **low Top-1 error** , **low Top-5 error** , **low inference time** on CPU and GPU and **low model size**.\nAll the experiments were performed on the same input image and multiple times so that the average of all the results for a particular model can be analyzed. The experiments were performed on Google Colab. Now, let’s have a look at the results obtained.\n### 2.1. Accuracy Comparison of Models\nThe first criterion we will discuss consists of Top-1 and Top-5 errors. Top-1 error is when the top predicted class differs from the ground truth. Since the problem is rather a difficult one, there is another error measure called Top-5 error. A prediction is classified as an error if none of the top-5 predicted classes are correct.\n![Accuracy Comparison of Models](../images/021.png)\nNotice from the graph that both errors follow a similar trend. **AlexNet** was the first attempt based on Deep Learning and there has been improvement in the error since then. **Notable** mentions are GoogLeNet, ResNet, VGGNet, ResNext.\n### 2.2. Inference Time Comparison\nNext, we will compare the models based on the time taken for model inference. One image was supplied to each model multiple times and the inference time for all the iterations was averaged. Similar process was performed for CPU and then for GPU on Google Colab. Even though there are some variations in the order, we can see that **SqueezeNet** , **ShuffleNet** and **ResNet-18** had a really low inference time, which is exactly what we want.\n![Inference time on CPU comparison](../images/024.png)  \n![Inference time on GPU comparison](../images/027.png)\n### 2.3. Model Size Comparison\nA lot of times when we are using a Deep Learning model on an android or iOS device, the model size becomes a deciding factor, sometimes even more important than accuracy. **SqueezeNet** has the minimum model size (5 MB), followed by **ShuffleNet V2** (6 MB) and **MobileNet V2** (14 MB). It’s obvious why these models are preferred in mobile apps utilizing deep learning.\n![Model size comparison](../images/030.png)\n## 2.4. Overall Comparison\nWe discussed which model performed better based on a particular criterion. We can squeeze all those important details in one bubble chart which we can then refer to for deciding which model to go for based on our requirements.\nThe x-coordinate we are using is Top-1 error (**lower is better**). The y-coordinate is the inference time on GPU in milliseconds (**lower is better**). The bubble size represents the model size (**lower is better**).\n#### NOTE :\n  * Smaller Bubbles are better in terms of model size.\n  * Bubbles near the origin are better in terms of both Accuracy and Speed.\n\n[![Comparison of all models using accuracy speed and model size](../images/032.png)](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095501/Pre-Trained-Model-Comparison.png)\n### From idea to working model to real-time deployment\nBig Vision takes computer vision projects through the full journey, not just the easy parts.\n![Satya Mallick](../images/body-fe65e1f717d94c873fd9.jpg)\n**Satya Mallick** Founder - Big Vision & LearnOpenCV\n[ Talk to our Team ](https://bigvision.ai/expert-ai-solution-builders/)\n## 3. Final Verdict\n  * It is clear from the above graph that **ResNet50** is the best model in terms of all three parameters ( small in size and closer to origin )\n  * DenseNets and ResNext101 are expensive on inference time.\n  * AlexNet and SqueezeNet have pretty high error rate.\n\n\n* * *\nWell, that’s all, folks! In this post, we covered how we can use the TorchVision module to carry out Image Classification using pre-trained models – a 4-step process. We also made model comparisons to decide what model to choose depending on our project requirements. In the next post, we will cover how to use transfer learning to train a model on a custom dataset using PyTorch.\nAlso read: PyTorch for Beginners: [Semantic Segmentation using torchvision](https://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/)\n* * *\nWas This Article Helpful?\nPost Views: 268\n## Subscribe & Download Code\nIf you liked this article and would like to download code (C++ and Python) and example images used in this post, please [click here](javascript:void\\(0\\)). In our newsletter, we share Computer Vision and AI tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.  \n  \n\n[ Download Example Code](javascript:void\\(0\\))\n  \n  \n\n[PrevPreviousPyTorch Tutorial For Beginners: All the Basics](https://learnopencv.com/pytorch-for-beginners-basics/)\n[NextTorchvision Semantic Segmentation – PyTorch for BeginnersNext](https://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/)\n![Satya Mallick](../images/body-fe65e1f717d94c873fd9.jpg)\n#### Satya Mallick, Ph.D.\nFounder - Big Vision & LearnOpenCV\n#####  Are you struggling with your Computer Vision Product Development?\nHelping startups and enterprises build and deploy scalable solutions that work in the real world.\n[ Schedule a Call ](https://bigvision.ai/expert-ai-solution-builders?utm_source=lopcv&utm_medium=post)\nGetting Started with OpenCV \n0/15\n  * [ Read, Display and Write an Image using OpenCV ](https://learnopencv.com/read-display-and-write-an-image-using-opencv/)\n  * [ Image Resizing with OpenCV ](https://learnopencv.com/image-resizing-with-opencv/)\n  * [ Cropping an Image using OpenCV ](https://learnopencv.com/cropping-an-image-using-opencv/)\n  * [ Image Translation and Rotation Using OpenCV ](https://learnopencv.com/image-rotation-and-translation-using-opencv/)\n  * [ Annotating Images Using OpenCV ](https://learnopencv.com/annotating-images-using-opencv/)\n  * [ Color spaces in OpenCV (C++ / Python) ](https://learnopencv.com/color-spaces-in-opencv-cpp-python/)\n  * [ Image Filtering Using Convolution in OpenCV ](https://learnopencv.com/image-filtering-using-convolution-in-opencv/)\n  * [ Image Thresholding in OpenCV ](https://learnopencv.com/opencv-threshold-python-cpp/)\n  * [ Blob Detection Using OpenCV ( Python, C++ ) ](https://learnopencv.com/blob-detection-using-opencv-python-c/)\n  * [ Edge Detection Using OpenCV ](https://learnopencv.com/edge-detection-using-opencv/)\n  * [ Mouse and Trackbar in OpenCV GUI ](https://learnopencv.com/mouse-and-trackbar-in-opencv-gui/)\n  * [ Contour Detection using OpenCV (Python/C++) ](https://learnopencv.com/contour-detection-using-opencv-python-c/)\n  * [ Simple Background Estimation in Videos using OpenCV (C++/Python) ](https://learnopencv.com/simple-background-estimation-in-videos-using-opencv-c-python/)\n  * [ Deep Learning with OpenCV DNN Module: A Definitive Guide ](https://learnopencv.com/deep-learning-with-opencvs-dnn-module-a-definitive-guide/)\n  * [ Reading and Writing Videos using OpenCV ](https://learnopencv.com/reading-and-writing-videos-using-opencv/)\n\n\nGetting Started with PyTorch \n1/6\n  * [ PyTorch Tutorial For Beginners: All the Basics ](https://learnopencv.com/pytorch-for-beginners-basics/)\n  * [ Pre Trained Models for Image Classification – PyTorch for Beginners ](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/)\n  * [ Torchvision Semantic Segmentation – PyTorch for Beginners ](https://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/)\n  * [ Transfer Learning For Pytorch Image Classification ](https://learnopencv.com/image-classification-using-transfer-learning-in-pytorch/)\n  * [ Faster R-CNN Object Detection with PyTorch ](https://learnopencv.com/faster-r-cnn-object-detection-with-pytorch/)\n  * [ Mask RCNN Pytorch – Instance Segmentation ](https://learnopencv.com/mask-r-cnn-instance-segmentation-with-pytorch/)\n\n\nGetting started with Tensorflow & Keras \n0/11\n  * [ Getting Started with Neural Networks: A 30,000-Foot View for Beginners ](https://learnopencv.com/neural-networks-a-30000-feet-view-for-beginners/)\n  * [ Training Neural Networks for Beginners ](https://learnopencv.com/how-to-train-neural-networks-for-beginners/)\n  * [ Tensorflow & Keras Tutorial: Linear Regression ](https://learnopencv.com/tensorflow-keras-tutorial-linear-regression/)\n  * [ Understanding Feedforward Neural Networks ](https://learnopencv.com/understanding-feedforward-neural-networks/)\n  * [ Implementing an MLP in TensorFlow & Keras ](https://learnopencv.com/implementing-mlp-tensorflow-keras/)\n  * [ Convolutional Neural Network (CNN): A Complete Guide ](https://learnopencv.com/understanding-convolutional-neural-networks-cnn/)\n  * [ Implementing a CNN in TensorFlow & Keras ](https://learnopencv.com/implementing-cnn-tensorflow-keras/)\n  * [ Image Classification using Pre-Trained ImageNet Models in TensorFlow & Keras ](https://learnopencv.com/image-classification-pretrained-imagenet-models-tensorflow-keras/)\n  * [ Unlock the Power of Fine-Tuning Pre-Trained Models in TensorFlow & Keras ](https://learnopencv.com/fine-tuning-pre-trained-models-tensorflow-keras/)\n  * [ A Step-by-Step Tutorial on Image Segmentation using Tensorflow Hub ](https://learnopencv.com/image-segmentation-tensorflow-hub/)\n  * [ Object Detection Made Easy with TensorFlow Hub: Step-by-Step Tutorial ](https://learnopencv.com/object-detection-tensorflow-hub/)\n\n\n####  Table of Contents\n  1. [1. Pre trained Models for Image Classification](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/#elementor-toc__heading-anchor-0)\n  2. [2. Model Comparison](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/#elementor-toc__heading-anchor-1)\n  3. [2.4. Overall Comparison](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/#elementor-toc__heading-anchor-2)\n  4. [3. Final Verdict](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/#elementor-toc__heading-anchor-3)\n\n\nWas This Article Helpful?\n  * [alexnet](https://learnopencv.com/tag/alexnet/), [how to use a pretrained model in pytorch](https://learnopencv.com/tag/how-to-use-a-pretrained-model-in-pytorch/), [Image Classification](https://learnopencv.com/tag/image-classification/), [PyTorch](https://learnopencv.com/tag/pytorch/), [pytorch models](https://learnopencv.com/tag/pytorch-models/), [pytorch pretrained models example](https://learnopencv.com/tag/pytorch-pretrained-models-example/), [Resnet](https://learnopencv.com/tag/resnet/), [TorchVision](https://learnopencv.com/tag/torchvision/), [torchvision models](https://learnopencv.com/tag/torchvision-models/), [tutorial](https://learnopencv.com/tag/tutorial/), [using pretrained model in pytorch](https://learnopencv.com/tag/using-pretrained-model-in-pytorch/)\n\n\n## Read Next\n[ ![Decoding Virat Kohli’s Flick Shot: AI-Based 3D Motion Reconstruction](../images/036.png) ](https://learnopencv.com/decoding-virat-kohlis-flick-shot-ai-based-3d-motion-reconstruction/)\n[3D Computer Vision](https://learnopencv.com/category/3d-computer-vision/)[3D Reconstruction](https://learnopencv.com/category/3d-computer-vision/3d-reconstruction/)[Segmentation](https://learnopencv.com/category/segmentation/)[SpatialAI-Depth](https://learnopencv.com/category/3d-computer-vision/spatialai-depth/)[Video Analysis](https://learnopencv.com/category/video-analysis/)\n[Adnan Shaikh](https://learnopencv.com/author/adnan/) July 18, 2026 \n##  [ Decoding Virat Kohli’s Flick Shot: AI-Based 3D Motion Reconstruction ](https://learnopencv.com/decoding-virat-kohlis-flick-shot-ai-based-3d-motion-reconstruction/)\nReconstruct the 3D swing plane of Virat Kohli's flick shot from a single 2D video with off-the-shelf vision models. No…\n[ ![How to Run Object Detection with OpenCV 5](../images/037.jpg) ](https://learnopencv.com/how-to-run-object-detection-with-opencv-5/)\n[Object Detection](https://learnopencv.com/category/object-detection/)[OpenCV 5](https://learnopencv.com/category/opencv/opencv-5/)[OpenCV DNN](https://learnopencv.com/category/opencv-dnn/)[YOLO](https://learnopencv.com/category/yolo/)\n[Sudip Chakrabarty](https://learnopencv.com/author/sudip/) July 9, 2026 \n##  [ How to Run Object Detection with OpenCV 5 ](https://learnopencv.com/how-to-run-object-detection-with-opencv-5/)\nLearn about the new OpenCV 5 and how to use its DNN engine to run object detection models on the…\n[ ![How to Fine-Tune YOLO26 for Safety Gear and Sign Language Detection](../images/038.jpg) ](https://learnopencv.com/how-to-fine-tune-yolo26-for-safety-gear-and-sign-language-detection/)\n[Computer Vision](https://learnopencv.com/category/computer-vision/)[Object Detection](https://learnopencv.com/category/object-detection/)[YOLO](https://learnopencv.com/category/yolo/)\n[Sudip Chakrabarty](https://learnopencv.com/author/sudip/) June 18, 2026 \n##  [ How to Fine-Tune YOLO26 for Safety Gear and Sign Language Detection ](https://learnopencv.com/how-to-fine-tune-yolo26-for-safety-gear-and-sign-language-detection/)\nLearn how to fine-tune YOLO26 on your own data. We build a construction PPE safety detector and a sign-language reader…\n##  Subscribe to our Newsletter\nSubscribe to our email newsletter to get the latest posts delivered right to your email.\nName \nEmail \nSend\n![](../images/039.png)\nEmpowering innovation through education, LearnOpenCV provides in-depth tutorials, code, and guides in AI, Computer Vision, and Deep Learning. Led by Dr. Satya Mallick, we're dedicated to nurturing a community keen on technology breakthroughs.\n[ Read More ](https://learnopencv.com/about/)\nCategories\n  * [Deep Learning](https://learnopencv.com/category/deep-learning/)\n  * [Object Detection](https://learnopencv.com/category/object-detection/)\n  * [Image Classification](https://learnopencv.com/category/image-classification)\n  * [YOLO](https://learnopencv.com/category/yolo/)\n  * [Image Processing](https://learnopencv.com/category/image-processing/)\n  * [Image Segmentation](https://learnopencv.com/category/image-segmentation/)\n\n\n  * [Deep Learning](https://learnopencv.com/category/deep-learning/)\n  * [Object Detection](https://learnopencv.com/category/object-detection/)\n  * [Image Classification](https://learnopencv.com/category/image-classification)\n  * [YOLO](https://learnopencv.com/category/yolo/)\n  * [Image Processing](https://learnopencv.com/category/image-processing/)\n  * [Image Segmentation](https://learnopencv.com/category/image-segmentation/)\n\n\nGetting Started\n  * [Installation](https://learnopencv.com/category/install/)\n  * [PyTorch](https://learnopencv.com/learn-pytorch/)\n  * [Getting Started with OpenCV](https://learnopencv.com/getting-started-with-opencv/)\n  * [Keras & Tensorflow](https://learnopencv.com/learn-keras-and-tensorflow/)\n\n\n  * [Installation](https://learnopencv.com/category/install/)\n  * [PyTorch](https://learnopencv.com/learn-pytorch/)\n  * [Getting Started with OpenCV](https://learnopencv.com/getting-started-with-opencv/)\n  * [Keras & Tensorflow](https://learnopencv.com/learn-keras-and-tensorflow/)\n\n\nBig Vision\n  * [AI Consulting](https://bigvision.ai/expert-ai-solution-builders/#talk?utm_source=locv&utm_medium=footer)\n  * [Case Studies](https://bigvision.ai/case-studies/?utm_source=locv&utm_medium=footer)\n  * [Get in Touch](https://bigvision.ai/contact-us/?utm_source=locv&utm_medium=footer)\n\n\n  * [AI Consulting](https://bigvision.ai/expert-ai-solution-builders/#talk?utm_source=locv&utm_medium=footer)\n  * [Case Studies](https://bigvision.ai/case-studies/?utm_source=locv&utm_medium=footer)\n  * [Get in Touch](https://bigvision.ai/contact-us/?utm_source=locv&utm_medium=footer)\n\n\nCopyright © 2026 – BIG VISION LLC [Privacy Policy](https://learnopencv.com/privacy-policy/) [Terms and Conditions](https://learnopencv.com/terms-and-conditions/)\n  * [ ](https://bit.ly/Facebook_LearnOpenCV)\n  * [ ](https://www.instagram.com/learnopencv/)\n  * [ ](https://www.linkedin.com/in/satyamallick/)\n  * [ ](https://x.com/LearnOpenCV)\n  * [ ](https://www.youtube.com/c/LearnOpenCV?utm_source=lopcv&utm_medium=footer&utm_campaign=yt)\n\n\n[ ![](../images/body-ab5d272e3cb213d5eb46.png) Click here to download the source code to this post ](javascript:void\\(0\\))\n",
  "metadata": {
    "title": "Pre Trained Models for Image Classification - PyTorch",
    "description": "Pre trained models for Image Classification - How we can use TorchVision module to load pre-trained models and carry out model inference to classify an image.",
    "keywords": "torchvision,image classification,pytorch,tutorial,alexnet,resnet,how to use a pretrained model in pytorch,pytorch models,pytorch pretrained models example,torchvision models,using pretrained model in pytorch",
    "author": "Vishwesh Shrimali",
    "og:locale": "en_US",
    "og:site_name": "LearnOpenCV – Learn OpenCV, PyTorch, Keras, Tensorflow with code, & tutorials",
    "og:type": "article",
    "og:title": "Pre Trained Models for Image Classification - PyTorch",
    "og:description": "Pre trained models for Image Classification - How we can use TorchVision module to load pre-trained models and carry out model inference to classify an image.",
    "og:url": "https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/",
    "og:image": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095459/Using_Pre-trained_Models_with_PyTorch.png",
    "og:image:secure_url": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095459/Using_Pre-trained_Models_with_PyTorch.png",
    "og:image:width": "1200",
    "og:image:height": "628",
    "twitter:card": "summary",
    "twitter:site": "@AiOpencv",
    "twitter:title": "Pre Trained Models for Image Classification - PyTorch",
    "twitter:description": "Pre trained models for Image Classification - How we can use TorchVision module to load pre-trained models and carry out model inference to classify an image.",
    "twitter:creator": "@AiOpencv",
    "twitter:image": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095459/Using_Pre-trained_Models_with_PyTorch.png",
    "article:published_time": "2019-06-03T12:00:37+00:00",
    "article:modified_time": "2024-07-18T11:01:22+00:00",
    "article:publisher": "https://www.facebook.com/Learnopencv-277284889389059"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2024/02/04103923/learnOpenCV_by_bigvision1.png",
      "bytes": 6527,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/000.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2024/02/04103923/learnOpenCV_by_bigvision1.png",
      "alt": "",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095459/Using_Pre-trained_Models_with_PyTorch-768x402.png",
      "bytes": 32440,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/006.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095459/Using_Pre-trained_Models_with_PyTorch-768x402.png",
      "alt": "",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095456/Model_Timeline.png",
      "bytes": 25251,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/010.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095456/Model_Timeline.png",
      "alt": "State of the art Model timeline - pre trained models for Image Classification",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2018/05/04100023/AlexNet-1.png",
      "bytes": 19218,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/012.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2018/05/04100023/AlexNet-1.png",
      "alt": "AlexNet Architecture",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2019/05/04095506/dog.jpg",
      "bytes": 78949,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/015.jpg",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2019/05/04095506/dog.jpg",
      "alt": "Sample test image",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2021/01/04094114/strawberries.jpg",
      "bytes": 46103,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/017.jpg",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2021/01/04094114/strawberries.jpg",
      "alt": "",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2021/01/04094113/automotive.jpg",
      "bytes": 94575,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/019.jpg",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2021/01/04094113/automotive.jpg",
      "alt": "",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095503/Accuracy-Comparison-of-Models.png",
      "bytes": 5683,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/021.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095503/Accuracy-Comparison-of-Models.png",
      "alt": "Accuracy Comparison of Models",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095502/Model-Inference-Time-Comparison-on-CPU-ms-Lower-is-better-.png",
      "bytes": 6786,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/024.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095502/Model-Inference-Time-Comparison-on-CPU-ms-Lower-is-better-.png",
      "alt": "Inference time on CPU comparison",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095502/Model-Inference-Time-Comparison-on-GPU-ms-Lower-is-better-.png",
      "bytes": 6782,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/027.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095502/Model-Inference-Time-Comparison-on-GPU-ms-Lower-is-better-.png",
      "alt": "Inference time on GPU comparison",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095502/Model-Size-Comparison.png",
      "bytes": 6176,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/030.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095502/Model-Size-Comparison.png",
      "alt": "Model size comparison",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095501/Pre-Trained-Model-Comparison-1024x633.png",
      "bytes": 25690,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/032.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095501/Pre-Trained-Model-Comparison-1024x633.png",
      "alt": "Comparison of all models using accuracy speed and model size",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://opencv.org/university/wp-content/uploads/sites/4/2023/04/Dr-Satya.jpg",
      "bytes": 17778,
      "reused": false,
      "content_type": "image/jpeg",
      "local_path": "attachments/e1dddcdf5067/images/body-fe65e1f717d94c873fd9.jpg",
      "src": "https://opencv.org/university/wp-content/uploads/sites/4/2023/04/Dr-Satya.jpg",
      "alt": "Satya Mallick",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2026/07/16015539/decoding-virat-kohli-flick-shot-3d-motion-reconstruction.png",
      "bytes": 1911733,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/036.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2026/07/16015539/decoding-virat-kohli-flick-shot-3d-motion-reconstruction.png",
      "alt": "Decoding Virat Kohli’s Flick Shot: AI-Based 3D Motion Reconstruction",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2026/07/11002616/OpenCV_5_Blog_Thumbnail_final-scaled.jpg",
      "bytes": 516266,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/037.jpg",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2026/07/11002616/OpenCV_5_Blog_Thumbnail_final-scaled.jpg",
      "alt": "How to Run Object Detection with OpenCV 5",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2026/06/17235330/YOLO26_Finetuning_Thumbnail_image-768x512.jpg",
      "bytes": 85542,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/038.jpg",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2026/06/17235330/YOLO26_Finetuning_Thumbnail_image-768x512.jpg",
      "alt": "How to Fine-Tune YOLO26 for Safety Gear and Sign Language Detection",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://cdn.learnopencv.com/wp-content/uploads/2025/12/04080755/learnOpenCV_by_bigvision1.png",
      "bytes": 6316,
      "reused": true,
      "local_path": "attachments/e1dddcdf5067/images/039.png",
      "src": "https://cdn.learnopencv.com/wp-content/uploads/2025/12/04080755/learnOpenCV_by_bigvision1.png",
      "alt": "",
      "downloaded": true,
      "discovery": "saved_markdown"
    },
    {
      "status": "ok",
      "url": "https://learnopencv.com/wp-content/uploads/2021/10/cropped-favicon-512x512-1.png",
      "bytes": 22729,
      "reused": false,
      "content_type": "image/png",
      "local_path": "attachments/e1dddcdf5067/images/body-ab5d272e3cb213d5eb46.png",
      "src": "https://learnopencv.com/wp-content/uploads/2021/10/cropped-favicon-512x512-1.png",
      "alt": "",
      "downloaded": true,
      "discovery": "saved_markdown"
    }
  ],
  "audio": [],
  "videos": [
    {
      "url": "https://www.youtube.com/c/LearnOpenCV?utm_source=lopcv&utm_medium=footer&utm_campaign=yt",
      "kind": "channel_reference",
      "status": "reference_only"
    }
  ],
  "links": [
    {
      "type": "internal",
      "href": "https://learnopencv.com"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/category/install/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/getting-started-with-opencv/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/getting-started-with-pytorch/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/getting-started-with-tensorflow-keras/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/about/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/category/deep-learning/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/author/vishwesh/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/category/getting-started-with-pytorch/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/category/image-classification/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/category/pytorch/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/category/tutorial/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/pytorch-for-beginners-basics/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/image-classification-using-transfer-learning-in-pytorch/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/pytorch-model-inference-using-onnx-and-caffe2/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/faster-r-cnn-object-detection-with-pytorch"
    },
    {
      "type": "internal",
      "href": "https://learnopencv.com/mask-r-cnn-instance-segmentation-with-pytorch/"
    },
    {
      "type": "external",
      "href": "https://opencv.org/university/free-courses/"
    },
    {
      "type": "external",
      "href": "https://opencv.org/university/vision-language-model-bootcamp/"
    },
    {
      "type": "external",
      "href": "https://opencv.org/university/free-pytorch-course/"
    },
    {
      "type": "external",
      "href": "https://opencv.org/university/free-tensorflow-keras-course/"
    },
    {
      "type": "external",
      "href": "https://opencv.org/university/free-opencv-course/"
    },
    {
      "type": "external",
      "href": "https://opencv.org/university/python-for-beginners/"
    },
    {
      "type": "external",
      "href": "https://bigvision.ai/expert-ai-solution-builders/"
    },
    {
      "type": "external",
      "href": "https://bigvision.ai/expert-ai-solution-builders"
    },
    {
      "type": "external",
      "href": "https://download.pytorch.org/models/alexnet-owt-"
    },
    {
      "type": "external",
      "href": "javascript:void(0)"
    },
    {
      "type": "external",
      "href": "https://bigvision.ai/case-studies/"
    },
    {
      "type": "external",
      "href": "https://bigvision.ai/contact-us/"
    },
    {
      "type": "external",
      "href": "https://bit.ly/Facebook_LearnOpenCV"
    },
    {
      "type": "external",
      "href": "https://www.instagram.com/learnopencv/"
    },
    {
      "type": "external",
      "href": "https://www.linkedin.com/in/satyamallick/"
    },
    {
      "type": "external",
      "href": "https://x.com/LearnOpenCV"
    },
    {
      "type": "external",
      "href": "https://www.youtube.com/c/LearnOpenCV"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-20T17:24:02Z",
  "source_url": "https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/",
  "delivery_status": "unverified",
  "discovery_status": "http_403",
  "missing_reason": "http_403",
  "repair_error": "",
  "actual_video_files": [],
  "expected_saved_body_images": 18,
  "image_discovery": "saved_markdown",
  "source_body_markdown": "We use essential cookies to make our site work. With your consent, we may also use non-essential cookies to improve user experience and analyze website traffic. By clicking “Accept,” you agree to our website's cookie use as described in our Cookie Policy. You can change your cookie settings at any time by clicking “Preferences.” \nAccept\n[ ![](https://cdn.learnopencv.com/wp-content/uploads/2024/02/04103923/learnOpenCV_by_bigvision1.png) ](https://learnopencv.com)\n  * [Getting Started](https://learnopencv.com/category/install/)\n    * [Subscribe](https://learnopencv.com/)\n    * [Installation](https://learnopencv.com/category/install/)\n    * [Getting Started with OpenCV](https://learnopencv.com/getting-started-with-opencv/)\n    * [PyTorch](https://learnopencv.com/getting-started-with-pytorch/)\n    * [Tensorflow & Keras](https://learnopencv.com/getting-started-with-tensorflow-keras/)\n  * [Free Courses](https://opencv.org/university/free-courses/?utm_source=lopcv&utm_medium=menu&utm_campaign=fcpage)\n    * [VLM Bootcamp](https://opencv.org/university/vision-language-model-bootcamp/?utm_source=lopcv&utm_medium=menu&utm_campaign=vlm)\n    * [PyTorch Bootcamp](https://opencv.org/university/free-pytorch-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=ptbc)\n    * [TensorFlow Keras Course](https://opencv.org/university/free-tensorflow-keras-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=tbc)\n    * [OpenCV Course](https://opencv.org/university/free-opencv-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=obc)\n    * [Python for Beginners](https://opencv.org/university/python-for-beginners/?utm_source=lopcv&utm_medium=menu&utm_campaign=pbc)\n  * [AI Consulting](https://bigvision.ai/expert-ai-solution-builders/#talk?utm_source=locv&utm_medium=menu)\n  * [About](https://learnopencv.com/about/)\n\n\n  * [Getting Started](https://learnopencv.com/category/install/)\n    * [Subscribe](https://learnopencv.com/)\n    * [Installation](https://learnopencv.com/category/install/)\n    * [Getting Started with OpenCV](https://learnopencv.com/getting-started-with-opencv/)\n    * [PyTorch](https://learnopencv.com/getting-started-with-pytorch/)\n    * [Tensorflow & Keras](https://learnopencv.com/getting-started-with-tensorflow-keras/)\n  * [Free Courses](https://opencv.org/university/free-courses/?utm_source=lopcv&utm_medium=menu&utm_campaign=fcpage)\n    * [VLM Bootcamp](https://opencv.org/university/vision-language-model-bootcamp/?utm_source=lopcv&utm_medium=menu&utm_campaign=vlm)\n    * [PyTorch Bootcamp](https://opencv.org/university/free-pytorch-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=ptbc)\n    * [TensorFlow Keras Course](https://opencv.org/university/free-tensorflow-keras-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=tbc)\n    * [OpenCV Course](https://opencv.org/university/free-opencv-course/?utm_source=lopcv&utm_medium=menu&utm_campaign=obc)\n    * [Python for Beginners](https://opencv.org/university/python-for-beginners/?utm_source=lopcv&utm_medium=menu&utm_campaign=pbc)\n  * [AI Consulting](https://bigvision.ai/expert-ai-solution-builders/#talk?utm_source=locv&utm_medium=menu)\n  * [About](https://learnopencv.com/about/)\n\n\nSearch ...\nResults\nSee all results\n  * [Home](https://learnopencv.com \"Home\")\n  * >\n  * [Deep Learning](https://learnopencv.com/category/deep-learning/ \"Deep Learning\")\n  * >\n  * Pre Trained Models for Image Classification – PyTorch for Beginners\n\n\n[Vishwesh Shrimali](https://learnopencv.com/author/vishwesh/)\n  * on  June 3, 2019\n\n\n# Pre Trained Models for Image Classification – PyTorch for Beginners\nHi! This post is part of our PyTorch series. In the previous post, Pytorch Tutorial for beginners, we discussed PyTorch, it’s strengths and why you should learn it. We also had a brief look at Tensors – the core data structure used in PyTorch. In this article, we will jump\n  * [Deep Learning](https://learnopencv.com/category/deep-learning/), [Getting Started with PyTorch](https://learnopencv.com/category/getting-started-with-pytorch/), [Image Classification](https://learnopencv.com/category/image-classification/), [PyTorch](https://learnopencv.com/category/pytorch/), [Tutorial](https://learnopencv.com/category/tutorial/)\n\n\n![](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095459/Using_Pre-trained_Models_with_PyTorch-768x402.png)\n  *   *   * [ ](https://bigvision.ai/expert-ai-solution-builders?utm_source=lopcv&utm_medium=post-icon)\n\n\nHi! This post is part of our PyTorch series.  \n| PyTorch for Beginners |  \n| --- |  \n| [PyTorch for Beginners: Basics](https://learnopencv.com/pytorch-for-beginners-basics/) |  \n| [PyTorch for Beginners: Image Classification using Pre-trained models](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/) |  \n| [Image Classification using Transfer Learning in PyTorch](https://learnopencv.com/image-classification-using-transfer-learning-in-pytorch/) |  \n| [PyTorch Model Inference using ONNX and Caffe2](https://learnopencv.com/pytorch-model-inference-using-onnx-and-caffe2/) |  \n| [PyTorch for Beginners: Semantic Segmentation using torchvision](https://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/) |  \n|  [Object Detection](https://learnopencv.com/faster-r-cnn-object-detection-with-pytorch)  \n |  \n|  [Instance Segmentation](https://learnopencv.com/mask-r-cnn-instance-segmentation-with-pytorch/)  \n |  \nIn the previous post, [Pytorch Tutorial for beginners](https://learnopencv.com/pytorch-for-beginners-basics/), we discussed PyTorch, it’s strengths and why you should learn it. We also had a brief look at Tensors – the core data structure used in PyTorch. In this article, we will jump into some hands-on examples of using pre-trained networks present in **TorchVision** module – pre trained models for **Image Classification**.\n**Torchvision** package consists of popular datasets, model architectures, and common image transformations for computer vision. Basically, if you are into Computer Vision and using PyTorch, Torchvision will be of great help.!\n## 1. Pre trained Models for Image Classification\nPre-trained models are Neural Network models trained on large benchmark datasets like ImageNet. The Deep Learning community has greatly benefitted from these open-source models. Also, the pre-trained models are a major factor for rapid advances in Computer Vision research. Other researchers and practitioners can use these state-of-the-art models instead of re-inventing everything from scratch.\nGiven below is a rough timeline of how the state-of-the-art models have improved over time. We have included only those models which are present in the Torchvision package.\n[![State of the art Model timeline - pre trained models for Image Classification](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095456/Model_Timeline.png)](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095456/Model_Timeline.png)\nBefore we jump into the details of how we can use pre-trained models for image classification, let’s see what the various pre-trained models available are. We will discuss **AlexNet** and **ResNet101** as the two main examples here. Both the networks have been trained on **ImageNet dataset**.\nImageNet dataset has over **14 million images** maintained by **Stanford University**. It is extensively used for a large variety of Image related deep learning projects. The images belong to various **classes** or **labels**. Even though we can use both terms interchangeably, we will stick to **classes**. The aim of the pre-trained models like AlexNet and ResNet101 is to take an image as an input and predict it’s class.\nThe word pre-trained here means that the deep learning architectures **AlexNet** and **ResNet101** , for instance, have been already trained on some (huge) dataset and thus carry the resultant weights and biases with them. This difference between **architecture** and **weights and biases** should be very clear because as we will see in the next section, TorchVision has both the architectures and the pre-trained models.\n### 1.1. Model Inference Process\nSince we will focus on how to use the pre-trained models for predicting the class (label) of input, let’s also discuss the process involved in this. This process is referred to as **Model Inference**. The entire process consists of the following main steps.\n  1. Reading the input image\n  2. Performing transformations on the image. For example – resize, center crop, normalization, etc.\n  3. Forward Pass: Use the pre-trained weights to find out the output vector. Each element in this output vector describes the **confidence** with which the model predicts the input image belongs to a particular class.\n  4. Based on the scores obtained (elements of the output vector mentioned in step 3), display the predictions.\n\n\n### 1.2. Loading Pre-Trained Network using TorchVision\nNow that we are equipped with the knowledge of model inference and know what a pre-trained model means, let’s see how we can use them with the help of **TorchVision** module.\nFirst, let’s install the **TorchVision** module using the command given below.  \n| 1  |  `pip ``install` `torchvision`  |  \n| --- | --- |  \nNext, let’s import **models** from **torchvision** module and see the different models and architectures available to us.  \n|  1 2 3 4  |  `from torchvision import models` `import torch` `dir(models)`  |  \n| --- | --- |  \nNot so fast! Carefully observe the output that we got above.  \n|  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22  |  `['AlexNet',` `'DenseNet',` `'GoogLeNet',` `'Inception3',` `'MobileNetV2',` `'ResNet',` `'ShuffleNetV2',` `'SqueezeNet',` `'VGG',` `...` `'alexnet',` `'densenet',` `'densenet121',` `'densenet161',` `'densenet169',` `'densenet201',` `'detection',` `'googlenet',` `'inception',` `'inception_v3',` `...` `]`  |  \n| --- | --- |  \nNotice that there is one entry called **AlexNet** and one called **alexnet**. The capitalized name refers to the Python class (**AlexNet**) whereas **alexnet** is a convenience function that returns the model instantiated from the **AlexNet** class. It’s also possible for these convenience functions to have different parameter sets. For example, **densenet121** , **densenet161** , **densenet169** , **densenet201,** are all instances of **DenseNet** class but with a different number of layers – 121,161,169 and 201, respectively.\n### 1.3. Using AlexNet for Image Classification\nLet’s first start with **AlexNet**. It is one of the early breakthrough networks in Image Recognition. If you are interested in learning about AlexNet’s architecture, you can check out our post on **[Understanding AlexNet](https://learnopencv.com/understanding-alexnet/).**\n![AlexNet Architecture](https://cdn.learnopencv.com/wp-content/uploads/2018/05/04100023/AlexNet-1.png)AlexNet Architecture\n#### **Step 1: Load the pre-trained model**\nIn the first step, we will create an instance of the network. We’ll also pass an argument so the function download the weights of the model.  \n|  1 2 3 4  |  `alexnet ``=` `models.alexnet(pretrained``=``True``)` `# You will see a similar output as below` `# Downloading: \"https://download.pytorch.org/models/alexnet-owt-[](https://download.pytorch.org/models/alexnet-owt-) 4df8aa71.pth\" to /home/hp/.cache/torch/checkpoints/alexnet-owt-4df8aa71.pth`  |  \n| --- | --- |  \nNote that usually the PyTorch models have an extension of **.pt** or **.pth**\nOnce the weights have been downloaded, we can proceed with the other steps. We can also check out some details of the network’s architecture as follows.  \n| 1  |  `print``(alexnet)`  |  \n| --- | --- |  \nDon’t worry about the overflowing output. This basically states the various operations and layers in the AlexNet architecture.\n#### **Step 2: Specify image transformations**\nOnce we have the model with us, the next step is to transform the input image so that they have the right shape and other characteristics like mean and standard deviation. These values should be similar to those used while training the model. This makes sure that the network will produce meaningful answers.\nWe can pre-process the input image with the help of **transforms** present in **TochVision** module. In this case, we can use the following transforms for **both** AlexNet and ResNet.\n**Download Code** To easily follow along this tutorial, please download code by clicking on the button below. It's FREE!\n[Download Code](javascript:void\\(0\\))  \n|  1 2 3 4 5 6 7 8 9  |  `from` `torchvision ``import` `transforms` `transform ``=` `transforms.Compose([            ``#[1]` `transforms.Resize(``256``),                    ``#[2]` `transforms.CenterCrop(``224``),                ``#[3]` `transforms.ToTensor(),                     ``#[4]` `transforms.Normalize(                      ``#[5]` `mean``=``[``0.485``, ``0.456``, ``0.406``],                ``#[6]` `std``=``[``0.229``, ``0.224``, ``0.225``]                  ``#[7]` `)])`  |  \n| --- | --- |  \nLet’s try to understand what happened in the above code snippet.\n**Line [1]** : Here we are defining a variable **transform** which is a combination of all the image transformations to be carried out on the input image.\n**Line [2]:** Resize the image to 256×256 pixels.\n**Line [3]** : Crop the image to 224×224 pixels about the center.\n**Line [4]** : Convert the image to PyTorch Tensor data type.\n**Line [5-7]:** Normalize the image by setting its mean and standard deviation to the specified values.\n#### **Step 3: Load the input image and pre-process it**\nNext, let’s load the input image and carry out the image transformations specified above. Note that we will use **Pillow** (PIL) module extensively with TorchVision as it’s the default image backend supported by TorchVision.  \n|  1 2 3  |  `# Import Pillow` `from` `PIL ``import` `Image` `img ``=` `Image.``open``(``\"dog.jpg\"``)`  |  \n| --- | --- |  \n![Sample test image](https://cdn.learnopencv.com/wp-content/uploads/2019/05/04095506/dog.jpg)A Yellow Labrador (Source: Wikipedia)\nNext, pre-process the image and prepare a batch to be passed through the network.  \n|  1 2  |  `img_t ``=` `transform(img)` `batch_t ``=` `torch.unsqueeze(img_t, ``0``)`  |  \n| --- | --- |  \n#### **Step 4: Model Inference**\nFinally, it’s time to use the pre-trained model to see what the model thinks the image is.\nFirst, we need to put our model in **eval** mode.  \n| 1  |  `alexnet.``eval``()`  |  \n| --- | --- |  \nNext, let’s carry out the inference.  \n|  1 2  |  `out ``=` `alexnet(batch_t)` `print``(out.shape)`  |  \n| --- | --- |  \nThis is all good but what do we do with this output vector **out** with **1000 elements**? We still haven’t got the **image’s class (or label)**. For this, we will first read and store the labels from a text file having a list of all the 1000 labels. Note that the line number specified the class number, so it’s very important to ensure that you don’t change that order.  \n|  1 2  |  `with open('imagenet_classes.txt') as f:` `classes = [line.strip() for line in f.readlines()]`  |  \n| --- | --- |  \nSince AlexNet and ResNet have been trained on the same ImageNet dataset, we can use the same **classes** list for both models.\nNow, we need to find the index where the maximum score in output vector **out** occurs. We will use this index to find out the prediction.  \n|  1 2 3 4 5  |  `_, index ``=` `torch.``max``(out, ``1``)` `percentage ``=` `torch.nn.functional.softmax(out, dim``=``1``)[``0``] ``*` `100` `print``(labels[index[``0``]], percentage[index[``0``]].item())`  |  \n| --- | --- |  \nThere we go! The model predicts the image to be of a **Labrador Retriever** with a **41.58% confidence.**\nBut that sounds too low. Let’s see what other classes the model thought the image belonged to.  \n|  1 2  |  `_, indices ``=` `torch.sort(out, descending``=``True``)` `[(labels[idx], percentage[idx].item()) ``for` `idx ``in` `indices[``0``][:``5``]]`  |  \n| --- | --- |  \nAnd here are the outputs:  \n|  1 2 3 4 5  |  `[('Labrador retriever', 41.585166931152344),` `('golden retriever', 16.59166145324707),` `('Saluki, gazelle hound', 16.286880493164062),` `('whippet', 2.8539133071899414),` `('Ibizan hound, Ibizan Podenco', 2.3924720287323)]`  |  \n| --- | --- |  \nIf you are not aware, all these are dog breeds. So the model managed to predict that it was a dog with a fairly high confidence but it was not very sure about the breed of the dog.\nLet’s try the same thing for an image of strawberries and a car and see the ouputs we get.\n![](https://cdn.learnopencv.com/wp-content/uploads/2021/01/04094114/strawberries.jpg)Strawberries\nHere is the output obtained for the above image of strawberries. As we can see, the class with highest score was “Strawberries” with a score of close to 99.99%.  \n|  1 2 3 4 5  |  `[('strawberry', 99.99365997314453),` `('custard apple', 0.001047826954163611),` `('banana', 0.0008201944874599576),` `('orange', 0.0007371827960014343),` `('confectionery, confectionary, candy store', 0.0005758354091085494)]`  |  \n| --- | --- |  \n![](https://cdn.learnopencv.com/wp-content/uploads/2021/01/04094113/automotive.jpg)Passenger car\nSimilarly, for the the image of the car given above, the output is as follows.  \n|  1 2 3 4 5 6  |  `[('cab, hack, taxi, taxicab', 33.30569839477539),` `('sports car, sport car', 14.424001693725586),` `('racer, race car, racing car', 10.685123443603516),` `('beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon',` `7.846532821655273),` `('passenger car, coach, carriage', 6.985556125640869)]`  |  \n| --- | --- |  \nThat’s it! All it takes is these 4 steps to carry out image classification using pre-trained models.\nHow about we try the same with ResNet?\n### 1.4. Using ResNet for Image Classification\nWe will use **resnet101** – a 101 layer Convolutional Neural Network. resnet101 has about **44.5 million parameters** tuned during the training process. That’s huge!\nLet’s quickly go through the steps required to use resnet101 for image classification.  \n|  1 2 3 4 5 6 7 8 9 10 11 12 13  |  `# First, load the model` `resnet ``=` `models.resnet101(pretrained``=``True``)` `# Second, put the network in eval mode` `resnet.``eval``()` `# Third, carry out model inference` `out ``=` `resnet(batch_t)` `# Forth, print the top 5 classes predicted by the model` `_, indices ``=` `torch.sort(out, descending``=``True``)` `percentage ``=` `torch.nn.functional.softmax(out, dim``=``1``)[``0``] ``*` `100` `[(labels[idx], percentage[idx].item()) ``for` `idx ``in` `indices[``0``][:``5``]]`  |  \n| --- | --- |  \nAnd here is what **resnet101** predicted.  \n|  1 2 3 4 5  |  `[('Labrador retriever', 48.25556945800781),` `('dingo, warrigal, warragal, Canis dingo', 7.900787353515625),` `('golden retriever', 6.916920185089111),` `('Eskimo dog, husky', 3.6434383392333984),` `('bull mastiff', 3.0461232662200928)]`  |  \n| --- | --- |  \nJust like AlexNet, ResNet managed to predict that it was a dog with very high confidence and predicted that it was a **Labrador Retriever** with a **48.25%** confidence. That’s pretty good!\n## 2. Model Comparison\nSo far we have discussed how we can use pre-trained models to perform image classification but one question that we have yet to answer is how do we decide which model to choose for a particular task. In this section we will compare the pre-trained models based on the following criteria:\n  1. **Top-1 Error** : A top-1 error occurs if the class predicted by a model with the highest confidence is **not** the same as the true class.\n  2. **Top-5 Error** : A top-5 error occurs when the true class is not among the top 5 classes predicted by a model (sorted in terms of confidence).\n  3. **Inference Time on CPU** : Inference time is the time taken for the model inference step.\n  4. **Inference Time on GPU**\n  5. **Model size** : Here size stands for the physical space occupied by the **.pth** file of the pre-trained model supplied by PyTorch\n\n\nA **good** model will have **low Top-1 error** , **low Top-5 error** , **low inference time** on CPU and GPU and **low model size**.\nAll the experiments were performed on the same input image and multiple times so that the average of all the results for a particular model can be analyzed. The experiments were performed on Google Colab. Now, let’s have a look at the results obtained.\n### 2.1. Accuracy Comparison of Models\nThe first criterion we will discuss consists of Top-1 and Top-5 errors. Top-1 error is when the top predicted class differs from the ground truth. Since the problem is rather a difficult one, there is another error measure called Top-5 error. A prediction is classified as an error if none of the top-5 predicted classes are correct.\n![Accuracy Comparison of Models](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095503/Accuracy-Comparison-of-Models.png)\nNotice from the graph that both errors follow a similar trend. **AlexNet** was the first attempt based on Deep Learning and there has been improvement in the error since then. **Notable** mentions are GoogLeNet, ResNet, VGGNet, ResNext.\n### 2.2. Inference Time Comparison\nNext, we will compare the models based on the time taken for model inference. One image was supplied to each model multiple times and the inference time for all the iterations was averaged. Similar process was performed for CPU and then for GPU on Google Colab. Even though there are some variations in the order, we can see that **SqueezeNet** , **ShuffleNet** and **ResNet-18** had a really low inference time, which is exactly what we want.\n![Inference time on CPU comparison](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095502/Model-Inference-Time-Comparison-on-CPU-ms-Lower-is-better-.png)  \n![Inference time on GPU comparison](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095502/Model-Inference-Time-Comparison-on-GPU-ms-Lower-is-better-.png)\n### 2.3. Model Size Comparison\nA lot of times when we are using a Deep Learning model on an android or iOS device, the model size becomes a deciding factor, sometimes even more important than accuracy. **SqueezeNet** has the minimum model size (5 MB), followed by **ShuffleNet V2** (6 MB) and **MobileNet V2** (14 MB). It’s obvious why these models are preferred in mobile apps utilizing deep learning.\n![Model size comparison](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095502/Model-Size-Comparison.png)\n## 2.4. Overall Comparison\nWe discussed which model performed better based on a particular criterion. We can squeeze all those important details in one bubble chart which we can then refer to for deciding which model to go for based on our requirements.\nThe x-coordinate we are using is Top-1 error (**lower is better**). The y-coordinate is the inference time on GPU in milliseconds (**lower is better**). The bubble size represents the model size (**lower is better**).\n#### NOTE :\n  * Smaller Bubbles are better in terms of model size.\n  * Bubbles near the origin are better in terms of both Accuracy and Speed.\n\n[![Comparison of all models using accuracy speed and model size](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095501/Pre-Trained-Model-Comparison-1024x633.png)](https://cdn.learnopencv.com/wp-content/uploads/2019/06/04095501/Pre-Trained-Model-Comparison.png)\n### From idea to working model to real-time deployment\nBig Vision takes computer vision projects through the full journey, not just the easy parts.\n![Satya Mallick](https://opencv.org/university/wp-content/uploads/sites/4/2023/04/Dr-Satya.jpg)\n**Satya Mallick** Founder - Big Vision & LearnOpenCV\n[ Talk to our Team ](https://bigvision.ai/expert-ai-solution-builders/)\n## 3. Final Verdict\n  * It is clear from the above graph that **ResNet50** is the best model in terms of all three parameters ( small in size and closer to origin )\n  * DenseNets and ResNext101 are expensive on inference time.\n  * AlexNet and SqueezeNet have pretty high error rate.\n\n\n* * *\nWell, that’s all, folks! In this post, we covered how we can use the TorchVision module to carry out Image Classification using pre-trained models – a 4-step process. We also made model comparisons to decide what model to choose depending on our project requirements. In the next post, we will cover how to use transfer learning to train a model on a custom dataset using PyTorch.\nAlso read: PyTorch for Beginners: [Semantic Segmentation using torchvision](https://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/)\n* * *\nWas This Article Helpful?\nPost Views: 268\n## Subscribe & Download Code\nIf you liked this article and would like to download code (C++ and Python) and example images used in this post, please [click here](javascript:void\\(0\\)). In our newsletter, we share Computer Vision and AI tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.  \n  \n\n[ Download Example Code](javascript:void\\(0\\))\n  \n  \n\n[PrevPreviousPyTorch Tutorial For Beginners: All the Basics](https://learnopencv.com/pytorch-for-beginners-basics/)\n[NextTorchvision Semantic Segmentation – PyTorch for BeginnersNext](https://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/)\n![Satya Mallick](https://opencv.org/university/wp-content/uploads/sites/4/2023/04/Dr-Satya.jpg)\n#### Satya Mallick, Ph.D.\nFounder - Big Vision & LearnOpenCV\n#####  Are you struggling with your Computer Vision Product Development?\nHelping startups and enterprises build and deploy scalable solutions that work in the real world.\n[ Schedule a Call ](https://bigvision.ai/expert-ai-solution-builders?utm_source=lopcv&utm_medium=post)\nGetting Started with OpenCV \n0/15\n  * [ Read, Display and Write an Image using OpenCV ](https://learnopencv.com/read-display-and-write-an-image-using-opencv/)\n  * [ Image Resizing with OpenCV ](https://learnopencv.com/image-resizing-with-opencv/)\n  * [ Cropping an Image using OpenCV ](https://learnopencv.com/cropping-an-image-using-opencv/)\n  * [ Image Translation and Rotation Using OpenCV ](https://learnopencv.com/image-rotation-and-translation-using-opencv/)\n  * [ Annotating Images Using OpenCV ](https://learnopencv.com/annotating-images-using-opencv/)\n  * [ Color spaces in OpenCV (C++ / Python) ](https://learnopencv.com/color-spaces-in-opencv-cpp-python/)\n  * [ Image Filtering Using Convolution in OpenCV ](https://learnopencv.com/image-filtering-using-convolution-in-opencv/)\n  * [ Image Thresholding in OpenCV ](https://learnopencv.com/opencv-threshold-python-cpp/)\n  * [ Blob Detection Using OpenCV ( Python, C++ ) ](https://learnopencv.com/blob-detection-using-opencv-python-c/)\n  * [ Edge Detection Using OpenCV ](https://learnopencv.com/edge-detection-using-opencv/)\n  * [ Mouse and Trackbar in OpenCV GUI ](https://learnopencv.com/mouse-and-trackbar-in-opencv-gui/)\n  * [ Contour Detection using OpenCV (Python/C++) ](https://learnopencv.com/contour-detection-using-opencv-python-c/)\n  * [ Simple Background Estimation in Videos using OpenCV (C++/Python) ](https://learnopencv.com/simple-background-estimation-in-videos-using-opencv-c-python/)\n  * [ Deep Learning with OpenCV DNN Module: A Definitive Guide ](https://learnopencv.com/deep-learning-with-opencvs-dnn-module-a-definitive-guide/)\n  * [ Reading and Writing Videos using OpenCV ](https://learnopencv.com/reading-and-writing-videos-using-opencv/)\n\n\nGetting Started with PyTorch \n1/6\n  * [ PyTorch Tutorial For Beginners: All the Basics ](https://learnopencv.com/pytorch-for-beginners-basics/)\n  * [ Pre Trained Models for Image Classification – PyTorch for Beginners ](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/)\n  * [ Torchvision Semantic Segmentation – PyTorch for Beginners ](https://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/)\n  * [ Transfer Learning For Pytorch Image Classification ](https://learnopencv.com/image-classification-using-transfer-learning-in-pytorch/)\n  * [ Faster R-CNN Object Detection with PyTorch ](https://learnopencv.com/faster-r-cnn-object-detection-with-pytorch/)\n  * [ Mask RCNN Pytorch – Instance Segmentation ](https://learnopencv.com/mask-r-cnn-instance-segmentation-with-pytorch/)\n\n\nGetting started with Tensorflow & Keras \n0/11\n  * [ Getting Started with Neural Networks: A 30,000-Foot View for Beginners ](https://learnopencv.com/neural-networks-a-30000-feet-view-for-beginners/)\n  * [ Training Neural Networks for Beginners ](https://learnopencv.com/how-to-train-neural-networks-for-beginners/)\n  * [ Tensorflow & Keras Tutorial: Linear Regression ](https://learnopencv.com/tensorflow-keras-tutorial-linear-regression/)\n  * [ Understanding Feedforward Neural Networks ](https://learnopencv.com/understanding-feedforward-neural-networks/)\n  * [ Implementing an MLP in TensorFlow & Keras ](https://learnopencv.com/implementing-mlp-tensorflow-keras/)\n  * [ Convolutional Neural Network (CNN): A Complete Guide ](https://learnopencv.com/understanding-convolutional-neural-networks-cnn/)\n  * [ Implementing a CNN in TensorFlow & Keras ](https://learnopencv.com/implementing-cnn-tensorflow-keras/)\n  * [ Image Classification using Pre-Trained ImageNet Models in TensorFlow & Keras ](https://learnopencv.com/image-classification-pretrained-imagenet-models-tensorflow-keras/)\n  * [ Unlock the Power of Fine-Tuning Pre-Trained Models in TensorFlow & Keras ](https://learnopencv.com/fine-tuning-pre-trained-models-tensorflow-keras/)\n  * [ A Step-by-Step Tutorial on Image Segmentation using Tensorflow Hub ](https://learnopencv.com/image-segmentation-tensorflow-hub/)\n  * [ Object Detection Made Easy with TensorFlow Hub: Step-by-Step Tutorial ](https://learnopencv.com/object-detection-tensorflow-hub/)\n\n\n####  Table of Contents\n  1. [1. Pre trained Models for Image Classification](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/#elementor-toc__heading-anchor-0)\n  2. [2. Model Comparison](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/#elementor-toc__heading-anchor-1)\n  3. [2.4. Overall Comparison](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/#elementor-toc__heading-anchor-2)\n  4. [3. Final Verdict](https://learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/#elementor-toc__heading-anchor-3)\n\n\nWas This Article Helpful?\n  * [alexnet](https://learnopencv.com/tag/alexnet/), [how to use a pretrained model in pytorch](https://learnopencv.com/tag/how-to-use-a-pretrained-model-in-pytorch/), [Image Classification](https://learnopencv.com/tag/image-classification/), [PyTorch](https://learnopencv.com/tag/pytorch/), [pytorch models](https://learnopencv.com/tag/pytorch-models/), [pytorch pretrained models example](https://learnopencv.com/tag/pytorch-pretrained-models-example/), [Resnet](https://learnopencv.com/tag/resnet/), [TorchVision](https://learnopencv.com/tag/torchvision/), [torchvision models](https://learnopencv.com/tag/torchvision-models/), [tutorial](https://learnopencv.com/tag/tutorial/), [using pretrained model in pytorch](https://learnopencv.com/tag/using-pretrained-model-in-pytorch/)\n\n\n## Read Next\n[ ![Decoding Virat Kohli’s Flick Shot: AI-Based 3D Motion Reconstruction](https://cdn.learnopencv.com/wp-content/uploads/2026/07/16015539/decoding-virat-kohli-flick-shot-3d-motion-reconstruction.png) ](https://learnopencv.com/decoding-virat-kohlis-flick-shot-ai-based-3d-motion-reconstruction/)\n[3D Computer Vision](https://learnopencv.com/category/3d-computer-vision/)[3D Reconstruction](https://learnopencv.com/category/3d-computer-vision/3d-reconstruction/)[Segmentation](https://learnopencv.com/category/segmentation/)[SpatialAI-Depth](https://learnopencv.com/category/3d-computer-vision/spatialai-depth/)[Video Analysis](https://learnopencv.com/category/video-analysis/)\n[Adnan Shaikh](https://learnopencv.com/author/adnan/) July 18, 2026 \n##  [ Decoding Virat Kohli’s Flick Shot: AI-Based 3D Motion Reconstruction ](https://learnopencv.com/decoding-virat-kohlis-flick-shot-ai-based-3d-motion-reconstruction/)\nReconstruct the 3D swing plane of Virat Kohli's flick shot from a single 2D video with off-the-shelf vision models. No…\n[ ![How to Run Object Detection with OpenCV 5](https://cdn.learnopencv.com/wp-content/uploads/2026/07/11002616/OpenCV_5_Blog_Thumbnail_final-scaled.jpg) ](https://learnopencv.com/how-to-run-object-detection-with-opencv-5/)\n[Object Detection](https://learnopencv.com/category/object-detection/)[OpenCV 5](https://learnopencv.com/category/opencv/opencv-5/)[OpenCV DNN](https://learnopencv.com/category/opencv-dnn/)[YOLO](https://learnopencv.com/category/yolo/)\n[Sudip Chakrabarty](https://learnopencv.com/author/sudip/) July 9, 2026 \n##  [ How to Run Object Detection with OpenCV 5 ](https://learnopencv.com/how-to-run-object-detection-with-opencv-5/)\nLearn about the new OpenCV 5 and how to use its DNN engine to run object detection models on the…\n[ ![How to Fine-Tune YOLO26 for Safety Gear and Sign Language Detection](https://cdn.learnopencv.com/wp-content/uploads/2026/06/17235330/YOLO26_Finetuning_Thumbnail_image-768x512.jpg) ](https://learnopencv.com/how-to-fine-tune-yolo26-for-safety-gear-and-sign-language-detection/)\n[Computer Vision](https://learnopencv.com/category/computer-vision/)[Object Detection](https://learnopencv.com/category/object-detection/)[YOLO](https://learnopencv.com/category/yolo/)\n[Sudip Chakrabarty](https://learnopencv.com/author/sudip/) June 18, 2026 \n##  [ How to Fine-Tune YOLO26 for Safety Gear and Sign Language Detection ](https://learnopencv.com/how-to-fine-tune-yolo26-for-safety-gear-and-sign-language-detection/)\nLearn how to fine-tune YOLO26 on your own data. We build a construction PPE safety detector and a sign-language reader…\n##  Subscribe to our Newsletter\nSubscribe to our email newsletter to get the latest posts delivered right to your email.\nName \nEmail \nSend\n![](https://cdn.learnopencv.com/wp-content/uploads/2025/12/04080755/learnOpenCV_by_bigvision1.png)\nEmpowering innovation through education, LearnOpenCV provides in-depth tutorials, code, and guides in AI, Computer Vision, and Deep Learning. Led by Dr. Satya Mallick, we're dedicated to nurturing a community keen on technology breakthroughs.\n[ Read More ](https://learnopencv.com/about/)\nCategories\n  * [Deep Learning](https://learnopencv.com/category/deep-learning/)\n  * [Object Detection](https://learnopencv.com/category/object-detection/)\n  * [Image Classification](https://learnopencv.com/category/image-classification)\n  * [YOLO](https://learnopencv.com/category/yolo/)\n  * [Image Processing](https://learnopencv.com/category/image-processing/)\n  * [Image Segmentation](https://learnopencv.com/category/image-segmentation/)\n\n\n  * [Deep Learning](https://learnopencv.com/category/deep-learning/)\n  * [Object Detection](https://learnopencv.com/category/object-detection/)\n  * [Image Classification](https://learnopencv.com/category/image-classification)\n  * [YOLO](https://learnopencv.com/category/yolo/)\n  * [Image Processing](https://learnopencv.com/category/image-processing/)\n  * [Image Segmentation](https://learnopencv.com/category/image-segmentation/)\n\n\nGetting Started\n  * [Installation](https://learnopencv.com/category/install/)\n  * [PyTorch](https://learnopencv.com/learn-pytorch/)\n  * [Getting Started with OpenCV](https://learnopencv.com/getting-started-with-opencv/)\n  * [Keras & Tensorflow](https://learnopencv.com/learn-keras-and-tensorflow/)\n\n\n  * [Installation](https://learnopencv.com/category/install/)\n  * [PyTorch](https://learnopencv.com/learn-pytorch/)\n  * [Getting Started with OpenCV](https://learnopencv.com/getting-started-with-opencv/)\n  * [Keras & Tensorflow](https://learnopencv.com/learn-keras-and-tensorflow/)\n\n\nBig Vision\n  * [AI Consulting](https://bigvision.ai/expert-ai-solution-builders/#talk?utm_source=locv&utm_medium=footer)\n  * [Case Studies](https://bigvision.ai/case-studies/?utm_source=locv&utm_medium=footer)\n  * [Get in Touch](https://bigvision.ai/contact-us/?utm_source=locv&utm_medium=footer)\n\n\n  * [AI Consulting](https://bigvision.ai/expert-ai-solution-builders/#talk?utm_source=locv&utm_medium=footer)\n  * [Case Studies](https://bigvision.ai/case-studies/?utm_source=locv&utm_medium=footer)\n  * [Get in Touch](https://bigvision.ai/contact-us/?utm_source=locv&utm_medium=footer)\n\n\nCopyright © 2026 – BIG VISION LLC [Privacy Policy](https://learnopencv.com/privacy-policy/) [Terms and Conditions](https://learnopencv.com/terms-and-conditions/)\n  * [ ](https://bit.ly/Facebook_LearnOpenCV)\n  * [ ](https://www.instagram.com/learnopencv/)\n  * [ ](https://www.linkedin.com/in/satyamallick/)\n  * [ ](https://x.com/LearnOpenCV)\n  * [ ](https://www.youtube.com/c/LearnOpenCV?utm_source=lopcv&utm_medium=footer&utm_campaign=yt)\n\n\n[ ![](https://learnopencv.com/wp-content/uploads/2021/10/cropped-favicon-512x512-1.png) Click here to download the source code to this post ](javascript:void\\(0\\))\n",
  "body_base_path": "attachments/e1dddcdf5067/pages/",
  "body_markdown_path": "attachments/e1dddcdf5067/pages/archived-body.md",
  "local_html": "attachments/e1dddcdf5067/pages/archived-body.html",
  "body_source": "prior_capture"
}