I’m a begginer using Pytorch, and i’m trying new things. I found a pre-trained model in PyTorch and i’d like to use it to extract the last layer’s output of the network (not the labels, but the last matrix used to extract the layers).
def image_loader(image_name):
"""load image, returns cuda tensor"""
image = Image.open(image_name)
image = loader(image).float()
image = Variable(image, requires_grad=True)
image = image.unsqueeze(0) #this is for VGG, may not be needed for ResNet
return image.cuda() #assumes that you're using GPU
model = torch.load('pytorch_model.bin', map_location='cpu')
image = image_loader(imagepath)
predictions = model(image)
but i get the error: TypeError: 'collections.OrderedDict' object is not callable
This will load the pretrained parameters of the model not the model defination itself, which are stored in the form of Ordered Dict hence the error.
You should create your model class first.
class Net(nn.Module):
// Your Model for which you want to load parameters
model = Net()
torch.optim.SGD(lr=0.001) #According to your own Configuration.
checkpoint = torch.load(pytorch_model)
model.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['opt'])
It depends on what your goal is, if you want to learn I highly recommend you to check the tutorial at pytorch website. else if you want to test a model on some dataset you can try this one too, but I guess it would be hard considering the complexity of the model. https://pytorch.org/tutorials/
Sure, the community can help you alot if you ran into an error, or find it difficult to understand something. Plus I’ll still suggest you to get some basic understanding of pytorch first.