解决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()
物联沃分享整理
物联沃-IOTWORD物联网 » 解决yolo系列复刻中遇到的BUG

发表评论