yolo7加载模型出现异常

背景:将训练好的yolo7集成在小程序上,需要做一个定时任务,每半小时监测一下图片

在加载模型时,出现下面异常

问题 ModuleNotFoundError: No module named 'models'

具体在这一句出现了异常

ckpt = torch.load(w, map_location=map_location)  # load
ef attempt_load(weights, map_location=None):
    # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
    model = Ensemble()
    for w in weights if isinstance(weights, list) else [weights]:
        attempt_download(w)
        ckpt = torch.load(w, map_location=map_location)  # load
        model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval())  # FP32 model
    
    # Compatibility updates
    for m in model.modules():
        if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]:
            m.inplace = True  # pytorch 1.7.0 compatibility
        elif type(m) is nn.Upsample:
            m.recompute_scale_factor = None  # torch 1.11.0 compatibility
        elif type(m) is Conv:
            m._non_persistent_buffers_set = set()  # pytorch 1.6.0 compatibility
    
    if len(model) == 1:
        return model[-1]  # return model
    else:
        print('Ensemble created with %s\n' % weights)
        for k in ['names', 'stride']:
            setattr(model, k, getattr(model[-1], k))
        return model  # return ensemble

根据issue提供的例子

解决办法在使用的yolo的文件下,加入如下代码
其中app是根目录

import sys
sys.path.append("./app")

参考
https://blog.csdn.net/weixin_41809530/article/details/116446002
https://github.com/ultralytics/yolov5/issues/353

你可能感兴趣的:(人工智能)