def random_square_crop(image_size, min_scale): """Generates a random square crop within an image. Args: image_size: a [height, width] tensor. min_scale: how much the minimum dimension can be scaled down when taking a crop. (e.g. if the image is 480 x 640, a min_scale of 0.8 means the output crop can have a height and width between 480 and 384, which is 480 * 0.8.) Returns: output_begin, output_size and image_size. output_begin and output_size are three element tensors specifying the shape to crop using crop_sequence below. image_size is a two element [height, width] tensor from the input. """ min_dim = tf.reduce_min(image_size[0:2]) sampled_size = tf.to_int32( tf.to_float(min_dim) * tf.random_uniform([], min_scale, 1.0)) output_size = tf.stack([sampled_size, sampled_size, -1]) height_offset = tf.random_uniform([], 0, image_size[0] - sampled_size + 1, dtype=tf.int32) width_offset = tf.random_uniform([], 0, image_size[1] - sampled_size + 1, dtype=tf.int32) output_begin = tf.stack([height_offset, width_offset, 0]) return output_begin, output_size, image_size
def largest_square_crop(image_size): """For given image size, returns the maximum square, central crop. Args: image_size: a [height, width] tensor. Returns: output_begin, output_size and image_size. output_begin and output_size are three element tensors specifying the shape to crop using crop_sequence below. image_size is a two element [height, width] tensor from the input. """ min_dim = tf.reduce_min(image_size[0:2]) output_size = tf.stack([min_dim, min_dim, -1]) height_offset = tf.to_int32((image_size[0] - min_dim) / 2) width_offset = tf.to_int32((image_size[1] - min_dim) / 2) output_begin = tf.stack([height_offset, width_offset, 0]) return output_begin, output_size, image_size