ultralytics / ultralytics

NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
28.06k stars 5.58k forks source link

augmentation multiple apply ? #14348

Open true5525 opened 1 month ago

true5525 commented 1 month ago

Search before asking

Question

First of all, thank you for developing YOLO. I have tested data augmentation during YOLO training. I am using zoom in, zoom out, vertical flip, and horizontal flip. Through testing, I found that the original image or the augmented image can be selectively used based on the probability at each epoch. However, it seems that the augmentation techniques are not applied in a multi-step manner. For example, it doesn't seem possible to apply zoom out + vertical flip or vertical flip + horizontal flip + zoom in in a multiplicative way. What part of the code should be modified to allow for the application of multiple augmentations? Thank you.

Additional

scale: 0.2 # image scale (+/- gain) shear: 0.0 # image shear (+/- deg) perspective: 0.0 # image perspective (+/- fraction), range 0-0.001 flipud: 0.3 # image flip up-down (probability) fliplr: 0.3 # image flip left-right (probability) mosaic: 0.0 # image mosaic (probability) mixup: 0.0 # image mixup (probability)

github-actions[bot] commented 1 month ago

👋 Hello @true5525, thank you for your interest in Ultralytics YOLOv8 🚀! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

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.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 1 month ago

Hello,

Thank you for your kind words and for using YOLO! 😊

To address your question about applying multiple augmentations in a multi-step manner, you are correct that the current augmentation pipeline in YOLOv8 applies each augmentation independently based on their respective probabilities. If you want to apply combinations of augmentations (e.g., zoom out + vertical flip), you would need to modify the augmentation pipeline.

Here's a high-level approach to achieve this:

  1. Custom Augmentation Function: You can create a custom augmentation function that applies a sequence of augmentations in the desired order. This function can then be integrated into the training pipeline.

  2. Modify the Dataset Class: Update the dataset class to use your custom augmentation function instead of the default one.

Below is an example of how you might implement a custom augmentation function in Python:

import cv2
import numpy as np

def custom_augment(image):
    # Example: Apply zoom out + vertical flip
    zoom_out_prob = 0.5
    flipud_prob = 0.5

    if np.random.rand() < zoom_out_prob:
        h, w = image.shape[:2]
        scale = 0.8  # Zoom out by 20%
        new_h, new_w = int(h * scale), int(w * scale)
        image = cv2.resize(image, (new_w, new_h))
        pad_h, pad_w = (h - new_h) // 2, (w - new_w) // 2
        image = cv2.copyMakeBorder(image, pad_h, pad_h, pad_w, pad_w, cv2.BORDER_CONSTANT, value=(0, 0, 0))

    if np.random.rand() < flipud_prob:
        image = cv2.flip(image, 0)  # Flip vertically

    return image

To integrate this function into the YOLO training pipeline, you would need to modify the dataset loading part of the code to apply custom_augment to each image.

For more detailed guidance on modifying the dataset class and integrating custom augmentations, you can refer to the Ultralytics documentation.

Additionally, if you haven't already, please ensure you are using the latest version of the Ultralytics package to benefit from the latest features and bug fixes.

If you need further assistance or have more questions, feel free to ask!

true5525 commented 1 month ago

Thank you very much for your response. I misunderstood before. Through more testing, I have confirmed that the yolo package applies augmentation to images in a multiple way. I have also checked the dataset.py code. By the way, the reason why I wanted to use the yolo package for augmentation instead of doing it manually is because of the coordinate data transformation in labels. Thank you for your answer. Have a great day.

-----Original Message----- From: "Glenn @.> To: @.>; Cc: @.>; @.>; Sent: 2024-07-11 (목) 16:57:06 (GMT+09:00) Subject: Re: [ultralytics/ultralytics] augmentation multiple apply ? (Issue #14348)

Hello, Thank you for your kind words and for using YOLO! 😊 To address your question about applying multiple augmentations in a multi-step manner, you are correct that the current augmentation pipeline in YOLOv8 applies each augmentation independently based on their respective probabilities. If you want to apply combinations of augmentations (e.g., zoom out + vertical flip), you would need to modify the augmentation pipeline. Here's a high-level approach to achieve this:

Custom Augmentation Function: You can create a custom augmentation function that applies a sequence of augmentations in the desired order. This function can then be integrated into the training pipeline.

Modify the Dataset Class: Update the dataset class to use your custom augmentation function instead of the default one.

Below is an example of how you might implement a custom augmentation function in Python: import cv2 import numpy as np def custom_augment(image): # Example: Apply zoom out + vertical flip zoom_out_prob = 0.5 flipud_prob = 0.5 if np.random.rand() < zoom_out_prob: h, w = image.shape[:2] scale = 0.8 # Zoom out by 20% new_h, new_w = int(h scale), int(w scale) image = cv2.resize(image, (new_w, new_h)) pad_h, pad_w = (h - new_h) // 2, (w - new_w) // 2 image = cv2.copyMakeBorder(image, pad_h, pad_h, pad_w, pad_w, cv2.BORDER_CONSTANT, value=(0, 0, 0)) if np.random.rand() < flipud_prob: image = cv2.flip(image, 0) # Flip vertically return image To integrate this function into the YOLO training pipeline, you would need to modify the dataset loading part of the code to apply custom_augment to each image. For more detailed guidance on modifying the dataset class and integrating custom augmentations, you can refer to the Ultralytics documentation. Additionally, if you haven't already, please ensure you are using the latest version of the Ultralytics package to benefit from the latest features and bug fixes. If you need further assistance or have more questions, feel free to ask! — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

glenn-jocher commented 1 month ago

@true5525 hello,

Thank you for your follow-up and for sharing your findings! 😊

I'm glad to hear that you've confirmed the augmentation functionality in the YOLO package and that it meets your needs for coordinate data transformation in labels. The built-in augmentation capabilities indeed make it convenient to handle both image transformations and corresponding label adjustments seamlessly.

If you have any further questions or need additional assistance with your project, feel free to reach out. We're here to help!

Have a great day!

github-actions[bot] commented 3 weeks 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 ⭐