python

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

最近換了新電腦(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)是否能用程式碼如下:
  1. import tensorflow as tf
  2. import os
  3. os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
  4. print(tf.__version__)
  5. a = tf.constant(1.)
  6. b = tf.constant(2.)
  7. c = tf.constant([1.0,2.0])
  8. d = tf.constant([2.0,3.0])
  9. r = a + b
  10. m = c + d
  11. sess=tf.InteractiveSession()
  12. print(r)
  13. print(r.eval())
  14. print(m)
  15. print(m.eval())
  16. print('GPU:', tf.test.is_gpu_available())
  17. sess.close()

正確結果如下:

 問題:“import tensorflow as tf”後出現如下錯誤

  1. Using TensorFlow backend.
  2. F:\anaconda\envs\Face\lib\site-packages\tensorflow\\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'.
  3. _np_qint8 = np.dtype([("qint8", np.int8, 1)])
  4. 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'.
  5. _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
  6. 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'.
  7. _np_qint16 = np.dtype([("qint16", np.int16, 1)])
  8. 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'.
  9. _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
  10. 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'.
  11. _np_qint32 = np.dtype([("qint32", np.int32, 1)])
  12. 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'.
  13. np_resource = np.dtype([("resource", np.ubyte, 1)])
  14. 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'.
  15. _np_qint8 = np.dtype([("qint8", np.int8, 1)])
  16. 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'.
  17. _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
  18. 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'.
  19. _np_qint16 = np.dtype([("qint16", np.int16, 1)])
  20. 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'.
  21. _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
  22. 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'.
  23. _np_qint32 = np.dtype([("qint32", np.int32, 1)])
  24. 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'.
  25. np_resource = np.dtype([("resource", np.ubyte, 1)])

解決:
很簡單,按照提示找到對應檔案,開啟以後進行修改(按照提示修改即可),也就是:將“_np_qint8 = np.dtype([(“qint8”, np.int8, 1)])”修改為“_np_qint8 = np.dtype([(“qint8”, np.int8, (1,))])”,修改的時候要認真,確保所有的都進行了修改。
改為

  1. _np_qint8 = np.dtype([("qint8", np.int8, (1,))])
  2. _np_quint8 = np.dtype([("quint8", np.uint8, (1,))])
  3. _np_qint16 = np.dtype([("qint16", np.int16, (1,))])
  4. _np_quint16 = np.dtype([("quint16", np.uint16, (1,))])
  5. _np_qint32 = np.dtype([("qint32", np.int32, (1,))])
  • 問題
    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.
    2. Instructions for updating:
    3. Colocations handled automatically by placer.
    4. 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
    5. 2020-10-15 15:44:20.376939: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties:
    6. name: GeForce RTX 2060 major: 7 minor: 5 memoryClockRate(GHz): 1.2
    7. pciBusID: 0000:01:00.0
    8. totalMemory: 6.00GiB freeMemory: 5.02GiB
    9. 2020-10-15 15:44:20.377158: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
    10. 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:
    11. 2020-10-15 15:44:20.730160: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0
    12. 2020-10-15 15:44:20.730226: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0: N

    解決:新增程式碼指定gpu(有人說這是暫時解決方案,還有一種徹底解決方案,比較麻煩,沒有嘗試,以後必要時再試)

    1. import os
    2. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  • 問題

  1. Using TensorFlow backend.
  2. 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.
  3. Instructions for updating:
  4. Colocations handled automatically by placer.
  5. 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
  6. 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,親測有效)

  1. from keras.backend.tensorflow_backend import set_session
  2. import tensorflow as tf
  3. config = tf.ConfigProto()
  4. config.gpu_options.allow_growth = True
  5. set_session(tf.Session(config=config))

如果只是tf,以下程式碼

  1. from tensorflow import ConfigProto
  2. from tensorflow import InteractiveSession
  3. config = ConfigProto()
  4. config.gpu_options.allow_growth = True
  5. session = InteractiveSession(config=config)

如果是注意如果是tensorflow2,用以下程式碼:

  1. from tensorflow.compat.v1 import ConfigProto
  2. from tensorflow.compat.v1 import InteractiveSession
  3. config = ConfigProto()
  4. config.gpu_options.allow_growth = True
  5. 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問題新增如下程式碼
 

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

 

本文章已修改原文用詞符合繁體字使用者習慣使其容易閱讀

版權宣告:此處為CSDN博主「dujuancao11」的原創文章,依據CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。

原文連結:https://blog.csdn.net/dujuancao11/article/details/109111198