tf.keras.preprocessing.image.smart_resize
Stay organized with collections
Save and categorize content based on your preferences.
Resize images to a target size without aspect ratio distortion.
tf.keras.preprocessing.image.smart_resize(
x,
size,
interpolation='bilinear',
data_format='channels_last',
backend_module=None
)
Image datasets typically yield images that have each a different
size. However, these images need to be batched before they can be
processed by Keras layers. To be batched, images need to share the same
height and width.
You could simply do, in TF (or JAX equivalent):
size = (200, 200)
ds = ds.map(lambda img: resize(img, size))
However, if you do this, you distort the aspect ratio of your images, since
in general they do not all have the same aspect ratio as size
. This is
fine in many cases, but not always (e.g. for image generation models
this can be a problem).
Note that passing the argument preserve_aspect_ratio=True
to resize
will preserve the aspect ratio, but at the cost of no longer respecting the
provided target size.
This calls for:
size = (200, 200)
ds = ds.map(lambda img: smart_resize(img, size))
Your output images will actually be (200, 200)
, and will not be distorted.
Instead, the parts of the image that do not fit within the target size
get cropped out.
The resizing process is:
- Take the largest centered crop of the image that has the same aspect
ratio as the target size. For instance, if
size=(200, 200)
and the input
image has size (340, 500)
, we take a crop of (340, 340)
centered along
the width.
- Resize the cropped image to the target size. In the example above,
we resize the
(340, 340)
crop to (200, 200)
.
Args |
x
|
Input image or batch of images (as a tensor or NumPy array).
Must be in format (height, width, channels)
or (batch_size, height, width, channels) .
|
size
|
Tuple of (height, width) integer. Target size.
|
interpolation
|
String, interpolation to use for resizing.
Defaults to 'bilinear' .
Supports bilinear , nearest , bicubic ,
lanczos3 , lanczos5 .
|
data_format
|
"channels_last" or "channels_first" .
|
backend_module
|
Backend module to use (if different from the default
backend).
|
Returns |
Array with shape (size[0], size[1], channels) .
If the input image was a NumPy array, the output is a NumPy array,
and if it was a backend-native tensor,
the output is a backend-native tensor.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-06-07 UTC.
[null,null,["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.preprocessing.image.smart_resize\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/utils/image_utils.py#L296-L454) |\n\nResize images to a target size without aspect ratio distortion. \n\n tf.keras.preprocessing.image.smart_resize(\n x,\n size,\n interpolation='bilinear',\n data_format='channels_last',\n backend_module=None\n )\n\nImage datasets typically yield images that have each a different\nsize. However, these images need to be batched before they can be\nprocessed by Keras layers. To be batched, images need to share the same\nheight and width.\n\nYou could simply do, in TF (or JAX equivalent): \n\n size = (200, 200)\n ds = ds.map(lambda img: resize(img, size))\n\nHowever, if you do this, you distort the aspect ratio of your images, since\nin general they do not all have the same aspect ratio as `size`. This is\nfine in many cases, but not always (e.g. for image generation models\nthis can be a problem).\n\nNote that passing the argument `preserve_aspect_ratio=True` to `resize`\nwill preserve the aspect ratio, but at the cost of no longer respecting the\nprovided target size.\n\n#### This calls for:\n\n size = (200, 200)\n ds = ds.map(lambda img: smart_resize(img, size))\n\nYour output images will actually be `(200, 200)`, and will not be distorted.\nInstead, the parts of the image that do not fit within the target size\nget cropped out.\n\nThe resizing process is:\n\n1. Take the largest centered crop of the image that has the same aspect ratio as the target size. For instance, if `size=(200, 200)` and the input image has size `(340, 500)`, we take a crop of `(340, 340)` centered along the width.\n2. Resize the cropped image to the target size. In the example above, we resize the `(340, 340)` crop to `(200, 200)`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `x` | Input image or batch of images (as a tensor or NumPy array). Must be in format `(height, width, channels)` or `(batch_size, height, width, channels)`. |\n| `size` | Tuple of `(height, width)` integer. Target size. |\n| `interpolation` | String, interpolation to use for resizing. Defaults to `'bilinear'`. Supports `bilinear`, `nearest`, `bicubic`, `lanczos3`, `lanczos5`. |\n| `data_format` | `\"channels_last\"` or `\"channels_first\"`. |\n| `backend_module` | Backend module to use (if different from the default backend). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Array with shape `(size[0], size[1], channels)`. If the input image was a NumPy array, the output is a NumPy array, and if it was a backend-native tensor, the output is a backend-native tensor. ||\n\n\u003cbr /\u003e"]]