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

Setting Custom Anchors in YOLOv5 for Mismatched Bounding Box Sizes #13437

Open
1 task done
shinzokuro opened this issue Nov 28, 2024 · 2 comments
Open
1 task done

Setting Custom Anchors in YOLOv5 for Mismatched Bounding Box Sizes #13437

shinzokuro opened this issue Nov 28, 2024 · 2 comments
Labels
detect Object Detection issues, PR's question Further information is requested

Comments

@shinzokuro
Copy link

Search before asking

Question

Hi,

I'm facing a challenge with my object detection model. My training data is quite limited, so I am using a different dataset for training and reserving my primary dataset for testing. However, there's a significant mismatch between the bounding box sizes in the training and test datasets. The objects in the test dataset are much closer to the camera, resulting in larger bounding boxes compared to the training dataset.

To address this, I decided to use the anchor sizes derived from the test dataset as custom anchors during the training process. After researching and experimenting, I found a way to set custom anchors by:

  1. Modifying the hyperparameter file to include my custom anchor sizes:
    image

  2. Changing a line in the YOLOv5 source code (yolov5/models/yolo.py):
    image

  3. Enabling the --noautoanchor flag during training.

I tried using the model.yaml file to set custom anchors as referenced in this [GitHub issue](#6838), but that approach didn't work for me.

Was my approach to setting custom anchors correct? Are there better or more efficient ways to achieve this? I'd greatly appreciate any insights or feedback.

Thanks in advance!

Additional

No response

@shinzokuro shinzokuro added the question Further information is requested label Nov 28, 2024
@UltralyticsAssistant UltralyticsAssistant added the detect Object Detection issues, PR's label Nov 28, 2024
@UltralyticsAssistant
Copy link
Member

👋 Hello @shinzokuro, thank you for your interest in YOLOv5 🚀! If you are looking to apply custom anchors for your training setup, it seems like you’ve put a lot of thought into your approach. Well done! Here are a few additional pointers to guide you:

If this is a 🐛 Bug Report, we recommend sharing a minimum reproducible example (MRE), including your model configurations, dataset details (if possible), and the exact steps you followed. This information will help us identify and debug any potential issues more efficiently.

For custom training or feature adjustment ❓ Questions, please provide additional context, such as your dataset distribution, training logs, and any errors or unexpected behaviors you’ve encountered during testing. This will help us better understand the challenges you are facing.

Requirements

Make sure you have Python>=3.8.0 installed, along with PyTorch>=1.8 and all dependencies specified in the requirements.txt file. To set up your environment:

git clone the YOLOv5 repository  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 can be used on local machines or via online environments such as Notebooks (e.g., Colab, Kaggle), cloud services with pre-installed deep learning environments (e.g., Google Cloud, AWS), or Docker containers. Using one of these verified environments can streamline your workflow and ensure compatibility with all dependencies.

Status

If you believe this might be related to an issue in the codebase, kindly check if the YOLOv5 Continuous Integration (CI) tests are passing. CI tests ensure the functionality of training, validation, inference, and other processes in YOLOv5 across supported platforms.

This is an automated response 🛠️ to help address your query more quickly. Rest assured, an Ultralytics engineer will review this soon and provide further assistance. If you encounter any reproducibility issues or need clarification, feel free to provide updates here! 😊

@pderrenger
Copy link
Member

Thank you for your detailed explanation. Your steps for setting custom anchors, including modifying the hyperparameter file, altering the YOLOv5 source code, and using the --noautoanchor flag, are valid. However, an easier approach would be to modify the anchors directly in the model.yaml file under the anchors parameter without altering the source code. Ensure the noautoanchor flag is enabled during training to prevent automatic anchor updates. You can find additional guidance on custom anchors in the YOLOv5 repository. If the issue persists, verify whether you're using the latest YOLOv5 version and that your anchor dimensions align with the test dataset bounding box distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
detect Object Detection issues, PR's question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants