码迷,mamicode.com
首页 > 其他好文 > 详细

PyTorch官方中文文档:torchvision.transforms

时间:2018-03-10 16:38:02      阅读:2925      评论:0      收藏:0      [点我收藏+]

标签:width   post   pytorch   log   填充   tuple   输入   and   pos   

 
 

pytorch torchvision transform

对PIL.Image进行变换

class torchvision.transforms.Compose(transforms)

将多个transform组合起来使用。

transforms: 由transform构成的列表.
例子:

transforms.Compose([
     transforms.CenterCrop(10),
     transforms.ToTensor(),
 ])
 ```


### class torchvision.transforms.Scale(size, interpolation=2)

将输入的`PIL.Image`重新改变大小成给定的`size`,`size`是最小边的边长。举个例子,如果原图的`height>width`,那么改变大小后的图片大小是`(size*height/width, size)`。
**用例:**
```python
from torchvision import transforms
from PIL import Image
crop = transforms.Scale(12)
img = Image.open(‘test.jpg‘)

print(type(img))
print(img.size)

croped_img=crop(img)
print(type(croped_img))
print(croped_img.size)
<class ‘PIL.PngImagePlugin.PngImageFile‘>
(10, 10)
<class ‘PIL.Image.Image‘>
(12, 12)

class torchvision.transforms.CenterCrop(size)

将给定的PIL.Image进行中心切割,得到给定的sizesize可以是tuple(target_height, target_width)size也可以是一个Integer,在这种情况下,切出来的图片的形状是正方形。

class torchvision.transforms.RandomCrop(size, padding=0)

切割中心点的位置随机选取。size可以是tuple也可以是Integer

class torchvision.transforms.RandomHorizontalFlip

随机水平翻转给定的PIL.Image,概率为0.5。即:一半的概率翻转,一半的概率不翻转。

class torchvision.transforms.RandomSizedCrop(size, interpolation=2)

先将给定的PIL.Image随机切,然后再resize成给定的size大小。

class torchvision.transforms.Pad(padding, fill=0)

将给定的PIL.Image的所有边用给定的pad value填充。
padding:要填充多少像素
fill:用什么值填充
例子:

from torchvision import transforms
from PIL import Image
padding_img = transforms.Pad(padding=10, fill=0)
img = Image.open(‘test.jpg‘)

print(type(img))
print(img.size)

padded_img=padding(img)
print(type(padded_img))
print(padded_img.size)
<class ‘PIL.PngImagePlugin.PngImageFile‘>
(10, 10)
<class ‘PIL.Image.Image‘>
(30, 30) #由于上下左右都要填充10个像素,所以填充后的size是(30,30)

对Tensor进行变换

class torchvision.transforms.Normalize(mean, std)

给定均值:(R,G,B) 方差:(R,G,B),将会把Tensor正则化。即:Normalized_image=(image-mean)/std

Conversion Transforms

class torchvision.transforms.ToTensor

把一个取值范围是[0,255]PIL.Image或者shape(H,W,C)numpy.ndarray,转换成形状为[C,H,W],取值范围是[0,1.0]torch.FloadTensor

data = np.random.randint(0, 255, size=300)
img = data.reshape(10,10,3)
print(img.shape)
img_tensor = transforms.ToTensor()(img) # 转换成tensor
print(img_tensor)

class torchvision.transforms.ToPILImage

shape(C,H,W)Tensorshape(H,W,C)numpy.ndarray转换成PIL.Image,值不变。

通用变换

class torchvision.transforms.Lambda(lambd)

使用lambd作为转换器。

艾伯特(http://www.aibbt.com/)国内第一家人工智能门户

PyTorch官方中文文档:torchvision.transforms

标签:width   post   pytorch   log   填充   tuple   输入   and   pos   

原文地址:https://www.cnblogs.com/aibbt/p/8540458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!