Keras tensorflow 后端未检测到 GPU
Posted
技术标签:
【中文标题】Keras tensorflow 后端未检测到 GPU【英文标题】:Keras tensorflow backend does not detect GPU 【发布时间】:2017-12-23 14:46:54 【问题描述】:我在 linux 上运行带有 tensorflow 后端的 keras。
首先我自己安装了tensorflow GPU版本,运行如下代码查看,发现运行在GPU上,显示运行的GPU,设备映射等。我使用的tensorflow来自https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-
0.11.0-cp27-none-linux_x86_64.whl
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
然后,我使用conda install keras
安装了 keras。我检查了conda list
,现在我有 2 个版本的 tensorflow(1.1.0 和 0.11.0)。我试过import tensorflow as tf
,结果是:
2017-07-18 16:35:59.569535: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-18 16:35:59.569629: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-18 16:35:59.569690: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-07-18 16:35:59.569707: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-18 16:35:59.569731: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Device mapping: no known devices.
2017-07-18 16:35:59.579959: I tensorflow/core/common_runtime/direct_session.cc:257] Device mapping:
MatMul: (MatMul): /job:localhost/replica:0/task:0/cpu:0
2017-07-18 16:36:14.369948: I tensorflow/core/common_runtime/simple_placer.cc:841] MatMul: (MatMul)/job:localhost/replica:0/task:0/cpu:0
b: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-07-18 16:36:14.370051: I tensorflow/core/common_runtime/simple_placer.cc:841] b: (Const)/job:localhost/replica:0/task:0/cpu:0
a: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-07-18 16:36:14.370109: I tensorflow/core/common_runtime/simple_placer.cc:841] a: (Const)/job:localhost/replica:0/task:0/cpu:0
我已经设置了CUDA_VISIBLE_DEVICES
,它在安装 keras 之前就可以使用。
这是因为 tensorflow 版本吗?安装keras的时候可以选择安装0.11.0而不是1.1.0吗?
如果问题是由于 tensorflow 没有检测到 GPU,我该如何解决这个问题?我在link 中读到,它说 tensorflow 会在检测到 GPU 时自动在 GPU 上运行。
【问题讨论】:
我能够通过使用pip install tensorflow-gpu
再次安装 tensorflow 来解决该问题。来自 keras GitHub 问题:github.com/fchollet/keras/issues/5712.
【参考方案1】:
Keras 可能会根据 TensorFlow 的较新版本安装仅 CPU 的 TensorFlow 包 (tensorflow
),该包隐藏了支持 GPU 的旧版本 (tensorflow-gpu
)。
我会先升级支持 GPU 的版本。通常你可以只做pip install --upgrade tensorflow-gpu
,但你在TensorFlow installation page 中有特定于Anaconda 的说明。然后您可以使用pip uninstall tensorflow
卸载仅 CPU 的 TensorFlow 包。现在import tensorflow as tf
应该实际导入支持 GPU 的包,正如您所建议的,它应该反过来自动检测您的 GPU。
【讨论】:
以上是关于Keras tensorflow 后端未检测到 GPU的主要内容,如果未能解决你的问题,请参考以下文章
如何强制 keras 使用 tensorflow GPU 后端
如何使用 TensorFlow 后端屏蔽 Keras 中的损失函数?
开源!基于TensorFlow/Keras/PyTorch实现对自然场景的文字检测及端到端的OCR中文文字识别