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
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 -
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
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.
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.