ultralytics / yolov5

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

How to solve KeyError: 1 while validating best custom model with plots? #5650

Closed mobassir94 closed 2 years ago

mobassir94 commented 3 years ago

Search before asking

Question

i am trying to train yolov5 on a custom dataset containing 3 classes.once the training is done,during validation i get this error :


Validating runs/train/Result4/weights/best.pt...
Fusing layers... 
Model Summary: 476 layers, 87212152 parameters, 0 gradients, 217.1 GFLOPs
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100% 1/1 [00:04<00:00,  4.19s/it]
Traceback (most recent call last):
  File "train.py", line 625, in <module>
    main(opt)
  File "train.py", line 522, in main
    train(opt.hyp, opt, device, callbacks)
  File "train.py", line 429, in train
    compute_loss=compute_loss)  # val best model with plots
  File "/usr/local/lib/python3.7/dist-packages/torch/autograd/grad_mode.py", line 28, in decorate_context
    return func(*args, **kwargs)
  File "/content/val.py", line 240, in run
    p, r, ap, f1, ap_class = ap_per_class(*stats, plot=plots, save_dir=save_dir, names=names)
  File "/content/utils/metrics.py", line 78, in ap_per_class
    plot_mc_curve(px, f1, Path(save_dir) / 'F1_curve.png', names, ylabel='F1')
  File "/content/utils/metrics.py", line 323, in plot_mc_curve
    ax.plot(px, y, linewidth=1, label=f'{names[i]}')  # plot(confidence, metric)
KeyError: 1

even though i wrote custom yaml file like this :

%%writetemplate /content/DataFile/data.yaml

train: /content/convertor/fold0/images/train 
val: /content/convertor/fold0/images/val 
test: /content/convertor/fold0/images/val 
nc: 3 
names: ["name","age","mobile"] 

i still get the error above and while inferring, all the bounding boxes are given "name" as label ,instead of predicting these 3 classes names: ["name","age","mobile"] my model always predicts "name" for all 3 fields(please check the last image attached in the colab code): here is the code with outputs and errors : https://colab.research.google.com/drive/1Pyz5hCi0fpmQ4lFV9rvAcdMp2yg9KN1P#scrollTo=ogZdrD4OY2Wg

how can i solve this issue? thanks in advance

Additional

no

github-actions[bot] commented 3 years ago

πŸ‘‹ Hello @mobassir94, 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.

mobassir94 commented 3 years ago

instead of names: ["name","age","mobile"] it will be : names: ['name','age','mobile']

sorry for the silly mistake!

mobassir94 commented 3 years ago

sorry the problem not finished yet,,got same error again, so it's names: ['name','age','mobile'] not the solution

glenn-jocher commented 3 years ago

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

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

mobassir94 commented 2 years ago

solved https://github.com/ultralytics/yolov5/issues/5662

raftaarrashedin commented 4 months ago

How did you solved it ?

glenn-jocher commented 4 months ago

Hi @raftaarrashedin,

Thank you for following up! I'm glad to hear that you managed to resolve the issue. For the benefit of others who might encounter a similar problem, could you please share the solution you found?

If anyone else is experiencing similar issues, here are a few steps you can take to troubleshoot:

  1. Verify Dataset Configuration: Ensure your data.yaml file is correctly formatted and paths are accurate. For example:

    train: /path/to/train/images
    val: /path/to/val/images
    nc: 3
    names: ['name', 'age', 'mobile']
  2. Check Label Files: Make sure your label files are in the correct YOLO format, with one .txt file per image and each line in the format class x_center y_center width height.

  3. Update to Latest Versions: Ensure you are using the latest versions of YOLOv5 and PyTorch. You can update YOLOv5 with:

    git pull
    pip install -r requirements.txt
  4. Reproduce the Issue: If the problem persists, please provide a minimal, reproducible example so we can investigate further. You can find more details on creating a reproducible example here.

Sharing your solution will greatly help the community. Thank you for your cooperation and happy coding! πŸš€