Begginer: Loading bin model and predicting image

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

This is the network im using: https://onedrive.live.com/?authkey=!AMkPexmU-lBVnZs&id=C826C2D6F4C7D993!3757&cid=C826C2D6F4C7D993

what i’m trying is:

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

And i don’t know why…
Any suggestions?

Hy there

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']) 

Also if you want to use pretrained model in pytorch, the best way is to get them through
torchvision package. Check this tutorial.
https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

Hey! Considering this:

Should I use on of these?

src https://github.com/microsoft/unilm/tree/master/layoutlm/layoutlm

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/

Firstly i’d like to run it and see the result. And yes, this is the model i’d like to use. Can you give me some help? Thanks!

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.

Hello. @tenet2001tenet . Have you run the pre-trained model of LyoutLM? Which model definition did you use in loading the model using state_dict ?