加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
02transforms方法2.py 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
LY 提交于 2024-09-21 17:18 . 线性层
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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化