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

Normalizes a tensor or image with mean and standard deviation.

  • 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.


Normalized mage. Data format is same as input img.

Return type

np.ndarray or Tensor


import numpy as np
from PIL import Image
from 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())