Theano Tensorflow多GPU 关于Theano和Tensorflow多GPU使用问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Theano Tensorflow多GPU 关于Theano和Tensorflow多GPU使用问题

Gamma Gao   2021-04-22 我要评论

我使用的是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使用问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们