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

torchvision 笔记:transforms.Compose()_第1张图片

 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()_第2张图片 

一样的效果

 

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