1 Star 0 Fork 0

gisleung/py_pytorch_learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
005 UsefulTransform.py 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
gisleung 提交于 2022-04-01 20:26 . 复习完毕
"""
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()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gisleung/py_pytorch_learn.git
git@gitee.com:gisleung/py_pytorch_learn.git
gisleung
py_pytorch_learn
py_pytorch_learn
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385