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 Values
- Success: KP_PT_STS_NO_ERR
- Failure: an error code
Error Codes
Error Code |
Description |
|---|---|
KP_PT_STS_NULL_PTR_ERR |
An error occurs in pointer memory allocation. |
KP_PT_STS_SIZE_ERR |
The input image size is incorrect. |
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.]]]) |
Parent topic: Interface Definition