Resize images to a target size without aspect ratio distortion.
Source:R/preprocessing-image.R
image_smart_resize.Rd
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 <- c(200, 200)
ds <- ds$map(\(img) tf$image$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 tf$image$resize()
will preserve the aspect ratio, but at the cost of no longer respecting the
provided target size.
This calls for:
size <- c(200, 200)
ds <- ds$map(\(img) image_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 = c(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)
.
Usage
image_smart_resize(
x,
size,
interpolation = "bilinear",
data_format = "channels_last",
backend_module = NULL
)
Arguments
- x
Input image or batch of images (as a tensor or 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. Supports
"bilinear"
,"nearest"
,"bicubic"
,"lanczos3"
,"lanczos5"
. Defaults to'bilinear'
.- data_format
"channels_last"
or"channels_first"
.- backend_module
Backend module to use (if different from the default backend).
Value
Array with shape (size[1], size[2], channels)
.
If the input image was an array, the output is an array,
and if it was a backend-native tensor,
the output is a backend-native tensor.
See also
Other image utils: image_array_save()
image_from_array()
image_load()
image_to_array()
op_image_affine_transform()
op_image_crop()
op_image_extract_patches()
op_image_hsv_to_rgb()
op_image_map_coordinates()
op_image_pad()
op_image_resize()
op_image_rgb_to_grayscale()
op_image_rgb_to_hsv()
Other utils: audio_dataset_from_directory()
clear_session()
config_disable_interactive_logging()
config_disable_traceback_filtering()
config_enable_interactive_logging()
config_enable_traceback_filtering()
config_is_interactive_logging_enabled()
config_is_traceback_filtering_enabled()
get_file()
get_source_inputs()
image_array_save()
image_dataset_from_directory()
image_from_array()
image_load()
image_to_array()
layer_feature_space()
normalize()
pad_sequences()
set_random_seed()
split_dataset()
text_dataset_from_directory()
timeseries_dataset_from_array()
to_categorical()
zip_lists()
Other preprocessing: image_dataset_from_directory()
text_dataset_from_directory()
timeseries_dataset_from_array()