ultralytics / yolov5

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

Convert closed-loop binary mask to yolo format polygon #12332

Closed halqadasi closed 10 months ago

halqadasi commented 11 months ago

Hello, I am trying to train yolov5 or yolov8 with cracks segmentation data. I read this issue posted below but it did not solve my problem since my binary mask is a bit tricky as shown below, it contains closed loop.

Volker_DSC01674_0_1080_1601_1598 The cracks image

Volker_DSC01674_0_1080_1601_1598 The binary mask

Could you please help me out with this problem since I spent hours?

Thank you for reaching out. You can convert your binary mask images to YOLO annotation format by using a script that extracts the bounding box coordinates from each mask and saves them in a text file with the same name as the corresponding image.

The YOLOv8 repo offers a useful script, create_masks.py, which can be used to generate YOLO annotations from binary masks. It converts each mask to a grayscale image, thresholded at 127, and then creates a bounding box around each object in the image. The script then saves the bounding box coordinates in a text file with the same name as the corresponding image.

Once you have generated the YOLO annotations, you can use them to train your YOLOv8 model on your dataset.

I hope this helps! Let me know if you have any further questions.

Best regards, Glenn Jocher Ultralytics Team

Originally posted by @glenn-jocher in https://github.com/ultralytics/ultralytics/issues/3085#issuecomment-1583067093

github-actions[bot] commented 11 months ago

πŸ‘‹ Hello @halqadasi, 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.8.0 with all requirements.txt installed including PyTorch>=1.8. 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 11 months ago

@halqadasi thank you for reaching out. You can convert your binary mask images to YOLO annotation format by using a script that extracts the bounding box coordinates from each mask and saves them in a text file with the same name as the corresponding image.

The YOLOv8 repo offers a useful script, create_masks.py, which can be used to generate YOLO annotations from binary masks. It converts each mask to a grayscale image, thresholded at 127, and then creates a bounding box around each object in the image. The script then saves the bounding box coordinates in a text file with the same name as the corresponding image.

Once you have generated the YOLO annotations, you can use them to train your YOLOv8 model on your dataset.

I hope this helps! Let me know if you have any further questions.

Ultralytics Team

halqadasi commented 11 months ago

@glenn-jocher , thank you for your reply. But you only copied and pasted your response from the quote I posted in my issue. I mentioned that I already read your response in the previous thread, but could you please take a minute to see how the binary mask is complicated and tricky, could you please answer these 3 questions:

  1. How can you draw a polygon for a binary mask containing a closed loop?
  2. How can you count the number of polygons?
  3. Could you please refer me to the create_masks.py since I searched in this repo and the yolov8 repo and I did not find this file?
glenn-jocher commented 11 months ago

Hi @halqadasi, my apologies for the confusion. I appreciate your patience. Let me address your questions:

  1. In order to draw a polygon for a binary mask containing a closed loop, you can use computer vision techniques. One approach is to use contour detection to identify the shapes in your binary mask. Once you have the contours, you can approximate them into polygons using techniques like the Ramer-Douglas-Peucker algorithm or the Freeman chain code.

  2. To count the number of polygons in your binary mask, you can use the same contour detection approach mentioned above. After detecting the contours, you can iterate over them and count the number of polygons found.

  3. I apologize for the confusion, but there is no create_masks.py script in the YOLOv5 or YOLOv8 repos. I must have made an error in my previous response. I apologize for any inconvenience caused.

If you need further assistance with implementing these steps or have any other questions, please let me know. I'm here to help.

Ultralytics Team

github-actions[bot] commented 10 months 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 ⭐