pytorch torchvision transform
transforms
: 由transform
构成的列表.
例子:
class torchvision.transforms.Scale(size, interpolation=2)
将输入的PIL.Image
重新改变大小成给定的size
,size
是最小边的边长。举个例子,如果原图的height>width
,那么改变大小后的图片大小是(size*height/width, size)
。
用例:
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 torchvision.transforms.CenterCrop(size)
将给定的PIL.Image
进行中心切割,得到给定的size
,size
可以是tuple
,(target_height, target_width)
。也可以是一个Integer
,在这种情况下,切出来的图片的形状是正方形。
class torchvision.transforms.RandomHorizontalFlip
随机水平翻转给定的PIL.Image
,概率为0.5
。即:一半的概率翻转,一半的概率不翻转。
class torchvision.transforms.RandomSizedCrop(size, interpolation=2)
先将给定的PIL.Image
随机切,然后再resize
成给定的size
大小。
将给定的PIL.Image
的所有边用给定的pad value
填充。padding:
要填充多少像素fill:
用什么值填充
例子:
from torchvision import transforms
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))
class torchvision.transforms.Normalize(mean, std)
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
将shape
为(C,H,W)
的Tensor
或shape
为(H,W,C)
的numpy.ndarray
转换成,值不变。
class torchvision.transforms.Lambda(lambd)
使用lambd
作为转换器。