PyTorch Pretrained Model

In this Python tutorial, we will learn about the PyTorch Pretrained model and we will also cover different examples related to the PyTorch pretrained model. And, we will cover these topics.

  • PyTorch pretrained model
  • PyTorch pretrained model example
  • PyTorch pretrained model feature extraction
  • PyTorch pretrained model cifar 10
  • PyTorch pretrained model load
  • PyTorch pretrained model remove last layer
  • PyTorch pretrained model normalization
  • PyTorch pretrained modify the last layer
  • PyTorch pretrained model change input size
  • PyTorch pretrained model add layer
  • PyTorch pretrained model fine tune
  • PyTorch pretrained model image classification
  • PyTorch pretrained model inference

PyTorch pretrained model

In this section, we will learn about the PyTorch pretrained model in python.

  • Before moving forward we should have a piece of knowledge about Pretrained model.
  • A pretrained model is defined as a neural network model trained on standard datasets like ImageNet.

Code:

In the following code, we will import pretrainedmodels module from which we can train a model on a standard dataset.

  • print(pretrainedmodels.model_names) is used to print the pretrained model data.
  • models = pretrainedmodels.__dict__[modelname](num_classes=1000, pretrained=’imagenet’) is used to load the pretrained models.
  • models.eval() is used to evaluate the model.
import pretrainedmodels
print(pretrainedmodels.model_names)
print(pretrainedmodels.pretrained_settings['nasnetalarge'])
modelname = 'nasnetalarge' 
models = pretrainedmodels.__dict__[modelname](num_classes=1000, pretrained='imagenet')
models.eval()

Output:

After running the above code, we get the following output in which we can see that the pretrained model data is printed on the screen.

PyTorch pretrained model
PyTorch pretrained model

Also, check: PyTorch Save Model

PyTorch pretrained model example

In this section, we will learn about PyTorch pretrained model with an example in python.

  • A Pretrained model means the deep learning architectures that have been already trained on some dataset.
  • A pretrained model is a neural network model trained on standard datasets like alexnet, ImageNet.

Code:

In the following code, we will import some libraries from which we can train a model on a standard dataset with the help of an example.

  • dir(model) is used to show the different models and architecture on the screen.
  • alexnet = model.alexnet(pretrained=True) is used as a pretrained model.
  • print(alexnet) is used to print the data of the pretrained model.
from torchvision import models as model
import torch
dir(model)
alexnet = model.alexnet(pretrained=True)
print(alexnet)

Output:

After running the above code, we get the following output in which we can see that the data of the pretrained model is printed on the screen.

PyTorch pretrained model example
PyTorch pretrained model example

Read: Cross Entropy Loss PyTorch

PyTorch pretrained model feature extraction

In this section, we will learn about how feature extraction is done in a pretrained model in python.

Feature Extraction is defined as the process of dimensionality reduction by which an initial set of raw data is reduced to more achievable groups for processing.

Code:

In the following code, we will import some libraries from which we can extract the feature from the pretrained model.

  • rn18 = model.resnet18(pretrained=True) is used as a pretrained model.
  • print(“Children Counter: “,Children_Counter,” Layer Name: “,i,) is used to print the children counter and layer name.
  • rn18._modules is used to display the modules on the screen.
import torch
import torch.nn as nn
from torchvision import models as model
rn18 = model.resnet18(pretrained=True)
Children_Counter = 0
for i,j in rn18.named_children():
    print("Children Counter: ",Children_Counter," Layer Name: ",i,)
    Children_Counter+=1
rn18._modules

Output:

In the following output, we can see that the feature can be extracted by the pretrained model and printed on the screen.

PyTorch pretrained model feature extraction
PyTorch pretrained model feature extraction

Read: Adam optimizer PyTorch with Examples

PyTorch pretrained model cifar 10

In this section, we will learn about the PyTorch pretrained model cifar 10 in python.

  • CiFAR-10 is a dataset that is a collection of data that is commonly used to train machine learning and it is also used for computer version algorithms.
  • Here we can use pretrained model trained on the standard dataset like cifar 10 and this CIFAR stand for Canadian Institute For Advanced Research.

Code:

In the following code, we will import the torch library to the pretrained model on the standard like cifar-10.

  • from pprint import pprint as print is used to pretty-print in a form of python data structures that can be used as input to the interpreter.
  • print(torch.hub.list(“chenyaofo/pytorch-cifar-models”, force_reload=True)) is used to print and reload the data o the screen.
import torch
from pprint import pprint as print
print(torch.hub.list("chenyaofo/pytorch-cifar-models", force_reload=True))

Output:

After running the above code, we get the following output in which we can see that the cifar model data is printed on the screen.

PyTorch pretrained cifar10
PyTorch pretraied cifar10

Read PyTorch Model Eval

PyTorch pretrained model load

In this section, we will learn about how to load a pretrained model in python.

  • A pretrained model is defined as a neural network model trained on a suitable dataset like AlexNet, ImageNet, etc.
  • The researcher can use these pretrained models instead of reinventing them again from scratch.
  • Here we can use the pretrained model and load the data of the pretrained model.

Code:

In the following code, we will import the pretrained models trained on the suitable dataset and load the data.

  • print(pretrainedmodels.model_names) is used to print the pretrained model name.
  • models = pretrainedmodels.__dict__[modelname](num_classes=1000, pretrained=’imagenet’) is used to load the model.
  • models.eval() is used to evaluate the model.
 import pretrainedmodels 
 print(pretrainedmodels.model_names)
 print(pretrainedmodels.pretrained_settings['vgg11'])
 modelname = 'vgg11' 
models = pretrainedmodels.__dict__[modelname](num_classes=1000, pretrained='imagenet')
models.eval()

Output:

After running the above code, we get the following output in which we can see that the PyTorch pretrained model data is loaded on the screen.

PyTorch pretrained model load
PyTorch pretrained model load

Read PyTorch Early Stopping

PyTorch pretrained model remove last layer

In section, we will learn about PyTorch pretrained model removing the last layer in python.

  • Pretrained model trained on a suitable dataset and here we want to remove the last layer of the trained model.
  • After removing the last layer from the pretrained model new data is generated on the screen.

Code:

In the following code, we will import some libraries from which we can remove the last layer of the Pretrained model.

  • models.resnet152(pretrained=True) is used as pretrained model.
  • print(remodel) is used to print the model.
  • list(premodel.modules()) is used to inspect the module of the model.
  • nn.Sequential(*list(premodel.modules())[:-1]) is used to accept any input that quickly implement the sequential module.
  • mymodel = nn.Sequential(*list(premodel.modules())[:-1]) is used to remove the last layer.
  • print(mymodel) is used to print the model.
from torchvision import datasets, transforms, models
premodel = models.resnet152(pretrained=True)
print(premodel)
list(premodel.modules()) 
mymodel = nn.Sequential(*list(premodel.modules())[:-1])
premodel = models.resnet152(pretrained=True)
list(premodel.modules())
mymodel = nn.Sequential(*list(premodel.modules())[:-1]) 
print(mymodel)

Output:

After running the above code we get the following output in which we can see that the last layer is removed from the pretrained model and the modified pretrained model is printed on the screen.

PyTorch pretrained model remove last layer
PyTorch pretrained model remove the last layer

PyTorch pretrained model normalization

In this section, we will learn about PyTorch pretrained model normalization in python.

Normalization in PyTorch is done using torchvision.transform.Normalization().This is used to normalize the data with mean and standard deviation.

Code:

In the following code, we will import some libraries from which we can normalize our pretrained model.

  • dataset = datasets.MNIST(“data”, download=True,train=True, transform=transforms.ToTensor()) is used to transform the dataset.
  • normalize = transforms.Normalize(mean=[0.495, 0.467, 0.418], std=[0.239, 0.324, 0.235]) is used to normalize the data.
  • mean = torch.zeros(1) is used to calculate the mean.
  • std = torch.zeros(1) is used to calculate the standard deviation.
  • print(‘Computing mean and std’) is used to print the computing mean and standard deviation.
import os
import torch
from torchvision import datasets, transforms
from torch.utils.data.dataset import Dataset
from tqdm.notebook import tqdm
from time import time

nchanels = 1

ds = datasets.MNIST("data", download=True,
                 train=True, transform=transforms.ToTensor())
normalize = transforms.Normalize(mean=[0.495, 0.467, 0.418],
                                  std=[0.239, 0.324, 0.235])
fulldr = torch.utils.data.DataLoader(ds, shuffle=False, num_workers=os.cpu_count())

mean_val = torch.zeros(1)
std_val = torch.zeros(1)
print('Calculating mean_val and std_val')
for input, lbls in tqdm(fulldr):
    for x in range(nchanels):
        mean_val[x] += input[:,x,:,:].mean()
        std_val[x] += input[:,x,:,:].std()
mean_val.div_(len(ds))
std_val.div_(len(ds))
print(mean_val, std_val)

Output:

In the following output, we can see that the mean and standard deviation is printed on the screen which normalizes the PyTorch pretrained model.

PyTorch pretrained model normalization
PyTorch pretrained model normalization

PyTorch pretrained modify the last layer

In this section, we will learn about how to modify the last layer of the PyTorch pretrained model in python.

  • A pretrained model is a neural network model trained on a suitable data set like ImageNet, Alexnet, etc.
  • Here we can modify the last layer of the pretrained model we can replace the last layer with the new layer.

Code:

In the following code, we will import some libraries from which we can modify the last layer.

  • vgg16 = model.vgg16(pretrained=True) is used as a pretrained model.
  • print(vgg16._modules.keys()) is used to print the model.
  • print(vgg16._modules[‘classifier’]) is used to print the classifier.
  • outfeatures = 100 is used this model with 100 other categories.
  • print(vgg16._modules[‘classifier’]) is used to print the new last layer classifier.
import torch
import torch.nn as nn
import torchvision.models as model

vgg16 = model.vgg16(pretrained=True)
print(vgg16._modules.keys())
print(vgg16._modules['classifier'])
infeatures = vgg16._modules['classifier'][-1].in_features
outfeatures = 100
vgg16._modules['classifier'][-1] = nn.Linear(infeatures, outfeatures, bias=True)
print(vgg16._modules['classifier'])

Output:

After running the above code, we get the following output in which we can see that the PyTorch pretrained model modifies the last layer is printed on the screen.

PyTorch pretrained model modify the last layer
PyTorch pretrained model modify the last layer

PyTorch pretrained model change input size

In this section, we will learn about how to change the input size of the PyTorch pretrained model in python.

A pretrained model is defined as a neural network model trained on a suitable dataset and we can also change the model input size.

Code:

In the following code, we will import some modules from which we can change the input size of the pretrained model.

  • X = torch.randn(1, 1, 224, 224) is used to generate the random numbers.
  • model = model.vgg16(pretrained=False): Pretrained = false is used just for debug reasons.
  • firstconvlayer = [nn.Conv2d(1, 3, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True)] is used to make the first convolution layer.
  • firstconvlayer.extend(list(model.features)) is used to extend the layer.
  • nn.Sequential(*firstconvlayer ) is used to implement the sequential modules.
  • print(output) is used to print the output on the screen.
import pretrainedmodels
import torchvision.models as model
import torch
import torch.nn as nn
X = torch.randn(1, 1, 224, 224)
model = model.vgg16(pretrained=False) 
firstconvlayer = [nn.Conv2d(1, 3, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, bias=True)]
firstconvlayer.extend(list(model.features))  
model.features= nn.Sequential(*firstconvlayer )  
output = model(X)
print(output)

Output:

After running the above code we get the following output in which we can see that the change in the input size of the pretrained model is printed on the screen.

PyTorch pretrained model change input size
PyTorch pretrained model change input size


PyTorch pretrained model add layer

In this section, we will learn about how to add a layer in PyTorch pretrained model.

PyTorch pretrained models are neural network models trained o large datasets like Imagenet.

Here we can add a new layer in the pretrained model to make a new model.

Code:

In the following code, we will import some modules from which we can add a layer in the Pretrained model.

  • nn.Sequential(nn.Linear(1000, 100),nn.ReLU(),nn.Linear(100, 4)) is used to implement the sequential modules.
  • X = self.pretrained_model(X) is used to pretrained the model.
  • X = self.new_layers(X) is used to add the new layer.
  • extended_model = AlexNet(pretrained_model=pretrained_model) is used to extend the model.
import torchvision
import pretrainedmodels
from torchvision import models 
pretrained_model = torchvision.models.alexnet(pretrained=True)
class AlexNet(nn.Module):
    def __init__(self, pretrained_model):
        super(AlexNet, self).__init__()
        self.pretrained = pretrained_model
        self.new_layers = nn.Sequential(nn.Linear(1000, 100),
                                           nn.ReLU(),
                                           nn.Linear(100, 4))
    
    def forward(self, X):
        X = self.pretrained_model(X)
        X = self.new_layers(X)
        return X

extended_model = AlexNet(pretrained_model=pretrained_model)
extended_model

Output:

In the following output, we can see that the new layer is added in the pretrained model and the data of the new layer is printed on the screen.

PyTorch pretrained model add layer
PyTorch pretrained model add layer


PyTorch pretrained model fine-tune

In this section, we will learn about the PyTorch pretrained model fine-tune in python.

Before moving forward we should have a piece of knowledge about fine-tune. Fine-tune is defined as the process to attain the best or desired performance.

Here we want to attain the best performance of the pretrained model PyTorch.

Code:

In the following code, we will import some modules from which we can attain the best performance of the model.

  • print(“PyTorch version: “,torch.__version__) is used to print the version of the torch on which we get the best performance of the model.
  • print(“Torchvision version: “,torchvision.__version__) is used to print the torch vision version on the screen.
from __future__ import print_function
from __future__ import division
import torch
import torch.nn as nn
import torch.optim as optimize
import numpy as num
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plot
import time
import os
import copy
print("PyTorch version: ",torch.__version__)
print("Torchvision version: ",torchvision.__version__)

Output:

After running the above code we get the following output in which we can see that the version of the PyTorch and torchvision is printed on the screen and we can attain the best performance of the model on it.

PyTorch pretrained model fine tune
PyTorch pretrained model fine-tune


PyTorch pretrained model image classification

In this section, we will learn the PyTorch pretrained model image classification in python.

  • Pretrained models are neural networks trained on the large dataset like ImageNet , AlexNet, etc.
  • Pretrained models are an important factor for rapid advancement in computer vision research.

Code:

In the following code, we will import some libraries from which we can do pretrained model image classification.

  • alexnet = model.alexnet(pretrained=True) is used as pretrained model.
  • print(alexnet) is used to print the alexnet.
  • image = Image.open(“dog.jfif”) is used to open the image.
  • plt.imshow(image) is used to plot the image on the screen.
  • image_t = transform(image) is used as transform the image.
from torchvision import models as model
import torch
import matplotlib.pyplot as plt
dir(model)
alexnet = model.alexnet(pretrained=True)
print(alexnet)
from torchvision import transforms
transform = transforms.Compose([           
transforms.Resize(258),                   
transforms.CenterCrop(226),                
transforms.ToTensor(),                     
transforms.Normalize(                      
mean=[0.487, 0.458, 0.408],                
std=[0.232, 0.226, 0.227]                  
)])

from PIL import Image
image = Image.open("dog.jfif")
plt.imshow(image)
image_t = transform(image)
batch_t = torch.unsqueeze(image_t, 0)

Output:

In the following output, we can see that the pretrained model training data and also pretrained model image classification is done on the screen.

PyTorch pretrained model image classification
PyTorch pretrained model image classification

PyTorch pretrained model inference

In this section, we will learn about the PyTorch pretrained model inference in python.

The inference is defined as a process that is going to focus on how to use the pretrained models for predicting the class of input.

Code:

In the following code, we will import some libraries from which we can pretrained model inference.

  • dir(model) is used to return the list of the attributes.
  • alexnet = model.alexnet(pretrained=True) is used a pretrained model.
  • alexnet.eval() is used to evaluate the pretrained model.
  • print(output.shape) is used to print the output on the screen.
from torchvision import models as model
import torch
dir(model)
alexnet = model.alexnet(pretrained=True)
alexnet.eval()
output = alexnet(batch_t)
print(output.shape)

Output:

After running the above code, we get the following output in which we can see that the PyTorch pretrained model inference is printed on the screen.

PyTorch pretrained model inference
PyTorch pretrained model inference

So, in this tutorial, we discussed the PyTorch Pretrained model and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • PyTorch pretrained model
  • PyTorch pretrained model example
  • PyTorch pretrained model feature extraction
  • PyTorch pretrained model cifar 10
  • PyTorch pretrained model load
  • PyTorch pretrained model remove last layer
  • PyTorch pretrained model normalization
  • PyTorch pretrained modify the last layer
  • PyTorch pretrained model change input size
  • PyTorch pretrained model add layer
  • PyTorch pretrained model fine tune
  • PyTorch pretrained model image classification
  • PyTorch pretrained model inference