|
解决:keras中model.predict都报错:显存不足
Failed to get convolution algorithm.
This is probably because cuDNN failed to initialize,
解释:允许显存动态增长,默认情况下tf和keras会分配所有的显存
如果有多块显卡:还需指定:
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
# 选择哪一块gpu,如果是-1,就是调用cpu
os.environ['CUDA_VISIBLE_DEVICES'] = "0,1"
有个细节要注意:函数allow_gpu_memory_growth()应该放在模型构建build_model所在的文件最前面哦!
用这个就不标红了:
from tensorflow.python.keras.backend import set_session
- # python 3.6 and tensorflow (both 1.x and 2.0)
- def allow_gpu_memory_growth(log_device_placement=True):
- """
- Allow dynamic memory growth (by default, tensorflow allocates all gpu memory).
- This sometimes fixes the
- <<Error : 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>>.
- May hurt performance slightly (see https://www.tensorflow.org/guide/gpu).
- Usage: Run before any other code.
- :param log_device_placement: set True to log device placement (on which device the operation ran)
- :return:None
- """
- # set_session 标红不用管 Sets the global TensorFlow session
- from tensorflow.compat.v1.keras.backend import set_session
- import tensorflow as tf
- config = tf.compat.v1.ConfigProto()
- config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU
- config.log_device_placement = log_device_placement
- sess = tf.compat.v1.Session(config=config)
- set_session(sess)
复制代码
遇到这样一个问题:
直接用pycharm启动服务 并使用 没问题
但是用终端启动服务,则报错:显存不够 显存明显是够的
报错:Failed to get convolution algorithm. This is probably because cuDNN failed to initialize,
问题一定在:两者启动的环境配置不一样竟然解决了:我看到一个提示说 cudnn一个是7.4.0 一个是7.6.0 说什么不一致 发现我本机安装的是7.4.0
然后我升级为7.6.0 就成功啦! 当然还得加allow_gpu_memory_growth()这个函数 cuda的版本是10.0的
这次要改成这样的:
- import os
- import tensorflow as tf
- import keras
- os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
- # 选择哪一块gpu,如果是-1,就是调用cpu
- os.environ['CUDA_VISIBLE_DEVICES'] = "0"
- # python 3.6 and tensorflow (both 1.x and 2.0)
- def allow_gpu_memory_growth(log_device_placement=True):
- """
- Allow dynamic memory growth (by default, tensorflow allocates all gpu memory).
- This sometimes fixes the
- <<Error : 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>>.
- May hurt performance slightly (see https://www.tensorflow.org/guide/gpu).
- Usage: Run before any other code.
- :param log_device_placement: set True to log device placement (on which device the operation ran)
- :return:None
- """
- # set_session 标红不用管 Sets the global TensorFlow session
- # from tensorflow.compat.v1.keras.backend import set_session
- # from tensorflow.python.keras.backend import set_session
- config = tf.compat.v1.ConfigProto()
- config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU
- config.log_device_placement = log_device_placement
- sess = tf.compat.v1.Session(config=config)
- # 一定要拿到全局的session来配置 因为模型预测会用到keras.backend.get_session()
- keras.backend.set_session(session=sess)
- allow_gpu_memory_growth()
- # 函数allow_gpu_memory_growth()应该放在这句的前面啊
- from hyperlpr_py3 import pipline
- # 导入车牌识别库
- app = Flask(__name__)
复制代码
|
|