tf-gpu(检测、np.dtype([(“qint8“, np.int8, 1)]) 、 not compiled to use: AVX2、CUDNN_STATUS_ALLOC_FAILED)

时间:2020-10-17 作者:admin

最近换了新电脑(RTX2060),重新安装tensorflow-gpu(1.13.1)遇到了一些问题,解决如下。

  1. 检测tensorflow-gpu(1.13.1)是否能用代码
  2. FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
      _np_qint8 = np.dtype([(“qint8”, np.int8, 1)])
  3. Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
  4. Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
  5. 查看显卡状态在CMD输入:nvidia-smi
  • 检测tensorflow-gpu(1.13.1)是否能用代码如下:
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
print(tf.__version__)
a = tf.constant(1.)
b = tf.constant(2.)
c = tf.constant([1.0,2.0])
d = tf.constant([2.0,3.0])
r = a + b
m = c + d
sess=tf.InteractiveSession()
print(r)
print(r.eval())
print(m)
print(m.eval())
print('GPU:', tf.test.is_gpu_available())
sess.close()

正确结果如下:

 问题:“import tensorflow as tf”后出现如下错误

Using TensorFlow backend.
F:\anaconda\envs\Face\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
F:\anaconda\envs\Face\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])

解决:
很简单,按照提示找到对应文件,打开以后进行修改(按照提示修改即可),也就是:将“_np_qint8 = np.dtype([(“qint8”, np.int8, 1)])”修改为“_np_qint8 = np.dtype([(“qint8”, np.int8, (1,))])”,修改的时候要认真,确保所有的都进行了修改。
改为

_np_qint8 = np.dtype([("qint8", np.int8, (1,))])
_np_quint8 = np.dtype([("quint8", np.uint8, (1,))])
_np_qint16 = np.dtype([("qint16", np.int16, (1,))])
_np_quint16 = np.dtype([("quint16", np.uint16, (1,))])
_np_qint32 = np.dtype([("qint32", np.int32, (1,))])
  • 问题
    WARNING:tensorflow:From F:\anaconda\envs\Face\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
    Instructions for updating:
    Colocations handled automatically by placer.
    2020-10-15 15:44:20.227474: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
    2020-10-15 15:44:20.376939: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties: 
    name: GeForce RTX 2060 major: 7 minor: 5 memoryClockRate(GHz): 1.2
    pciBusID: 0000:01:00.0
    totalMemory: 6.00GiB freeMemory: 5.02GiB
    2020-10-15 15:44:20.377158: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
    2020-10-15 15:44:20.730040: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
    2020-10-15 15:44:20.730160: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990]      0 
    2020-10-15 15:44:20.730226: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0:   N 

    解决:添加代码指定gpu(有人说这是暂时解决方案,还有一种彻底解决方案,比较麻烦,没有尝试,以后必要时再试)

    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  • 问题

Using TensorFlow backend.
WARNING:tensorflow:From F:\anaconda\envs\Face\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2020-10-15 16:41:10.805751: E tensorflow/stream_executor/cuda/cuda_dnn.cc:334] Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
2020-10-15 16:41:10.806304: E tensorflow/stream_executor/cuda/cuda_dnn.cc:334] Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED

 解决:添加如下代码(用到了keras,tf1.13.1,亲测有效)

from keras.backend.tensorflow_backend import set_session
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
set_session(tf.Session(config=config))

如果只是tf,以下代码

from tensorflow import ConfigProto
from tensorflow import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(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)

  • 查看显卡状态在CMD输入:nvidia-smi


     

启示:遇坑不可怕,耐心,搜索,尝试。 
学习:

  • tensorflow目前不支持python3.7, 需要用python3.6。可建立多个环境。例如,新建一个环境名:py36,进入aniconda prompt后(默认环境),用命令activate py36可以切换到另一个环境py36。

(虚拟环境可参考另一篇博文https://blog.csdn.net/dujuancao11/article/details/107468687?utm_source=app

  • Tf2.x版本需要vs环境来支持
  • Anaconda虚拟环境可以通过直接复制虚拟环境所在的文件夹来简单复制虚拟环境,省着重新pip
  • 原有py3.7安装3.6py虚拟环境,如果自己之前引入太多下载源,可能会不成功,在多个源之间选择而发生冲突。
  • 模型训练时发现训练缓慢,检查GPU是否开启,下面分别是异常和正常截图

小结:解决34问题添加如下代码
 

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
from keras.backend.tensorflow_backend import set_session
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
set_session(tf.Session(config=config))

 

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。