Normalize

class paddle.vision.transforms. Normalize ( mean=0.0, std=1.0, data_format='CHW', to_rgb=False, keys=None ) [source]

Normalize the input data with mean and standard deviation. Given mean: (M1,...,Mn) and std: (S1,..,Sn) for n channels, this transform will normalize each channel of the input data. output[channel] = (input[channel] - mean[channel]) / std[channel]

Parameters
  • mean (int|float|list|tuple) – Sequence of means for each channel.

  • std (int|float|list|tuple) – Sequence of standard deviations for each channel.

  • data_format (str, optional) – Data format of img, should be ‘HWC’ or ‘CHW’. Default: ‘CHW’.

  • to_rgb (bool, optional) – Whether to convert to rgb. Default: False.

  • keys (list[str]|tuple[str], optional) – Same as BaseTransform. Default: None.

Shape:
  • img(PIL.Image|np.ndarray|Paddle.Tensor): The input image with shape (H x W x C).

  • output(PIL.Image|np.ndarray|Paddle.Tensor): A normalized array or tensor.

Returns

A callable object of Normalize.

Examples

import numpy as np
from PIL import Image
from paddle.vision.transforms import Normalize

normalize = Normalize(mean=[127.5, 127.5, 127.5],
                      std=[127.5, 127.5, 127.5],
                      data_format='HWC')

fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8))

fake_img = normalize(fake_img)
print(fake_img.shape)
print(fake_img.max, fake_img.max)