Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Sizes of tensors must match except in dimension 2. Got15 and 16(The offending index is 0) #1237

Closed
zorro2017zdc opened this issue Oct 29, 2020 · 16 comments
Labels
question Further information is requested Stale

Comments

@zorro2017zdc
Copy link

❔Question

I set image_size (320,320), origin image_size 600x450.
I used yolov5s.yaml , no problem. but when I changed yolov5s.yaml.
During training work , test work get following error.
RuntimeError: Sizes of tensors must match except in dimension 2.Got15 and 16(The offending index is 0)

Additional context

I changed yolov5s.yaml like this.
image
image

@zorro2017zdc zorro2017zdc added the question Further information is requested label Oct 29, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Oct 29, 2020

Hello @zorro2017zdc, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Jupyter Notebook Open In Colab, Docker Image, and Google Cloud Quickstart Guide for example environments.

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 model or data training question, please note Ultralytics does not provide free personal support. As a leader in vision ML and AI, we do offer professional consulting, from simple expert advice up to delivery of fully customized, end-to-end production solutions for our clients, such as:

  • Cloud-based AI systems operating on hundreds of HD video streams in realtime.
  • Edge AI integrated into custom iOS and Android apps for realtime 30 FPS video inference.
  • Custom data training, hyperparameter evolution, and model exportation to any destination.

For more information please visit https://www.ultralytics.com.

@glenn-jocher
Copy link
Member

glenn-jocher commented Oct 29, 2020

@zorro2017zdc I just tested your changes and started training and everything works correctly. I'm just going to link our default reply here:

Hello, thank you for your interest in our work! This issue seems to lack the minimum requirements for a proper response, or is insufficiently detailed for us to help you. Please note that most technical problems are due to:

  • Your changes to the default repository. If your issue is not reproducible in a new git clone version of this repository we can not debug it. Before going further run this code and ensure your issue persists:
sudo rm -rf yolov5  # remove existing
git clone https://github.com/ultralytics/yolov5 && cd yolov5 # clone latest
python detect.py  # verify detection
# CODE TO REPRODUCE YOUR ISSUE HERE
  • Your custom data. If your issue is not reproducible with COCO or COCO128 data we can not debug it. Visit our Custom Training Tutorial for guidelines on training your custom data. Examine train_batch0.jpg and test_batch0.jpg for a sanity check of training and testing data.

  • Your environment. If your issue is not reproducible in one of the verified environments below we can not debug it. If you are running YOLOv5 locally, ensure your environment meets all of the requirements.txt dependencies specified below.

If none of these apply to you, we suggest you close this issue and raise a new one using the Bug Report template, providing screenshots and minimum viable code to reproduce your issue. Thank you!

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.6. To install run:

$ pip install -r requirements.txt

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 passing. These tests evaluate proper operation of basic YOLOv5 functionality, including training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu.

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@AilvenLiu
Copy link

I meet the same problem when I using yolov5's detect.py to detect Scaled YOLOv4. When I resize the img_shape to hava same width and height, it runs. However, train.py without any problem. It seems some of models can only run with images having equal w and h.

@xiaowo1996
Copy link
Contributor

I met same problem, who solve it

@glenn-jocher
Copy link
Member

glenn-jocher commented Feb 20, 2021

@xiaowo1996 👋 Hello, thank you for your interest in our work! This issue seems to lack the minimum requirements for a proper response, or is insufficiently detailed for us to help you. Please note that most technical problems are due to:

  • Your modified or out-of-date code. If your issue is not reproducible in a new git clone version of this repo we can not debug it. Before going further run this code and verify your issue persists:
$ git clone https://github.com/ultralytics/yolov5 yolov5_new  # clone latest
$ cd yolov5_new
$ python detect.py  # verify detection

# CODE TO REPRODUCE YOUR ISSUE HERE
  • Your custom data. If your issue is not reproducible in one of our 3 common datasets (COCO, COCO128, or VOC) we can not debug it. Visit our Custom Training Tutorial for guidelines on training your custom data. Examine train_batch0.jpg and test_batch0.jpg for a sanity check of your labels and images.

  • Your environment. If your issue is not reproducible in one of the verified environments below we can not debug it. If you are running YOLOv5 locally, verify your environment meets all of the requirements.txt dependencies specified below. If in doubt, download Python 3.8.0 from https://www.python.org/, create a new venv, and install requirements.

If none of these apply to you, we suggest you close this issue and raise a new one using the 🐛 Bug Report template, providing screenshots and a minimum reproducible example of your issue. Thank you!

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

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 passing. These tests evaluate proper operation of basic YOLOv5 functionality, including training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu.

@HoseinHasani
Copy link

HoseinHasani commented Jul 29, 2021

I have the same problem, please help!
The model works fine only if I use 640x640 images (I trained the model with 300x300 size)!

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 29, 2021

@HoseinHasani 👋 hi, thanks for letting us know about this problem with YOLOv5 🚀. We've created a few short guidelines below to help users provide what we need in order to get started 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 that still produces the same problem
  • Complete – Provide all parts someone else needs to reproduce your problem in the question itself
  • Reproducible – Test the code you're about to provide to make sure it reproduces the problem

In addition to the above requirements, for Ultralytics to provide assistance your code should be:

  • Current – Verify that your code is up-to-date with current GitHub master, and if necessary git pull or git clone a new copy to ensure your problem has not already been resolved by previous commits.
  • Unmodified – Your problem must be reproducible without any modifications to the codebase in this repository. Ultralytics does not provide support for custom code ⚠️.

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

Thank you! 😃

@juny7878
Copy link

juny7878 commented May 13, 2022

@glenn-jocher. No more useless comments. plz

@suzijun
Copy link

suzijun commented May 19, 2022

i have the same problem when i use yolov5 l6。I just can set the image size 640 to val or detect my image。other size are the problem that ------ return torch.cat(x, self.d)
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 14 but got size 13 for tensor number 1 in the list.

@glenn-jocher
Copy link
Member

glenn-jocher commented May 19, 2022

@suzijun @HoseinHasani 👋 hi, thanks for letting us know about this possible problem with YOLOv5 🚀.

YOLOv5l6 inference runs correctly at any image size. Your issue in not reproducible.

Screenshot 2022-05-19 at 15 08 01

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! 😃

@suzijun
Copy link

suzijun commented May 19, 2022

Thank your reply,The image size I input can only be a multiple of 64. I don't know whether it is related to the step size of the model. I have visualized that the step size of Yolol6 is 64 while that of L is 32 So when I use yoloV5 L model input 416 image size detection is no problem.

@glenn-jocher
Copy link
Member

@suzijun yes all models have stride constraints, P5 = 2 ** 5 = 32, P6 = 2 ** 6 = 64 etc.

This is a fundamental property of all object detection models in AI. In any case YOLOv5 automatically handles incorrect sizes, so I have no idea how you are producing errors. As I said please supply a minimum reproducible example.
Screenshot 2022-05-19 at 15 37 52

@qingtianwu
Copy link

I met same problem, who solve it

+1

@gerrylwk
Copy link

Faced the same issue. The fix is to resize the image such that the image that is fed into the forward pass of the model should be suitable for the YOLOv5 architecture that you're using. E.g. For YOLOv5s, v5n, v5m, v5l, v5x, you should be using 640x640 images. Hope this helps!

@glenn-jocher
Copy link
Member

@gerrylwk thanks for lending a hand and providing a workaround for the issue! It's important to ensure that the input image size aligns with the specific YOLOv5 architecture being used. If anyone else is encountering this, resizing the image to adhere to the requirements of the YOLOv5 architecture being utilized should resolve the problem. Great insight!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

9 participants