关于类型为numpy,TensorFlow.tensor,torch.tensor的shape变化以及相互转化
https://blog.csdn.net/zz2230633069/article/details/82669546
1.numpy类型:numpy.ndarray 对于图片读取之后(H,W,C)或者(batch,H,W,C)
(1)在元素总数不变的情况下:numpy类型的可以直接使用方法numpy.reshape任意改变大小,numpy.expand_dims增加维度,大小是1(这个函数可以参考numpy.expand_dims的用法)
(2)元素总数可以变化:scipy.misc.imresize(a,size)
2.TensorFlow的类型:tensorflow.python.framework.ops.tensor 图片的计算格式(H,W,C)或者(batch,H,W,C)
(1)在元素总数不变的情况下:numpy可以直接作为Tensor的输入,一旦被放在tf的函数下则失去了numpy的使用方法。tf.expand_dims在指定维度增加1维,大小为1;tf.squeeze刚好相反,删掉维度为1的轴(这两个函数可以参考tf.expand_dims和tf.squeeze函数);
(2)元素总数可以变化:
- '''
- tf和numpy之间的转化
- '''
- import tensorflow as tf
- a= tf.zeros((3,2))
- sess=tf.Session()
- sess.run(tf.global_variables_initializer())
- print("type(a)=",type(a)) # type(a)= <class 'tensorflow.python.framework.ops.Tensor'>
- #转化为numpy数组
- a_np=a.eval(session=sess)
- print("type(a_np)=",type(a_np)) # type(a_np)= <class 'numpy.ndarray'>
- #转化为tensor
- a2= tf.convert_to_tensor(a_np)
- print("type(a2)=",type(a2)) # type(a2)= <class 'tensorflow.python.framework.ops.Tensor'>
3.torch类型:torch.tensor 图片的计算格式是(C,H,W)或者(batch,C,H,W)
numpy类型不能直接作为Tensor的输入,所以在运用torch之前一定要进行转化。
- from PIL import Image
- import torch
- import numpy as np
- import matplotlib.pyplot as plt
- a=Image.open('/home/zzp/um_lane_000000.png') # 加载图片数据,返回的是一个PIL类型
- b=np.array(a).astype(np.float32) # 先将PIL类型转化成numpy类型,并且把数据变成浮点数
- c=b.transpose((2,0,1)) # 调整成torch的通道
- d=torch.from_numpy(c).float() # 再将numpy类型转化成torch.tensor类型
- # 或者另外一种加载图片的方式
- import scipy.misc
- import torch
- import numpy as np
- a=scipy.misc.imread('/home/zzp/um_lane_000000.png') # 加载图片数据,返回的是一个numpy类型
- c=a.transpose((2,0,1)).astype(np.float32) # 直接调整成torch的通道,不需要转化成numpy类型了,还是要变为浮点数
- d=torch.from_numpy(c).float() # 再将numpy类型转化成torch.tensor类型
- # 三种加载图像的方法
- a=Image.open('/home/zzp/um_lane_000000.png')
- b=scipy.misc.imread('/home/zzp/um_lane_000000.png')
- c=plt.imread('/home/zzp/um_lane_000000.png')
- #显示
(1)在元素总数不变的情况下
(2)元素总数可以变化
关于类型为numpy,TensorFlow.tensor,torch.tensor的shape变化以及相互转化的更多相关文章
- torch.tensor(),torch.Tensor()
Pytorch tensor操作 https://www.cnblogs.com/jeshy/p/11366269.html 我们需要明确一下,torch.Tensor()是python类,更明 ...
- Python中 list, numpy.array, torch.Tensor 格式相互转化
1.1 list 转 numpy ndarray = np.array(list) 1.2 numpy 转 list list = ndarray.tolist() 2.1 list 转 torch. ...
- torch.Tensor和numpy.ndarray
1. torch.Tensor和numpy.ndarray相互转换 import torch import numpy as np # <class 'numpy.ndarray'> np ...
- PyTorch官方中文文档:torch.Tensor
torch.Tensor torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU te ...
- 解决Tensorflow ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)
问题描述 在将一个数组送入tensorflow训练时,报错如下: ValueError: Failed to convert a NumPy array to a Tensor (Unsupporte ...
- TensorFlow使用基础-Tensor
使用 TensorFlow 之前你需要了解关于 TensorFlow 的以下基础知识 :• 使用图 (graphs) 来表示计算 .• 在会话 ( Session ) 中执行图 .• 使用张量 (te ...
- torch Tensor学习:切片操作
torch Tensor学习:切片操作 torch Tensor Slice 一直使用的是matlab处理矩阵,想从matlab转到lua+torch上,然而在matrix处理上遇到了好多类型不匹配问 ...
- torch.Tensor文档学习笔记
A torch.Tensor is a multi-dimensional matrix containing elements of a single data type. 张量(torch.Ten ...
- [深度学习] Pytorch学习(一)—— torch tensor
[深度学习] Pytorch学习(一)-- torch tensor 学习笔记 . 记录 分享 . 学习的代码环境:python3.6 torch1.3 vscode+jupyter扩展 #%% im ...
随机推荐
- python中的一些算法
两个基础知识点:递归和时间复杂度 递归 递归函数的特点:自己调用自己,有结束条件,看下面例子: def fun1(x): """无结束条件,报错""& ...
- python 中requests的返回数可直接使用json
对Python的request不是很了解,在使用时才发现,可以把request的请求结果,直接使用.json()['字段名']方法进行一个取值,案例如下 def test_tiantian(self) ...
- Python的dict字典结构操作方法学习笔记
Python的dict字典结构操作方法学习笔记 这篇文章主要介绍了Python的dict字典结构操作方法学习笔记本,字典的操作是Python入门学习中的基础知识,需要的朋友可以参考下 一.字典的基本方 ...
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- springboot集成mybatisplus小例子
集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用mapper的xml文件的. 目录 pom.xml文件 <?xml version="1.0&q ...
- jenkins自动打tag
思路: 1.手动输入需要tag的版本号,如“build001”,填写svn有权限的密码(账号默认值),填写打tag的说明 2.脚本根据tag的版本号,自动创建目录(版本号为目录名称) 3.将需要打ta ...
- python学习-38迭代器和生成器
迭代器和生成器 ---- 迭代器协议和for循环工作机制 1.迭代器协议:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么引起一个Stoplteration异常,以终止迭代(只能往 ...
- 启动mysql服务器
介绍了启动服务器的两种方式,以及可能遇到的问题 第一种:系统服务 1)可以通过右击方式一步步找到服务 右击计算机->选择管理->找到服务,然后双击打开,找到mysql,我安装的是mysql ...