问题解决:Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR

这几天用一个tensorflow版的目标检测算法进行推理的时候出现了错误以下错误(取了一部分):

2020-03-15 22:00:40.933209: E tensorflow/stream_executor/cuda/cuda_dnn.cc:334] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
2020-03-15 22:00:40.952977: E tensorflow/stream_executor/cuda/cuda_dnn.cc:334] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
Traceback (most recent call last):
  File "/home/smy/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/home/smy/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/home/smy/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
	 [[{{node conv2d_0_1/convolution}}]]
	 [[{{node loc_branch_concat_1/concat}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tenforflow_infer.py", line 143, in <module>
    inference(img, show_result=True, target_shape=(260, 260))
  File "tenforflow_infer.py", line 53, in inference
    y_bboxes_output, y_cls_output = tf_inference(sess, graph, image_exp)
  File "/home/smy/FaceMaskDetection/load_model/tensorflow_loader.py", line 38, in tf_inference
    feed_dict={image_tensor: img_arr})
  File "/home/smy/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/home/smy/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "/home/smy/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1328, in _do_run
    run_metadata)
  File "/home/smy/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1348, in _do_call
……

解决方法是在代码前面加上以下代码:

from tensorflow import ConfigProto
from tensorflow import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

也可以这样添加,作用相同,但调用了keras,不推荐,当然,如果你用的正是keras以下代码比上面代码效率更高:

import os
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session

os.environ["CUDA_VISIBLE_DEVICES"] = "0"  # 这一句根据需要添加,作用是指定GPU

config = tf.ConfigProto()
config.gpu_options.allow_growth = True  
set_session(tf.Session(config=config))

注意如果是tensorflow2,用以下代码:

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

你可能感兴趣的:(TensorFlow,计算机视觉,Python,python,tensorflow,linux,深度学习)