我在尝试加载Pytoch模型时遇到问题:“在模块中找不到身份”

时间:2020-08-27 11:47:27

标签: python model pytorch

尝试加载pytorch模型时,出现以下属性错误

model = torch.load('../input/melanoma-model/melanoma_model_0.pth')
model = model.to(device)
model.eval()

AttributeError跟踪(最近的调用) 最后) 1拱= EfficientNet.from_pretrained('efficiency-b2') 2个模型= Net(arch = arch) ----> 3个torch.load('../ input / melanoma-model / melanoma_model_0.pth') 4型号= model.to(设备) 5 model.eval()

/opt/conda/lib/python3.7/site-packages/torch/serialization.py在 负载(f,map_location,pickle_module,** pickle_load_args) 591返回torch.jit.load(f) 592返回_负荷(opened_zipfile,map_location,pickle_module,** pickle_load_args) -> 593 return _legacy_load(opened_file,map_location,pickle_module,** pickle_load_args) 594 595

/opt/conda/lib/python3.7/site-packages/torch/serialization.py在 _legacy_load(f,map_location,pickle_module,** pickle_load_args) 第771章真相(二更) 第772章 -> 773结果= unpickler.load() 774 775 deserialized_storage_keys = pickle_module.load(f,** pickle_load_args)

AttributeError:无法在

1 个答案:

答案 0 :(得分:0)

首先,您需要一个模型类来将.pth中的参数加载到其中。而且您缺少一步:

model = Model()   # the model class (yours has probably another name)
model.load_state_dict(torch.load('../input/melanoma-model/melanoma_model_0.pth'))
model = model.to(device)
model.eval()

走了,我希望能解决您的问题!