ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.98k stars 16.16k forks source link

_clip_augmented: clarifications required #13060

Closed sergiev closed 2 months ago

sergiev commented 4 months ago

Search before asking

Question

During experiments with custom TTA, i wondered: am I missing something? Let's see screenshot below: image

Here's the split of the question to the two aspects, respectively more practical and theoretical:

  1. Practical one. As you can see in example above, I've increased the amount of augmented and their scale factors. A. Does it mean that _clip_augmented should be modified? B. If so, what are the rules below it? C. To comply with my changes, is there anything else that should also be modified?
  2. Theoretical. For the answers on it, I really hope to see something much more comprehensive than a polite wrapped instruction to reread the code, documentation, comments and tutorials - sadly, nothing of it gives a definitive response, at least currently. A. Why such postprocessing (_clip_augmented method) ever emerged? In other words: why do we need drop some tails in the first place? B. Why it affects only first and last outputs? In other words: why we should modify tails in this dimension instead of tail-clipping of each output in list?

Additional

I'm sincerely grateful for your awesome work ❤️

github-actions[bot] commented 4 months ago

👋 Hello @sergiev, 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 4 months ago

Hello! Thank you for reaching out with your detailed questions regarding the _clip_augmented function in your custom Test Time Augmentation (TTA) setup. Let's dive into your queries:

  1. Practical Aspect:

    • A: If you've increased the amount and scale of augmentations, it might be necessary to adjust _clip_augmented depending on how these changes affect the output dimensions or the distribution of the predictions.
    • B: The underlying rule for _clip_augmented is typically to ensure that any augmented outputs still conform to the expected input size and distribution for subsequent processing stages or metrics calculations.
    • C: Besides modifying _clip_augmented, ensure that any changes in augmentation do not violate the input requirements of other components in your pipeline, such as normalization layers or the model's input layer.
  2. Theoretical Aspect:

    • A: The _clip_augmented method is used to manage the effects of edge cases introduced by augmentations. For instance, extreme scale changes or translations might lead to predictions that are out of sync with the original image dimensions or context, necessitating some form of clipping or adjustment.
    • B: The focus on the first and last outputs generally relates to how data is batched and processed through the network. It's often these parts of the data that encapsulate the most significant distortions from augmentations, though this can vary based on the specific methods and models used.

Your feedback and engagement with the YOLOv5 community are greatly appreciated, and they play a crucial role in improving the tool. If you have further questions or need more detailed explanations, please feel free to ask. Happy experimenting! 🚀

sergiev commented 3 months ago

Thank you @glenn-jocher for the answer!

  1. But to understand the dependency you've mentioned, could you please share how exactly _clip_augmented should be modified in order to comply with my changes?
  2. Or, if that's too much, why g is calculated as sum(4**x for x in range(nl)) and not, for example sum(2//x for x in some_iter). Same question about both definitions of i variable:
    def _clip_augmented(self, y):
    """Clips augmented inference tails for YOLOv5 models, affecting first and last tensors based on grid points and
    layer counts.
    """
    nl = self.model[-1].nl  # number of detection layers (P3-P5)
    g = sum(4**x for x in range(nl))  # grid points
    e = 1  # exclude layer count
    i = (y[0].shape[1] // g) * sum(4**x for x in range(e))  # indices
    y[0] = y[0][:, :-i]  # large
    i = (y[-1].shape[1] // g) * sum(4 ** (nl - 1 - x) for x in range(e))  # indices
    y[-1] = y[-1][:, i:]  # small
    return y
  3. Also, why won't simplify i definitions due to range(e)=[0]?
glenn-jocher commented 3 months ago

@sergiev hello! I'm glad to assist with your queries regarding the _clip_augmented function:

  1. Modifying _clip_augmented: To adapt this function to your changes, you'll need to consider how your augmentations affect the output tensor sizes. If your augmentations alter the grid sizes or the aspect ratios significantly, you may need to adjust the calculation of i to ensure that the indices being clipped still align with the new dimensions of your output tensors. Essentially, you'll want to ensure that the sections being clipped are those that contain distorted or irrelevant predictions due to the augmentations.

  2. Calculation of g and i:

    • The expression g = sum(4**x for x in range(nl)) is used to calculate the total number of grid points across all detection layers. The use of 4**x reflects an exponential increase in grid points with each subsequent layer, which is typical in YOLO architectures where each layer operates at a different scale.
    • The definition of i uses similar logic to determine how many indices should be clipped based on the layer's scale and the number of grid points. The use of range(e) where e=1 in this context means that the sum will only consider the first term of the series, simplifying the calculation to focus on the immediate layer's grid scaling.
  3. Simplifying i definitions: The current setup allows for easy adjustments to e if ever needed for different configurations or more complex layer interactions. Simplifying it directly in the code as range(e)=[0] would hard-code this behavior, reducing flexibility for future modifications where e might need to differ.

I hope this clarifies your questions! If you have more, feel free to ask. Happy coding! 🚀

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