Torchvision中的Transforms的使用

发布时间:2023年12月24日

一、transforms结构及用法

在这里插入图片描述

查看tansforms.py说明文档:

ToTensor类作用是:将一个PIL图片或numpy形式转换成tensor的数据类型

python的用法-》tensor数据类型
通过 transforms.ToTensor去看两个问题
1、transforms该如何使用(python)
2、为什么我们需要Tensor数据类型

from PIL import Image
from torchvision import transforms

img_path = "dataset/train/ants/0013035.jpg"
img = Image.open(img_path)
# 1、transforms该如何使用(python)
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(tensor_img)

在这里插入图片描述
输出结果为向量。

·补充:利用opencv获得numpy类型:
import cv2
cv_img = cv2.imread(img_path)
print(type(cv_img))

在这里插入图片描述

使用ToTensor在tensorboard中加载图片:

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

img_path = "dataset/train/ants/0013035.jpg"
img = Image.open(img_path)

writer = SummaryWriter("logs")

tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
# print(tensor_img)
writer.add_image("Tensor_img", tensor_img)
writer.close()

在这里插入图片描述
在Terminal中运行,点击链接即可:

 tensorboard --logdir=logs

二、常见Transforms的使用

在这里插入图片描述
新建一个文件夹images,里面存放一张图片

from PIL import Image
img = Image.open("images/0013035.jpg")
print(img)

运行后输出:

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=768x512 at 0x2490E696FA0>

打开transforms说明文档,找到结构中的Compose:
在这里插入图片描述

Python中__call__的用法:

可以省略方法的调用。

class Person:
    def __call__(self, name):
        print("__call__" + " hello " + name)

    def hello(self, name):
        print(" hello " + name)


person = Person()
person("zhangsan")
person.hello("lisi")

输出:

__call__ hello zhangsan
 hello lisi

ToTensor的使用

(与上面 使用ToTensor在tensorboard中加载图片 相同)

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

writer = SummaryWriter("logs")
img = Image.open("images/0013035.jpg")
print(img)

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

Normalize的使用

归一化:
在这里插入图片描述
归一化公式:

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

案例中:
在这里插入图片描述

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

writer = SummaryWriter("logs")
img = Image.open("images/0013035.jpg")
# print(img)

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

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

在这里插入图片描述

Resize()的使用

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

在这里插入图片描述
在这里插入图片描述

Compose()的使用

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

在这里插入图片描述
在这里插入图片描述

RandomCrop()的使用

# RandomCrop
trans_random = transforms.RandomCrop(512)
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)

在这里插入图片描述

所有代码:

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

writer = SummaryWriter("logs")
img = Image.open("images/0013035.jpg")
# print(img)

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

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

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

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

# RandomCrop
trans_random = transforms.RandomCrop(512)
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()

总结:

1.关注输入和输出类型
2.多看官方文档
3.关注方法需要什么参数
4.不知道返回值的时候:
· print
· print(type())
· debug

文章来源:https://blog.csdn.net/shi_jiaye/article/details/135161965
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。