PyTorch教程-6:詳解PyTorch中的transforms

筆者PyTorch的全部簡單教程請訪問:http://www.lxweimin.com/nb/48831659

PyTorch教程-6:詳解PyTorch中的transforms

對于視覺方向的圖像處理方面,PyTorch提供了很好的預(yù)處理接口,對于圖像的轉(zhuǎn)換處理,使用 torchvision.tranforms 模塊使得這些操作非常高效。本文就介紹這個非常強(qiáng)大的工具,先引入transforms模塊:

import torchvision.transforms as transforms

完整的參考:https://pytorch.org/docs/stable/torchvision/transforms.html

組合多個變換操作

transforms.Compose() 方法接收一個 transforms 方法的list為參數(shù),將這些操作組合到一起,返回一個新的tranforms。通常用于包裝一個完整的變換操作的pipeline

import torchvision.transforms as transforms

myTransforms = transforms.Compose([
    transforms.CenterCrop(10),
    transforms.ToTensor()
])

對PIL Image和Tensor同時起作用的操作

本節(jié)所提到的所有的transforms的操作都可以以三種數(shù)據(jù)中的一種為參數(shù):

  • PIL格式的Image
  • tensor表示的圖片,其形狀為 (Channel, Height, Weight)
  • 一個tensor數(shù)組表示的多個圖片,其形狀為 (Batch, Channel, Height, Weight)

這些方法列在下邊,對于需要詳細(xì)查看其參數(shù)含義的操作,可以直接參考:
https://pytorch.org/docs/stable/torchvision/transforms.html#transforms-on-pil-image-and-torch-tensor

  • torchvision.transforms.CenterCrop(size)size為一個整數(shù)或者tuple,從圖片中心裁剪出size大小的圖片
  • torchvision.transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0):對圖像的亮度、對比度、飽和度和色調(diào)進(jìn)行調(diào)節(jié),每個參數(shù)接收一個float值或者一個float tuple (min, max)
  • torchvision.transforms.FiveCrop(size)size為一個整數(shù)或者tuple,從圖片中心和四個角裁剪出一共五張size大小的圖片
  • torchvision.transforms.Grayscale(num_output_channels=1):將圖片轉(zhuǎn)換為灰度圖,參數(shù)可取1或3,取3時,三個通道返回同樣的值
  • torchvision.transforms.Pad(padding, fill=0, padding_mode='constant'):對圖片做padding操作
  • torchvision.transforms.RandomAffine(degrees, translate=None, scale=None, shear=None, resample=0, fillcolor=0):對圖像進(jìn)行隨機(jī)的仿射變換
  • torchvision.transforms.RandomApply(transforms, p=0.5):接收一個transforms的list為參數(shù),依據(jù)它對圖片進(jìn)行隨機(jī)的變換
  • torchvision.transforms.RandomCrop(size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant'):在圖片的隨機(jī)位置剪裁并返回新圖像
  • torchvision.transforms.RandomGrayscale(p=0.1):以p的概率將圖片隨機(jī)轉(zhuǎn)換為灰度圖
  • torchvision.transforms.RandomHorizontalFlip(p=0.5):以p的概率隨機(jī)將圖片水平翻轉(zhuǎn)
  • torchvision.transforms.RandomPerspective(distortion_scale=0.5, p=0.5, interpolation=2, fill=0):以p的概率將圖片進(jìn)行隨機(jī)透視變換
  • torchvision.transforms.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=2):將圖片隨機(jī)地進(jìn)行剪裁得到給定大小或者長寬比的圖片
  • torchvision.transforms.RandomRotation(degrees, resample=False, expand=False, center=None, fill=None):隨機(jī)對圖片旋轉(zhuǎn)一個角度
  • torchvision.transforms.RandomVerticalFlip(p=0.5):以p的概率隨機(jī)將圖片垂直(上下)翻轉(zhuǎn)
  • torchvision.transforms.Resize(size, interpolation=2):將圖片大小放縮到給定大小
  • torchvision.transforms.TenCrop(size, vertical_flip=False):將圖片的四個角和中心進(jìn)行裁剪后,返回他們的反轉(zhuǎn)后的圖片,默認(rèn)水平反轉(zhuǎn)
  • torchvision.transforms.GaussianBlur(kernel_size, sigma=(0.1, 2.0)):對圖片進(jìn)行高斯模糊

僅對PIL Image起作用的操作

本小節(jié)提到的方法僅對PIL Image格式的圖片起作用,即不能用于torchscript(什么是TorchScripthttps://pytorch.org/docs/stable/jit.html?highlight=torchscript)。詳細(xì)的參數(shù)說明請參考:https://pytorch.org/docs/stable/torchvision/transforms.html#transforms-on-pil-image-only

  • torchvision.transforms.RandomChoice(transforms):接收一個transforms的list為參數(shù),從list中隨機(jī)挑選一個對圖片進(jìn)行變換
  • torchvision.transforms.RandomOrder(transforms):接收一個transforms的list為參數(shù),將list中的所有變換操作進(jìn)行隨機(jī)排序后對圖片進(jìn)行變換

僅對Tensor起作用的操作

本小節(jié)提到的方法僅能夠作用于 Tensor 上,詳細(xì)的參數(shù)說明請參考:https://pytorch.org/docs/stable/torchvision/transforms.html#transforms-on-torch-tensor-only

  • torchvision.transforms.LinearTransformation(transformation_matrix, mean_vector):對tensor進(jìn)行矩陣變換后減去一個向量
  • torchvision.transforms.Normalize(mean, std, inplace=False):對圖片進(jìn)行標(biāo)準(zhǔn)化
  • torchvision.transforms.RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0, inplace=False):隨機(jī)選取圖片中的矩形區(qū)域并刪除其像素值
  • torchvision.transforms.ConvertImageDtype(dtype: torch.dtype):將tensor的數(shù)據(jù)類型轉(zhuǎn)換為給定的torch.dtype的類型

用于進(jìn)行轉(zhuǎn)換的操作

本小節(jié)提到的方法用于數(shù)據(jù)類型的轉(zhuǎn)換,詳細(xì)的參數(shù)說明請參考:https://pytorch.org/docs/stable/torchvision/transforms.html#conversion-transforms

  • torchvision.transforms.ToPILImage(mode=None):將一個tensor格式的圖片轉(zhuǎn)換為PIL Image格式的圖片,不支持torchscript
  • torchvision.transforms.ToTensor:將一個PIL Image格式的圖片轉(zhuǎn)換為tensor格式的圖片,不支持torchscript,這個很重要
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容