mindspore.dataset.vision.RandomResizeWithBBox

class mindspore.dataset.vision.RandomResizeWithBBox(size)[源代码]

对输入图像使用随机选择的 mindspore.dataset.vision.Inter 插值方式去调整它的尺寸大小,并相应地调整边界框的尺寸大小。

参数:
  • size (Union[int, Sequence[int]]) - 调整后图像的输出尺寸大小。若输入整型,则放缩至(size, size)大小;若输入2元素序列,则以2个元素分别为高和宽放缩至(高度, 宽度)大小。

异常:
  • TypeError - 当 size 的类型不为int或Sequence[int]类型。

  • ValueError - 当 size 不为正数。

  • RuntimeError - 当输入图像的shape不为<H, W>或<H, W, C>。

支持平台:

CPU

样例:

>>> 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
教程样例: