torchvision.transforms

torchvision.transforms是pytorch中的图像预处理包,用于常见的一些图形变换

Crop

随机裁剪:transforms.RandomCrop

torchvision.transforms.RandomCrop(size,padding = None,pad_if_needed = False,fill = 0,padding_mode ='constant' )
  • size(sequence 或int) - 作物的所需输出大小。如果size是int而不是像(h,w)这样的序列,则进行正方形裁剪(大小,大小)
  • padding(int或sequence ,optional) - 图像每个边框上的可选填充。默认值为None,即无填充。如果提供长度为4的序列,则它用于分别填充左,上,右,下边界。如果提供长度为2的序列,则分别用于填充左/右,上/下边界
  • pad_if_needed(boolean) - 如果小于所需大小,它将填充图像以避免引发异常。由于在填充之后完成裁剪,因此填充似乎是在随机偏移处完成的。
  • fill - 恒定填充的像素填充值。默认值为0.如果长度为3的元组,则分别用于填充R,G,B通道。仅当padding_mode为常量时才使用此值.
  • padding_mode-填充类型。应该是:恒定,边缘,反射或对称。默认值是常量。
    • 常量:具有常量值的焊盘,该值用填充指定
    • edge:填充图像边缘的最后一个值
    • 反射:具有图像反射的垫(不重复边缘上的最后一个值),填充[1,2,3,4]在反射模式下两侧有2个元素将导致[3,2,1,2,3,4,3,2]
    • 对称:具有图像反射的垫(重复边缘上的最后一个值),填充[1,2,3,4]在对称模式下两侧有2个元素将导致[2,1,1,2,3,4,4,3]

中心裁剪:transforms.CenterCrop

torchvision.transforms.CenterCrop(size) 

依据给定的size从中心裁剪 参数: size- (sequence or int),若为sequence,则为(h,w),若为int,则(size,size)

随机长宽比裁剪 transforms.RandomResizedCrop

torchvision.transforms.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=2)

将给定的PIL图像裁剪为随机大小和宽高比。
将原始图像大小变成随机大小(默认值:是原始图像的0.08到1.0倍)和随机宽高比(默认值:3/4到4/3倍)。这种方法最终调整到适当的大小。这通常用于训练Inception网络。

  • size - 每条边的预期输出大小
  • scale - 裁剪的原始尺寸的大小范围
  • ratio - 裁剪的原始宽高比的宽高比范围
  • interpolation - 默认值:PIL.Image.BILINEAR

Flip and Rotation

依概率p水平翻转transforms.RandomHorizontalFlip

torchvision.transforms.RandomHorizontalFlip(p=0.5)

依概率p垂直翻转transforms.RandomVerticalFlip

torchvision.transforms.RandomVerticalFlip(p=0.5)

随机旋转:transforms.RandomRotation

torchvision.transforms.RandomRotation(degrees, resample=False, expand=False, center=None)

按角度旋转图像。

  • degrees(sequence 或float或int) -要选择的度数范围。如果degrees是一个数字而不是像(min,max)这样的序列,则度数范围将是(-degrees,+ degrees)。
  • resample({PIL.Image.NEAREST ,PIL.Image.BILINEAR ,PIL.Image.BICUBIC} ,可选) - 可选的重采样过滤器。请参阅过滤器以获取更多信 如果省略,或者图像具有模式“1”或“P”,则将其设置为PIL.Image.NEAREST。
  • expand(bool,optional) - 可选的扩展标志。如果为true,则展开输出以使其足够大以容纳整个旋转图像。如果为false或省略,则使输出图像与输入图像的大小相同。请注意,展开标志假定围绕中心旋转而不进行平移。
  • center(2-tuple ,optional) - 可选的旋转中心。原点是左上角。默认值是图像的中心。

图像变换

transforms.Resize

torchvision.transforms.Resize(size, interpolation=2)

将输入PIL图像的大小调整为给定大小。

  • size(sequence 或int) -所需的输出大小。如果size是类似(h,w)的序列,则输出大小将与此匹配。如果size是int,则图像的较小边缘将与此数字匹配。即,如果高度>宽度,则图像将重新缩放为(尺寸*高度/宽度,尺寸)
  • interpolation(int,optional) - 所需的插值。默认是 PIL.Image.BILINEAR

transforms.Normalize

torchvision.transforms.Normalize(mean, std)

transforms.ToTensor

torchvision.transforms.ToTensor

功能:将PIL Image或者 ndarray 转换为tensor,并且归一化至[0-1] 注意事项:归一化至[0-1]是直接除以255,若自己的ndarray数据尺度有变化,则需要自行修改。

Compose()

一般使用torchvision.transforms.Compose()类串联多个transform操作

data_transform = {
        "train": transforms.Compose([transforms.RandomResizedCrop(224),
                                     transforms.RandomHorizontalFlip(),
                                     transforms.ToTensor(),
                                     transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])]),
        "val": transforms.Compose([transforms.Resize(256),
                                   transforms.CenterCrop(224),
                                   transforms.ToTensor(),
                                   transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])}
posted @ 2021-11-18 15:12  梦想家肾小球  阅读(187)  评论(0编辑  收藏  举报