东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 3766|回复: 1
打印 上一主题 下一主题

[课堂笔记] Failed to get convolution algorithm解决:keras中model.predict都报错:...

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14431
QQ
跳转到指定楼层
楼主
发表于 2020-2-21 12:27:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
解决: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



  1. # python 3.6 and tensorflow (both 1.x and 2.0)
  2. def allow_gpu_memory_growth(log_device_placement=True):
  3.     """
  4.     Allow dynamic memory growth (by default, tensorflow allocates all gpu memory).
  5.     This sometimes fixes the
  6.     <<Error : Failed to get convolution algorithm.
  7.     This is probably because cuDNN failed to initialize,
  8.     so try looking to see if a warning log message was printed above>>.
  9.     May hurt performance slightly (see https://www.tensorflow.org/guide/gpu).

  10.     Usage: Run before any other code.

  11.     :param log_device_placement: set True to log device placement (on which device the operation ran)
  12.     :return:None
  13.     """
  14.     # set_session 标红不用管 Sets the global TensorFlow session
  15.     from tensorflow.compat.v1.keras.backend import set_session
  16.     import tensorflow as tf
  17.     config = tf.compat.v1.ConfigProto()
  18.     config.gpu_options.allow_growth = True  # dynamically grow the memory used on the GPU
  19.     config.log_device_placement = log_device_placement
  20.     sess = tf.compat.v1.Session(config=config)
  21.     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的

这次要改成这样的:
  1. import os
  2. import tensorflow as tf
  3. import keras
  4. os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
  5. # 选择哪一块gpu,如果是-1,就是调用cpu
  6. os.environ['CUDA_VISIBLE_DEVICES'] = "0"


  7. # python 3.6 and tensorflow (both 1.x and 2.0)
  8. def allow_gpu_memory_growth(log_device_placement=True):
  9.     """
  10.     Allow dynamic memory growth (by default, tensorflow allocates all gpu memory).
  11.     This sometimes fixes the
  12.     <<Error : Failed to get convolution algorithm.
  13.     This is probably because cuDNN failed to initialize,
  14.     so try looking to see if a warning log message was printed above>>.
  15.     May hurt performance slightly (see https://www.tensorflow.org/guide/gpu).

  16.     Usage: Run before any other code.

  17.     :param log_device_placement: set True to log device placement (on which device the operation ran)
  18.     :return:None
  19.     """
  20.     # set_session 标红不用管 Sets the global TensorFlow session
  21.     # from tensorflow.compat.v1.keras.backend import set_session
  22.     # from tensorflow.python.keras.backend import set_session
  23.     config = tf.compat.v1.ConfigProto()
  24.     config.gpu_options.allow_growth = True  # dynamically grow the memory used on the GPU
  25.     config.log_device_placement = log_device_placement
  26.     sess = tf.compat.v1.Session(config=config)
  27.     # 一定要拿到全局的session来配置 因为模型预测会用到keras.backend.get_session()
  28.     keras.backend.set_session(session=sess)


  29. allow_gpu_memory_growth()

  30. # 函数allow_gpu_memory_growth()应该放在这句的前面啊
  31. from hyperlpr_py3 import pipline

  32. # 导入车牌识别库


  33. app = Flask(__name__)
复制代码











让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14431
QQ
沙发
 楼主| 发表于 2020-5-28 10:33:38 | 只看该作者

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()应该放在这句的前面啊
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-24 13:53 , Processed in 0.174940 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表