当前位置:网站首页>PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
2022-04-23 06:11:00 【小风_】
目录
0.导包
import torch
import numpy as np
import PIL
1.tensor类型转换
# 设置Torch默认类型
torch.set_default_tensor_type(torch.FloatTensor)
# CPU GPU # 数据类型
# torch.FloatTensor torch.cuda.FloatTensor # 32位浮点数
# torch.DoubleTensor torch.cuda.DoubleTensor # 64位浮点数
# torch.cuda.HalfTensor # 16位浮点数
# 类型转换
tensor = torch.rand(1,3,224,224)
tensor = tensor.float() # 转为float类型,其他:half() int() double() char() byte() short() long()
tensor = tensor.cuda() # 转为GPU对象
tensor = tensor.cpu() # 转为CPU对象
# type_as()方法
tensor1 = torch.rand(2,3)
tensor2 = torch.IntTensor(2,3)
tensor3 = tensor1.type_as(tensor2) # 将tensor1转为tensor2的类型
# print(tensor1,tensor2,tensor3)
2.tensor和list转换
'''tensor和list转换'''
tensor1 = torch.ones([1,5]) # 创建一个1x5的单位张量
list1 = tensor1.tolist() # tensor -> list
tensor1 = torch.tensor(list1) # list -> tensor
3.tensor和np.ndarray转换
'''tensor和np.ndarray转换'''
ndarray = tensor.numpy() # 注意GPU对象要先转为CPU对象
tensor = torch.from_numpy(ndarray) # 注意一般转为浮点数对象
4.图像tensor和图像ndarray转换,tensor -> cv2
'''图像tensor和图像ndarray转换,tensor -> cv2'''
# tensor的图像是维度信息是[N, C, H, W]
ndarray = np.random.random((3,224,224))
tensor = torch.tensor(ndarray).permute(2,0,1) #ndarray转tensor图像要注意维度是HWC->CHW
img = tensor.permute(1,2,0).cpu().numpy() #反之同理
5.cv2(ndarray)与PIL之间的转换
'''PIL.Image和np.ndarray转换,也可是cv2与PIL之间的转换'''
path = '1.jpg' #这里传入自己的图像地址
ndarray = np.asarray(PIL.Image.open(path)) #Image转ndarray
img = PIL.Image.fromarray(ndarray.astype(np.uint8)) #ndarray转Image
版权声明
本文为[小风_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_33952811/article/details/124293373
边栏推荐
- 常用UI控件简写名
- 一款png生成webp,gif, apng,同时支持webp,gif, apng转化的工具iSparta
- Tiny4412 HDMI显示
- JVM basics you should know
- MySQL notes 5_ Operation data
- npm ERR code 500解决
- C#新大陆物联网云平台的连接(简易理解版)
- Using stack to realize queue out and in
- Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
- MySQL笔记1_数据库
猜你喜欢
Cancel remote dependency and use local dependency
AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
PaddleOCR 图片文字提取
谷歌AdMob广告学习
Record WebView shows another empty pit
Ffmpeg common commands
取消远程依赖,用本地依赖
Encapsulate a set of project network request framework from 0
杂七杂八的学习
红外传感器控制开关
随机推荐
利用栈实现队列的出队入队
webView因证书问题显示一片空白
adb shell top 命令详解
oracle视图相关
机器学习 二:基于鸢尾花(iris)数据集的逻辑回归分类
第三篇:docker安装mysql容器(自定义端口)
launcher隐藏不需要显示的app icon
组件化学习(1)思想及实现方式
JS 比较2个数组中不同的元素
DCMTK(DCM4CHE)与DICOOGLE协同工作
winform滚动条美化
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide
机器学习 三: 基于逻辑回归的分类预测
Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
Encapsulate a set of project network request framework from 0
HandlerThread原理和实际应用
MySQL笔记3_约束_主键约束
BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView 滑动问题
What did you do during the internship
基于BottomNavigationView实现底部导航栏