mindspore.dataset.vision.RandomResize

class mindspore.dataset.vision.RandomResize(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 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=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> # 1) randomly resize image, keeping aspect ratio
>>> transforms_list1 = [vision.RandomResize(50)]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list1, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(50, 50, 3) uint8
>>> # 2) randomly resize image to landscape style
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> transforms_list2 = [vision.RandomResize((40, 60))]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list2, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(40, 60, 3) uint8
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> output = vision.RandomResize(10)(data)
>>> print(output.shape, output.dtype)
(10, 10, 3) uint8
教程样例: