YOLOv5-5.x版本报错Expected more than 1 value per channel when training, got input size torch.Size([1, *]

本文介绍了在使用YOLOv5 5.x版本时遇到的训练错误,即`Expected more than 1 value per channel when training`。错误源于batch size为1时不能执行Batch Normalization操作。通过修改`yolo.py`和`utils/torch_utils.py`中的代码,将全零数据的shape改为`[2, ch, s, s]`并调整计算FLOPs的公式,以及注释掉`train.py`中与plot相关的代码,可以成功解决这个问题,使训练过程能够正常进行。" 43951829,3428957,Xcode调试技巧:断点使用详解,"['xcode', 'debug']
摘要由CSDN通过智能技术生成

yolov5在运行之前会先用全0的数据跑一遍,确保我们的网络配置能够跑通,但是源码设置的全0数据的shape为[1,3,256,256],batch size为1,这就导致了我们的有些Batch Normalization操作会出现以下报错:
Expected more than 1 value per channel when training, got input size torch.Size([1, *])
因为batch size==1是不能进行BN操作的,所以我们需要修改源码给的全零数据的shape,首先在yolo.py文件的Model类下,将下面的torch.zeros(1, ch, s, s)改为torch.zeros(2, ch, s, s)。

# Build strides, anchors
m = self.model[-1]  # Detect()
if isinstance(m, Detect):
    s = 256  # 2x min stride
    m.inplace = self.inplace
    # 这边可以修改运行yolo.py时生成的默认batch_size,channel,height和weight
    # m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))])  # forward
    m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(2, ch, s, s))])  # forward
  
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值