torchvision.transforms中的常用方法

目录

1.转tensor ToTensor()

2.转PIL ToPILImage()

3.归一化 Normalize()

4.组合不同的transforms Compose() + 调整图片像素 Resize()

1.转tensor ToTensor()

将数据转为tensor类

# 导入必要的模块
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter

# 图片路径
img_path = 'test.png'
# 读取图片
img_PIL = Image.open(img_path)

# 新建一个ToTensor()工厂
trans_totensor = transforms.ToTensor()
# 使用ToTensor()工厂
img_tensor = trans_totensor(img_PIL)
# 转为tensor类
print(type(img_tensor))

# 新建一个SummaryWriter
writer = SummaryWriter(log_dir='logs_trans')
# 将数据写入到tensorboard
writer.add_image(tag='trans_test', img_tensor=img_tensor)
# 关闭writer
writer.close()

2.转PIL ToPILImage()

将tensor类的图片转为PIL Image

# 导入必要的模块
from torchvision import transforms
import cv2
import torch.nn as nn

# 图片路径
img_path = 'test.png'
# 使用opencv读取图片
img_arr = cv2.imread(img_path)
# 新建一个ToTensor工厂
trans_totensor = transforms.ToTensor()
# 将图片转为tensor类
img_tensor = trans_totensor(img_arr) #  参数-pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
# 查看size
print(img_tensor.shape)# torch.Size([3, 450, 500])

class MyModel(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        # 对这个图片做一次卷积
        '''
            彩色图:in_channels=3,为了查看图片,out_channels也设为3
            卷积核大小:kernal_size=9,步长stride=1
        '''
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=9, stride=1)

    def forward(self, input):
        return self.conv1(input)
    
mymodel = MyModel()
# 需要先对图片reshape,原来是[3,450,500]
img_tensor = torch.reshape(img_tensor,(1,3,450,500))
output = mymodel(img_tensor)
print(output.shape)
# 新建一个ToPILImage()工厂
trans_PIL = transforms.ToPILImage()
# 需要先对输出图片reshape,原来是[1,3,442,492]
output = torch.reshape(output,(3,442,492))
# 使用ToPILImage()
img_0 = trans_PIL(output)
# 展示图片
img_0.show()

原图

torchvision.transforms中的常用方法_第1张图片

处理后

torchvision.transforms中的常用方法_第2张图片

3.归一化 Normalize()

# 导入必要的模块
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter

# 图片路径
img_path = 'test.png'
# 读取图片
img = Image.open(img_path)

# 新建一个Normalize()工厂
'''
    mean-均值
    std-标准差
'''
trans_norm = transforms.Normalize(mean=[0.5,0.5,1], std=[1,1,1])

# 对图片进行归一化
img_norm = trans_norm(img)

# 新建一个SummaryWriter
writer = SummaryWriter(log_dir='logs_norm')
# 将图片写入tensorboard
writer.add_image(tag='origin', img_tensor=img)
writer.add_image(tag='normalize', img_tensor=img_norm)
# 关闭writer
writer.close()

原图

torchvision.transforms中的常用方法_第3张图片

归一化后

torchvision.transforms中的常用方法_第4张图片

4.组合不同的transforms Compose() + 调整图片像素 Resize()

# 导入必要的模块
from torchvision import transforms
import cv2
from torch.utils.tensorborad import SummaryWriter

# 图片路径
img_path = 'test.png'
# 读取图片
img = cv2.imread(img_path)

# 新建一个ToTensor()
trans_totensor = transforms.ToTensor()
# 新建一个Resize()
'''
    参数可以是一个数或者一个元组
'''
trans_resize = transforms.Resize(500) # 等比例调整图片像素
trans_resize1 = transforms.Resize((1024,900))# 调整图片像素的长,宽
# 新建一个Compose()
trans_comp = transforms.Compose([
    trans_totensor,
    trans_resize,
    trans_resize1
])
# 按顺序执行totensor=>resize=>resize1

# 使用trans_comp()
img_comp = trans_comp(img)

# 将原图和结果写到tensorboard
writer = SummaryWriter('logs_comp')
writer.add_image('origin',trans_totensor(img))
writer.add_image('compose',img_comp)
writer.close()

你可能感兴趣的:(深度学习基础,深度学习,python)