# Random Cropping as an augmentation

**URL:** <https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546>\
**Category:** YOLO\
**Tags:** code, yolo\
**Created:** [November 28, 2024, 2:56am UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546 "2024-11-28T02:56:32Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![redon](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@redon](https://community.ultralytics.com/u/redon)\
**Post date:** [November 28, 2024, 2:56am UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/1 "2024-11-28T02:56:32Z")

</div>

Hey guys,  
How can I use an augmentation of random cropping?  
Thanks

---

<div class="post-metadata">

**Author:** ![Toxite](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/toxite/32/123_2.png) [@Toxite](https://community.ultralytics.com/u/Toxite)\
**Post date:** [November 28, 2024, 8:43am UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/2 "2024-11-28T08:43:18Z")

</div>

You can use `mosaic` which is enabled by default.

---

<div class="post-metadata">

**Author:** ![redon](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@redon](https://community.ultralytics.com/u/redon)\
**Post date:** [November 28, 2024, 3:07pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/3 "2024-11-28T15:07:57Z")

</div>

Thanks. Used it.  
As far as I understand, it just resizes the image into four tiles and combine them as one puzzle.  
I want to randomly crop patches from the original image without resizing them.

---

<div class="post-metadata">

**Author:** ![Toxite](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/toxite/32/123_2.png) [@Toxite](https://community.ultralytics.com/u/Toxite)\
**Post date:** [November 28, 2024, 5:17pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/4 "2024-11-28T17:17:45Z")

</div>

It crops a portion of the image during the tiling process. You can also use `scale` and `translate` augmentations to achieve a combined random crop effect.

---

<div class="post-metadata">

**Author:** ![redon](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@redon](https://community.ultralytics.com/u/redon)\
**Post date:** [November 28, 2024, 10:33pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/5 "2024-11-28T22:33:34Z")

</div>

Just updating that so far, by looking at the `mosaic` batches, none of them showed a random crop from the original image. All four of them were resized…

Btw, the problem with `mosaic` is that bounding boxes might be cut off in the new image, resulting in false detections, in my opinion, whereas some objects require the entire bounding box’s context to better detect the object.

---

<div class="post-metadata">

**Author:** ![Toxite](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/toxite/32/123_2.png) [@Toxite](https://community.ultralytics.com/u/Toxite)\
**Post date:** [November 28, 2024, 10:53pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/6 "2024-11-28T22:53:10Z")

</div>

By resizing, do you mean the original image is “squished” to fit the tile? Because Ultralytics never does that. It never squishes the image. Not even for classification. When the image doesn’t fit the tile after resizing, it will crop a square portion out of it. So if the the images are originally a square, then you might not see this effect. In that case, you should increase `scale` and `translate` augmentation.

---

<div class="post-metadata">

**Author:** ![Toxite](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/toxite/32/123_2.png) [@Toxite](https://community.ultralytics.com/u/Toxite)\
**Post date:** [November 28, 2024, 11:01pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/7 "2024-11-28T23:01:17Z")

</div>

> [@redon](#):
>
> Btw, the problem with `mosaic` is that bounding boxes might be cut off in the new image, resulting in false detections, in my opinion, whereas some objects require the entire bounding box’s context to better detect the object.

`mosaic` as it is currently implemented was found [empirically to perform better](https://github.com/WongKinYiu/CrossStagePartialNetworks/issues/8) than other augmentation techniques and hence it’s used

You can take care of the bounding boxes while cropping, but carefully checking for that for every image introduces additional overhead because it’s almost like a “search” operation to find the right random crop during training which will increase training time.

---

<div class="post-metadata">

**Author:** ![redon](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@redon](https://community.ultralytics.com/u/redon)\
**Post date:** [November 28, 2024, 11:35pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/8 "2024-11-28T23:35:07Z")

</div>

Good takes. I’ll try to clarify myself:

Just to use a common ground: Let’s say that we have an image with resolution of 4K (3840 x 2160 pixels).  
Then:

- `crop` = selecting a smaller patch (say 640x640) out of the original image (4K)
- `resize` = resize the resolution from 3840 x 2160 to 1137x640 (with related proportions), then crop the 640x640 in the center, getting a 640x640 patch without distortions.
- `squish` = resize the resolution from 3840 x 2160 to 640x640 (every dimensions is squished without related proportions), leading to some smearing.

My problem with mosaic is that it does this:  
`Resize` \> `Combine` (into a mosaic).

This is a problem because the resizing might lose some critical super small details that could be seen with higher resolution.

I want to apply only a `Random Crop` augmentation on the original image.

I’d use an original image of 4K, and randomly crop big enough patches out of it (without downgrading the resolution).

It looks like this:  
[https://pytorch.org/vision/0.13/auto\_examples/plot\_transforms.html#randomcrop](https://pytorch.org/vision/0.13/auto_examples/plot_transforms.html#randomcrop)

You suggested `Scale` and `Translate`, I’ll try it now, but now sure that it does exactly what I need… I might find out that `Scale` is some sort of `resizing`?

Here’s the list of augmentations from the documentations:

> **[Train](https://docs.ultralytics.com/modes/train/#train-settings)**
>
> Learn how to efficiently train object detection models using YOLO11 with comprehensive instructions on settings, augmentation, and hardware utilization.

---

<div class="post-metadata">

**Author:** ![Toxite](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/toxite/32/123_2.png) [@Toxite](https://community.ultralytics.com/u/Toxite)\
**Post date:** [November 29, 2024, 7:34am UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/9 "2024-11-29T07:34:46Z")

</div>

Ah ok. Yes, it’s resized in the dataloader while loading the image. I guess it helps keep the memory requirement low.

---

<div class="post-metadata">

**Author:** ![Toxite](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/toxite/32/123_2.png) [@Toxite](https://community.ultralytics.com/u/Toxite)\
**Post date:** [November 29, 2024, 10:09am UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/10 "2024-11-29T10:09:03Z")

</div>

I created a branch that uses full resolution during augmentations.

You can install it by

```bash
git clone https://github.com/Y-T-G/ultralytics
cd ultralytics
git checkout hires_train
pip install .

```

Pass `hi_res=True` during training in `model.train()`

From my brief testing, the results were worse. But you can try it, especially with `scale` and `translate`.

---

<div class="post-metadata">

**Author:** ![redon](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@redon](https://community.ultralytics.com/u/redon)\
**Post date:** [November 29, 2024, 12:48pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/11 "2024-11-29T12:48:11Z")

</div>

Wow man, impressive work! I’m deeply grateful for your support. I thought you’d guide me where I may modify things in the code, and I’d deep dive into it.

So once again, thank you! Appreciating it very much. I’ll try it out 🙂

---

<div class="post-metadata">

**Author:** ![redon](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@redon](https://community.ultralytics.com/u/redon)\
**Post date:** [January 6, 2025, 9:10pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/12 "2025-01-06T21:10:44Z")

</div>

I’m having trouble installing the poetry pyproject.toml file on win11 on VSCode.

I’ve git cloned it, cd-d into it.

However, when I try to poetry install it, I get this weird state where it doesn’t install anything, but it shows as if it did.

 ![image](https://us1.discourse-cdn.com/flex001/uploads/ultralytics1/original/1X/bbd470772f153d988c3c6243954e967030ec8d9e.png)

How could I troubleshoot it and run it?

Thanks

---

<div class="post-metadata">

**Author:** ![BurhanQ](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/burhanq/32/7_2.png) [@BurhanQ](https://community.ultralytics.com/u/BurhanQ)\
**Post date:** [January 7, 2025, 2:31pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/13 "2025-01-07T14:31:23Z")

</div>

You don’t need to include L189-194, those are not standard for the `pyproject.toml`. AFAIK, Poetry doesn’t support editable installs, so you’ll have to use `pip -e .` like in the [docs](https://docs.ultralytics.com/quickstart/#__tabbed_1_3) to install in editable mode. Poetry is more about managing projects and their dependencies, and can be helpful with package installs but might not be a full replacement. If you’re developing your own project/library that uses Ultralytics, you can use `poetry add ultralytics` to install the package from PyPi.

If you want something that can do both, `uv` is quite popular and does support editable installs using the `uv pip -e .` command.

---

<div class="post-metadata">

**Author:** ![BurhanQ](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/burhanq/32/7_2.png) [@BurhanQ](https://community.ultralytics.com/u/BurhanQ)\
**Post date:** [January 7, 2025, 2:36pm UTC](https://community.ultralytics.com/t/random-cropping-as-an-augmentation/546/14 "2025-01-07T14:36:25Z")

</div>

Definitely review the [Poetry documentation](https://python-poetry.org/docs/cli/#add). You might be able to use the `add` functionality for your project to include your custom forked repository as the install source.
