代码拉取完成,页面将自动刷新
"""
transforms:
提供了对图像操作的一些方法。
如:ToTensor Normalize Resize RandomCrop Compose ..
"""
from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs/005")
img_path = "images/pytorch.jpg"
img = Image.open(img_path)
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([1, 3, 5], [3, 2, 1]) # 平均值、标准差
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize", img_norm)
# Resize 调整图像大小
trans_resize = transforms.Resize((512, 512))
img_resize = trans_resize(img)
print(img_resize)
writer.add_image("Resize", trans_totensor(img_resize))
# Compose 把对图像的transform流水操作串起来
trans_resize_2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("Compose", img_resize_2)
# RandomCrop 随机裁剪图像
trans_random = transforms.RandomCrop(300)
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。