torchvision.transforms
更新了,所以一部分代码可能得改成torchvision.transforms.v2
When an image is transformed into a PyTorch tensor, the pixel values are scaled between 0.0 and 1.0.
This transformation can be done using torchvision.transforms.ToTensor()
. It converts the PIL image with a pixel range of to a PyTorch FloatTensor of shape (C, H, W) with a range .
After transform image to tensor, we may perform image normalization
Normalization
- Normalizing the images means transforming the images into such values that the mean and standard deviation of the image become 0.0 and 1.0 respectively.
- It helps get data within a range and reduces the skewness(偏斜), which helps learn faster and better.
- It can help tackle the diminishing and exploding gradients problems
In Pytorch
torchvision.transforms.Normalize()
Normalizes the tensor image with mean and standard deviation
Parameter:
- mean: Sequence of means for each channel
- std: Sequence of std
- inplace: Whether operate data in-place
Returns: Normalized Tensor Image
To normalizing images in Pytorch, we need to
- Load and visualize image and plot pixel values uisng PIL
- Transform image to Tensors using
torchvision.transorms.ToTensor()
- Calculate mean and standard deviation
- Normalize the image
- Visualize normalized image
- verify normalization
Load the image
It may look like this
Transforming images to Tensors
value transform into
Calculate mean and std
Calculated the mean and std of the image for all three channels Red, Green, and Blue.
For images that are similar with ImageNet, we can use mean and std instead from ImageNet
Normalizing the images
Visualize the normalized image
Calculate the mean and std again