我从Tensorflow获取了CNN示例,当我运行prediction.eval()
时,GPU耗尽了内存。
此处显示跟踪信息
ResourceExhaustedErrorTraceback (most recent call last)
<ipython-input-23-1bdd4afd1d9a> in <module>()
----> 1 test_error, confusions = error_rate(test_prediction.eval(), test_labels)
2 print('Test error: %.1f%%' % test_error)
3
4 plt.xlabel('Actual')
5 plt.ylabel('Predicted')
/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in eval(self, feed_dict, session)
573
574 """
--> 575 return _eval_using_default_session(self, feed_dict, self.graph, session)
576
577
/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in _eval_using_default_session(tensors, feed_dict, graph, session)
3771 "the tensor's graph is different from the session's "
3772 "graph.")
-> 3773 return session.run(tensors, feed_dict)
3774
3775
/usr/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
764 try:
765 result = self._run(None, fetches, feed_dict, options_ptr,
--> 766 run_metadata_ptr)
767 if run_metadata:
768 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
962 if final_fetches or final_targets:
963 results = self._do_run(handle, final_targets, final_fetches,
--> 964 feed_dict_string, options, run_metadata)
965 else:
966 results = []
/usr/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1012 if handle is None:
1013 return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1014 target_list, options, run_metadata)
1015 else:
1016 return self._do_call(_prun_fn, self._session, handle, feed_dict,
/usr/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
1032 except KeyError:
1033 pass
-> 1034 raise type(e)(node_def, op, message)
1035
1036 def _extend_graph(self):
ResourceExhaustedError: OOM when allocating tensor with shape[10000,32,28,28]
[[Node: Conv2D_4 = Conv2D[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/gpu:0"](Const_1, Variable/read)]]
Caused by op u'Conv2D_4', defined at:
File "/usr/lib64/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/lib/python2.7/site-packages/ipykernel/__main__.py", line 3, in <module>
app.launch_new_instance()
File "/usr/lib/python2.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/usr/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 474, in start
ioloop.IOLoop.instance().start()
File "/usr/lib64/python2.7/site-packages/zmq/eventloop/ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "/usr/lib64/python2.7/site-packages/tornado/ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "/usr/lib64/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/usr/lib64/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/usr/lib64/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher
return self.dispatch_shell(stream, msg)
File "/usr/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
handler(stream, idents, msg)
File "/usr/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 390, in execute_request
user_expressions, allow_stdin)
File "/usr/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/lib/python2.7/site-packages/ipykernel/zmqshell.py", line 501, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2821, in run_ast_nodes
if self.run_code(code, result):
File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-14-5c825de8dd77>", line 31, in <module>
test_prediction = tf.nn.softmax(model(test_data_node))
File "<ipython-input-13-ddd4bdc4d43d>", line 9, in model
padding='SAME')
File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 396, in conv2d
data_format=data_format, name=name)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
op_def=op_def)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2371, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1258, in __init__
self._traceback = _extract_stack()
ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[10000,32,28,28]
[[Node: Conv2D_4 = Conv2D[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/gpu:0"](Const_1, Variable/read)]]
在预测/测试阶段之前,GPU的内存使用率为92%,因此,在预测阶段,运行prediction.eval()
的内存不多。
如何释放GPU的内存
答案 0 :(得分:4)
我认为这不仅仅是nvidia-smi的问题。我对tensorflow 1.0.0和我的GTX 1060也有类似的问题。
2016年6月发出的git问题(https://github.com/tensorflow/tensorflow/issues/1727)表明存在以下问题:
目前GPUDevice中的Allocator属于ProcessState, 这本质上是一个全球性的单身人士。第一个会话使用GPU 初始化它,并在进程关闭时释放自己。
并且表明tensorflow团队缺乏短期资源来处理这个问题。到目前为止,我没有找到任何证据证明这已得到解决。
答案 1 :(得分:0)
您确定可以将所有10,000张测试版图像放入2GB GPU中吗?
我在4GB GPU上遇到了同样的问题,最终我改变了代码,以便在1000个图像的批量测试集上进行评估。我实际上并没有寻找最大值,我只选择了1.000。我的更改概述如下。
首先,定义评估批量大小:
# Evaluation batch size (for simplicity, make it a divisor of 10.000)
EVAL_BATCH_SIZE = 1000
然后,将test_data_node
定义从tf.constant
更改为tf.placeholder
:
test_data_node = tf.placeholder(tf.float32, shape=(EVAL_BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))
定义第二个错误率函数,在计算所有批次的所有预测时调用第一个错误率函数:
def error_rate_batch(predictions_node, data, labels):
"""Run computation node in batches"""
predictions = None
for batch in range(data.shape[0] // EVAL_BATCH_SIZE):
feed_dict = {test_data_node: data[batch * EVAL_BATCH_SIZE : (batch + 1) * EVAL_BATCH_SIZE, :, :, :]}
if predictions is None:
predictions = s.run(predictions_node, feed_dict=feed_dict)
else:
predictions = numpy.concatenate((predictions, s.run(predictions_node, feed_dict=feed_dict)), axis=0)
return error_rate(predictions, labels)
最后,评估测试数据如下:
test_error, confusions = error_rate_batch(test_prediction, test_data, test_labels)
希望我没有忘记一些事情。