normalize

paddle.vision.transforms. normalize ( img, mean, std, data_format='CHW', to_rgb=False ) [source]

Normalizes a tensor or image with mean and standard deviation.

Parameters
  • img (PIL.Image|np.array|paddle.Tensor) – input data to be normalized.

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

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

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

  • to_rgb (bool, optional) – Whether to convert to rgb. If input is tensor, this option will be igored. Default: False.

Returns

Normalized mage. Data format is same as input img.

Return type

PIL.Image|np.array|paddle.Tensor

Examples

>>> import numpy as np
>>> from PIL import Image
>>> from paddle.vision.transforms import functional as F
>>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8')
>>> fake_img = Image.fromarray(fake_img)
>>> mean = [127.5, 127.5, 127.5]
>>> std = [127.5, 127.5, 127.5]
>>> normalized_img = F.normalize(fake_img, mean, std, data_format='HWC')
>>> print(normalized_img.max(), normalized_img.min())
0.99215686 -1.0