import torchvision.transforms as transform
transform.ToTensor()
transform.Normalize((0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
torchvision.transforms.ToTensor()
一般读入图像像素值值域范围为[0, 255],ToTensor()能够把范围从[0, 255]变换到[0, 1]。

class ToTensor(object):
"""Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
Converts a PIL Image or numpy.ndarray (H x W x C) in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]
if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1)
or if the numpy.ndarray has dtype = np.uint8
In the other cases, tensors are returned without scaling.
"""
def __call__(self, pic):
"""
Args:
pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
Returns:
Tensor: Converted image.
"""
return F.to_tensor(pic)
def __repr__(self):
return self.__class__.__name__ + '()'
torchvision.transforms.Normalize()
Normalize()把值域范围从[0, 1]变换到[-1, 1]。对每个通道执行value_n=(value-mean)/std。其中均值mean和标准差std分别由(0.5,0.5,0.5)和(0.5,0.5,0.5)指定

本文详细解析了PyTorch中`torchvision.transforms`模块的`ToTensor()`和`Normalize()`函数。`ToTensor()`将图像像素值从[0,255]缩放到[0,1];`Normalize()`进一步将值域变换到[-1,1],通过指定的均值和标准差进行归一化。这两个转换常用于预处理图像数据,以便于输入到深度学习模型中。对于不同通道数的图像,如RGB或灰度图,`Normalize()`的参数设置也有所不同。
最低0.47元/天 解锁文章
1043

被折叠的 条评论
为什么被折叠?



