How to use predefined models in a closed system

Hi Guys,
I want to use predefined models in a system with no internet connectivity.
I know how to download it but I don’t know how to use them in a model-
I downloaded it using the below method -

but now I don’t know how to use them in a network-

Given that you have the pre-trained model, you can always use load() to load the state dictionary from pre-trained model file into your model. The link below is very detailed and answers what you need. https://pytorch.org/tutorials/beginner/saving_loading_models.html

1 Like

I mean I want to use, let’s say - resnet50 as a transfer learning but using the above method, they are asking me to define the model.
I used the below statement -

model_transfer = model.load_state_dict(torch.load('model.path'))

You need to define the model before you load weight in the model. This definition has to match the values in the state dictionary. If you are using standard ResNet50 for ImageNet then you can do

import torchvision.models as models
model = models.resnet50()

and then load your state dictionary into the model

2 Likes

It is like @babababadukeduke has mentioned, you need to first define the model.

Below is the example with your own defined model.

# Custom Model
class TwoLayerNet(torch.nn.Module):
   def __init__(self, D_in, H, D_out):
       """
       In the constructor we instantiate two nn.Linear modules and assign them as
       member variables.
       """
       super(TwoLayerNet, self).__init__()
       self.linear1 = torch.nn.Linear(D_in, H)
       self.linear2 = torch.nn.Linear(H, D_out)

   def forward(self, x):
       """
       In the forward function we accept a Tensor of input data and we must return
       a Tensor of output data. We can use Modules defined in the constructor as
       well as arbitrary operators on Tensors.
       """
       h_relu = self.linear1(x).clamp(min=0)
       y_pred = self.linear2(h_relu)
       return y_pred

# Model Object / Blueprint
model = TwoLayerNet(D_in, H, D_out)


# Load the weights into the model.
model_transfer = model.load_state_dict(torch.load('model.path')) # Weight from same blue print.
2 Likes

I just did that but I don’t think it is behaving exactly I intend it to behave-

model_transfer = models.resnet50()
model_transfer = model_transfer.load_state_dict(torch.load(model_path))

Now, if I print model_transfer then isn’t the complete architecture should come up but I am getting -
<All keys matched successfully> and no architecture.
Also, if then just want to modify last layers using model_transfer.fc.in_features then it should work but instead I am getting
AttributeError: '_IncompatibleKeys' object has no attribute 'fc'

<All keys matched successfully> means that you were able to successfully load the model parameters in the file. You can do print(model_transfer) and see the architecture. If you can post the code of how you are trying to modify the model then maybe I will be able to point out the exact problem.

1 Like

I went here , then downloaded the required model file and saved in my computer.
Then, I did -

import torchvision.models as models
import torch.nn as nn
model_path = 'Path_to_model/resnet_50.pth'
model_transfer = models.resnet50()
model_transfer = model_transfer.load_state_dict(torch.load(model_path))
print(model_transfer)

and I am getting <All keys matched successfully>
on doing this.

import torchvision.models as models
import torch.nn as nn
model_path = 'Path_to_model/resnet_50.pth'
model_transfer = models.resnet50()
missing_keys, unexpected_keys = model_transfer.load_state_dict(torch.load(model_path))
print(model_transfer)

https://pytorch.org/docs/stable/nn.html?highlight=load_state_dict#torch.nn.Module.load_state_dict

Looking at the documentation, load_state_dict returns missing_keys,unexpected_keys and loads the model in the model_transfer object itself.

1 Like

Thank You, it worked.

1 Like