Ad

PyTorch 1.0 Loading VGGFace2 Weights In Python3.7

- 1 answer

I am using Python3.7 and PyTorch 1.0 to develop a face recognition system. I want to use VGGFace2 Resnet50 pretrained model as described here as a feature extractor. I have downloaded the model and weights. I run the following codes as project readme says:

MainModel = imp.load_source('MainModel', 'resnet50_128_pytorch.py') 
model = torch.load('resnet50_128_pytorch.pth')

First line executed as expected but in the second line i got

'ascii' codec can't decode byte 0xc3 in position 1124: ordinal not in range(128)

I searched in the Stackoverflow and Google and i saw that it may be about this model saved with Python2 and loading from Python3 creates a problem. Is there any way that i can solve this?

Thank you.

Ad

Answer

I found a solution which currently looks like it's working. It basically changes the pickle load with latin1 encoding.

from functools import partial
import pickle
pickle.load = partial(pickle.load, encoding="latin1")
pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
MainModel = imp.load_source('MainModel', 'resnet50_ft_pytorch.py') 
model = torch.load('resnet50_ft_pytorch.pth', map_location=lambda storage, loc: storage, pickle_module=pickle)
Ad
source: stackoverflow.com
Ad