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.26k stars 4.18k forks source link

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

Open hamdimina opened 1 year ago

hamdimina commented 1 year ago

I'm training yolov7 on a custom dataset using colab:

!python train.py --batch 12 --cfg cfg/training/yolov7_custom.yaml --epochs 50 --data data/custom_data.yaml --weights 'yolov7.pt' --device 0 and i face this issue below:

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 "/content/gdrive/MyDrive/yolov7/yolov7/utils/loss.py", line 585, in call bs, as, gjs, gis, targets, anchors = self.build_targets(p, targets, imgs) File "/content/gdrive/MyDrive/yolov7/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)

please anyone can help me!

jro17002 commented 1 year ago

This worked for me Changing line 685 in utils/loss.py to: from_which_layer.append((torch.ones(size=(len(b),)) * i).to('cuda'))

and adding a new line after 756 fg_mask_inboxes = fg_mask_inboxes.to(torch.device('cuda'))

p0wned17 commented 1 year ago

This worked for me and also to most people

Changing line 685 in utils/loss.py to: from_which_layer.append(torch.ones(size=(len(b), ), device=targets.device) * i) and that's it.

dsbyprateekg commented 1 year ago

I have also faced the same error in Colab for training with W6 weight file- image

But changing the above-mentioned could not solve the error.

hamdimina commented 1 year ago

This worked for me and also to most people

Changing line 685 in utils/loss.py to: from_which_layer.append(torch.ones(size=(len(b), ), device=targets.device) * i) and that's it.

can you please provide how to put the command and change the line, cause i'm new in this domain and my knowledge are not wide and thanks

p0wned17 commented 1 year ago

This worked for me and also to most people Changing line 685 in utils/loss.py to: from_which_layer.append(torch.ones(size=(len(b), ), device=targets.device) * i) and that's it.

can you please provide how to put the command and change the line, cause i'm new in this domain and my knowledge are not wide and thanks

you work in google colab?

hamdimina commented 1 year ago

This worked for me and also to most people Changing line 685 in utils/loss.py to: from_which_layer.append(torch.ones(size=(len(b), ), device=targets.device) * i) and that's it.

can you please provide how to put the command and change the line, cause i'm new in this domain and my knowledge are not wide and thanks

you work in google colab?

yes

p0wned17 commented 1 year ago

This worked for me and also to most people Changing line 685 in utils/loss.py to: from_which_layer.append(torch.ones(size=(len(b), ), device=targets.device) * i) and that's it.

can you please provide how to put the command and change the line, cause i'm new in this domain and my knowledge are not wide and thanks

you work in google colab?

yes

you have telegram, I can help you there?

kuotunyu commented 1 year ago

1210

Colab

!sed -i '759s/from_which_layer[fg_mask_inboxes]/from_which_layer.to(fg_mask_inboxes.device)[fg_mask_inboxes]/' /content/gdrive/MyDrive/yolov7/yolov7/utils/loss.py
!sed -n -e 759p /content/gdrive/MyDrive/yolov7/yolov7/utils/loss.py
LeAyky commented 1 year ago

I still get the error mentioned above.

Is there any news on a fix?

AdeelH commented 1 year ago

A lot of the code in this file is repeated, so it is important to fix all occurrences.

What worked for me is replacing all occurrences of the line

from_which_layer = torch.cat(from_which_layer, dim=0)

with

from_which_layer = torch.cat(from_which_layer, dim=0).to(targets.device)