pytorch transforms使用和演示


pytorch transforms實例演示

演示下transforms的效果,供大家一個參考吧

首先准備下需要用到的圖片

import matplotlib.pyplot as plt
import torchvision.transforms as tf
from PIL import Image
import PIL
import numpy as np
import torch
import imageio


def show(img, transform=None):
    if transform:
        if isinstance(transform, (tf.FiveCrop, tf.TenCrop)):
            t = transform(img)
            for i in range(len(t)):
                plt.subplot(len(t)/5, 5, 1+i)
                plt.axis('off')
                plt.imshow(t[i])
        else:
            for i in range(9):
                plt.subplot(331 + i)
                plt.axis('off')
                t= transform(img)
                plt.imshow(t)
        print(str(transform))
        print(t)
    else:
        plt.imshow(img)
        plt.axis('off')
    plt.show()

    
img = imageio.imread('http://ww1.sinaimg.cn/large/7ec23716gy1g6rx3xt8x1j203d02j3ye.jpg')
img = Image.fromarray(img)
print(img)
show(img)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F1687DD8D0>

[外鏈圖片轉存失敗(img-2qZJP3Nq-1567917282504)(output_1_1.png)]

RandomHorizontalFlip,RandomVerticalFlip - 水平翻轉,垂直翻轉

# multiplt img
t = tf.RandomHorizontalFlip()
show(img, t)
show(img, tf.RandomVerticalFlip())
RandomHorizontalFlip(p=0.5)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F1687DD8D0>

在這里插入圖片描述

RandomVerticalFlip(p=0.5)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F1687DD8D0>

在這里插入圖片描述

CenterCrop - 中心裁剪,以圖片中心為起點,按像素大小進行裁剪

show(img, tf.CenterCrop(60))  # 小於圖片大小則進行中心縮放
show(img, tf.CenterCrop(150))  # 大於圖片大小則進行中心縮小,邊緣填充0
CenterCrop(size=(60, 60))
<PIL.Image.Image image mode=RGB size=60x60 at 0x1F168D8A390>

在這里插入圖片描述

CenterCrop(size=(150, 150))
<PIL.Image.Image image mode=RGB size=150x150 at 0x1F169F81240>

在這里插入圖片描述

RandomCrop - 隨機取不同的點作為中心進行裁剪

show(img, tf.RandomCrop(60))
show(img, tf.RandomCrop(100,padding=50, pad_if_needed=True))
show(img, tf.RandomCrop(100,padding=50, pad_if_needed=True, fill=255))
RandomCrop(size=(60, 60), padding=None)
<PIL.Image.Image image mode=RGB size=60x60 at 0x1F16A029048>

在這里插入圖片描述

RandomCrop(size=(100, 100), padding=50)
<PIL.Image.Image image mode=RGB size=100x100 at 0x1F168B805F8>

在這里插入圖片描述

RandomCrop(size=(100, 100), padding=50)
<PIL.Image.Image image mode=RGB size=100x100 at 0x1F168AABC50>

在這里插入圖片描述

RandomResizedCrop - 先crop,再resize,將給定圖像隨機裁剪為不同的大小和寬高比,然后縮放所裁剪得到的圖像為制定的大小

  • size為縮放之后的大小
  • scala為裁剪比例
  • ratio為伸縮比例
  • interpolation為resize模式
show(img, tf.RandomResizedCrop(60))
show(img, tf.RandomResizedCrop(150))
show(img, tf.RandomResizedCrop(60, scale=(0.5, 2.0)))  # 縮放比例
show(img, tf.RandomResizedCrop(60, ratio=(1/2, 2/1)))  # 拉伸比例
RandomResizedCrop(size=(60, 60), scale=(0.08, 1.0), ratio=(0.75, 1.3333), interpolation=PIL.Image.BILINEAR)
<PIL.Image.Image image mode=RGB size=60x60 at 0x1F16A189668>

在這里插入圖片描述

RandomResizedCrop(size=(150, 150), scale=(0.08, 1.0), ratio=(0.75, 1.3333), interpolation=PIL.Image.BILINEAR)
<PIL.Image.Image image mode=RGB size=150x150 at 0x1F1689A66A0>

在這里插入圖片描述

RandomResizedCrop(size=(60, 60), scale=(0.5, 2.0), ratio=(0.75, 1.3333), interpolation=PIL.Image.BILINEAR)
<PIL.Image.Image image mode=RGB size=60x60 at 0x1F168DAAAC8>

在這里插入圖片描述

RandomResizedCrop(size=(60, 60), scale=(0.08, 1.0), ratio=(0.5, 2.0), interpolation=PIL.Image.BILINEAR)
<PIL.Image.Image image mode=RGB size=60x60 at 0x1F169F69BE0>

在這里插入圖片描述

ColorJitter - hsv顏色空間 色調(H),飽和度(S),明度(V)


show(img, tf.ColorJitter(brightness=0.5))  # 亮度
show(img, tf.ColorJitter(contrast=0.5))  # 對比度
show(img, tf.ColorJitter(saturation=0.5))  # 飽和度
show(img, tf.ColorJitter(hue=0.1))  # 色調,只能是-0.5~0.5,應該指的是可以逆時針旋轉180度或順時針旋轉180度
ColorJitter(brightness=[0.5, 1.5], contrast=None, saturation=None, hue=None)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F16A025D68>

在這里插入圖片描述

ColorJitter(brightness=None, contrast=[0.5, 1.5], saturation=None, hue=None)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F168AB0D68>

在這里插入圖片描述

ColorJitter(brightness=None, contrast=None, saturation=[0.5, 1.5], hue=None)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F168BE3EF0>

在這里插入圖片描述

ColorJitter(brightness=None, contrast=None, saturation=None, hue=[-0.1, 0.1])
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F168A45828>

在這里插入圖片描述

FiveCrop,TenCrop - 將一張圖片裁剪出5張出來

  • 輸入(batch_size, channel, height, width)
  • 輸出(batch_size, ncrops, channel, height, width)
show(img, tf.FiveCrop(60))
# show(img, tf.FiveCrop(121))
FiveCrop(size=(60, 60))
(<PIL.Image.Image image mode=RGB size=60x60 at 0x1F16A00D470>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F16A00D208>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F16A00D5C0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168ACD048>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F16A00DA90>)

在這里插入圖片描述

show(img, tf.TenCrop(60))
show(img, tf.TenCrop(60, vertical_flip=True))
TenCrop(size=(60, 60), vertical_flip=False)
(<PIL.Image.Image image mode=RGB size=60x60 at 0x1F16A00D710>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A7FF28>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A524E0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A52CC0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A52DA0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A520F0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A52E10>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A592B0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168B8A7F0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A59D68>)

在這里插入圖片描述

TenCrop(size=(60, 60), vertical_flip=True)
(<PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A52CC0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A52DA0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A520F0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A52E10>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168B8A7F0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A59D68>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F162FB22B0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168A59668>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168CB14E0>, <PIL.Image.Image image mode=RGB size=60x60 at 0x1F168CB1898>)

在這里插入圖片描述

Pad - 填充值

show(img, tf.Pad(20))
show(img, tf.Pad(20,padding_mode='edge'))
show(img, tf.Pad(20,padding_mode='reflect'))
Pad(padding=20, fill=0, padding_mode=constant)
<PIL.Image.Image image mode=RGB size=161x131 at 0x1F168B0B240>

在這里插入圖片描述

Pad(padding=20, fill=0, padding_mode=edge)
<PIL.Image.Image image mode=RGB size=161x131 at 0x1F168900D30>

在這里插入圖片描述

Pad(padding=20, fill=0, padding_mode=reflect)
<PIL.Image.Image image mode=RGB size=161x131 at 0x1F16A17AF60>

在這里插入圖片描述

RandomPerspective - 透視變換

show(img, tf.RandomPerspective())
C:\ProgramData\Anaconda3\lib\site-packages\torchvision\transforms\functional.py:440: UserWarning: torch.gels is deprecated in favour of torch.lstsq and will be removed in the next release. Please use torch.lstsq instead.
  res = torch.gels(B, A)[0]


RandomPerspective(p=0.5)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F1687DD8D0>

在這里插入圖片描述

RandomAffine - 仿射變換,有旋轉,平移,縮放,剪切變換

show(img, tf.RandomAffine(50))  # 旋轉角度
show(img, tf.RandomAffine(0, translate=(0.1,0.3)))  # 平移范圍
show(img, tf.RandomAffine(0, scale=(0.5, 1)))  # 縮放比例
show(img, tf.RandomAffine(0, shear=50))  # 剪切變換
show(img, tf.RandomAffine(50, resample=PIL.Image.BILINEAR))  # TODO
RandomAffine(degrees=(-50, 50))
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F16A0BE358>

在這里插入圖片描述

RandomAffine(degrees=(0, 0), translate=(0.1, 0.3))
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F16A1DDC50>

在這里插入圖片描述

RandomAffine(degrees=(0, 0), scale=(0.5, 1))
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F166449A90>

在這里插入圖片描述

RandomAffine(degrees=(0, 0), shear=(-50, 50))
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F16883F860>

在這里插入圖片描述

RandomAffine(degrees=(-50, 50), resample=PIL.Image.BILINEAR)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F168A43198>

在這里插入圖片描述

RandomGrayscale - 灰度模式

show(img, tf.RandomGrayscale(0.5))  # 使用0.5的概率進行灰度化
RandomGrayscale(p=0.5)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F1687DD8D0>

在這里插入圖片描述

RandomErasing - 隨機擦除

# Erasing還需要組合使用
t = tf.Compose([tf.ToTensor(),tf.RandomErasing(), tf.ToPILImage()])
show(img, t)
Compose(
    ToTensor()
    <torchvision.transforms.transforms.RandomErasing object at 0x000001F169F9CA20>
    ToPILImage()
)
<PIL.Image.Image image mode=RGB size=121x91 at 0x1F168A6DDA0>

在這里插入圖片描述

lambda

def foo(x):
    r = x[0, ...] + 0.1
    print(x.shape, x.max(), x.min(), r.shape, r.max(), r.min())
    return r
t = tf.Compose([tf.ToTensor(),tf.Lambda(foo), tf.ToPILImage()])
show(img, t)
torch.Size([3, 91, 121]) tensor(0.9686) tensor(0.) torch.Size([91, 121]) tensor(1.0686) tensor(0.1275)
torch.Size([3, 91, 121]) tensor(0.9686) tensor(0.) torch.Size([91, 121]) tensor(1.0686) tensor(0.1275)
torch.Size([3, 91, 121]) tensor(0.9686) tensor(0.) torch.Size([91, 121]) tensor(1.0686) tensor(0.1275)
torch.Size([3, 91, 121]) tensor(0.9686) tensor(0.) torch.Size([91, 121]) tensor(1.0686) tensor(0.1275)
torch.Size([3, 91, 121]) tensor(0.9686) tensor(0.) torch.Size([91, 121]) tensor(1.0686) tensor(0.1275)
torch.Size([3, 91, 121]) tensor(0.9686) tensor(0.) torch.Size([91, 121]) tensor(1.0686) tensor(0.1275)
torch.Size([3, 91, 121]) tensor(0.9686) tensor(0.) torch.Size([91, 121]) tensor(1.0686) tensor(0.1275)
torch.Size([3, 91, 121]) tensor(0.9686) tensor(0.) torch.Size([91, 121]) tensor(1.0686) tensor(0.1275)
torch.Size([3, 91, 121]) tensor(0.9686) tensor(0.) torch.Size([91, 121]) tensor(1.0686) tensor(0.1275)
Compose(
    ToTensor()
    Lambda()
    ToPILImage()
)
<PIL.Image.Image image mode=L size=121x91 at 0x1F168AA24E0>

在這里插入圖片描述




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM