mindspore.dataset.vision.Normalize

View Source On Gitee
class mindspore.dataset.vision.Normalize(mean, std, is_hwc=True)[source]

Normalize the input image with respect to mean and standard deviation. This operation will normalize the input image with: output[channel] = (input[channel] - mean[channel]) / std[channel], where channel >= 1.

Supports Ascend hardware acceleration and can be enabled through the .device(“Ascend”) method.

Note

This operation is executed on the CPU by default, but it is also supported to be executed on the GPU or Ascend via heterogeneous acceleration.

Parameters
  • mean (sequence) – List or tuple of mean values for each channel, with respect to channel order. The mean values must be in range [0.0, 255.0].

  • std (sequence) – List or tuple of standard deviations for each channel, with respect to channel order. The standard deviation values must be in range (0.0, 255.0].

  • is_hwc (bool, optional) – Whether the input image is HWC. True - HWC format, False - CHW format. Default: True.

Raises
  • TypeError – If mean is not of type sequence.

  • TypeError – If std is not of type sequence.

  • TypeError – If is_hwc is not of type bool.

  • ValueError – If mean is not in range [0.0, 255.0].

  • ValueError – If std is not in range (0.0, 255.0].

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

Supported Platforms:

CPU GPU 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"])
>>> normalize_op = vision.Normalize(mean=[121.0, 115.0, 100.0], std=[70.0, 68.0, 71.0], is_hwc=True)
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=[normalize_op],
...                                                 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
(100, 100, 3) float32
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> output = vision.Normalize(mean=[121.0, 115.0, 100.0], std=[70.0, 68.0, 71.0])(data)
>>> print(output.shape, output.dtype)
(100, 100, 3) float32
Tutorial Examples:
device(device_target='CPU')[source]

Set the device for the current operator execution.

  • When the device is CPU, input type support uint8/float32/float64, input channel support 1/2/3.

  • When the device is Ascend, input type supports uint8/float32, input channel supports 1/3. input shape should be limited from [4, 6] to [8192, 4096].

Parameters

device_target (str, optional) – The operator will be executed on this device. Currently supports CPU and Ascend . 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
>>> from mindspore.dataset.vision import Inter
>>>
>>> # 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"])
>>> resize_op = vision.Resize([100, 75], Inter.BICUBIC)
>>> transforms_list = [resize_op]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, input_columns=["image"])
>>> normalize_op = vision.Normalize(mean=[121.0, 115.0, 100.0], std=[70.0, 68.0, 71.0]).device("Ascend")
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=normalize_op, 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
(100, 75, 3) float32
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> output = vision.Normalize(mean=[121.0, 115.0, 100.0], std=[70.0, 68.0, 71.0]).device("Ascend")(data)
>>> print(output.shape, output.dtype)
(100, 100, 3) float32
Tutorial Examples: