yolov5 icon indicating copy to clipboard operation
yolov5 copied to clipboard

RuntimeError: result type Float can't be cast to the desired output type long int

Open happylulu666 opened this issue 2 years ago ā€¢ 17 comments

Search before asking

  • [X] I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

No response

Bug

Today, it threw an error while running train.py as follows. Before this, I successfully completed the training process. I have not made any changes to the document.

Traceback (most recent call last): File "train.py", line 650, in main(opt) File "train.py", line 546, in main train(opt.hyp, opt, device, callbacks) File "train.py", line 337, in train loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size File "/yolov5/utils/loss.py", line 121, in call tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets File "yolov5/utils/loss.py", line 220, in build_targets indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices RuntimeError: result type Float can't be cast to the desired output type long int

Environment

YOLOv5 šŸš€ v6.1-11-g63ddb6f0 torch 1.12.0+cu102 GPU

colab

Python:3.8

GPU:Tesla P100 16280MiB

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • [ ] Yes I'd like to help by submitting a PR!

happylulu666 avatar Jun 30 '22 07:06 happylulu666

šŸ‘‹ Hello @happylulu666, thank you for your interest in YOLOv5 šŸš€! Please visit our ā­ļø Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a šŸ› Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ā“ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email [email protected].

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

github-actions[bot] avatar Jun 30 '22 07:06 github-actions[bot]

@happylulu666 this is an incompatibility of older versions of YOLOv5 with torch 1.12. Updating YOLOv5 will resolve this.

To update YOLOv5:

  • Git ā€“ git pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub ā€“ Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks ā€“ View updated notebooks Open In Colab Open In Kaggle
  • Docker ā€“ sudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

glenn-jocher avatar Jun 30 '22 15:06 glenn-jocher

thank you

YuWhale avatar Jun 30 '22 15:06 YuWhale

git pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5

this does not solve the problem, still getting same error

been using the same script in Colab, and this issue only start from yesterday

yuhe0925 avatar Jul 13 '22 02:07 yuhe0925

@yuhe0925 this issue has been resolved in master, please update your code and make sure you are using the latest compatible version of torch==1.11.0. If this doesn't resolve your issue then please submit a minimum reproducible example to help us investigate.

How to create a Minimal, Reproducible Example

When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:

  • āœ… Minimal ā€“ Use as little code as possible to produce the problem
  • āœ… Complete ā€“ Provide all parts someone else needs to reproduce the problem
  • āœ… Reproducible ā€“ Test the code you're about to provide to make sure it reproduces the problem

For Ultralytics to provide assistance your code should also be:

  • āœ… Current ā€“ Verify that your code is up-to-date with GitHub master, and if necessary git pull or git clone a new copy to ensure your problem has not already been solved in master.
  • āœ… Unmodified ā€“ Your problem must be reproducible using official YOLOv5 code without changes. Ultralytics does not provide support for custom code āš ļø.

If you believe your problem meets all the above criteria, please close this issue and raise a new one using the šŸ› Bug Report template with a minimum reproducible example to help us better understand and diagnose your problem.

Thank you! šŸ˜ƒ

glenn-jocher avatar Jul 13 '22 09:07 glenn-jocher

I had the same issue and since I have made a lot of changes to the base code I couldn't pull master branch, thanks to glenn's comment I solved this error by editing the requirements.txt file and setting torch version to == 1.7.0 and it solved the issue

hoseinhamzei avatar Jul 13 '22 11:07 hoseinhamzei

I had the same issue and since I have made a lot of changes to the base code I couldn't pull master branch, thanks to glenn's comment I solved this error by editing the requirements.txt file and setting torch version to == 1.7.0 and it solved the issue

I got the same issue and setup torch to version 1.7.0 solved it. Thanks dude....

oki-aryawan avatar Jul 15 '22 15:07 oki-aryawan

My same issue was resolved by changing requirement.txt file . Torch version >1.7.0 when changed to ==1.7.0 resolved the issue.

mishi-sarfraz avatar Jul 17 '22 07:07 mishi-sarfraz

My same issue was resolved by changing requirement.txt file . Torch version >1.7.0 when changed to ==1.7.0 resolved the issue.

I tried this method, but it showed that No matching distribution found for torch==1.7.0

molua avatar Jul 17 '22 11:07 molua

Did u check it in Requirement.txt?

On Sun, Jul 17, 2022 at 4:13 PM GongZu @.***> wrote:

My same issue was resolved by changing requirement.txt file . Torch version >1.7.0 when changed to ==1.7.0 resolved the issue.

I tried this method, but it showed that No matching distribution found for torch==1.7.0

ā€” Reply to this email directly, view it on GitHub https://github.com/ultralytics/yolov5/issues/8405#issuecomment-1186485226, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALTFBVIWXEB6FTRLNEI24VLVUPTD7ANCNFSM52IJGYNA . You are receiving this because you commented.Message ID: @.***>

mishi-sarfraz avatar Jul 17 '22 12:07 mishi-sarfraz

@happylulu666 @yuhe0925 @hoseinhamzei @happylulu666 @oki-aryawan @molua @mishi-sarfraz good news šŸ˜ƒ! This issue should now be fixed āœ… in PR #8067 from June 1st. To receive this update:

  • Git ā€“ git pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub ā€“ Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks ā€“ View updated notebooks Open In Colab Open In Kaggle
  • Docker ā€“ sudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 šŸš€!

glenn-jocher avatar Jul 18 '22 12:07 glenn-jocher

Thanks for Glenn's kind reply and useful advice! I would also like to thank the friends who have the same problems as me for discussing this.

happylulu666 avatar Jul 19 '22 13:07 happylulu666

I have the same issue on Windows, so I uninstalled torch (1.12) and tried installing 1.7.0, but that doesn't match with my CUDA 11.3. I then safely moved to Linux , where I have torch 1.12 and CUDA 10.2.

bit-scientist avatar Aug 04 '22 07:08 bit-scientist

I have the same issue on Windows, so I uninstalled torch (1.12) and tried installing 1.7.0, but that doesn't match with my CUDA 11.3. I then safely moved to Linux , where I have torch 1.12 and CUDA 10.2.

I'm having the same issue as well. Torch version 1.7.0 is not compatible with CUDA 11.3.

plastic-waste-database avatar Aug 05 '22 03:08 plastic-waste-database

Bug File "train.py", line 642, in main(opt) File "train.py", line 538, in main train(opt.hyp, opt, device, callbacks) File "train.py", line 330, in train loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size File "C:\Users\owent\Desktop\yolov5 (latest)\utils\loss.py", line 120, in call tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets File "C:\Users\owent\Desktop\yolov5 (latest)\utils\loss.py", line 217, in build_targets indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices RuntimeError: result type Float can't be cast to the desired output type __int64

Environment Python: 3.8 CUDA: 11.3 Torch version: 1.12.1 Windows: 11

Note I'm running the code in Jupyter and also have updated YOLOv5. But still facing the bug errors as shown above. Really need help with these issues :(

plastic-waste-database avatar Aug 05 '22 03:08 plastic-waste-database

@owentamin1996 update your YOLOv5 code, it's out of date. To update:

  • Git ā€“ git pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub ā€“ Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks ā€“ View updated notebooks Open In Colab Open In Kaggle
  • Docker ā€“ sudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

glenn-jocher avatar Aug 05 '22 15:08 glenn-jocher

Simply use, install torch==1.11, torchvision==0.12, torchtext==0.12, torchaudio==0.11 Compatible versions for torch 1.11 No error will be reflected.

Benjamin2506 avatar Aug 06 '22 11:08 Benjamin2506

I have solved the issue by downgrading Torch version to 1.10.1. However, another problem arise in which the value for P,R and other relevant metrics are all zero. Iā€™m using CUDA 11.3 for running the code.

6A2505B1-8B38-4DA3-851A-F4EE63014831

plastic-waste-database avatar Aug 15 '22 08:08 plastic-waste-database

@happylulu666 this is an incompatibility of older versions of YOLOv5 with torch 1.12. Updating YOLOv5 will resolve this.

To update YOLOv5:

  • Git ā€“ git pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub ā€“ Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks ā€“ View updated notebooks Open In Colab Open In Kaggle
  • Docker ā€“ sudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

I have done this but still getting the same error "RuntimeError: result type Float can't be cast to the desired output type long int"

lucysumi avatar Aug 18 '22 03:08 lucysumi

@lucysumi šŸ‘‹ hi, thanks for letting us know about this possible problem with YOLOv5 šŸš€. We've created a few short guidelines below to help users provide what we need in order to start investigating a possible problem.

How to create a Minimal, Reproducible Example

When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:

  • āœ… Minimal ā€“ Use as little code as possible to produce the problem
  • āœ… Complete ā€“ Provide all parts someone else needs to reproduce the problem
  • āœ… Reproducible ā€“ Test the code you're about to provide to make sure it reproduces the problem

For Ultralytics to provide assistance your code should also be:

  • āœ… Current ā€“ Verify that your code is up-to-date with GitHub master, and if necessary git pull or git clone a new copy to ensure your problem has not already been solved in master.
  • āœ… Unmodified ā€“ Your problem must be reproducible using official YOLOv5 code without changes. Ultralytics does not provide support for custom code āš ļø.

If you believe your problem meets all the above criteria, please close this issue and raise a new one using the šŸ› Bug Report template with a minimum reproducible example to help us better understand and diagnose your problem.

Thank you! šŸ˜ƒ

glenn-jocher avatar Aug 18 '22 09:08 glenn-jocher

Seeing same error, here are my versions

Type "help", "copyright", "credits" or "license" for more information.
>>> import yolov5
>>> yolov5.__version__
'6.1.8'


>>> import torch
>>> torch.__version__
'1.12.1+cu116'

apiszcz avatar Sep 01 '22 09:09 apiszcz

@apiszcz yolov5 pip package has nothing to do with Ultralytics. To use correct code install using git clone commands in readme.

glenn-jocher avatar Sep 01 '22 10:09 glenn-jocher

will do/try next, thank you. git clone update worked! I guess i need to do that more frequently. Thank you for yv5 and the fast response.

apiszcz avatar Sep 01 '22 10:09 apiszcz

šŸ‘‹ Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 šŸš€ resources:

  • Wiki ā€“ https://github.com/ultralytics/yolov5/wiki
  • Tutorials ā€“ https://docs.ultralytics.com/yolov5
  • Docs ā€“ https://docs.ultralytics.com

Access additional Ultralytics āš” resources:

  • Ultralytics HUB ā€“ https://ultralytics.com/hub
  • Vision API ā€“ https://ultralytics.com/yolov5
  • About Us ā€“ https://ultralytics.com/about
  • Join Our Team ā€“ https://ultralytics.com/work
  • Contact Us ā€“ https://ultralytics.com/contact

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 šŸš€ and Vision AI ā­!

github-actions[bot] avatar Oct 03 '22 00:10 github-actions[bot]

This may also have something to do with the version of onnxruntime, which was resolved when I went back to onnxruntime1.10.0

tinason-yang avatar Oct 06 '22 08:10 tinason-yang

šŸ‘‹ Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 šŸš€ resources:

  • Wiki ā€“ https://github.com/ultralytics/yolov5/wiki
  • Tutorials ā€“ https://docs.ultralytics.com/yolov5
  • Docs ā€“ https://docs.ultralytics.com

Access additional Ultralytics āš” resources:

  • Ultralytics HUB ā€“ https://ultralytics.com/hub
  • Vision API ā€“ https://ultralytics.com/yolov5
  • About Us ā€“ https://ultralytics.com/about
  • Join Our Team ā€“ https://ultralytics.com/work
  • Contact Us ā€“ https://ultralytics.com/contact

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 šŸš€ and Vision AI ā­!

github-actions[bot] avatar Nov 07 '22 00:11 github-actions[bot]

In loss.py (line 173 ), gain = torch.ones(7, device=targets.device) ā†’ gain = torch.ones(7, device=targets.device).long()

Tefor avatar Dec 05 '22 10:12 Tefor

In loss.py (line 173 ), gain = torch.ones(7, device=targets.device) ā†’ gain = torch.ones(7, device=targets.device).long()

Thank you. You are right

liyanluo avatar Dec 06 '22 09:12 liyanluo

@Tefor @liyanluo is this a change that we should apply to master, or are you applying this as a patch to older code?

glenn-jocher avatar Dec 06 '22 21:12 glenn-jocher

'gain = torch.ones(7, device=targets.device).long()' worked for me. Thank you @Tefor

Zafar343 avatar Dec 09 '22 07:12 Zafar343