torchvision 笔记:transforms.Compose()

Posted UQI-LIUWJ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了torchvision 笔记:transforms.Compose()相关的知识,希望对你有一定的参考价值。

torchvision.transforms.Compose()类的主要作用是串联多个transforms列表里面的transform操作

比如,在torchvision 笔记:transforms.Normalize()_UQI-LIUWJ的博客-CSDN博客 中的代码,可以用Compose来代替

不变的部分

from PIL import Image
from torchvision import transforms, utils
a=Image.open(b+'img/00000.jpg')
a

 Compose的部分

t=transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(
        mean=[0.485, 0.456, 0.406], 
        std=[0.229, 0.224, 0.225])
])
a=t(a)
a
'''
tensor([[[1.9235, 1.9235, 1.9235,  ..., 1.8893, 1.8893, 1.8893],
         [1.9235, 1.9235, 1.9235,  ..., 1.8893, 1.8893, 1.8893],
         [1.9235, 1.9235, 1.9235,  ..., 1.8893, 1.8893, 1.8893],
         ...,
         [1.3242, 1.3242, 1.3242,  ..., 1.3413, 1.3413, 1.3413],
         [1.3242, 1.3242, 1.3242,  ..., 1.3413, 1.3413, 1.3413],
         [1.3242, 1.3242, 1.3242,  ..., 1.3413, 1.3413, 1.3413]],

        [[2.0959, 2.0959, 2.0959,  ..., 2.0784, 2.0784, 2.0784],
         [2.0959, 2.0959, 2.0959,  ..., 2.0784, 2.0784, 2.0784],
         [2.0959, 2.0959, 2.0959,  ..., 2.0784, 2.0784, 2.0784],
         ...,
         [1.5182, 1.5182, 1.5182,  ..., 1.5007, 1.5007, 1.5007],
         [1.5182, 1.5182, 1.5182,  ..., 1.5007, 1.5007, 1.5007],
         [1.5182, 1.5182, 1.5182,  ..., 1.5007, 1.5007, 1.5007]],

        [[2.3088, 2.3088, 2.3088,  ..., 2.3263, 2.3263, 2.3263],
         [2.3088, 2.3088, 2.3088,  ..., 2.3263, 2.3263, 2.3263],
         [2.3088, 2.3088, 2.3088,  ..., 2.3263, 2.3263, 2.3263],
         ...,
         [1.7163, 1.7163, 1.7163,  ..., 1.7511, 1.7511, 1.7511],
         [1.7163, 1.7163, 1.7163,  ..., 1.7511, 1.7511, 1.7511],
         [1.7163, 1.7163, 1.7163,  ..., 1.7511, 1.7511, 1.7511]]])
'''

 

一样的效果

 

以上是关于torchvision 笔记:transforms.Compose()的主要内容,如果未能解决你的问题,请参考以下文章

PyTorch学习笔记——图像处理(transforms.Normalize 归一化)

极智AI | OpenCV and torchvision.transforms 实现图像裁剪方法

极智AI | OpenCV and torchvision.transforms 实现图像等比例缩放方法

PyTorch 1.0 中文文档:torchvision.transforms

torchvision.transforms.Compose()详解Pytorch入门手册

torchvision.transforms.Compose()详解Pytorch入门手册