使用YOLOv5 5.0版本加载6.0版本以上的模型时,会产生报错,本质原因是6.0及以上版本的网络结构发生了变化,如取消Focus结构,新增SPPF结构等。
下面进行解决。
AttributeError: Can’t get attribute ‘SPPF’ on <module ‘models.common’
这个报错只需要在5.0版本的工程中models/common
文件中添加:
RuntimeError: The size of tensor a (40) must match the size of tensor b (84) at non-singleton dimension 3
这个报错需要在models/yolo
将Detect类进行替换。
可以注意到,替换之后的Detect类在原报错位置增加了一个判断分支,这使得该类能够同时兼容5.0和6.0的推理模型。