我有一个预先训练好的yolov7.pt模型,我试图用torch.load()
加载它。下面是我运行的完整代码。
import torch
model_path = "/content/yolov7/model/yolov7.pt"
model = torch.load(model_path,map_location=torch.device('cpu'))
字符串
但由于某种原因,我得到以下错误:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-8-cc86721fbe65> in <cell line: 4>()
2
3 model_path = "/content/yolov7/model/yolov7.pt"
----> 4 model = torch.load(model_path,map_location=torch.device('cpu'))
2 frames
/usr/local/lib/python3.10/dist-packages/torch/serialization.py in find_class(self, mod_name, name)
1163 pass
1164 mod_name = load_module_mapping.get(mod_name, mod_name)
-> 1165 return super().find_class(mod_name, name)
1166
1167 # Load the data (which may in turn use `persistent_load` to load tensors)
ModuleNotFoundError: No module named 'models'
---------------------------------------------------------------------------
型
为什么会这样?
1条答案
按热度按时间vngu2lb81#
当你训练你的模型时,你声明了一个类来包含它的定义。
torch.load
需要这个精确的类定义才能加载模型权重。您可以复制并传递代码上的模型定义,或者在不同的文件夹中为其创建模块。