Skip to content

Memory changed when changing the value of crop_to_aspect_ratio in case that this arg actually not work #21773

@ILCSFNO

Description

@ILCSFNO

Bug Issue

The doc of keras.layers.Resizing() shows its description as below:

crop_to_aspect_ratio: If `True`, resize the images without aspect
ratio distortion. 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. By default
(`crop_to_aspect_ratio=False`), aspect ratio may not be preserved.

For the repro below, when I tried to change crop_to_aspect_ratio from False to True, the memory used turns to be fewer.

I don't know if this is actually expected.

In my opinion, this behavior is not expected for that only when the original aspect ratio differs from the target aspect ratio, the output image will be cropped. The result should be the same anyway.

Repro 1 (crop_to_aspect_ratio==False)

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '3,4'
# tensorflow memory usage test
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
    for gpu in gpus:
        tf.config.experimental.set_memory_growth(gpu, True)
# Main Code -->
import tensorflow as tf
import keras
input_image = tf.random.uniform((1, 256, 256, 3), minval=0, maxval=255, dtype=tf.float32)
resized_image = keras.layers.Resizing(height=224, width=224, interpolation='bilinear', crop_to_aspect_ratio=False)(input_image)
print('Resized Image:', resized_image.shape)
# Main Code <--
memory = 0
for i in range(len(gpus)):
    memory += tf.config.experimental.get_memory_usage('GPU:%d' % i)
print(memory)

Output 1

Resized Image: (1, 224, 224, 3)
2097152

Repro 1 (crop_to_aspect_ratio==True)

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '3,4'
# tensorflow memory usage test
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
    for gpu in gpus:
        tf.config.experimental.set_memory_growth(gpu, True)
# Main Code -->
import tensorflow as tf
import keras
input_image = tf.random.uniform((1, 256, 256, 3), minval=0, maxval=255, dtype=tf.float32)
resized_image = keras.layers.Resizing(height=224, width=224, interpolation='bilinear', crop_to_aspect_ratio=True)(input_image)
print('Resized Image:', resized_image.shape)
# Main Code <--
memory = 0
for i in range(len(gpus)):
    memory += tf.config.experimental.get_memory_usage('GPU:%d' % i)
print(memory)

Output 1

Resized Image: (1, 224, 224, 3)
2096384

Thanks a lot!

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions