使用Emotic演示时,pytorch.loads()函数中的第一个参数错误



基本上,我正在创建一个情绪识别应用程序,并使用Emotic的图像数据集。他们有自己的预制程序和用于演示的训练模型(下面的colab链接(,但由于某种原因,I. Prepare places pretrained model下的第三个单元遇到了错误:CCD_ 2在第4个Google Colab小区的19线上。代码:

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

有人知道为什么会发生这种事吗?(没有更改任何代码,这是Emotic提供的演示(

错误:

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

公共Gopogle Colab链接:https://colab.research.google.com/github/Tandon-A/emotic/blob/master/Colab_train_emotic.ipynb

我是这个存储库的作者。我早在2021年8月就解决了这个问题。该问题是由于Python 3.7版本中的Python Pickle模块发生了一些更改而导致的。该代码曾在Python 3.6版本中正常工作。

您可以尝试Colab_train_emotic.ipynb文件。

相关内容

最新更新