transforms的一些工具

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

img_dir = "5.jpg"
img = Image.open(img_dir)  # PIL RGB 768x512


writer = SummaryWriter("log")  # torch.Tensor, numpy.array

# ToTensor
tensor = transforms.ToTensor()
tensor_img = tensor(img)
print(tensor_img.shape)
writer.add_image("ToTensor",tensor_img,1)

# Normalize
normalize = transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
normalize_img = normalize(tensor_img)
print(normalize_img.shape)
writer.add_image("Normalize",normalize_img,1)

# Resize
resize = transforms.Resize((40,100))  # (h, w)
resize_img = resize(tensor_img)
print(resize_img.shape)
writer.add_image("Resize",resize_img,1)

# Compose
compose = transforms.Compose([
    transforms.ToTensor(),
    transforms.Resize((200,300)),
    transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)),
])
compose_img = compose(img)
print(compose_img.shape)
writer.add_image("Compose",compose_img,1)

# RandomCrop
randomcrop = transforms.RandomCrop((200))
randomcrop_img = randomcrop(tensor_img)
print(randomcrop_img.shape)
writer.add_image("RandomCrop",randomcrop_img,1

writer.close()

![在这里插入图片描述](https://img-blo
查看日志 写log的绝对路径
Tensorboard --logdir “E:\python_file\pytorch\log” # (默认6006端口)
Tensorboard --logdir “E:\python_file\pytorch\log” --port=6007 # (打开6007端口)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶叶梓梓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值