5.初识Pytorch使用常用的transforms

1.使用的是transforms中常用的Normalize方法,如下公式,其中mean为均值,std的标准差

Normalize i.e.,
output[channel] = (input[channel]-mean[channel])/std[channel]

简单粗暴上代码:

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

root_path = "D:\\data\\basic\\Image"
label_path = "aligned"
img_dir = os.path.join(root_path,label_path)
img_list = os.listdir(img_dir)
img_path = img_list[1]

# 找aligned中的第二张图像
img_path = os.path.join(root_path,label_path,img_path)
img = Image.open(img_path)

# 创建SummaryWriter模板
writer = SummaryWriter("logs")

# 创建transforms.ToTensor模板
tran_tensor = transforms.ToTensor()
# 创建具体的ToTensor模板
img_tensor = tran_tensor(img)
writer.add_image("img_tensor",img_tensor,0)

# 做一个Normalize
# 创建transfors.Normalize模板
# 三个均值,三个标准差
tran_norm = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
img_norm = tran_norm(img_tensor)
writer.add_image("img_norm",img_norm,0)

writer.close()

run之后,logs的上一级目录中,在命令行输入:

tensorboard --logdir=logs

5.初识Pytorch使用常用的transforms_第1张图片

结果:
5.初识Pytorch使用常用的transforms_第2张图片

  1. 使用transforms中常用的Compose方法:先变换大小,再转化成tensor类型,这次使用的是original的图像好resize大小

简单粗暴上代码:

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

root_path = "D:\\data\\basic\\Image"
label_path = "original"
img_dir = os.path.join(root_path, label_path)
img_list = os.listdir(img_dir)
img_path = img_list[1]
# 找aligned 中第二张图像
img_path = os.path.join(img_dir, img_path)
img = Image.open(img_path)
print(img)

# 创建SummaryWriter模板
writer = SummaryWriter("logs")

# 创建transforms.ToTensor模板
tran_tensor = transforms.ToTensor()
img_tensor = tran_tensor(img)
# 创建transforms.Resize模板
# 按照比例缩放 原比例(1222,1834) -> (h=512,w=512/1222*1834)
# resize = transforms.Resize(512)

# resize成正方形
resize = transforms.Resize((512, 512))

# 创建transforms.Compose模板
compose = transforms.Compose([resize, tran_tensor])
img_compose = compose(img)

# 显示原图
writer.add_image("img_tensor", img_tensor, 0)
# 显示compose的图像
writer.add_image("img_compose", img_compose, 0)

writer.close()

结果:
5.初识Pytorch使用常用的transforms_第3张图片

  1. 使用transforms中常用的Compose方法:先随机裁剪,再转化成tensor类型,这次使用的是original的图像,随机裁剪10张图像
import os
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter

root_path = "D:\\data\\basic\\Image"
label_path = "original"
img_dir = os.path.join(root_path, label_path)
img_list = os.listdir(img_dir)
img_path = img_list[1]
# 找aligned 中第二张图像
img_path = os.path.join(img_dir, img_path)
img = Image.open(img_path)
print(img)

# 创建SummaryWriter模板
writer = SummaryWriter("logs")

# 创建transforms.ToTensor模板
tran_tensor = transforms.ToTensor()
img_tensor = tran_tensor(img)
# 创建transforms.Resize模板
# 按照比例缩放 原比例(1222,1834) -> (h=512,w=512/1222*1834)
# resize = transforms.Resize(512)

# resize成正方形
resize = transforms.Resize((512, 512))

# 创建RandomCrop的模板
rand_crop = transforms.RandomCrop(512)

# 创建transforms.Compose模板
compose = transforms.Compose([rand_crop, tran_tensor])
for i in range(10):
    img_compose = compose(img)
    # 显示compose的图像
    writer.add_image("img_compose", img_compose, i)

# 显示原图
writer.add_image("img_tensor", img_tensor, 0)


writer.close()

结果:
5.初识Pytorch使用常用的transforms_第4张图片上一章 4.初识Pytorch之Tensorboard与Transforms搭配使用
下一章 6.初识Pytorch之torchvision中的数据集使用

你可能感兴趣的:(手把手学习Pytorch,pytorch,深度学习,python)