当前位置:网站首页>PyTorch②---transforms结构及用法
PyTorch②---transforms结构及用法
2022-08-02 14:08:00 【伏月三十】
transforms结构及用法
transforms.py工具箱
输入(图片)—>工具箱(常用:totensor、resize等方法)—>结果
from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
import cv2
#python的用法--->tensor数据类型
#通过transforms.ToTensor解决两个问题
#1.transforms该如何使用
#2.为什么需要tensor数据类型
'''绝对路径:D:\pythonProject\pytorch\dataset\train\ants\0013035.jpg 相对路径:dataset/train/ants/0013035.jpg '''
#读取图片:PIL格式
img_path="dataset/train/ants/0013035.jpg"
img=Image.open(img_path)
print(img)
#读取图片:opencv,numpy格式
cv_img=cv2.imread(img_path)
print(cv_img)
writer=SummaryWriter("logs")
#将PIL文件或numpy文件转换为tensor类型(1.transforms该如何使用)
#实例化
tensor_trans=transforms.ToTensor()
#调用、传入参数:PIL类型,点击CTRL+P
tensor_img=tensor_trans(img)
print(tensor_img)
#2.为什么需要tensor数据类型:tensor数据类型包括了神经网络需要的一些参数
#直接传入tensor型
writer.add_image("Tensor_img",tensor_img)
writer.close()
注意:上节课用tensorboard读入的图片是PIL—>numpy类型
这节课用tensorboard读入的图片是PIL、numpy—>tensor类型

边栏推荐
- 还是别看学位论文
- 7.如何给RecyclerView添加Click和LongClick事件
- Redis持久化机制
- UIWindow的makeKeyAndVisible不调用rootviewController 的viewDidLoad的问题
- PyTorch(14)---使用现有的模型及其修改
- 关于UDF
- The problem that UIWindow's makeKeyAndVisible does not call viewDidLoad of rootviewController
- LLVM系列第二十三章:写一个简单的运行时函数调用统计器(Pass)
- 无人驾驶综述:等级划分
- MySQL知识总结 (三) 索引
猜你喜欢
随机推荐
Kubernetes介绍
flutter中App签名
每周招聘|PostgreSQL专家,年薪60+,高能力高薪资
spark(standalone,yarn)
LLVM系列第十七章:控制流语句for
Redis持久化机制
一文带你快速掌握Kotlin核心技能
spark资源调度和任务调度
MySQL知识总结 (三) 索引
Hession使用
checkPermissions Missing write access to /usr/local/lib
无人驾驶综述:等级划分
基于ThinkPHP6.0 - 宝塔搭建漫画CMS管理系统源码实测
【目标检测】YOLO v5 吸烟行为识别检测
It is not allowed to subscribe with a(n) xxx multiple times.Please create a fresh instance of xxx
LLVM系列第八章:算术运算语句Arithmetic Statement
“非图灵完备”到底意味着什么
6. How to use the CardView production card layout effect
对疫情期间量化策略表现的看法
内存申请(malloc)和释放(free)之下篇









