Skip to content

Commit

Permalink
use tf-ops for smart_resize
Browse files Browse the repository at this point in the history
  • Loading branch information
sampathweb committed Nov 22, 2023
1 parent 9b47aed commit 5dfe9b0
Showing 1 changed file with 1 addition and 61 deletions.
62 changes: 1 addition & 61 deletions keras_cv/backend/tf_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,67 +52,7 @@
def smart_resize(x, size, interpolation="bilinear"):
"""Resize images to a target size without aspect ratio distortion.
Warning: `tf.keras.preprocessing.image.smart_resize` is not recommended for
new code. Prefer `tf.keras.layers.Resizing`, which provides the same
functionality as a preprocessing layer and adds `tf.RaggedTensor` support.
See the [preprocessing layer guide](
https://www.tensorflow.org/guide/tf_keras/preprocessing_layers)
for an overview of preprocessing layers.
TensorFlow image datasets typically yield images that have each a different
size. However, these images need to be batched before they can be
processed by TF-Keras layers. To be batched, images need to share the same
height and width.
You could simply do:
```python
size = (200, 200)
ds = ds.map(lambda 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 GANs 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. Because `tf.image.resize` doesn't crop images,
your output images will still have different sizes.
This calls for:
```python
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:
1. 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.
2. 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. Supports
`bilinear`, `nearest`, `bicubic`, `area`, `lanczos3`, `lanczos5`,
`gaussian`, `mitchellcubic`. Defaults to `'bilinear'`.
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 TF tensor,
the output is a TF tensor.
Copied from `tf_keras` for Keras 3 and for use in `tf.data` pipeline.
"""
if len(size) != 2:
raise ValueError(
Expand Down

0 comments on commit 5dfe9b0

Please sign in to comment.