在谷歌colab上训练yolov5 2.0的模型,训练时遇到问题如下:
Analyzing anchors... Best Possible Recall (BPR) = 1.0000
Image sizes 640 train, 640 test
Using 2 dataloader workers
Starting training for 300 epochs...
Epoch gpu_mem GIoU obj cls total targets img_size
0% 0/93 [00:14
Traceback (most recent call last):
File "train.py", line 469, in
train(hyp, tb_writer, opt, device)
File "train.py", line 291, in train
loss, loss_items = compute_loss(pred, targets.to(device), model) # scaled by batch_size
File "/content/yolov5-2.0/utils/utils.py", line 444, in compute_loss
tcls, tbox, indices, anchors = build_targets(p, targets, model) # targets
File "/content/yolov5-2.0/utils/utils.py", line 533, in build_targets
a, t = at[j], t.repeat(na, 1, 1)[j] # filter
RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)
这个是什么原因造成的,需要怎样修改?求能指点迷津!
看错误应该是t和indices使用了不同的设备存储。
解决办法是将indices移动到与t相同的设备上,或者将t移动到CPU上。你可以使用indices.to(device)或t.to("cpu")来实现。
indices = indices.to(device)
t = t.repeat(na, 1, 1)[indices]
或者:
t = t.to("cpu")
t = t.repeat(na, 1, 1)[indices]