【PyTorch】Transforms基本使用

文章目录

  • 二、Transforms基本使用
    • 1、Transforms的结构及用法
      • 1.1 如何使用
      • 1.2 TensorBoard查看
    • 2、常用的Transforms
      • 2.1 ToTensor
      • 2.2 Normalize
      • 2.3 Resize
      • 2.4 Compose
      • 2.5 RandomCrop

二、Transforms基本使用

Transforms主要是对特定格式的图片进行一些变化。

【PyTorch】Transforms基本使用_第1张图片

1、Transforms的结构及用法

Compose:

【PyTorch】Transforms基本使用_第2张图片

ToTensor:

【PyTorch】Transforms基本使用_第3张图片

1.1 如何使用

PIL Image to Tensor

from torchvision import transforms
from PIL import Image

img_path = "../data/tensorboard_data/train/ants_image/0013035.jpg"
img = Image.open(img_path)  # 

tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

print(tensor_img)
tensor([[[0.3137, 0.3137, 0.3137,  ..., 0.3176, 0.3098, 0.2980],
         [0.3176, 0.3176, 0.3176,  ..., 0.3176, 0.3098, 0.2980],
         [0.3216, 0.3216, 0.3216,  ..., 0.3137, 0.3098, 0.3020],
         ...,
         ...,
         [0.9294, 0.9294, 0.9255,  ..., 0.5529, 0.9216, 0.8941],
         [0.9294, 0.9294, 0.9255,  ..., 0.8863, 1.0000, 0.9137],
         [0.9294, 0.9294, 0.9255,  ..., 0.9490, 0.9804, 0.9137]]])

numpy.ndarry to Tensor

from torchvision import transforms
import cv2

img_path = "../data/tensorboard_data/train/ants_image/0013035.jpg"
img = cv2.imread(img_path)  # 

tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

print(tensor_img)
tensor([[[0.9137, 0.9137, 0.9137,  ..., 0.9176, 0.9098, 0.8980],
         [0.9176, 0.9176, 0.9176,  ..., 0.9176, 0.9098, 0.8980],
         [0.9216, 0.9216, 0.9216,  ..., 0.9137, 0.9098, 0.9020],
         ...,
         ...,
         [0.3412, 0.3412, 0.3373,  ..., 0.1725, 0.3725, 0.3529],
         [0.3412, 0.3412, 0.3373,  ..., 0.3294, 0.3529, 0.3294],
         [0.3412, 0.3412, 0.3373,  ..., 0.3098, 0.3059, 0.3294]]])

1.2 TensorBoard查看

from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image

img_path = "../data/tensorboard_data/train/ants_image/0013035.jpg"

img = Image.open(img_path)

writer = SummaryWriter("logs")

tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

writer.add_image("tensor_img", tensor_img)

writer.close()

【PyTorch】Transforms基本使用_第4张图片

2、常用的Transforms

2.1 ToTensor

【PyTorch】Transforms基本使用_第5张图片

from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image

writer = SummaryWriter("logs")
img = Image.open("../images/pytorch.webp")

trans_totensor = transforms.ToTensor()
img_totensor = trans_totensor(img)

writer.add_image("ToTensor", img_totensor)
writer.close()

【PyTorch】Transforms基本使用_第6张图片

2.2 Normalize

【PyTorch】Transforms基本使用_第7张图片

1、计算公式:

output[channel] = (input[channel] - mean[channel]) / std[channel]

2、本实例中即:

(input - 0.5)/0.5 = 2 * input - 1

3、大致范围:

input[0, 1] —> result[-1, 1]

from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image

writer = SummaryWriter("logs")
img = Image.open("../images/pytorch.webp")

# 1.ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)

# 2.Normalize
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])

writer.add_image("Normalize", img_norm)
writer.close()

【PyTorch】Transforms基本使用_第8张图片

2.3 Resize

【PyTorch】Transforms基本使用_第9张图片

from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image

writer = SummaryWriter("logs")
img = Image.open("../images/pytorch.webp")

# 1.ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)

# 2.Resize
print(img.size)  # (889, 500)
# img PIL -> resize -> img_resize PIL
trans_resize = transforms.Resize((450, 450))
# img_resize PIL -> totensor -> img_resize tensor
img_resize = trans_resize(img)
print(img_resize)  # 
img_resize = trans_totensor(img_resize)
writer.add_image("Resize", img_resize, 0)

writer.close()

【PyTorch】Transforms基本使用_第10张图片

2.4 Compose

还可以直接使用Compose

from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image

writer = SummaryWriter("logs")
img = Image.open("../images/pytorch.webp")

# 1.ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)

# 2.Compose - Resize - 2
trans_resize_2 = transforms.Resize(100)
# PIL -> PIL -> tensor
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("Compose - Resize", img_resize_2, 0)
writer.close()

【PyTorch】Transforms基本使用_第11张图片

2.5 RandomCrop

【PyTorch】Transforms基本使用_第12张图片

from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image

writer = SummaryWriter("logs")
img = Image.open("../images/pytorch.webp")

# 1.ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)

# 2.RandomCrop
trans_random = transforms.RandomCrop((250, 444))
trans_compose_2 = transforms.Compose([trans_random, trans_totensor])
for i in range(10):
    img_crop = trans_compose_2(img)
    writer.add_image("RandomCrop", img_crop, i)

writer.close()

你可能感兴趣的:(小土堆PyTorch,pytorch,深度学习,python,tensorflow)