不同框架toTensor和Normalize的区别

使用不同框架的时候,数据归一化操作toTensor和Normalize时,发现mean和std取值不同,本质都是imagenet的mean和std,下面阅读相应代码比较不同之处

1. Pytorch

torchvison.transform.toTensor()

class ToTensor:
    """Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor. This transform does not support torchscript.

    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.

    .. note::
        Because the input image is scaled to [0.0, 1.0], this transformation should not be used when
        transforming target image masks. See the `references`_ for implementing the transforms for image masks.

    .. _references: https://github.com/pytorch/vision/tree/master/references/segmentation
    """

    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__ + '()'

F.to_tensor(pic)中有以下语句,将img矩阵/255,从0-255范围变成0-1范围

img.to(dtype=default_float_dtype).div(255)

torchvison.transform.Normalize()

所以pytorch Normalize的mean和std取:

  • mean = [0.485, 0.456, 0.406]

  • Std = [0.229, 0.224, 0.225]

2. MMDetection

mmdetection的pipelines中有ToTensor和Normalize

但此处的ToTensor没有/255的操作,所以mmdetection中的mean和std取:

  • mean = [123.675, 116.28, 103.53],

  • std = [58.395, 57.12, 57.375]

和 pytorch中的mean、std是*255的关系

3. Detectron2

Detectron2中ToTensor和Normalize和Pytorch类似

你可能感兴趣的:(#,pytorch,pytorch,深度学习)