复刻yolo系列时出现的BUG及解决方法

目录

1、ValueError: too many values to unpack (expected 2)

2、RuntimeError: result type Float can't be cast to the desired output type __int64

3、AssertionError: Torch not compiled with CUDA enabled

4、RuntimeError: "slow_conv2d_cpu" not implemented for 'Half'

5、RuntimeError: Input type (torch.FloatTensor) and weight type (torch.HalfTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor

一、出错代码

使用creat_dataloader()函数,发现返回值是三个,而我只需要两个

train_loader, dataset = create_dataloader(train_path, imgsz, batch_size // WORLD_SIZE, gs, single_cls,
                                          hyp=hyp, augment=True, cache=opt.cache, rect=opt.rect, rank=LOCAL_RANK,
                                          workers=workers, image_weights=opt.image_weights, quad=opt.quad,
                                          prefix=colorstr('train: '), shuffle=True)

报错信息

ValueError: too many values to unpack (expected 2)

报错原因

代码返回值多加了dataset.counter_per_cls,因为这就是dataset的一个属性,但我不确定将dataset.counter_per_cls删了有什么影响

return loader(dataset,
              batch_size=batch_size,
              shuffle=shuffle and sampler is None,
              num_workers=nw,
              sampler=sampler,
              pin_memory=True,
              collate_fn=LoadImagesAndLabels.collate_fn4 if quad else LoadImagesAndLabels.collate_fn), dataset, dataset.counter_per_cls

二、出错代码

gain = torch.ones(7, device=targets.device)

报错信息

RuntimeError: result type Float can't be cast to the desired output type __int64

报错原因

主要是变量类型不匹配引起的,原因是新版本的torch无法自动执行变量类型转换,旧版本torch可以。

解决办法

后面手动使之变成long型

gain = torch.ones(7, device=targets.device).long()

三、出错代码

label = label.long().cuda()

报错信息

AssertionError: Torch not compiled with CUDA enabled

报错原因

电脑没有GPU,但是程序调用了GPU

解决办法

将cuda()变成cpu()

label = label.long().cpu()

四、出错代码

im = im.half() 

报错信息

RuntimeError: "slow_conv2d_cpu" not implemented for 'Half'

报错原因

将输入数据的类型设置为half(半精度浮点数,能加快计算速度),但是half只有GPU支持

解决办法

im = im.half() if half else im.float()

并且希望以后要尽量这样写,健壮性高

五、出错代码

model.half()

报错信息

RuntimeError: Input type (torch.FloatTensor) and weight type (torch.HalfTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor

报错原因

输入数据的数据类型与模型类型不一致

torch.FloatTensor:cpu的float类型

torch.HalfTensor:cou的half类型

torch.cudaFloatTensor:gpu的float类型

torch.cudaHalfTensor:gpu的half类型

解决方法

model.half() if half else model.float()
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值