mindspore.dataset.vision.RandomResizeWithBBox

View Source On Gitee
class mindspore.dataset.vision.RandomResizeWithBBox(size)[source]

Tensor operation to resize the input image using a randomly selected interpolation mode Inter and adjust bounding boxes accordingly.

Parameters

size (Union[int, Sequence[int]]) – The output size of the resized image. The size value(s) must be positive. If size is an integer, smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

Raises
  • TypeError – If size is not of type int or Sequence[int].

  • ValueError – If size is not positive.

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> import copy
>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.float32)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> func = lambda img: (data, np.array([[0, 0, data.shape[1], data.shape[0]]]).astype(np.float32))
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=[func],
...                                                 input_columns=["image"],
...                                                 output_columns=["image", "bbox"])
>>> numpy_slices_dataset2 = copy.deepcopy(numpy_slices_dataset)
>>>
>>> # 1) randomly resize image with bounding boxes, keeping aspect ratio
>>> transforms_list1 = [vision.RandomResizeWithBBox(60)]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list1,
...                                                 input_columns=["image", "bbox"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     print(item["bbox"].shape, item["bbox"].dtype)
...     break
(60, 60, 3) float32
(1, 4) float32
>>>
>>> # 2) randomly resize image with bounding boxes to portrait style
>>> transforms_list2 = [vision.RandomResizeWithBBox((80, 60))]
>>> numpy_slices_dataset2 = numpy_slices_dataset2.map(operations=transforms_list2,
...                                                   input_columns=["image", "bbox"])
>>> for item in numpy_slices_dataset2.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     print(item["bbox"].shape, item["bbox"].dtype)
...     break
(80, 60, 3) float32
(1, 4) float32
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.float32)
>>> func = lambda img: (data, np.array([[0, 0, data.shape[1], data.shape[0]]]).astype(data.dtype))
>>> func_data, func_bboxes = func(data)
>>> output = vision.RandomResizeWithBBox(64)(func_data, func_bboxes)
>>> print(output[0].shape, output[0].dtype)
(64, 64, 3) float32
>>> print(output[1].shape, output[1].dtype)
(1, 4) float32
Tutorial Examples: