def crop_and_resize_image()

in benchmarks/horovod-resnet/train_imagenet_resnet_hvd.py [0:0]


def crop_and_resize_image(image, original_bbox, height, width, distort=False, nsummary=10):
    with tf.name_scope("crop_and_resize"):
        # Evaluation is done on a center-crop of this ratio
        eval_crop_ratio = 0.8
        if distort:
            initial_shape = [
                int(round(height / eval_crop_ratio)),
                int(round(width / eval_crop_ratio)),
                3,
            ]
            bbox_begin, bbox_size, bbox = tf.image.sample_distorted_bounding_box(
                initial_shape,
                bounding_boxes=tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4]),
                # tf.zeros(shape=[1,0,4]), # No bounding boxes
                min_object_covered=0.1,
                aspect_ratio_range=[3.0 / 4.0, 4.0 / 3.0],
                area_range=[0.08, 1.0],
                max_attempts=100,
                seed=11 * hvd.rank(),  # Need to set for deterministic results
                use_image_if_no_bounding_boxes=True,
            )
            bbox = bbox[0, 0]  # Remove batch, box_idx dims
        else:
            # Central crop
            ratio_y = ratio_x = eval_crop_ratio
            bbox = tf.constant(
                [0.5 * (1 - ratio_y), 0.5 * (1 - ratio_x), 0.5 * (1 + ratio_y), 0.5 * (1 + ratio_x)]
            )
        image = tf.image.crop_and_resize(image[None, :, :, :], bbox[None, :], [0], [height, width])[
            0
        ]
        return image