Basically, I'm creating an emotion recognition application, and I'm using Emotic's image dataset. They have their own premade program and trained model for a demo (The colab link below) but for some reason the third cell under I. Prepare places pretrained model is encountering the error:
the first argument must be callable on line 19 in the 4th Google Colab cell.
Code:
# Converting model weights to python3.6 format
import torch
from PIL import Image
from torch.autograd import Variable as V
import torchvision.models as models
from torchvision import transforms as trn
from torch.nn import functional as F
import os
model_path = './places'
archs = ['resnet18']
for arch in archs:
model_file = os.path.join(model_path,'%s_places365.pth.tar' % arch)
save_file = os.path.join(model_path,'%s_places365_py36.pth.tar' % arch)
from functools import partial
import pickle
pickle.load = partial(pickle.load, encoding="latin1")
pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
model = torch.load(model_file, map_location=lambda storage, loc: storage, pickle_module=pickle)
torch.save(model, save_file)
print('converting %s -> %s'%(model_file, save_file))
print ('completed cell')
# Saving the model weights to use ahead in the notebook
# the architecture to use
arch = 'resnet18'
model_weight = os.path.join(model_path, 'resnet18_places365_py36.pth.tar')
# create the network architecture
model = models.__dict__[arch](num_classes=365)
#model_weight = '%s_places365.pth.tar' % arch
checkpoint = torch.load(model_weight) # model trained in GPU could be deployed in CPU machine like this!
state_dict = {str.replace(k,'module.',''): v for k,v in checkpoint['state_dict'].items()} # the data parallel layer will add 'module' before each layer name
model.load_state_dict(state_dict)
model.eval()
model.cpu()
torch.save(model, os.path.join(model_path, 'res_context' + '.pth'))
print ('completed cell')
Does anyone have any idea why this is happening? (Haven't changed any code this is the demo offered by Emotic)
Error:
TypeError Traceback (most recent call last)
<ipython-input-8-1a9e3bc55eae> in <module>()
17 #model_weight = '%s_places365.pth.tar' % arch
18
---> 19 checkpoint = torch.load(model_weight) # model trained in GPU could be deployed in CPU machine like this!
20 state_dict = {str.replace(k,'module.',''): v for k,v in checkpoint['state_dict'].items()} # the data parallel layer will add 'module' before each layer name
21 model.load_state_dict(state_dict)
1 frames
/usr/local/lib/python3.7/dist-packages/torch/serialization.py in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
867 # because it's marked readonly in pickle.
868 # The type: ignore is because mypy can't statically determine the type of this class.
--> 869 class UnpicklerWrapper(pickle_module.Unpickler): # type: ignore[name-defined]
870 # from https://stackoverflow.com/questions/13398462/unpickling-python-objects-with-a-changed-module-path/13405732
871 # Lets us override the imports that pickle uses when unpickling an object.
TypeError: the first argument must be callable
Public Gopogle Colab link: https://colab.research.google.com/github/Tandon-A/emotic/blob/master/Colab_train_emotic.ipynb
picklelibrary is installed (should be, it's default in python3) and that it's a suitable version.picklewas installed but the same error occurred.