transforms可以用來改變樣本的多樣性,例如:旋轉、平移等等,訓練圖像辨識時,一定會用到的工具,現在來跟大家介紹他有哪些用法!我拿還未處理過的資料來當示範。這裡我把圖片都轉換成400*400的大小。
from torchvision import transforms
from PIL import Image
def img_loader(img_path):
image = Image.open(img_path)
img = image.resize((400, 400),Image.ANTIALIAS)
return img.convert('RGB')
transforms.Resize
重置影像解析度。
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.Resize(size=(300))])
img = tranform(img)
img.show()
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.Resize(size=(300,200))])
img = tranform(img)
img.show()
標準化:transforms.Normalize
對資料按通道進行標準化,即先減均值,再除以標準差。
轉為tensor:transforms.ToTensor
修改亮度、對比度和飽和度:transforms.ColorJitter
调整亮度、对比度、饱和度、色相。
調整亮度
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.ColorJitter(brightness=(0.3,1.5))])
img = tranform(img)
img.show()
調整對比
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.ColorJitter(contrast=(0.3,0.5))])
img = tranform(img)
img.show()
調整飽和度
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.ColorJitter(saturation=(0.3,0.5))])
img = tranform(img)
img.show()
調整色相
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.ColorJitter(hue=(0.3,0.5))])
img = tranform(img)
img.show()
轉為灰階圖:transforms.Grayscale
轉為灰階圖。
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.Grayscale(num_output_channels=1)])
img = tranform(img)
img.show()
對圖像進行仿射變換,為2維的線性轉換。有五種基本操作,旋轉、平移、縮放、錯切及翻轉。
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.RandomAffine(degrees=30)])
img = tranform(img)
img.show()
平移
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.RandomAffine(degrees= 0 ,translate=(0.2,0.5))])
img = tranform(img)
img.show()
縮放
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.RandomAffine(degrees= 0 ,scale=(0.2,0.5))])
img = tranform(img)
img.show()
錯切
img = img_loader(r"C:\Users\Frank\PycharmProjects\practice\mountain\train\5_拓.jpg")
tranform = transforms.Compose([transforms.RandomAffine(degrees= 0 ,shear=(30))])
img = tranform(img)
img.show()