Skip to content
Snippets Groups Projects
Verified Commit 526c783d authored by Yoann Schneider's avatar Yoann Schneider :tennis:
Browse files

Add new transformation to resize to FixedSize

parent 721eef7d
No related branches found
No related tags found
1 merge request!383Add new transformation to resize to FixedSize
......@@ -44,6 +44,11 @@ class Preprocessing(str, Enum):
Resize the width to a fixed value while keeping the original ratio
"""
FixedResize = "fixed_resize"
"""
Resize both the width and the height to a fixed value
"""
class FixedHeightResize:
"""
......@@ -79,6 +84,19 @@ class FixedWidthResize:
return round(self.width * aspect_ratio)
class FixedResize:
"""
Resize an image tensor to a fixed width and height
"""
def __init__(self, height: int, width: int) -> None:
self.height = height
self.width = width
def __call__(self, img: Tensor) -> Tensor:
return resize(img, (self.height, self.width), antialias=False)
class MaxResize:
"""
Resize an image tensor if it is bigger than the maximum size
......@@ -179,6 +197,13 @@ def get_preprocessing_transforms(
)
case Preprocessing.FixedWidthResize:
transforms.append(FixedWidthResize(width=preprocessing["fixed_width"]))
case Preprocessing.FixedResize:
transforms.append(
FixedResize(
height=preprocessing["fixed_height"],
width=preprocessing["fixed_width"],
)
)
if to_pil_image:
transforms.append(ToPILImage())
return Compose(transforms)
......
......@@ -169,6 +169,18 @@ Usage:
]
```
- Resize to a fixed width and a fixed height
```py
[
{
"type": "fixed_resize",
"fixed_height": 1900,
"fixed_width": 1250,
}
]
```
- Resize to a maximum size (only if the image is bigger than the given size)
```py
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment