关于Theano和Tensorflow多GPU使用问题


Posted in Python onJune 19, 2020

我使用的是tensorflow-gpu (1.2.1)和Theano (0.9.0),2个4G显存Nvidia Quadro M2000 GPU。

1. theano: ValueError: Could not infer context from inputs

THEANO_FLAGS="contexts=dev0->cuda0;dev1->cuda1,gpuarray.preallocate=0.95,mode=FAST_RUN,floatX=float32,on_unused_input=warn" python config.py

ERROR (theano.gof.opt): SeqOptimizer apply <theano.gpuarray.opt.GraphToGPU object at 0xdfe69210>
ERROR: SeqOptimizer apply <theano.gpuarray.opt.GraphToGPU object at 0xdfe69210>
ERROR (theano.gof.opt): Traceback:
ERROR: Traceback:
ERROR (theano.gof.opt): Traceback (most recent call last):
 File "/usr/lib/python2.7/site-packages/theano/gof/opt.py", line 235, in apply
  sub_prof = optimizer.optimize(fgraph)
 File "/usr/lib/python2.7/site-packages/theano/gof/opt.py", line 87, in optimize
  ret = self.apply(fgraph, *args, **kwargs)
 File "/usr/lib/python2.7/site-packages/theano/gpuarray/opt.py", line 322, in apply
  target = infer_context_name(*fgraph.inputs)
 File "/usr/lib/python2.7/site-packages/theano/gpuarray/basic_ops.py", line 122, in infer_context_name
  raise ValueError("Could not infer context from inputs")
ValueError: Could not infer context from inputs

theano不能自动支持多GPU,需要自己指定一个,只能在一个上面跑, 需要指定一个设备device=cuda0。

支持多GPU, 需要自己编程,参考http://deeplearning.net/software/theano/tutorial/using_multi_gpu.html#

2. tensorflow: ResourceExhaustedError: OOM when allocating tensor with

theano: MemoryError: Error allocating 1440000000 bytes of device memory (out of memory).

说明GPU内存不够,要调小输入或网络单元。

3. theano切换成新的GPU backend

WARNING (theano.sandbox.cuda): The cuda backend is deprecated and will be removed in the next release (v0.10)

theano 0.9.0从cuda backend切换gpuarray backend,需要安装python2-Cython-0.25+和libgpuarray-0.6.3+, 然后通过gpuarray.preallocate来指定。

补充知识:pytorch网络输入图片通道在前在后(channel_first和channel_last)的问题

刚开始学习pytorch卷积神经网络的时候,网络输入要求是(batch,3,32,32),我们如果想要测试自己电脑上的图片格式为(32,32,3)。即网络要求channel_first,本地图片是channel_last,此时我们只需要使用numpy中的np.transpose()函数调整下通道的顺序即可。

代码如下:

import numpy as np
import cv2
path = r"C:\Users\X_man\Desktop\image\cat.jpg"
image = cv2.imread(path,0)
image = cv2.resize(image,(32,32))
image = cv2.cvtColor(image,cv2.COLOR_GRAY2BGR)
print(image.shape)

(32,32,3)

image = np.transpose(image,(2,0,1))
print(image.shape)

(3,32,32)

以上这篇关于Theano和Tensorflow多GPU使用问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
pycharm 使用心得(七)一些实用功能介绍
Jun 06 Python
wxPython框架类和面板类的使用实例
Sep 28 Python
wxPython窗口的继承机制实例分析
Sep 28 Python
在cmd命令行里进入和退出Python程序的方法
May 12 Python
Python单元测试简单示例
Jul 03 Python
基于随机梯度下降的矩阵分解推荐算法(python)
Aug 31 Python
详解多线程Django程序耗尽数据库连接的问题
Oct 08 Python
python GUI库图形界面开发之PyQt5时间控件QTimer详细使用方法与实例
Feb 26 Python
Django bulk_create()、update()与数据库事务的效率对比分析
May 15 Python
Spring http服务远程调用实现过程解析
Jun 11 Python
Django中ORM的基本使用教程
Dec 22 Python
Python+SeaTable实现计算两个日期间的工作日天数
Jul 07 Python
如何对python的字典进行排序
Jun 19 #Python
浅谈Python中的继承
Jun 19 #Python
python程序需要编译吗
Jun 19 #Python
python中round函数如何使用
Jun 19 #Python
keras实现theano和tensorflow训练的模型相互转换
Jun 19 #Python
Keras 切换后端方式(Theano和TensorFlow)
Jun 19 #Python
python中怎么表示空值
Jun 19 #Python
You might like
php header()函数使用说明
2008/07/10 PHP
php操作XML、读取数据和写入数据的实现代码
2014/08/15 PHP
PHP实现获取FLV文件的时间
2015/02/10 PHP
PHP关联数组实现根据元素值删除元素的方法
2015/06/26 PHP
PHP下载文件的函数实例代码
2016/05/18 PHP
Laravel框架实现发送短信验证功能代码
2016/06/06 PHP
微信小程序 消息推送php服务器验证实例详解
2017/03/30 PHP
使用jQuery的ajax功能实现的RSS Reader 代码
2009/09/03 Javascript
文本框获得焦点和失去焦点的判断代码
2012/03/18 Javascript
(跨浏览器基础事件/浏览器检测/判断浏览器)经验代码分享
2013/01/24 Javascript
JS+DIV+CSS实现仿表单下拉列表效果
2015/08/18 Javascript
JS+CSS实现六级网站导航主菜单效果
2015/09/28 Javascript
详解JavaScript基于面向对象之继承实例
2015/12/16 Javascript
Python常用随机数与随机字符串方法实例
2015/04/09 Python
python实现mysql的单引号字符串过滤方法
2015/11/14 Python
python动态加载包的方法小结
2016/04/18 Python
详解Python多线程
2016/11/14 Python
Python中input与raw_input 之间的比较
2017/08/20 Python
python re模块findall()函数实例解析
2018/01/19 Python
Python数据分析库pandas基本操作方法
2018/04/08 Python
tensorflow使用神经网络实现mnist分类
2018/09/08 Python
Python逐行读取文件中内容的简单方法
2019/02/26 Python
python pyinstaller打包exe报错的解决方法
2019/11/02 Python
Python3安装模块报错Microsoft Visual C++ 14.0 is required的解决方法
2020/07/28 Python
Selenium 配置启动项参数的方法
2020/12/04 Python
CSS3 优势以及网页设计师如何使用CSS3技术
2009/07/29 HTML / CSS
基于html5 canvas做批改作业的小插件
2020/05/20 HTML / CSS
加拿大领先的优质厨具产品在线购物网站:Golda’s Kitchen
2017/11/17 全球购物
铭万公司.net面试题笔试题
2014/07/20 面试题
简历上的自我评价
2014/02/03 职场文书
百年校庆节目主持词
2014/03/27 职场文书
志愿者活动总结
2014/04/28 职场文书
校园文化艺术节宣传标语
2014/10/09 职场文书
幼儿园园长工作总结2015
2015/05/25 职场文书
2016年精神文明建设先进个人事迹材料
2016/02/29 职场文书
学者《孟子》名人名言
2019/08/09 职场文书