【问题标题】:Indices should be either on cpu or on the same device as the indexed tensor索引应该在 cpu 上或与索引张量在同一设备上
【发布时间】:2022-11-12 10:16:17
【问题描述】:

我为 YoloV7 下载了一个准备好的数据集。我还克隆了 yoloV7 Repo。

我想用这个下载的数据集训练一个模型,为此我使用这个命令。

python train.py --workers 8 --device 0 --batch-size 16 --data data.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights yolov7x.pt --name yolov7 --hyp data/hyp.scratch.p5.yaml

我得到了这个 RuntimeError

autoanchor: Analyzing anchors... anchors/target = 5.50, Best Possible Recall (BPR) = 1.0000
Image sizes 640 train, 640 test
Using 8 dataloader workers
Logging results to runs\train\yolov74
Starting training for 300 epochs...

     Epoch   gpu_mem       box       obj       cls     total    labels  img_size
  0%|                                                                                                                                                                                                               | 0/372 [00:03<?, ?it/s]
Traceback (most recent call last):
  File "D:\projects\yolov7\train.py", line 618, in <module>
    train(hyp, opt, device, tb_writer)
  File "D:\projects\yolov7\train.py", line 363, in train
    loss, loss_items = compute_loss_ota(pred, targets.to(device), imgs)  # loss scaled by batch_size
  File "D:\projects\yolov7\utils\loss.py", line 585, in __call__
    bs, as_, gjs, gis, targets, anchors = self.build_targets(p, targets, imgs)
  File "D:\projects\yolov7\utils\loss.py", line 759, in build_targets
    from_which_layer = from_which_layer[fg_mask_inboxes]
RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)

我的系统包含 1xCpu、1x Cuda GPU(它是默认的游戏电脑)

【问题讨论】:

    标签: yolo


    【解决方案1】:

    我相信这是当前实现中的一个错误。您可以通过将utils/loss.py 第 685 行更改为来修复它

    from_which_layer.append((torch.ones(size=(len(b),)) * i).to('cuda'))
    

    并在 756 之后添加一行以将 fg_mask_inboxes 放在您的 cuda 设备上:

    fg_mask_inboxes = fg_mask_inboxes.to(torch.device('cuda'))
    

    【讨论】:

    • 有效!!在网上搜索了两天......
    • 看起来当你要使用 p6 模型进行训练时,你需要使用 train_aux.py 但它会给你类似的错误,你知道如何修复它吗? ------ yolov7/utils/loss.py",第 1559 行,在 build_targets2 from_which_layer = from_which_layer[fg_mask_inboxes] RuntimeError: 索引应该在 cpu 或与索引张量 (cpu) 相同的设备上
    【解决方案2】:

    如果您要使用 yolov7 和 train_aux.py 的 p6 模型,您还需要更改几行:

    第1336章——from_which_layer.append((torch.ones(size=(len(b),))

    第1407章

    【讨论】:

      猜你喜欢
      • 2023-02-25
      • 2022-12-10
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-03
      • 1970-01-01
      • 2020-03-28
      相关资源
      最近更新 更多