parsing_rcnn_model.py
def roi_crop_and_resize(image, boxes, box_ind, crop_shape):
    """
    Better-aligned version of tf.image.crop_and_resize, following our definition of floating point boxes.

    Args:
        image: BHWC
        boxes: nx4, y1x1y2x2 normalized coor
        box_ind: (n,)
        crop_size (int,int):
    Returns:
        n,C,size,size
    """

    def transform_fpcoor_for_tf(boxes, image_shape, crop_shape):
        """
        The way tf.image.crop_and_resize works (with normalized box):
        Initial point (the value of output[0]): x0_box * (W_img - 1)
        Spacing: w_box * (W_img - 1) / (W_crop - 1)
        Use the above grid to bilinear sample.

        However, what we want is (with fpcoor box):
        Spacing: w_box / W_crop
        Initial point: x0_box + spacing/2 - 0.5
        (-0.5 because bilinear sample assumes floating point coordinate (0.0, 0.0) is the same as pixel value (0, 0))

        This function transform fpcoor boxes to a format to be used by tf.image.crop_and_resize

        Returns:
            y1x1y2x2
        """
        y0, x0, y1, x1 = tf.split(boxes, 4, axis=1)

        # each bin shape
        spacing_w = (x1 - x0) / tf.to_float(crop_shape[1])
        spacing_h = (y1 - y0) / tf.to_float(crop_shape[0])

        nx0 = (x0 + spacing_w / 2 - 0.5) / tf.to_float(image_shape[1] - 1)
        ny0 = (y0 + spacing_h / 2 - 0.5) / tf.to_float(image_shape[0] - 1)

        nw = spacing_w * tf.to_float(crop_shape[1] - 1) / tf.to_float(image_shape[1] - 1)
        nh = spacing_h * tf.to_float(crop_shape[0] - 1) / tf.to_float(image_shape[0] - 1)
        ny1 = ny0 + nh
        nx1 = nx0 + nw
        return tf.concat([ny0, nx0, ny1, nx1], axis=1)

    image_shape = tf.shape(image)[1:3]
    boxes = boxes * tf.cast(tf.stack([image_shape[0], image_shape[1], image_shape[0], image_shape[1]]), tf.float32)
    boxes = transform_fpcoor_for_tf(boxes, image_shape, crop_shape)
    ret = tf.image.crop_and_resize(image, boxes, box_ind, crop_shape)
    return ret

    Better-aligned version of tf.image.crop_and_resize, following our definition of floating point boxes.

    Args:
        image: BHWC
        boxes: nx4, y1x1y2x2 normalized coor
        box_ind: (n,)
        crop_size (int,int):
    Returns:
        n,C,size,size
    
Better-aligned version of tf.image.crop_and_resize, following our definition of floating point boxes.
Better - aligned version of tf . image . crop_and_resize following our definition of floating point boxes .
def roi_crop_and_resize(image, boxes, box_ind, crop_shape):
    def transform_fpcoor_for_tf(boxes, image_shape, crop_shape):
        y0, x0, y1, x1 = tf.split(boxes, 4, axis=1)
        spacing_w = (x1 - x0) / tf.to_float(crop_shape[1])
        spacing_h = (y1 - y0) / tf.to_float(crop_shape[0])
        nx0 = (x0 + spacing_w / 2 - 0.5) / tf.to_float(image_shape[1] - 1)
        ny0 = (y0 + spacing_h / 2 - 0.5) / tf.to_float(image_shape[0] - 1)
        nw = spacing_w * tf.to_float(crop_shape[1] - 1) / tf.to_float(image_shape[1] - 1)
        nh = spacing_h * tf.to_float(crop_shape[0] - 1) / tf.to_float(image_shape[0] - 1)
        ny1 = ny0 + nh
        nx1 = nx0 + nw
        return tf.concat([ny0, nx0, ny1, nx1], axis=1)
    image_shape = tf.shape(image)[1:3]
    boxes = boxes * tf.cast(tf.stack([image_shape[0], image_shape[1], image_shape[0], image_shape[1]]), tf.float32)
    boxes = transform_fpcoor_for_tf(boxes, image_shape, crop_shape)
    ret = tf.image.crop_and_resize(image, boxes, box_ind, crop_shape)
    return ret
def roi_crop_and_resize ( image , boxes , box_ind , crop_shape ) : def transform_fpcoor_for_tf ( boxes , image_shape , crop_shape ) : """
        The way tf.image.crop_and_resize works (with normalized box):
        Initial point (the value of output[0]): x0_box * (W_img - 1)
        Spacing: w_box * (W_img - 1) / (W_crop - 1)
        Use the above grid to bilinear sample.

        However, what we want is (with fpcoor box):
        Spacing: w_box / W_crop
        Initial point: x0_box + spacing/2 - 0.5
        (-0.5 because bilinear sample assumes floating point coordinate (0.0, 0.0) is the same as pixel value (0, 0))

        This function transform fpcoor boxes to a format to be used by tf.image.crop_and_resize

        Returns:
            y1x1y2x2
        """ y0 , x0 , y1 , x1 = tf . split ( boxes , 4 , axis = 1 ) spacing_w = ( x1 - x0 ) / tf . to_float ( crop_shape [ 1 ] ) spacing_h = ( y1 - y0 ) / tf . to_float ( crop_shape [ 0 ] ) nx0 = ( x0 + spacing_w / 2 - 0.5 ) / tf . to_float ( image_shape [ 1 ] - 1 ) ny0 = ( y0 + spacing_h / 2 - 0.5 ) / tf . to_float ( image_shape [ 0 ] - 1 ) nw = spacing_w * tf . to_float ( crop_shape [ 1 ] - 1 ) / tf . to_float ( image_shape [ 1 ] - 1 ) nh = spacing_h * tf . to_float ( crop_shape [ 0 ] - 1 ) / tf . to_float ( image_shape [ 0 ] - 1 ) ny1 = ny0 + nh nx1 = nx0 + nw return tf . concat ( [ ny0 , nx0 , ny1 , nx1 ] , axis = 1 ) image_shape = tf . shape ( image ) [ 1 : 3 ] boxes = boxes * tf . cast ( tf . stack ( [ image_shape [ 0 ] , image_shape [ 1 ] , image_shape [ 0 ] , image_shape [ 1 ] ] ) , tf . float32 ) boxes = transform_fpcoor_for_tf ( boxes , image_shape , crop_shape ) ret = tf . image . crop_and_resize ( image , boxes , box_ind , crop_shape ) return ret
Better-aligned version of tf.image.crop_and_resize, following our definition of floating point boxes.
Better - aligned version of tf . image . crop_and_resize following our definition of floating point boxes .
"""
    Better-aligned version of tf.image.crop_and_resize, following our definition of floating point boxes.

    Args:
        image: BHWC
        boxes: nx4, y1x1y2x2 normalized coor
        box_ind: (n,)
        crop_size (int,int):
    Returns:
        n,C,size,size
    """ """
        The way tf.image.crop_and_resize works (with normalized box):
        Initial point (the value of output[0]): x0_box * (W_img - 1)
        Spacing: w_box * (W_img - 1) / (W_crop - 1)
        Use the above grid to bilinear sample.

        However, what we want is (with fpcoor box):
        Spacing: w_box / W_crop
        Initial point: x0_box + spacing/2 - 0.5
        (-0.5 because bilinear sample assumes floating point coordinate (0.0, 0.0) is the same as pixel value (0, 0))

        This function transform fpcoor boxes to a format to be used by tf.image.crop_and_resize

        Returns:
            y1x1y2x2
        """ # each bin shape
None
image
None
boxes
nx4, y1x1y2x2 normalized coor
nx4 y1x1y2x2 normalized coor
box_ind
None
crop_shape
crop_size
None