tf-gpu(檢測、np.dtype([(「qint8「, np.int8, 1)]) 、 not compiled to use: AVX2、CUDNN_STATUS_ALLOC_FAILED)

2020-10-17 14:02:16

最近換了新電腦(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))