Does tf.keras.layers.Resizing with crop_to_aspect_ratio=True crop a photo in the center?

Hello,

I am making a TensorFlow model that can recognise images. I want to base it on mobilenet (Google | mobilenet_v2 | Kaggle) so the pictures need to be 224x224. Because of this my first layer is a tf.keras.layers.Resizing tf.keras.layers.Resizing  |  TensorFlow v2.16.1. Does this layer crop the images in the center or from the top?

Kind regards,

Hi @Egbert
The answer to your question is the image is “cropped in the center”.
As you can see in the Tensorflow blog entry you shared, none of the tulips of the original photo is missing in the resized picture.

Hello,

Thank you for the answer, but I am having difficulty understanding what you mean with “none of the tulips of the original photo is missing in the resized picture”. Could you tell me where it says this and/or what you mean with it so i can better understand it? In the documentation i found: “When the original aspect ratio differs from the target aspect ratio, the output image will be cropped so as to return the largest possible window in the image (of size (height, width) ) that matches the target aspect ratio.”.

Kind regards,

Hi @Egbert
P;ease run the code below to realize by yourself what is going on while using layers.Resizing.

import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_datasets as tfds

(train_ds, val_ds, test_ds), metadata = tfds.load(
‘tf_flowers’,
split=[‘train[:80%]’, ‘train[80%:90%]’, ‘train[90%:]’],
with_info=True,
as_supervised=True,
)

get_label_name = metadata.features[‘label’].int2str
image, label = next(iter(train_ds))
_ = plt.imshow(image)
_ = plt.title(get_label_name(label))

And using what follows, play with IMG_HEIGHT, IMG_WIDTH, also setting alternatively crop_to_aspect_ratio to True and False, and see what is going on:

IMG_HEIGHT = 180
IMG_WIDTH = 90

resize_and_rescale = tf.keras.Sequential([
layers.Resizing(height=IMG_HEIGHT, width=IMG_WIDTH, crop_to_aspect_ratio=False),
layers.Rescaling(1./255)
])
result = resize_and_rescale(image)
_ = plt.imshow(result)

NB: Running the code on a few examples probably isn’t really proof of what the code is doing.

OTOH, it was simple enough to go through the source code itself (‘view on GitHub’):

Looking at the way in which the ‘offset’ is calculated as half the (total height less the crop height), it’s clear that a center crop is done. Same for width…

Hope this helps!