mindspore.ops.CropAndResize

View Source On Gitee
class mindspore.ops.CropAndResize(method='bilinear', extrapolation_value=0.0)[source]

Extracts crops from the input image tensor and resizes them.

Note

In case that the output shape depends on crop_size, the crop_size must be constant. For now, the backward of the operator only support bilinear method, for other methods, will return 0.

Parameters
  • method (str, optional) –

    An optional string that specifies the sampling method for resizing. It can be "bilinear" , "nearest" or "bilinear_v2" . Default: "bilinear" .

    • "nearest": Nearest neighbor interpolation. Each output pixel is assigned the value of the nearest input pixel. This method is simple and fast but can result in blocky or pixelated outputs.

    • "bilinear": Bilinear interpolation. Each output pixel is a weighted average of the four nearest input pixels, computed using bilinear interpolation. This method produces smoother results compared to nearest neighbor interpolation.

    • "bilinear_v2": The optimized variant of "bilinear", it may achieve better result(higher precision and speed) in some cases.

  • extrapolation_value (float, optional) – An optional float value used extrapolation, if applicable. Default: 0.0 .

Inputs:
  • x (Tensor) - The input image must be a 4-D tensor of shape \((batch, image\_height, image\_width, depth)\). Types allowed: int8, int16, int32, int64, float16, float32, float64, uint8, uint16.

  • boxes (Tensor) - A 2-D tensor of shape \((num\_boxes, 4)\). The i-th row of the tensor specifies the coordinates of a box in the box_ind[i] image and is specified in normalized coordinates [y1, x1, y2, x2]. A normalized coordinate value of y is mapped to the image coordinate at y * (image_height - 1), so as the [0, 1] interval of normalized image height is mapped to [0, image_height - 1] in image height coordinates. We do allow y1 > y2, in which case the sampled crop is an up-down flipped version of the original image. The width dimension is treated similarly. Normalized coordinates outside the [0, 1] range are allowed, in which case we use extrapolation_value to extrapolate the input image values. Types allowed: float32.

  • box_index (Tensor) - A 1-D tensor of shape \((num\_boxes)\) with int32 values in [0, batch). The value of box_index[i] specifies the image that the i-th box refers to. Types allowed: int32.

  • crop_size (Tuple[int]) - A tuple of two int32 elements: (crop_height, crop_width). Only constant value is allowed. All cropped image patches are resized to this size. The aspect ratio of the image content is not preserved. Both crop_height and crop_width need to be positive.

Outputs:

A 4-D tensor of shape \((num\_boxes, crop\_height, crop\_width, depth)\) with type: float32.

Raises
  • TypeError – If x or boxes or box_index is not a Tensor.

  • TypeError – If crop_size is not a Tuple with two int32 elements.

  • TypeError – If dtype of boxes is not float or that of box_index is not int.

  • TypeError – If method is not a str.

  • TypeError – If extrapolation_value is not a float.

  • ValueError – If the shape rank of x is not 4.

  • ValueError – If the shape rank of boxes is not 2.

  • ValueError – If the second dim of boxes is not 4.

  • ValueError – If the shape rank of box_index is not 1.

  • ValueError – If the first dim of box_index is not equal to that of boxes.

  • ValueError – If existing element in box_index is out of range [0, batch).

  • ValueError – If the data of crop_size is not positive.

  • ValueError – If method is not one of ‘bilinear’, ‘nearest’, ‘bilinear_v2’.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import nn, ops, Tensor
>>> class CropAndResizeNet(nn.Cell):
...     def __init__(self, crop_size):
...         super(CropAndResizeNet, self).__init__()
...         self.crop_and_resize = ops.CropAndResize()
...         self.crop_size = crop_size
...
...     def construct(self, x, boxes, box_index):
...         return self.crop_and_resize(x, boxes, box_index, self.crop_size)
...
>>> BATCH_SIZE = 1
>>> NUM_BOXES = 5
>>> IMAGE_HEIGHT = 256
>>> IMAGE_WIDTH = 256
>>> CHANNELS = 3
>>> image = np.random.normal(size=[BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS]).astype(np.float32)
>>> boxes = np.random.uniform(size=[NUM_BOXES, 4]).astype(np.float32)
>>> box_index = np.random.uniform(size=[NUM_BOXES], low=0, high=BATCH_SIZE).astype(np.int32)
>>> crop_size = (24, 24)
>>> crop_and_resize = CropAndResizeNet(crop_size=crop_size)
>>> output = crop_and_resize(Tensor(image), Tensor(boxes), Tensor(box_index))
>>> print(output.shape)
(5, 24, 24, 3)