【PyTorch】Transforms基本使用

简介: 【PyTorch】Transforms基本使用

二、Transforms基本使用

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

1、Transforms的结构及用法

Compose:

ToTensor:

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)  # <class 'PIL.JpegImagePlugin.JpegImageFile'>
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)  # <class 'numpy.ndarray'>
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()

2、常用的Transforms

2.1 ToTensor

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()

2.2 Normalize

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()

2.3 Resize

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)  # <PIL.Image.Image image mode=RGB size=450x450 at 0x1485DFE6310>
img_resize = trans_totensor(img_resize)
writer.add_image("Resize", img_resize, 0)
writer.close()

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()

2.5 RandomCrop

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学习笔记(3):图像的预处理(transforms)
Pytorch学习笔记(3):图像的预处理(transforms)
1332 1
Pytorch学习笔记(3):图像的预处理(transforms)
|
机器学习/深度学习 数据可视化 PyTorch
【PyTorch】TensorBoard基本使用
【PyTorch】TensorBoard基本使用
235 0
|
6月前
|
机器学习/深度学习 PyTorch 算法框架/工具
【单点知识】基于实例讲解PyTorch中的transforms类
【单点知识】基于实例讲解PyTorch中的transforms类
86 0
|
机器学习/深度学习 PyTorch Serverless
Pytorch基本使用—参数初始化
使用Pytorch进行参数初始化教程,重点是Xavier
243 0
|
机器学习/深度学习 存储 算法
Pytorch基本使用——优化器
总结了两种优化器,SGD和Adam及变种AdamW
257 0
|
自然语言处理 PyTorch 算法框架/工具
|
数据采集 并行计算 PyTorch
Pytorch基本使用—自定义数据集
Pytorch基本使用—自定义数据集
266 0
|
机器学习/深度学习 PyTorch 算法框架/工具
pytorch基本使用——定义模型
pytorch基本使用——定义模型
172 0
|
机器学习/深度学习 数据采集 存储
Pytorch介绍以及基本使用、深入了解、案例分析。(下)
Pytorch介绍以及基本使用、深入了解、案例分析。(下)
|
机器学习/深度学习 PyTorch TensorFlow
Pytorch介绍以及基本使用、深入了解、案例分析。(上)
Pytorch介绍以及基本使用、深入了解、案例分析。(上)