Skip to content

A problem on autoanchors with tensor.shape #2394

Closed

Description

@GaryXXD

❔Question

Hi, I'm a rookie in object detection, I setup yolov5 environment completely and yolov5 can run successfully on dataset COCO128. (train, test, detect)
When I use my own dataset, a problem occurred and I don't know why and how to solve it.
My command setting is below:
Picture size are all 640x480 (original size is 4000x3000).
Model config is copied from yolov5s.yaml, but the number of classes is 89
python train.py
--weights "none"
--cfg "copy_from_5s.yaml"
--data "my_dataset.yaml"
--img_size 640 640
--name "my_dataset_test"
The settings which aren't desplay here are default.

螢幕擷取畫面 2021-03-08 141941

Please help me, thank you very much.

Activity

  1. github-actions commented on Mar 8, 2021

    @github-actions
    Contributor

    👋 Hello @GaryXXD, 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://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

    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 currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

  2. glenn-jocher commented on Mar 8, 2021

    @glenn-jocher
    Member

    @GaryXXD 👋 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.

  3. GaryXXD commented on Mar 15, 2021

    @GaryXXD
    Author

    @glenn-jocher Thanks for your response. I clone yoloV5 and check my environment again, but this problem still happens.
    Sadly, I can't reproduce the problem with dataset you mentioned.
    So, I trace the code in utils autoanchor.py and find out the problem is probably on scipy kmeans function.

    In utils.antoanchor, function kmean_anchors, row 121, the line of code is below

    k, dist = kmeans(wh / s, n, iter=30) # points, mean distance

    the scipy kmeans function cause the problem,
    e.g. In my case, probably because of lack of my own data, the labels are less various.
    When run the code above, n=9, the function output k only contains 6 points.
    This situation may cause the code in utils.antoanchor, row 49, which is below,

    m.anchor_grid[:] = new_anchors.clone().view_as(m.anchor_grid) # for inference

    to make mistake on reshaping from tensor(12 elements) to tensor(18 elements).
    I'm not sure whether it is bug or not.

    Thanks for spending time reading this

  4. glenn-jocher commented on Mar 15, 2021

    @glenn-jocher
    Member

    @GaryXXD oh, thanks for the details, interesting edge case failure mode.

    If there are 9 points requested but the scipy kmeans function only returns 6 points then yes autoanchor will break. Can you submit your labels.jpg and labels_correlogram.jpg? How many instances does your dataset have? Are most instances the same shape?

  5. glenn-jocher commented on Mar 15, 2021

    @glenn-jocher
    Member

    @GaryXXD maybe we could add an assert to at least better clarify the origin of the error:

        s = wh.std(0)  # sigmas for whitening
        k, dist = kmeans(wh / s, n, iter=30)  # points, mean distance
        assert len(k) == n, print(f'{prefix}ERROR: scipy.cluster.vq.kmeans requested {n} points but returned only {len(k)}')

    There is no good solution, as an incident like this is mainly an indicator of underlying data issues. The data appears suitable to the number of anchors requested, and the current architecture (3 or 4 output layers) may not be appropriate for monotonically sized data.

    The only real quick fix available is to skip autoanchor with python train.py --noautoanchor

  6. linked a pull request that will close this issueScipy kmeans-robust autoanchor update #2470on Mar 15, 2021
  7. GaryXXD commented on Mar 15, 2021

    @GaryXXD
    Author

    @glenn-jocher Thanks for your response again. My label examples are below,
    concat_outcome.txt

    There are 350 images, each image contains one object.
    These images are about screen dialoguebox detect, some of them are similar.
    I think it is the point which you mentioned.
    Thanks for your help.

  8. glenn-jocher commented on Mar 15, 2021

    @glenn-jocher
    Member

    @GaryXXD I've created a PR that should solve this in #2470

    Can you clone this branch and see if your issue is fixed with it?

    git clone https://github.com/ultralytics/yolov5 -b glenn-jocher-patch-1  # clone PR branch
  9. GaryXXD commented on Mar 15, 2021

    @GaryXXD
    Author

    @glenn-jocher It works. Now, it automatically skip anchor calcutlation if the length is not match.
    image

    Thanks a lot.

  10. glenn-jocher commented on Mar 15, 2021

    @glenn-jocher
    Member

    @GaryXXD ok, well that's an improvement at least. Though I see your BPR is only 0.65, which means that your recall and mAP will never be higher than 0.65. You should try to supply your own manual anchors in your yolov5s.yaml file if possible to better match your data:

    # anchors
    anchors:
    - [10,13, 16,30, 33,23] # P3/8
    - [30,61, 62,45, 59,119] # P4/16
    - [116,90, 156,198, 373,326] # P5/32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions