前提
文章目录
【PyTorch】深度学习PyTorch环境配置及安装【详细清晰】
【PyTorch】深度学习PyTorch加载数据
【PyTorch】关于Tensorboard的简单使用
文章目录
torchvision中的transforms
transforms主要是对图片进行变换
transforms该如何使用(python)
绝对路径:D:\PyCharm\learn_pytorch\dataset\train\ants_image\0013035.jpg
相对路径:dataset/train/ants_image/0013035.jpg
一般采用相对路径 因为Windows系统\会被当作转义符
from PIL import Image
from torchvision import transforms
img_path = "dataset/train/ants_image/0013035.jpg"
img = Image.open(img_path)
# print(img) # <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=768x512 at 0x28E7F9E23B0>
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(tensor_img)
使用Tensor的类型
tensorboard 的SummaryWriter需要使用Tensor的类型的图片
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img_path = "dataset/train/ants_image/0013035.jpg"
img = Image.open(img_path)
write = SummaryWriter("logs")
# 1、transforms该如何使用(python)
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(tensor_img)
#2、使用Tensor的类型
write.add_image("Tensor_img",tensor_img)
write.close()
常见的Transforms
Transforms的部分方法的使用
Python中的__call__用法
class Person:
# 下划线表示内置函数
def __call__(self, name):
print("__call__" + "Hello" + name)
def hello(self, name):
print("hello" + name)
person = Person()
person("张三") # 内置函数可以不用调用.方法名(参数)
person.hello("李四") # 不是内置函数需要调用.方法名(参数)
ToTensor使用
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer = SummaryWriter("logs")
img = Image.open("images/pytorch.png")
# print(img) # <PIL.PngImagePlugin.PngImageFile image mode=RGB size=1314x684 at 0x1644E187520>
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor",img_tensor)
writer.close()
Normalize归一化的使用
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer = SummaryWriter("logs")
img = Image.open("images/pytorch.png")
# print(img) # <PIL.PngImagePlugin.PngImageFile image mode=RGB size=1314x684 at 0x1644E187520>
# 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]) # 3个颜色通道
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize",img_norm)
writer.close()
替换参数再次尝试:
trans_norm = transforms.Normalize([6, 3, 2], [9, 3, 5]) # 3个颜色通道
writer.add_image("Normalize", img_norm, 2)
Resize的使用
Resize输入两个参数时,输出图片的高和宽的像素点数量会按照你设定的值进行输出。而只输入一个参数时,代表你最短的那个边输出的像素点数量
# Resize使用
print(img.size) # (3200, 1800)
trans_resize = transforms.Resize((512, 512)) # 注意这里一定要多加一个括号
img_resize = trans_resize(img)
print(img_resize) # <PIL.Image.Image image mode=RGB size=512x512 at 0x15D57A081C0>
# img PIL ----> Resize ----> img_resize PIL ----> ToTensor ----> img_resize tensor
# 要用tensorboard显示要将PIL的类型转换为tensor类型
img_resize = trans_totensor(img_resize)
writer.add_image("Resize", img_resize, 0)
writer.close()
图片被拉长了
Compose()的使用
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])
# 裁剪10个
for i in range(10):
img_crop = trans_compose_2(img)
writer.add_image("RandomCrop", img_crop, i)
加序列进行裁剪
trans_random = transforms.RandomCrop((500, 1000)) # 注意2个括号,裁剪500X1000的
writer.add_image("RandomCropHW", img_crop, i)
总结Transforms方法使用
以上测试全部代码:
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer = SummaryWriter("logs")
img = Image.open("images/pytorch.png")
# print(img) # <PIL.PngImagePlugin.PngImageFile image mode=RGB size=1314x684 at 0x1644E187520>
# 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]) # 3个颜色通道
trans_norm = transforms.Normalize([6, 3, 2], [9, 3, 5]) # 3个颜色通道
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize", img_norm, 2)
# Resize使用
print(img.size) # (3200, 1800)
trans_resize = transforms.Resize((512, 512)) # 注意这里一定要多加一个括号
img_resize = trans_resize(img)
print(img_resize) # <PIL.Image.Image image mode=RGB size=512x512 at 0x15D57A081C0>
# img PIL ----> Resize ----> img_resize PIL ----> ToTensor ----> img_resize tensor
# 要用tensorboard显示要将PIL的类型转换为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_random = transforms.RandomCrop((500, 1000)) # 注意2个括号,裁剪500X1000的
trans_compose_2 = transforms.Compose([trans_random, trans_totensor])
# 裁剪10个
for i in range(10):
img_crop = trans_compose_2(img)
writer.add_image("RandomCropHW", img_crop, i)
writer.close()