Pytorch 学习之Transforms

文章目录

Transforms 的使用

py 复制代码
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")

image_path = "data/train/ants_image/0013035.jpg"
img =Image.open(image_path)
print(img)
# 将图片转换为 tensor 类型
tensor_trans=transforms.ToTensor()
tensor_img =tensor_trans(img)

writer.add_image("test",tensor_img)

writer.close()
print(tensor_img)


归一化

py 复制代码
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")

image_path = "data/train/ants_image/0013035.jpg"
img =Image.open(image_path)
print(img)
# 将图片转换为 tensor 类型
tensor_trans=transforms.ToTensor()
tensor_img =tensor_trans(img)
writer.add_image("test",tensor_img)
writer.close()

#Normalize 归一化
print(tensor_img[0][0][0])
trans_norm=transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
img_norm=trans_norm(tensor_img)
print(img_norm[0][0][0])
writer.add_image("Normalize",img_norm)
writer.close()

Resize

c 复制代码
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")

image_path = "data/train/ants_image/0013035.jpg"
img =Image.open(image_path)
print(img)
# 将图片转换为 tensor 类型
tensor_trans=transforms.ToTensor()
tensor_img =tensor_trans(img)
writer.add_image("test",tensor_img)
writer.close()

#Normalize 归一化
print(tensor_img[0][0][0])
trans_norm=transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
img_norm=trans_norm(tensor_img)
print(img_norm[0][0][0])
writer.add_image("Normalize",img_norm)
writer.close()

#Resize
print(img.size)
trans_size=transforms.Resize((512,512))
img_resize=trans_size(img)
#img_resize PIL ->ToTensor ->img tensor
img_resize=tensor_trans(img_resize)
print(img_resize)
writer.add_image("resize",img_resize,0)
writer.close()

trans_size_2 =transforms.Resize(512)
trans_compose =transforms.Compose([trans_size_2,tensor_trans])
img_resize_2=trans_compose(img)
writer.add_image("Resize",img_resize_2,0)
writer.close()

随机裁剪

c 复制代码
trans_random =transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_random,tensor_trans])
for i in range(10):
    img_crop=trans_compose_2(img)
    writer.add_image("RandomCrop",img_crop,i)
writer.close()
相关推荐
咚咚王者11 小时前
人工智能之数据分析 Matplotlib:第三章 基本属性
人工智能·数据分析·matplotlib
车载测试工程师11 小时前
CAPL学习-IP API函数-1
网络·学习·tcp/ip·capl·canoe·doip
Mintopia11 小时前
开源AIGC模型对Web技术生态的影响与机遇 🌐✨
人工智能·aigc·敏捷开发
codetown11 小时前
openai-go通过SOCKS5代理调用外网大模型
人工智能·后端
世优科技虚拟人11 小时前
2026数字展厅设计核心关键,AI数字人交互大屏加速智慧展厅升级改造
人工智能·大模型·数字人·智慧展厅·展厅设计
艾莉丝努力练剑12 小时前
【Python基础:语法第一课】Python 基础语法详解:变量、类型、动态特性与运算符实战,构建完整的编程基础认知体系
大数据·人工智能·爬虫·python·pycharm·编辑器
MobotStone12 小时前
数字沟通之道
人工智能·算法
Together_CZ12 小时前
Cambrian-S: Towards Spatial Supersensing in Video——迈向视频中的空间超感知
人工智能·机器学习·音视频·spatial·cambrian-s·迈向视频中的空间超感知·supersensing
YJlio12 小时前
ShareEnum 学习笔记(9.5):内网共享体检——开放共享、匿名访问与权限风险
大数据·笔记·学习
caiyueloveclamp13 小时前
【功能介绍05】ChatPPT好不好用?如何用?用户操作手册来啦!——【AI辅写+分享篇】
人工智能·powerpoint·ai生成ppt·aippt·免费aippt