LoginSignup
5
5

More than 3 years have passed since last update.

Windows Anaconda環境にTensorflow GPUをインストール

Last updated at Posted at 2019-07-18

初めに

今取り組んでいる予測モデルについて、機械学習手法でこれ以上精度を上げる方法が思いつかないため、DeepLearningを試してみようと思い、Tensorflow GPUをインストールしたメモ。(2019/7/22 手順変更のため追記)

環境

  • Windows10
  • Anaconda
  • Python 3.7 3.6(2019/7/22変更)
  • Tensorflow 1.13.1 1.5 (2019/7/22変更)

手順(まずはconda)

以下の通りcondaでインストール完了~。ただし、バージョンは少し古く1.13.1だ。
インストールは10分~20分程度かかった。

>conda install  tensorflow-gpu
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.5.12
  latest version: 4.7.5

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: C:\Users\dff40\AppData\Local\conda\conda\envs\nekoshi2

  added / updated specs:
    - tensorflow-gpu


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    markdown-3.1.1             |             py_0          60 KB  conda-forge
    absl-py-0.7.1              |           py37_0         154 KB  conda-forge
    _tflow_select-2.1.0        |              gpu           3 KB
    tensorflow-base-1.13.1     |gpu_py37h871c8ca_0       218.5 MB
    pyreadline-2.1             |        py37_1000         140 KB  conda-forge
    cudatoolkit-10.0.130       |                0       371.0 MB
    keras-preprocessing-1.0.9  |             py_1          33 KB  conda-forge
    tensorflow-1.13.1          |gpu_py37h83e5d6a_0           4 KB
    cudnn-7.6.0                |       cuda10.0_0       215.3 MB
    tensorboard-1.13.1         |           py37_0         3.3 MB  conda-forge
    gast-0.2.2                 |             py_0          10 KB  conda-forge
    termcolor-1.1.0            |             py_2           6 KB  conda-forge
    werkzeug-0.15.5            |             py_0         259 KB  conda-forge
    hdf5-1.10.4                |nompi_hcc15c50_1106        34.9 MB  conda-forge
    grpcio-1.16.1              |   py37h351948d_1         947 KB
    tensorflow-gpu-1.13.1      |       h0d30ee6_0           2 KB
    keras-applications-1.0.7   |             py_1          31 KB  conda-forge
    h5py-2.9.0                 |nompi_py37h3cb27cb_1102         905 KB  conda-forge
    protobuf-3.9.0             |   py37he025d50_0         576 KB  conda-forge
    libprotobuf-3.9.0          |       h1a1b453_0         2.2 MB  conda-forge
    certifi-2019.6.16          |           py37_1         149 KB  conda-forge
    astor-0.7.1                |             py_0          22 KB  conda-forge
    tensorflow-estimator-1.13.0|   py37h39e3cac_0         470 KB  conda-forge
    ------------------------------------------------------------
                                           Total:       848.9 MB

The following NEW packages will be INSTALLED:

    _tflow_select:        2.1.0-gpu
    absl-py:              0.7.1-py37_0                  conda-forge
    astor:                0.7.1-py_0                    conda-forge
    cudatoolkit:          10.0.130-0
    cudnn:                7.6.0-cuda10.0_0
    gast:                 0.2.2-py_0                    conda-forge
    grpcio:               1.16.1-py37h351948d_1
    h5py:                 2.9.0-nompi_py37h3cb27cb_1102 conda-forge
    hdf5:                 1.10.4-nompi_hcc15c50_1106    conda-forge
    keras-applications:   1.0.7-py_1                    conda-forge
    keras-preprocessing:  1.0.9-py_1                    conda-forge
    libprotobuf:          3.9.0-h1a1b453_0              conda-forge
    markdown:             3.1.1-py_0                    conda-forge
    protobuf:             3.9.0-py37he025d50_0          conda-forge
    pyreadline:           2.1-py37_1000                 conda-forge
    tensorboard:          1.13.1-py37_0                 conda-forge
    tensorflow:           1.13.1-gpu_py37h83e5d6a_0
    tensorflow-base:      1.13.1-gpu_py37h871c8ca_0
    tensorflow-estimator: 1.13.0-py37h39e3cac_0         conda-forge
    tensorflow-gpu:       1.13.1-h0d30ee6_0
    termcolor:            1.1.0-py_2                    conda-forge
    werkzeug:             0.15.5-py_0                   conda-forge

The following packages will be UPDATED:

    certifi:              2019.6.16-py37_0              conda-forge --> 2019.6.16-py37_1 conda-forge

Proceed ([y]/n)? y


Downloading and Extracting Packages
markdown-3.1.1       | 60 KB     | ######################################################### | 100%
absl-py-0.7.1        | 154 KB    | ######################################################### | 100%
_tflow_select-2.1.0  | 3 KB      | ######################################################### | 100%
tensorflow-base-1.13 | 218.5 MB  | ######################################################### | 100%
pyreadline-2.1       | 140 KB    | ######################################################################### | 100%
cudatoolkit-10.0.130 | 371.0 MB  | ######################################################################### | 100%
keras-preprocessing- | 33 KB     | ######################################################################### | 100%
tensorflow-1.13.1    | 4 KB      | ######################################################################### | 100%
cudnn-7.6.0          | 215.3 MB  | ######################################################################### | 100%
tensorboard-1.13.1   | 3.3 MB    | ######################################################################### | 100%
gast-0.2.2           | 10 KB     | ######################################################################### | 100%
termcolor-1.1.0      | 6 KB      | ######################################################################### | 100%
werkzeug-0.15.5      | 259 KB    | ######################################################################### | 100%
hdf5-1.10.4          | 34.9 MB   | ######################################################################### | 100%
grpcio-1.16.1        | 947 KB    | ######################################################################### | 100%
tensorflow-gpu-1.13. | 2 KB      | ######################################################################### | 100%
keras-applications-1 | 31 KB     | ######################################################################### | 100%
h5py-2.9.0           | 905 KB    | ######################################################################### | 100%
protobuf-3.9.0       | 576 KB    | ######################################################################### | 100%
libprotobuf-3.9.0    | 2.2 MB    | ######################################################################### | 100%
certifi-2019.6.16    | 149 KB    | ######################################################################### | 100%
astor-0.7.1          | 22 KB     | ######################################################################### | 100%
tensorflow-estimator | 470 KB    | ######################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

インストール後のチェック

続いてGPUが認識されているかチェック。参考サイトそのままです。助かります。

>>> from tensorflow.python.client import device_lib
>>> device_lib.list_local_devices()
2019-07-19 00:02:20.502892: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-07-19 00:02:20.940061: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties: 
name: GeForce GTX 1060 6GB major: 6 minor: 1 memoryClockRate(GHz): 1.7085
pciBusID: 0000:01:00.0
totalMemory: 6.00GiB freeMemory: 4.97GiB
2019-07-19 00:02:20.946181: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\kimisyo\AppData\Local\conda\conda\envs\chemsyo\lib\site-packages\tensorflow\python\client\device_lib.py", line 41, in list_local_devices
    for s in pywrap_tensorflow.list_devices(session_config=session_config)
  File "C:\Users\kimisyo\AppData\Local\conda\conda\envs\chemsyo\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 1829, in list_devices
    return ListDevices(status)
  File "C:\Users\kimisyo\AppData\Local\conda\conda\envs\chemsyo\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InternalError: cudaGetDevice() failed. Status: CUDA driver version is insufficient for CUDA runtime version

インストール後に問題発生(2019/7/22追記)

実際に、 with tf.Session() as sess:でセッションを作って実行しようとすると以下エラーが発生。tensorflowのバージョンを1.4にしてもダメだった。

2019-07-22 20:38:08.489670: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-07-22 20:38:08.826513: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties:
name: GeForce GTX 1060 6GB major: 6 minor: 1 memoryClockRate(GHz): 1.7085
pciBusID: 0000:01:00.0
totalMemory: 6.00GiB freeMemory: 4.97GiB
2019-07-22 20:38:08.832424: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
Traceback (most recent call last):
  File "DeepLearning.py", line 36, in <module>
    main()
  File "DeepLearning.py", line 31, in main
    with tf.Session() as sess:
  File "C:\Users\kimisyo\AppData\Local\conda\conda\envs\kimisyo\lib\site-packages\tensorflow\python\client\session.py", line 1551, in __init__
    super(Session, self).__init__(target, graph, config=config)
  File "C:\Users\kimisyo\AppData\Local\conda\conda\envs\kimisyo\lib\site-packages\tensorflow\python\client\session.py", line 676, in __init__
    self._session = tf_session.TF_NewSessionRef(self._graph._c_graph, opts)
tensorflow.python.framework.errors_impl.InternalError: cudaGetDevice() failed. Status: CUDA driver version is insufficient for CUDA runtime version

そこで以前、このマシンで1.5が動いた記憶があり、tensorflow1.5をインストールしてみればよいのではと考えた。インストールしたtensorflow-gpuをアンインストールし、pip install tensorflow==1.5を実行したところ、そのバージョンは存在しないというエラーが出る。
試したPythonのバージョンが3.7であり、以前の環境を参照したところ、python3.6だったため、
以下ように仮想環境を再構築するところからやり直しをしてみる。

conda create -n nekoshi3 python==3.6

続いてtensorflow-gpuの1.5をインストール。今度はパッケージは存在するようだ。

pip install tensorflow-gpu==1.5

サンプルとして、以下コードを実行。

import tensorflow as tf
x = tf.constant(5)
y = tf.constant(10)   # グラフの実行

with tf.Session() as sess:
    print(z.eval())

すると今度は別のエラーが。

Traceback (most recent call last):
  File "DeepLearning.py", line 5, in <module>
    import tensorflow as tf
  File "C:\Users\xxx\AppData\Local\conda\conda\envs\xxx\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "C:\Users\xxx\AppData\Local\conda\conda\envs\xxx\lib\site-packages\tensorflow\python\__init__.py", line 52, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "C:\Users\xxx\AppData\Local\conda\conda\envs\xxx\lib\site-packages\tensorflow\core\framework\graph_pb2.py", line 6, in <module>
    from google.protobuf import descriptor as _descriptor
  File "C:\Users\xxx\AppData\Local\conda\conda\envs\xxx\lib\site-packages\google\protobuf\descriptor.py", line 47, in <module>
    from google.protobuf.pyext import _message
ImportError: DLL load failed: 指定されたプロシージャが見つかりません。

色々ググったところ、以下の通りprotobufというパッケージをインストールして再実行したところ、無事動作した。

pip install protobuf==3.4.

結論としては、搭載されているGPUの種類と、Tensorflow GPU のバージョンの整合性が取れていなかったといういうことか。
やはり、DeepLearningは、通常の機械学習手法に比べ、環境管理で手間かかるということを実感した次第である。

参考

windows10にTensorFlow GPUを入れる

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5