代码拉取完成,页面将自动刷新
from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
img = Image.open("111.jpg")
print(img)
# ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor, 0)
# Normalize
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("norm", img_norm, 0)
# Resize
print(img.size)
trans_resize = transforms.Resize([512, 512])
img_resize = trans_resize(img)
print(img_resize.size)
img_resize = trans_totensor(img_resize)
print(img_resize.size)
writer.add_image("resize", img_resize, 0)
# Compose
trans_resize_2 = transforms.Resize(512)
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_rand = transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_rand, trans_totensor])
for i in range(10):
img_rand = trans_compose_2(img)
writer.add_image("rand", img_rand, i)
writer.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。