当参数保存为 numpy 数组时如何加载 Pytorch 模型?
How to load a Pytorch model when the parameters are saved as numpy arrays?
在 this GitHub repo,我下载了预训练模型 senet50_ft
。
我是这样加载的:
import pickle
f = open('pretrained_models/senet50_ft_weight.pkl', 'rb')
state_dict = pickle.load(f, encoding='latin1')
f.close()
状态已加载,Github repos还提供了SENet模型Class here.
所以我设法实例化了那个模型:
model = senet.senet50()
然后我尝试加载状态,但出现错误:
model.load_state_dict(state_dict)
Traceback (most recent call last):
File "...\module.py", line 982, in _load_from_state_dict
param.copy_(input_param)
TypeError: copy_(): argument 'other' (position 1) must be Tensor, not numpy.ndarray
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...\module.py", line 1037, in load_state_dict
load(self)
File "...\module.py", line 1035, in load
load(child, prefix + name + '.')
File "...\module.py", line 1032, in load
state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs)
File "...\module.py", line 988, in _load_from_state_dict
.format(key, param.size(), input_param.size(), ex.args))
TypeError: 'int' object is not callable
我尝试通过执行以下操作将 ndarray
转换为 Tensor
:
for key in state_dict.keys():
state_dict[key] = torch.from_numpy(state_dict[key])
但是我又遇到了一个错误,我想我哪儿也去不了。
我是 PyTorch 的新手,但我怀疑这个模型是用旧版本的 PyTorch 序列化的。您知道是否存在解决方案吗?
他们有一个 load_state_dict 功能可以满足您的需求。
在 this GitHub repo,我下载了预训练模型 senet50_ft
。
我是这样加载的:
import pickle
f = open('pretrained_models/senet50_ft_weight.pkl', 'rb')
state_dict = pickle.load(f, encoding='latin1')
f.close()
状态已加载,Github repos还提供了SENet模型Class here.
所以我设法实例化了那个模型:
model = senet.senet50()
然后我尝试加载状态,但出现错误:
model.load_state_dict(state_dict)
Traceback (most recent call last):
File "...\module.py", line 982, in _load_from_state_dict
param.copy_(input_param)
TypeError: copy_(): argument 'other' (position 1) must be Tensor, not numpy.ndarray
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...\module.py", line 1037, in load_state_dict
load(self)
File "...\module.py", line 1035, in load
load(child, prefix + name + '.')
File "...\module.py", line 1032, in load
state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs)
File "...\module.py", line 988, in _load_from_state_dict
.format(key, param.size(), input_param.size(), ex.args))
TypeError: 'int' object is not callable
我尝试通过执行以下操作将 ndarray
转换为 Tensor
:
for key in state_dict.keys():
state_dict[key] = torch.from_numpy(state_dict[key])
但是我又遇到了一个错误,我想我哪儿也去不了。
我是 PyTorch 的新手,但我怀疑这个模型是用旧版本的 PyTorch 序列化的。您知道是否存在解决方案吗?
他们有一个 load_state_dict 功能可以满足您的需求。