WongKinYiu / yolov7

Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
GNU General Public License v3.0
13.27k stars 4.19k forks source link

RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu) #1045

Open Dev-ori opened 1 year ago

Dev-ori commented 1 year ago

I've got this error when training, is there any similar errors or suggestions?

USED Script:

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

Where I modified in your code: create new yaml for Customdataset.(cfg/training/.yaml) create new data yaml (data/.yaml)

else, nothing modified.

Displayed Error

Epoch   gpu_mem       box       obj       cls     total    labels  img_size

0%| | 0/468 [00:03<?, ?it/s] Traceback (most recent call last): File "train.py", line 616, in train(hyp, opt, device, tb_writer) File "train.py", line 363, in train loss, loss_items = compute_loss_ota(pred, targets.to(device), imgs) # loss scaled by batchsize File "/home/dhan/PycharmProjects/yolov7/utils/loss.py", line 585, in call bs, as, gjs, gis, targets, anchors = self.build_targets(p, targets, imgs) File "/home/dhan/PycharmProjects/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)

ybenabed commented 1 year ago

I resolved this problem by modifying the file utils/loss.py line 759. The idea is to put both variables in the same device, so I replaced from_which_layer = from_which_layer[fg_mask_inboxes] with from_which_layer = from_which_layer.to(fg_mask_inboxes.device)[fg_mask_inboxes] in order to put from_which_layerin the same device as fg_mask_inboxes. This resolved the problem and it is working properly.

eltonfernando commented 1 year ago

Thanks @ybenabed, this resolved for me.

np-n commented 1 year ago

@ybenabed Thanks, it works for me while training in Colab GPU.

ali-john commented 1 year ago

It did not work for me. Any other sol?

vs65497 commented 1 year ago

I resolved this problem by modifying the file utils/loss.py line 759. The idea is to put both variables in the same device, so I replaced from_which_layer = from_which_layer[fg_mask_inboxes] with from_which_layer = from_which_layer.to(fg_mask_inboxes.device)[fg_mask_inboxes] in order to put from_which_layerin the same device as fg_mask_inboxes. This resolved the problem and it is working properly.

this fix (and the one from stackoverflow) were working last week but they seem to have stopped working for me as well

FreeworkEarth commented 1 year ago

Any new solutions??? Urgent plsease help

impradeepkumar commented 3 months ago

0%| | 0/53 [00:02<?, ?it/s] Traceback (most recent call last): File "C:\Users\Pradeep Kumar\Documents\IEEE\GAN_YO_FBG\train_aux.py", line 612, in train(hyp, opt, device, tb_writer) File "C:\Users\Pradeep Kumar\Documents\IEEE\GAN_YO_FBG\train_aux.py", line 362, in train loss, loss_items = compute_loss_ota(pred, targets.to(device), imgs) # loss scaled by batch_size File "C:\Users\Pradeep Kumar\Documents\IEEE\GAN_YOFBG\utils\loss.py", line 1206, in call bs, as, gjs, gis, targets, anchors = self.build_targets(p[:self.nl], targets, imgs) File "C:\Users\Pradeep Kumar\Documents\IEEE\GAN_YO_FBG\utils\loss.py", line 1404, 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) PS C:\Users\Pradeep Kumar\Documents\IEEE\GAN_YO_FBG>

can any one help me to solve this??