I couldn't find any equivalent in the library, but a somewhat "official" solution is present in the NNCLR tutorial. It indeed relies on the tf.image.crop_and_resize function, as @TFer pointed out.
I modified it a bit to make sure it also has the equivalent of the size argument in the PyTorch implementation, that I called crop_shape that I found clearer:
import tensorflow as tf
class RandomResizedCrop(tf.keras.layers.Layer):
# taken from
# https://keras.io/examples/vision/nnclr/#random-resized-crops
def __init__(self, scale, ratio, crop_shape):
super(RandomResizedCrop, self).__init__()
self.scale = scale
self.log_ratio = (tf.math.log(ratio[0]), tf.math.log(ratio[1]))
self.crop_shape = crop_shape
def call(self, images):
batch_size = tf.shape(images)[0]
random_scales = tf.random.uniform(
(batch_size,),
self.scale[0],
self.scale[1]
)
random_ratios = tf.exp(tf.random.uniform(
(batch_size,),
self.log_ratio[0],
self.log_ratio[1]
))
new_heights = tf.clip_by_value(
tf.sqrt(random_scales / random_ratios),
0,
1,
)
new_widths = tf.clip_by_value(
tf.sqrt(random_scales * random_ratios),
0,
1,
)
height_offsets = tf.random.uniform(
(batch_size,),
0,
1 - new_heights,
)
width_offsets = tf.random.uniform(
(batch_size,),
0,
1 - new_widths,
)
bounding_boxes = tf.stack(
[
height_offsets,
width_offsets,
height_offsets + new_heights,
width_offsets + new_widths,
],
axis=1,
)
images = tf.image.crop_and_resize(
images,
bounding_boxes,
tf.range(batch_size),
self.crop_shape,
)
return images