https://blog.csdn.net/zz2230633069/article/details/82669546

2018年09月12日 22:56:50 一只tobey 阅读数:727
 

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)元素总数可以变化:

  1. '''
  2. tf和numpy之间的转化
  3. '''
  4. import tensorflow as tf
  5.  
  6. a= tf.zeros((3,2))
  7. sess=tf.Session()
  8. sess.run(tf.global_variables_initializer())
  9.  
  10. print("type(a)=",type(a)) # type(a)= <class 'tensorflow.python.framework.ops.Tensor'>
  11.  
  12. #转化为numpy数组
  13. a_np=a.eval(session=sess)
  14. print("type(a_np)=",type(a_np)) # type(a_np)= <class 'numpy.ndarray'>
  15. #转化为tensor
  16. a2= tf.convert_to_tensor(a_np)
  17. print("type(a2)=",type(a2)) # type(a2)= <class 'tensorflow.python.framework.ops.Tensor'>
  18.  
  19.  

3.torch类型:torch.tensor  图片的计算格式是(C,H,W)或者(batch,C,H,W)

numpy类型不能直接作为Tensor的输入,所以在运用torch之前一定要进行转化。

  1. from PIL import Image
  2. import torch
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. a=Image.open('/home/zzp/um_lane_000000.png') # 加载图片数据,返回的是一个PIL类型
  6. b=np.array(a).astype(np.float32) # 先将PIL类型转化成numpy类型,并且把数据变成浮点数
  7. c=b.transpose((2,0,1)) # 调整成torch的通道
  8. d=torch.from_numpy(c).float() # 再将numpy类型转化成torch.tensor类型
  9.  
  10. # 或者另外一种加载图片的方式
  11. import scipy.misc
  12. import torch
  13. import numpy as np
  14. a=scipy.misc.imread('/home/zzp/um_lane_000000.png') # 加载图片数据,返回的是一个numpy类型
  15. c=a.transpose((2,0,1)).astype(np.float32) # 直接调整成torch的通道,不需要转化成numpy类型了,还是要变为浮点数
  16. d=torch.from_numpy(c).float() # 再将numpy类型转化成torch.tensor类型
  17.  
  18. # 三种加载图像的方法
  19. a=Image.open('/home/zzp/um_lane_000000.png')
  20. b=scipy.misc.imread('/home/zzp/um_lane_000000.png')
  21. c=plt.imread('/home/zzp/um_lane_000000.png')
  22. #显示

(1)在元素总数不变的情况下

(2)元素总数可以变化

关于类型为numpy,TensorFlow.tensor,torch.tensor的shape变化以及相互转化的更多相关文章

  1. torch.tensor(),torch.Tensor()

    Pytorch tensor操作 https://www.cnblogs.com/jeshy/p/11366269.html    我们需要明确一下,torch.Tensor()是python类,更明 ...

  2. 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. ...

  3. torch.Tensor和numpy.ndarray

    1. torch.Tensor和numpy.ndarray相互转换 import torch import numpy as np # <class 'numpy.ndarray'> np ...

  4. PyTorch官方中文文档:torch.Tensor

    torch.Tensor torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU te ...

  5. 解决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 ...

  6. TensorFlow使用基础-Tensor

    使用 TensorFlow 之前你需要了解关于 TensorFlow 的以下基础知识 :• 使用图 (graphs) 来表示计算 .• 在会话 ( Session ) 中执行图 .• 使用张量 (te ...

  7. torch Tensor学习:切片操作

    torch Tensor学习:切片操作 torch Tensor Slice 一直使用的是matlab处理矩阵,想从matlab转到lua+torch上,然而在matrix处理上遇到了好多类型不匹配问 ...

  8. torch.Tensor文档学习笔记

    A torch.Tensor is a multi-dimensional matrix containing elements of a single data type. 张量(torch.Ten ...

  9. [深度学习] Pytorch学习(一)—— torch tensor

    [深度学习] Pytorch学习(一)-- torch tensor 学习笔记 . 记录 分享 . 学习的代码环境:python3.6 torch1.3 vscode+jupyter扩展 #%% im ...

随机推荐

  1. Global.asax.cs 为 /.aspx 执行子请求时出错。 Server.Transfer

    x 后台代码 Global.asax.cs protected void Application_Error(object sender, EventArgs e){Server.Transfer(& ...

  2. [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 ...

  3. [LeetCode] 771. Jewels and Stones 珠宝和石头

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  4. Redis 主从、哨兵Sentinel、Jedis

    Redis 主从.哨兵Sentinel.Jedis 2017年02月15日 15:52:48 有且仅有 阅读数 6183 文章标签: redis主从sentineljedis 更多 分类专栏: 7/1 ...

  5. python:pytest中的setup和teardown

    原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...

  6. 第07组 Beta冲刺(1/4)

    队名:秃头小队 组长博客 作业博客 组长徐俊杰 过去两天完成的任务:学习了很多东西 Github签入记录 接下来的计划:继续学习 还剩下哪些任务:后端部分 燃尽图 遇到的困难:自己太菜了 收获和疑问: ...

  7. kmeans 对表达量进行聚类

    代码如下 df = pd.read_csv("../kmeans/gene.fpkm.csv",header=None) print df.head() #去掉第一行 tdf = ...

  8. Git的各种工作流

    Git工作流可以理解为团队成员遵守的一种代码管理方案,在Git中有以下几种常见工作流: 集中式工作流 功能开发工作流 Gitflow工作流 Forking工作流 1)集中式工作流 这种工作方式跟svn ...

  9. IDEA 获取类的相对路径和绝对路径

    1.相对路径: 结果:action.HelloAction 2.绝对路径 结果:E:\javaProject\JavaEEProject\day08_Struts2_test01\src\action ...

  10. 数据分析-numpy的用法

    一.jupyter notebook 两种安装和启动的方式: 第一种方式: 命令行安装:pip install jupyter 启动:cmd 中输入 jupyter notebook 缺点:必须手动去 ...