(image, bboxes,
                                         target_height, target_width)
Crops and/or pads an image to a target width and height.
    Resizes an image to a target width and height by either centrally
    cropping the image or padding it evenly with zeros.

    If `width` or `height` is greater than the specified `target_width` or
    `target_height` respectively, this op centrally crops along that dimension.
    If `width` or `height` is smaller than the specified `target_width` or
    `target_height` respectively, this op centrally pads with 0 along that
    dimension.
    Args:
      image: 3-D tensor of shape `[height, width, channels]`
      target_height: Target height.
      target_width: Target width.
    Raises:
      ValueError: if `target_height` or `target_width` are zero or negative.
    Returns:
      Cropped and/or padded image of shape
        `[target_height, target_width, channels]`
Crops and/or pads an image to a target width and height.
    Resizes an image to a target width and height by either centrally
    cropping the image or padding it evenly with zeros.
Crops and / or pads an image to a target width and height . Resizes an image to a target width and height by either centrally cropping the image or padding it evenly with zeros .
def resize_image_bboxes_with_crop_or_pad(image, bboxes,
                                         target_height, target_width):
    """Crops and/or pads an image to a target width and height.
    Resizes an image to a target width and height by either centrally
    cropping the image or padding it evenly with zeros.

    If `width` or `height` is greater than the specified `target_width` or
    `target_height` respectively, this op centrally crops along that dimension.
    If `width` or `height` is smaller than the specified `target_width` or
    `target_height` respectively, this op centrally pads with 0 along that
    dimension.
    Args:
      image: 3-D tensor of shape `[height, width, channels]`
      target_height: Target height.
      target_width: Target width.
    Raises:
      ValueError: if `target_height` or `target_width` are zero or negative.
    Returns:
      Cropped and/or padded image of shape
        `[target_height, target_width, channels]`
    """
    with tf.name_scope('resize_with_crop_or_pad'):
        image = ops.convert_to_tensor(image, name='image')

        assert_ops = []
        assert_ops += _Check3DImage(image, require_static=False)
        assert_ops += _assert(target_width > 0, ValueError,
                              'target_width must be > 0.')
        assert_ops += _assert(target_height > 0, ValueError,
                              'target_height must be > 0.')

        image = control_flow_ops.with_dependencies(assert_ops, image)
        # `crop_to_bounding_box` and `pad_to_bounding_box` have their own checks.
        # Make sure our checks come first, so that error messages are clearer.
        if _is_tensor(target_height):
            target_height = control_flow_ops.with_dependencies(
                assert_ops, target_height)
        if _is_tensor(target_width):
            target_width = control_flow_ops.with_dependencies(assert_ops, target_width)

        def max_(x, y):
            if _is_tensor(x) or _is_tensor(y):
                return math_ops.maximum(x, y)
            else:
                return max(x, y)

        def min_(x, y):
            if _is_tensor(x) or _is_tensor(y):
                return math_ops.minimum(x, y)
            else:
                return min(x, y)

        def equal_(x, y):
            if _is_tensor(x) or _is_tensor(y):
                return math_ops.equal(x, y)
            else:
                return x == y

        height, width, _ = _ImageDimensions(image)
        width_diff = target_width - width
        offset_crop_width = max_(-width_diff // 2, 0)
        offset_pad_width = max_(width_diff // 2, 0)

        height_diff = target_height - height
        offset_crop_height = max_(-height_diff // 2, 0)
        offset_pad_height = max_(height_diff // 2, 0)

        # Maybe crop if needed.
        height_crop = min_(target_height, height)
        width_crop = min_(target_width, width)
        cropped = tf.image.crop_to_bounding_box(image, offset_crop_height, offset_crop_width,
                                                height_crop, width_crop)
        bboxes = bboxes_crop_or_pad(bboxes,
                                    height, width,
                                    -offset_crop_height, -offset_crop_width,
                                    height_crop, width_crop)
        # Maybe pad if needed.
        resized = tf.image.pad_to_bounding_box(cropped, offset_pad_height, offset_pad_width,
                                               target_height, target_width)
        bboxes = bboxes_crop_or_pad(bboxes,
                                    height_crop, width_crop,
                                    offset_pad_height, offset_pad_width,
                                    target_height, target_width)

        # In theory all the checks below are redundant.
        if resized.get_shape().ndims is None:
            raise ValueError('resized contains no shape.')

        resized_height, resized_width, _ = _ImageDimensions(resized)

        assert_ops = []
        assert_ops += _assert(equal_(resized_height, target_height), ValueError,
                              'resized height is not correct.')
        assert_ops += _assert(equal_(resized_width, target_width), ValueError,
                              'resized width is not correct.')

        resized = control_flow_ops.with_dependencies(assert_ops, resized)
        return resized, bboxes
def resize_image_bboxes_with_crop_or_pad ( image , bboxes , target_height , target_width ) : with tf . name_scope ( 'resize_with_crop_or_pad' ) : image = ops . convert_to_tensor ( image , name = 'image' ) assert_ops = [ ] assert_ops += _Check3DImage ( image , require_static = False ) assert_ops += _assert ( target_width > 0 , ValueError , 'target_width must be > 0.' ) assert_ops += _assert ( target_height > 0 , ValueError , 'target_height must be > 0.' ) image = control_flow_ops . with_dependencies ( assert_ops , image ) # `crop_to_bounding_box` and `pad_to_bounding_box` have their own checks. # Make sure our checks come first, so that error messages are clearer. if _is_tensor ( target_height ) : target_height = control_flow_ops . with_dependencies ( assert_ops , target_height ) if _is_tensor ( target_width ) : target_width = control_flow_ops . with_dependencies ( assert_ops , target_width ) def max_ ( x , y ) : if _is_tensor ( x ) or _is_tensor ( y ) : return math_ops . maximum ( x , y ) else : return max ( x , y ) def min_ ( x , y ) : if _is_tensor ( x ) or _is_tensor ( y ) : return math_ops . minimum ( x , y ) else : return min ( x , y ) def equal_ ( x , y ) : if _is_tensor ( x ) or _is_tensor ( y ) : return math_ops . equal ( x , y ) else : return x == y height , width , _ = _ImageDimensions ( image ) width_diff = target_width - width offset_crop_width = max_ ( - width_diff // 2 , 0 ) offset_pad_width = max_ ( width_diff // 2 , 0 ) height_diff = target_height - height offset_crop_height = max_ ( - height_diff // 2 , 0 ) offset_pad_height = max_ ( height_diff // 2 , 0 ) # Maybe crop if needed. height_crop = min_ ( target_height , height ) width_crop = min_ ( target_width , width ) cropped = tf . image . crop_to_bounding_box ( image , offset_crop_height , offset_crop_width , height_crop , width_crop ) bboxes = bboxes_crop_or_pad ( bboxes , height , width , - offset_crop_height , - offset_crop_width , height_crop , width_crop ) # Maybe pad if needed. resized = tf . image . pad_to_bounding_box ( cropped , offset_pad_height , offset_pad_width , target_height , target_width ) bboxes = bboxes_crop_or_pad ( bboxes , height_crop , width_crop , offset_pad_height , offset_pad_width , target_height , target_width ) # In theory all the checks below are redundant. if resized . get_shape ( ) . ndims is None : raise ValueError ( 'resized contains no shape.' ) resized_height , resized_width , _ = _ImageDimensions ( resized ) assert_ops = [ ] assert_ops += _assert ( equal_ ( resized_height , target_height ) , ValueError , 'resized height is not correct.' ) assert_ops += _assert ( equal_ ( resized_width , target_width ) , ValueError , 'resized width is not correct.' ) resized = control_flow_ops . with_dependencies ( assert_ops , resized ) return resized , bboxes