Rate This Document
Findability
Accuracy
Completeness
Readability

normalize

Usage

Normalizes an image by converting data into a standard normal distribution dataset to accelerate model convergence.

Interface

1
torchvision.transforms.normalize(mean,std,inplace=False)  

Parameters

Parameter

Description

Value Range

Input/Output

mean

A sequence that contains the mean value of each channel.

[0, inf)

Input

std

A sequence that contains the standard deviation of each channel.

(0, inf)

Input

inplace

In-place operation. The default value is False.

bool

Input

Return Value

  • Normalized image

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import numpy as np
from torchvision import transforms

src = np.array([[0, 0, 0, 0, 0],
                 [0, 1, 1, 1, 0],
                 [0, 1, 0, 1, 0],
                 [0, 1, 1, 1, 0],
                 [0, 0, 0, 0, 0]], dtype=np.float32)


# Define the mean value and standard deviation.
mean = 0.5 
std = 0.5

transf = transforms.Compose(
    [   transforms.ToTensor(),
        transforms.Normalize(mean, std)
    ]
)
normalized = transf(src)
print(normalized)

Output:

1
2
3
4
5
tensor([[[-1., -1., -1., -1., -1.],
         [-1.,  1.,  1.,  1., -1.],
         [-1.,  1., -1.,  1., -1.],
         [-1.,  1.,  1.,  1., -1.],
         [-1., -1., -1., -1., -1.]]])