yolov3/yolov5 val.py训练正常测试错误

基于u版的yolov3/yolov5上搭建自己的模型,修改主干网络或者head,训练可以正常训练,训练完成后用命令python val.py --weights runs/exp/train/best.pt进行测试,报错:
RuntimeError: Sizes of tensors must match except in dimension 2. Got 27 and 28 (The offending index is 0)
如下图所示
在这里插入图片描述
开始以为是维度错误或者特征图大小不匹配,但能正常训练,应该不是搭建的网络有问题,最后在github/yolov5找到了解决方法:
定位common.py,将#stride = int(model.stride.max())修改为stride = max(int(model.stride.max()), 32),如下:

elif pt:  # PyTorch
            from models.experimental import attempt_load  # scoped to avoid circular import
            model = torch.jit.load(w) if 'torchscript' in w else attempt_load(weights, map_location=device)
            #stride = int(model.stride.max())  # model stride
            stride = max(int(model.stride.max()), 32)  # model stride

猜你喜欢

转载自blog.csdn.net/weixin_44769034/article/details/125392620