Skip to content

Keep original aspect ratio / size of images#1312

Draft
FennelFetish wants to merge 5 commits intoNerogar:masterfrom
FennelFetish:keep-image-size
Draft

Keep original aspect ratio / size of images#1312
FennelFetish wants to merge 5 commits intoNerogar:masterfrom
FennelFetish:keep-image-size

Conversation

@FennelFetish
Copy link
Copy Markdown

This PR changes the toggle for aspect_ratio_bucketing to a combo box image_preprocessing with a new third option: Keep Aspect Ratio.
The corresponding config value is migrated.

The other two options are:

  • Square Center Crop (previously: aspect_ratio_bucketing = off)
  • Aspect Ratio Bucketing (previously: aspect_ratio_bucketing = on)

I've also expanded on the tooltip. This was quite confusing to me when I started using OT and I hope these changes also make it more clear.

The new Keep Aspect Ratio preprocessing strategy:

  • Downscales (and only downscales - no upscaling) images to roughly resolution² pixels.
    • For training on the original image size, use a high value for resolution which disables scaling, e.g. 2048.
  • Preserves the exact aspect ratio if possible.
    • If the width or height of the scaled image is not divisible by 64 (or the model-specific value), the image is cropped.
  • In any case, images with the same original size will also have the same final size and be batched together.
    • Providing enough images with the correct size is up to the user.

As with the other processing strategies, the resolution can be overridden for each concept.
Multiple resolutions can be specified, e.g. 1024, 768, 1280, in which case it will randomly select a value for each image.


This PR depends on the KeepAspectCalculation module from: Nerogar/mgds#29
Which is part of: #975
... and hasn't been merged yet.

The image_preprocessing setting could also be used to select Mixed Resolution Batching.

My version of the module with less verbose logging is here: https://github.com/FennelFetish/mgds/blob/keep-image-size/src/mgds/pipelineModules/KeepAspectCalculation.py

I think it needs to somehow handle possible_resolutions like the other modules do.
Since I'm not the author of that pipeline module, I'll first do the changes in my MGDS fork and we can see later how to integrate the changes in upstream MGDS, ok?

Closes #1276

TODO:

  • Handle possible_resolutions.
  • Check if the calculation can be adjusted to minimize the cropped region by slightly deviating from resolution².

@dxqb
Copy link
Copy Markdown
Collaborator

dxqb commented Feb 12, 2026

Thanks!
#975 will not be merged anytime soon (or at all), so feel free to modify KeepAspectCalculation is you need it

about possible_resolutions: I don't know a lot about it, but when I suggested this functionality to Nerogar earlier, he mentioned that it would fail with inpainting models because they need to know the possible resolutions at "compile time".
Please double check if this is true or if I remember it wrong. If it's true I wouldn't mind that it cannot be used for inpainting, but gives an error message in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feat]: Prevent resizing images

2 participants