keras-cv icon indicating copy to clipboard operation
keras-cv copied to clipboard

RandomCrop bugs

Open martin-gorner opened this issue 2 years ago • 3 comments

Repro Colab here

Input image size: 360*512px

Problem #1: cropping to 128x513 crashes with a div by 0 Problem #2: cropping to 128x514 no longer produces random results (there are still many possible random positions along the Y axis)

martin-gorner avatar Sep 28 '22 19:09 martin-gorner

Yes for # 1 it is an edge case of get_random_transformation in Keras https://github.com/keras-team/keras/blob/v2.10.0/keras/layers/preprocessing/image_preprocessing.py#L581-L582 w_start = rands[1] % (w_diff + 1)

As your image in the colab cell is

tf.Tensor([372 512   3], shape=(3,), dtype=int32)

With

random_crop = tf.keras.layers.RandomCrop(height=128, width=513)

You are going to have a w_diff of -1 and so you crash with % 0

bhack avatar Sep 28 '22 22:09 bhack

For # 2 I don't understand well the logic of using dtype.max https://github.com/keras-team/keras/blob/v2.10.0/keras/layers/preprocessing/image_preprocessing.py#L580

rands = self._random_generator.random_uniform([2], 0, dtype.max, dtype)

bhack avatar Sep 28 '22 22:09 bhack

Thank you Martin for the bug report!

LukeWood avatar Sep 28 '22 23:09 LukeWood