栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

不同框架toTensor和Normalize的区别

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339507.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号