不同框架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类似

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值