ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
51.26k stars 16.44k forks source link

when i using detect.py in win10! File "\general.py", line 484, in non_max_suppression nc = prediction.shape[2] - 5 # number of classes IndexError: tuple index out of range #4430

Closed whiteless9 closed 3 years ago

whiteless9 commented 3 years ago

❔Question

Additional context

github-actions[bot] commented 3 years ago

πŸ‘‹ Hello @whiteless9, 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 Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

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

$ git clone https://github.com/ultralytics/yolov5
$ cd yolov5
$ 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 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.

glenn-jocher commented 3 years ago

@whiteless9 πŸ‘‹ 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:

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

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! πŸ˜ƒ

Hezhexi2002 commented 3 years ago

I meet the same error and it works well before,I don't know what happened

glenn-jocher commented 3 years ago

@Hezhexi2002 πŸ‘‹ 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:

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

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! πŸ˜ƒ

github-actions[bot] commented 3 years ago

πŸ‘‹ 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:

Access additional Ultralytics ⚑ resources:

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

Alek-dr commented 2 years ago

@whiteless9 I got same error after git pool Fix - add dim before nms:

pred = torch.unsqueeze(pred, 0)

pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
RoyCopter commented 2 years ago

@Alek-dr @glenn-jocher I had the same problem using YOLOv5 v6.1 with my custom code. (I did everything that required like the original code, but for some reason it throw that error).

after adding

pred = torch.unsqueeze(pred, 0)

it solved as @Alek-dr said.

thanks!

glenn-jocher commented 2 years ago

@whiteless9 @Alek-dr @RoyCopter can you help us by supplying a reproducible example that we can use to view the error ourselves?

FaragSeif commented 2 years ago

@glenn-jocher I'm having the same issue, also using Windows 10. Here is the minimum reproducible code:

import numpy as np
import yolov5
from yolov5.utils.general import non_max_suppression

model = yolov5.load("./yolov5s.pt")

results = model(np.zeros((3, 640, 640)))
pred = results.pred[0]
print(non_max_suppression(pred))

It throws the following error:

Traceback (most recent call last):
  File "yolo_test.py", line 9, in <module>
    print(non_max_suppression(pred))
  File "venv\lib\site-packages\yolov5\utils\general.py", line 1055, in non_max_suppression
    nc = prediction.shape[2] - nm - 5  # number of classes
IndexError: tuple index out of range

Am I perhaps using it incorrectly?

glenn-jocher commented 2 years ago

@SeifAbdElrhman πŸ‘‹ Hello! Thanks for asking about handling inference results. YOLOv5 πŸš€ PyTorch Hub models allow for simple model loading and inference in a pure python environment without using detect.py.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # yolov5n - yolov5x6 official model
#                                            'custom', 'path/to/best.pt')  # custom model

# Images
im = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, URL, PIL, OpenCV, numpy, list

# Inference
results = model(im)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.
results.xyxy[0]  # im predictions (tensor)

results.pandas().xyxy[0]  # im predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

results.pandas().xyxy[0].value_counts('name')  # class counts (pandas)
# person    2
# tie       1

See YOLOv5 PyTorch Hub Tutorial for details.

Good luck πŸ€ and let us know if you have any other questions!

SuperMaximus1984 commented 2 years ago

Same error here. Appeared all of a sudden in the loop if inferences (same source). What may it be?

Traceback (most recent call last):
  File "/home/max/yolov5-flask/camdetect.py", line 56, in <module>
    results = model('http://192.168.0.6/jpg/1/image.jpg', size=2688)
  File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/root/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 689, in forward
    if im.shape[0] < 5:  # image in CHW
IndexError: tuple index out of range
glenn-jocher commented 2 years ago

@SuperMaximus1984 πŸ‘‹ 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:

For Ultralytics to provide assistance your code should also be:

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! πŸ˜ƒ

nolby003 commented 3 months ago

I am opening this back up as this is not resolved. I classified using Yolov8, but I am using Yolov5 to detect , and getting this same error, could it be that Yolov8 generates a weight incompatible with yolov5?

glenn-jocher commented 3 months ago

It's likely that YOLOv8 weights are incompatible with YOLOv5. Please ensure you use YOLOv5-trained weights with YOLOv5 models.