Python 无法从联机资源下载保存的模型,pickle错误

Python 无法从联机资源下载保存的模型,pickle错误,python,download,pytorch,torch,Python,Download,Pytorch,Torch,我无法下载并使用先前从在线存储库保存的模型。代码如下: model = Model().double() # Model is defined in another class state_dict = torch.hub.load_state_dict_from_url(r'https://filebin.net/j2977ux7kts41aft/checkpoint_best.pt?t=wjbujfoo') model.load_state_dict(state_dict) model

我无法下载并使用先前从在线存储库保存的模型。代码如下:


model = Model().double()   # Model is defined in another class
state_dict = torch.hub.load_state_dict_from_url(r'https://filebin.net/j2977ux7kts41aft/checkpoint_best.pt?t=wjbujfoo')
model.load_state_dict(state_dict)
model.eval()
这给了我以下错误:

Traceback (most recent call last):
  File "/path/file.py", line 47, in <module>
    state_dict = torch.hub.load_state_dict_from_url(r'https://filebin.net/j2977ux7kts41aft/checkpoint_best.pt?t=wjbujfoo')
  File "anaconda3/envs/torch_env/lib/python3.6/site-packages/torch/hub.py", line 466, in load_state_dict_from_url
    return torch.load(cached_file, map_location=map_location)
  File "/anaconda3/envs/torch_env/lib/python3.6/site-packages/torch/serialization.py", line 386, in load
    return _load(f, map_location, pickle_module, **pickle_load_args)
  File "anaconda3/envs/torch_env/lib/python3.6/site-packages/torch/serialization.py", line 563, in _load
    magic_number = pickle_module.load(f, **pickle_load_args)
_pickle.UnpicklingError: invalid load key, '\x0a'.

回溯(最近一次呼叫最后一次):
文件“/path/File.py”,第47行,在
state\u dict=torch.hub.load\u state\u dict\u from\u url(r'https://filebin.net/j2977ux7kts41aft/checkpoint_best.pt?t=wjbujfoo')
文件“anaconda3/envs/torch\u env/lib/python3.6/site packages/torch/hub.py”,第466行,处于加载状态
return torch.load(缓存的文件,map\u location=map\u location)
文件“/anaconda3/envs/torch_env/lib/python3.6/site packages/torch/serialization.py”,第386行,已加载
返回加载(f,映射位置,pickle模块,**pickle加载参数)
文件“anaconda3/envs/torch_env/lib/python3.6/site packages/torch/serialization.py”,第563行,in_load
magic_number=pickle_module.load(f,**pickle_load_参数)
_pickle.UnpicklingError:无效的加载密钥“\x0a”。
该模型位于:

请注意,我完全可以手动下载它,然后使用
torch.load(path)
加载它而不出错,但我需要从代码开始!可能是从url下载时的序列化某种程度上弄乱了pickle编码吗


编辑:我不必使用filebin,任何支持我尝试使用的在线存储都足够了

这段带有“下载按钮”链接和“地图位置”参数的代码对我来说很好:

state_dict = torch.hub.load_state_dict_from_url(r'https://filebin.net/j2977ux7kts41aft/checkpoint_best.pt?t=wjbujfoo', map_location=torch.device('cpu'))

问题确实出在环境配置中。我用PyTorch 1.0.2创建了模型,然后更新到1.2.0,以便使用
torch.hub
。这给了我一个错误。在1.2.0中训练一个新模型后,错误现在消失了


希望这对将来的人有所帮助:)

你能分享你的python和torch版本吗?谢谢