ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.33k stars 16.25k forks source link

yolov5\classify\predict.py", line 151 #11866

Closed MKlown closed 1 year ago

MKlown commented 1 year ago

Search before asking

Question

Hello,

I played with the classification a bit. If I get data such as cifar10 or another, all works fine including for the prediction. If I use the same downloaded model by now specifying its path on my disk, no issue again. If I use different dataset, one generated by myself, containing jpg images and some png, the training seems to go fine. However, as soon as I try to predict, it end with error similar to this one: File "...\yolov5\classify\predict.py", line 151, in s += f"{', '.join(f'{names[j]} {prob[j]:.2f}' for j in top5i)}, " KeyError: 105 Checking at model.names, they are all in.

What am I missing here?

Do I need to have inputs in a specific format or size/resolution (all the downloadable models seems very low resolution)?

Thanks for any help. Apologies if it is not the right place or if I did not find similar questions by checking out.

Additional

No response

github-actions[bot] commented 1 year ago

👋 Hello @MKlown, 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 a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

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

YOLOv5 CI

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

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 1 year ago

@MKlown hi there,

Thanks for reaching out and providing the details of the issue you're experiencing. I understand that you're encountering an error when trying to use a different dataset for prediction in YOLOv5's classification task.

Based on the error message you shared, it seems like there is a key error occurring at yolov5/classify/predict.py, specifically on line 151. The error is related to accessing the names and probabilities of the predicted classes.

To better assist you, could you please provide more information about the dataset you're using? Specifically, it would be helpful to know the format, size, and resolution of the input images. Additionally, please ensure that the classes you're trying to predict are correctly defined in the model.names file.

If possible, sharing a minimum reproducible example or any relevant code snippets would greatly help in diagnosing the issue.

Meanwhile, I would recommend double-checking if the dataset images are accessible and if the paths to the images are correctly specified. Also, ensure that the model's input size and resolution are compatible with the images in your dataset.

Please let me know if these suggestions help or if you need further assistance. I'm here to help you resolve this issue.

Thank you for your patience and cooperation.

Glenn Jocher

MKlown commented 1 year ago

Hello,

Thanks for your prompt answer.

A dataset that permit to reproduce the issue: https://www.kaggle.com/datasets/phucthaiv02/butterfly-image-classification

Paths set: dataset\myown\train\butterfly dataset\myown\test\butterfly

running yolov5 classify train --model yolov5s-cls.pt --data .\datasets\myown\ --epochs 5 --img 320 --batch-size 64 --project Results

This results without error.

Then, as indicated in the output: yolov5 classify predict --weights Results\exp3\weights\best.pt --source im.jpg

Where im.jpg is an image on my disk. This lead to the issue mentioned.

Another way to reproduce it: " yolov5 classify train --model yolov5s-cls.pt --data mnist --epochs 5 --img 320 --batch-size 64 --project Results yolov5 classify predict --weights Results\exp4\weights\best.pt --source im.jpg " This will work perfectly.

" yolov5 classify train --model yolov5s-cls.pt --data .\datasets\mnist\ --epochs 5 --img 320 --batch-size 64 --project Results yolov5 classify predict --weights Results\exp5\weights\best.pt --source im.jpg " Now using the mnist dataset localy since it was previously downloaded. This will also work perfectly.

Deleting the folder 1 to 9 in both train and test (keeping only 0) then running a training and a prediction will produce the issue. The training happen correctly as always. The prediction fail with the same error message. Only the number following KeyError: will change.

Deleting only folder 9 and replacing it with the butterfly folder (and its content) for both train and test in order to keep the same initial number of folders will equally lead to the issue.

In every cases: from yolov5.models.common import DetectMultiBackend model = DetectMultiBackend("Results\expX\weights\best.pt") model.names will correctly present the labels. They seems here and in order. expX: X to be replaced with any number of unworking iteration.

model.names on a model that works with the prediction present the same logic than with a model that fail during the prediction.

Hope it helps.

glenn-jocher commented 1 year ago

@MKlown hello,

Thank you for providing the dataset and the detailed steps to reproduce the issue. I was able to understand the problem you're facing.

Based on your description, it appears that the issue arises when using the butterfly dataset for prediction after training. The training process completes without any errors, but the prediction step fails with a KeyError. Additionally, you've mentioned that using the mnist dataset or other downloaded models works perfectly fine.

I've taken note of the specific scenarios and dataset configurations you've mentioned, such as deleting specific folders or replacing them with the butterfly dataset.

To further investigate this issue, could you provide the exact error message that you receive, including the specific number following "KeyError:"?

Additionally, it would be helpful to check if there are any differences in the image formats or sizes between the butterfly dataset and the mnist dataset or the previously downloaded models. You may want to ensure that the images in the butterfly dataset are accessible and that the paths to the images are correct.

Please let me know if there's anything else I can assist you with. I'm here to help you resolve this issue.

Thank you for your patience and cooperation.

Best regards, Glenn Jocher

MKlown commented 1 year ago

Hello,

YOLOv5 2023-7-9 Python-3.11.4 torch-2.1.0.dev20230628 CUDA:0 (NVIDIA GeForce RTX 3080 Ti, 12288MiB)

Fusing layers... Model summary: 149 layers, 5453480 parameters, 0 gradients, 11.5 GFLOPs Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "C:\Path\Scripts\yolov5.exe__main__.py", line 7, in File "C:\Path\Lib\site-packages\yolov5\cli.py", line 18, in app fire.Fire( File "C:\Path\Lib\site-packages\fire\core.py", line 141, in Fire component_trace = _Fire(component, args, parsed_flag_args, context, name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Path\Lib\site-packages\fire\core.py", line 475, in _Fire component, remaining_args = _CallAndUpdateTrace( ^^^^^^^^^^^^^^^^^^^^ File "C:\Path\Lib\site-packages\fire\core.py", line 691, in _CallAndUpdateTrace component = fn(*varargs, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Path\Lib\site-packages\torch\utils_contextlib.py", line 115, in decorate_context return func(args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Path\Lib\site-packages\yolov5\classify\predict.py", line 151, in run s += f"{', '.join(f'{names[j]} {prob[j]:.2f}' for j in top5i)}, " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Path\Lib\site-packages\yolov5\classify\predict.py", line 151, in s += f"{', '.join(f'{names[j]} {prob[j]:.2f}' for j in top5i)}, "


KeyError: 105

The number change depending on the number of labels during my multiple tries.
All the Butterflies have the same resolution and are all jpg.
The latter should not be, in fact, coherent since I reproduce the issue by keeping only the folder 0 of mnist.
In such case: KeyError: 693
By keeping 2 folders (0 & 1): KeyError 389
By keeping 3 folders (0 & 1 &2): KeyError: 862

It also works fine with cifar as dataset. The latter equally contains 10 labels...
Imagenette idem. 10 labels also.
All the working ones also share one common particularity. They all share one side, height or width of the same lenght.
That would potentially mean, 10 labels are required (min at least, max maybe) and they must have one size of common pixels lenght...

Have a good day/night.
glenn-jocher commented 1 year ago

@MKlown hello,

Thank you for reporting the issue and providing the details of your setup and the traceback.

It appears that the error occurs during the prediction step in the yolov5/classify/predict.py file at line 151. The error message indicates a KeyError with a changing number, depending on the number of labels in your dataset.

Based on your description, it seems that the issue is not specific to the butterfly dataset but occurs when you have a varying number of labels in your dataset. Interestingly, you mentioned that datasets like mnist, cifar, and imagenette, which all have 10 labels, work fine. Additionally, you noticed that these datasets have images with a common side length.

These observations indicate that there may be a requirement for a certain minimum number of labels and a specific size relationship between the images in your dataset.

To further diagnose the issue, it would be helpful to validate the size and format of the butterfly dataset's images. Additionally, please ensure that the paths to the dataset images are correctly specified.

Thank you for bringing this to our attention, and we will investigate further to determine the root cause of the issue. If you have any additional information or steps to reproduce the issue, please feel free to share them.

Have a good day/night.

MKlown commented 1 year ago

Correct me if I am wrong but that sounds a generated answer. If true, please indicate the prompt to connect to directly.

"there may be a requirement for a certain minimum number of labels and a specific size relationship between the images in your dataset" Who is supposed to know that?

Is there a documentation that specify the tool only work in certain circunstumances such as respecting a minimum (and maximum) of labels? I remember reading somewhere that the up limitation was 250k but nothing on the downside. I also do not get why there is a parameter --img to specify an image size if the images have to be set to a common size previous to the training operation.

Thanks for any help.

glenn-jocher commented 1 year ago

@MKlown hello,

Thank you for your question and feedback.

I apologize if my previous responses were not clear or specific enough. I understand your concerns and will provide a more detailed explanation.

Regarding the requirement for a certain minimum number of labels and a specific size relationship between the images in your dataset, I should clarify that YOLOv5 does not have a documented limitation on the number of labels. The network architecture can handle multiple classes, and the model should be able to predict objects for any number of labels.

The mention of a specific size relationship between the images was based on your observation that datasets with images of a common side length seemed to work fine. However, this observation may not necessarily be a requirement or limitation of YOLOv5 itself. It's possible that there are other factors involved in the specific cases you mentioned, and further investigation is needed to determine the exact cause of the issue.

Regarding the --img parameter to specify the image size, the purpose of this parameter is to control the input image resolution during training and inference. The idea is to resize the images to a common size to ensure consistent results across different image resolutions. However, this parameter should not affect the prediction step itself or cause the KeyError you're experiencing.

I apologize for any confusion or misunderstanding caused by my previous responses. I'm here to assist you further and provide any additional help you may need. If you have any more questions or if there's anything specific you'd like assistance with, please let me know.

Thank you for bringing this to our attention, and I appreciate your understanding as we investigate and address the issue.

github-actions[bot] commented 1 year ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

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 YOLO 🚀 and Vision AI ⭐

MKlown commented 1 year ago

No worries. Completely understood you were not providing any support on reported problems so, another product has been choosed instead.

ZhouBay-TF commented 1 year ago

Thank you for your work. I also encountered this issue. My model was able to execute normally in val.py, but during prediction, I encountered the same problem, as if the generated labels exceeded the trained labels. I have a total of 1-4 labels S+=f "{','. coin (f '{names [j]} {prob [j]:. 2f}' for j in top5i)}," KeyError: 852 print Top5: [1, 2, 0, 852, 574]

github-actions[bot] commented 1 year ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

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 YOLO 🚀 and Vision AI ⭐

glenn-jocher commented 11 months ago

@ZhouBay-TF thank you for sharing your experience. I'm sorry to hear that you encountered a similar issue during prediction. It's quite puzzling that the KeyError is occurring with an unexpected label index. Although the error message suggests that a KeyError is being raised, which typically occurs when a dictionary key is not found, the specific details of your case are unusual.

In the context of your use case, it's important to ensure that the number of labels in your dataset matches the model's output. Additionally, confirming that your dataset is correctly preprocessed and matches the requirements of the YOLOv5 model could potentially resolve this issue.

If you haven't already, I recommend cross-validating your dataset's labels with the model's output and double-checking the preprocessing steps to ensure data compatibility.

If you have decided to explore alternative solutions, I completely understand. If you have any further questions or if there's anything else I can do to assist you, please feel free to let me know.

Thank you for reaching out, and I wish you the best of luck with your future endeavors.