mindspore.dataset.vision.Pad
- class mindspore.dataset.vision.Pad(padding, fill_value=0, padding_mode=Border.CONSTANT)[source]
Pad the image according to padding parameters.
Supports Ascend hardware acceleration and can be enabled through the .device(“Ascend”) method.
- Parameters
padding (Union[int, Sequence[int, int], Sequence[int, int, int, int]]) – The number of pixels to pad each border of the image. If a single number is provided, it pads all borders with this value. If a tuple or lists of 2 values are provided, it pads the (left and right) with the first value and (top and bottom) with the second value. If 4 values are provided as a list or tuple, it pads the left, top, right and bottom respectively. The pad values must be non-negative.
fill_value (Union[int, tuple[int]], optional) – The pixel intensity of the borders, only valid for padding_mode
Border.CONSTANT
. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255]. Default:0
.padding_mode (Border, optional) –
The method of padding. Default:
Border.CONSTANT
. Can beBorder.CONSTANT
,Border.EDGE
,Border.REFLECT
,Border.SYMMETRIC
.Border.CONSTANT
, means it fills the border with constant values.Border.EDGE
, means it pads with the last value on the edge.Border.REFLECT
, means it reflects the values on the edge omitting the last value of edge.Border.SYMMETRIC
, means it reflects the values on the edge repeating the last value of edge.
- Raises
TypeError – If padding is not of type int or Sequence[int, int], Sequence[int, int, int, int].
TypeError – If fill_value is not of type int or tuple[int].
TypeError – If padding_mode is not of type
mindspore.dataset.vision.Border
.ValueError – If padding is negative.
ValueError – If fill_value is not in range [0, 255].
RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.
- Supported Platforms:
CPU
Examples
>>> 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"]) >>> transforms_list = [vision.Pad([100, 100, 100, 100])] >>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, 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 (300, 300, 3) uint8 >>> >>> # Use the transform in eager mode >>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8) >>> output = vision.Pad([100, 100, 100, 100])(data) >>> print(output.shape, output.dtype) (300, 300, 3) uint8
- Tutorial Examples:
- device(device_target='CPU')[source]
Set the device for the current operator execution.
When the device is Ascend, input/output shape should be limited from [4, 6] to [32768, 32768].
- Parameters
device_target (str, optional) – The operator will be executed on this device. Currently supports
CPU
andAscend
. Default:CPU
.- Raises
TypeError – If device_target is not of type str.
ValueError – If device_target is not within the valid set of [‘CPU’, ‘Ascend’].
- Supported Platforms:
CPU
Ascend
Examples
>>> 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"]) >>> pad_op = vision.Pad([100, 100, 100, 100]).device("Ascend") >>> transforms_list = [pad_op] >>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, 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 (300, 300, 3) uint8 >>> >>> # Use the transform in eager mode >>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8) >>> output = vision.Pad([100, 100, 100, 100]).device("Ascend")(data) >>> print(output.shape, output.dtype) (300, 300, 3) uint8
- Tutorial Examples: