当前位置:网站首页>Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
2022-04-23 07:17:00 【Breeze_】
Catalog
0. Guide pack
import torch
import numpy as np
import PIL
1.tensor Type conversion
# Set up Torch Default type
torch.set_default_tensor_type(torch.FloatTensor)
# CPU GPU # data type
# torch.FloatTensor torch.cuda.FloatTensor # 32 Bit floating point
# torch.DoubleTensor torch.cuda.DoubleTensor # 64 Bit floating point
# torch.cuda.HalfTensor # 16 Bit floating point
# Type conversion
tensor = torch.rand(1,3,224,224)
tensor = tensor.float() # To float type , other :half() int() double() char() byte() short() long()
tensor = tensor.cuda() # To GPU object
tensor = tensor.cpu() # To CPU object
# type_as() Method
tensor1 = torch.rand(2,3)
tensor2 = torch.IntTensor(2,3)
tensor3 = tensor1.type_as(tensor2) # take tensor1 To tensor2 The type of
# print(tensor1,tensor2,tensor3)
2.tensor and list transformation
'''tensor and list transformation '''
tensor1 = torch.ones([1,5]) # Create a 1x5 Unit tensor
list1 = tensor1.tolist() # tensor -> list
tensor1 = torch.tensor(list1) # list -> tensor
3.tensor and np.ndarray transformation
'''tensor and np.ndarray transformation '''
ndarray = tensor.numpy() # Be careful GPU The object must be converted to CPU object
tensor = torch.from_numpy(ndarray) # Note that it is generally converted to floating-point objects
4. Images tensor And images ndarray transformation ,tensor -> cv2
''' Images tensor And images ndarray transformation ,tensor -> cv2'''
# tensor The image is dimensional, and the information is [N, C, H, W]
ndarray = np.random.random((3,224,224))
tensor = torch.tensor(ndarray).permute(2,0,1) #ndarray turn tensor Note that the dimension of the image is HWC->CHW
img = tensor.permute(1,2,0).cpu().numpy() # On the contrary, the same is true
5.cv2(ndarray) And PIL Conversion between
'''PIL.Image and np.ndarray transformation , But cv2 And PIL Conversion between '''
path = '1.jpg' # Pass in your own image address here
ndarray = np.asarray(PIL.Image.open(path)) #Image turn ndarray
img = PIL.Image.fromarray(ndarray.astype(np.uint8)) #ndarray turn Image
版权声明
本文为[Breeze_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610322978.html
边栏推荐
猜你喜欢
C#新大陆物联网云平台的连接(简易理解版)
Easyui combobox 判断输入项是否存在于下拉列表中
Bottomsheetdialogfragment conflicts with listview recyclerview Scrollview sliding
记录webView显示空白的又一坑
第2章 Pytorch基础2
C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported
AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
机器学习 二:基于鸢尾花(iris)数据集的逻辑回归分类
[2021 book recommendation] artistic intelligence for IOT Cookbook
给女朋友写个微信双开小工具
随机推荐
AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
Reading notes - activity
Component learning (2) arouter principle learning
MySQL notes 1_ database
C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported
HandlerThread原理和实际应用
树莓派:双色LED灯实验
DCMTK(DCM4CHE)与DICOOGLE协同工作
PyTorch训练一个网络的基本流程5步法
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
利用官方torch版GCN训练并测试cora数据集
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
WebRTC ICE candidate里面的raddr和rport表示什么?
Project, how to package
谷歌AdMob广告学习
【2021年新书推荐】Artificial Intelligence for IoT Cookbook
Recyclerview 批量更新View:notifyItemRangeInserted、notifyItemRangeRemoved、notifyItemRangeChanged
【动态规划】三角形最小路径和
常见的正则表达式