5

These threads did not solve my problem: Keras does not use GPU on Pycharm having python 3.5 and Tensorflow 1.4

Keras with TensorFlow backend not using GPU

I have installed Tensorflow and Tensorflow-gpu (v.1.12.0) on my PC which is running Windows 10 and has GTX 750 Ti graphics card, So it does support CUDA. I have also installed CUDA Toolkit v10 and cuDNN libraries and when I run nvcc -V on command prompt I get:

nvcc: NVIDIA (R) Cuda compiler...

I am using PyCharm and I don't have any problem running Keras on CPU. But it doesn't use my GPU.

When I type

from keras import backend as K
K.tensorflow_backend._get_available_gpus()

it says

2018-11-25 10:47:57.448275: I tensorflow/core/platform/cpu_feature_gaurd.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

[ ]

What I have tried:

1) I tried uninstalling Tensorflow and Tensorflow-gpu and reinstalling Tensorflow-gpu like the above thread says. Didn't work and my code didn't run on CPU anymore and gave an error regarding Tensorflow. Once I reinstalled Tensorflow, It was OK again.

2) I tried using a library named Theano, which is mentioned on Keras official documentation here. When I add the following lines

import theano
theano.config.device='gpu'
theano.config.floatX='float32'

it says

WARNING (theano.configdefaults): g++ not available, if using conda: 'conda install m2w64-toolchain' C:\Users\HOME-PC\PyCharmProjects\env\lib\site-packages\theano\configdefaults.py.=:560: UserWarning: DeprecationWarning: there is no c++ compiler.This is deprecated and with theano 0.11 a c++ compiler will be mandatory. warning.warn("DeprecationWarning: there is no c++ compiler."

3) I tried adding these lines and also nothing happened.

import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="0" 

What am I missing here? How should I introduce CUDA to PyCharm?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Alireza
  • 410
  • 3
  • 17
  • 1
    So uninstalling tensorflow and keras didn't help? – Matthieu Brucher Nov 25 '18 at 08:55
  • @MatthieuBrucher When I uninstall TF,TF-gpu and keras and reinstall just keras and TF-gpu I get the error "Failed to load the native TensorFlow runtime" and when I install TF this error goes away. – Alireza Nov 25 '18 at 09:06
  • 1
    But if it fails to load the native runtime, that's all right, that what we want. The native runtime is the CPU one. – Matthieu Brucher Nov 25 '18 at 09:07
  • @MatthieuBrucher But nothing else also happens and it doesn't run on GPU. I believe PyCharm somehow can't connect to CUDA? – Alireza Nov 25 '18 at 09:09
  • 1
    So if you don't have TF, only TF-gpu, you get a failure with keras? Did you try using the TF keras layers instead of keras directly? – Matthieu Brucher Nov 25 '18 at 09:12
  • Yes exactly. No, I haven't. But when I try to import tensorflow or tensorflow_gpu it says it's not installed. What is the library name for TF-gpu? because using tensorflow-gpu gives invalid syntax error. And just trying to import keras rises an error "Failed to load native TF runtime". Where are TF keras layers located? – Alireza Nov 25 '18 at 09:18
  • 1
    They are in `tensorflow.keras`, so you should be able to do `from TensorFlow import keras` and use the API more or less like before (https://www.tensorflow.org/guide/keras). If you use this API, you can have `tensorflow`. – Matthieu Brucher Nov 25 '18 at 09:20
  • @MatthieuBrucher So should I reinstall TF? because from TensorFlow import keras says tensorflow is not installed. – Alireza Nov 25 '18 at 09:29
  • 1
    yes, try with tensorflow, and let us know if using the keras layer in TF solves your issue. Would be nice that it does! – Matthieu Brucher Nov 25 '18 at 09:34
  • @MatthieuBrucher I replaced every import keras... with import tensorflow.keras... and my code compiles and runs but again only on CPU. – Alireza Nov 25 '18 at 10:02
  • 1
    Annoying... So `tf.Session(config=tf.ConfigProto(log_device_placement=True))` says CPU then? Have you tried declaring your model in a `with device('/gpu:0'):` section? – Matthieu Brucher Nov 25 '18 at 10:06
  • @MatthieuBrucher It says `I tensorflow/core/platform/cpu_feature_gaurd.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 Device mapping: no known devices.` and when I use `with tf.device('/gpu:0'):` and define and compile my model under it it runs on CPU again. – Alireza Nov 25 '18 at 10:12
  • 2
    Are you sure that you installed "exactly" the Cuda version required by tensorflow 1.4? Tensoflow is picky and it needs "excactly" the one version that is compatible. I suggest you go to the latest tensorflow and install the exact Cuda version it says in the tutorial. -- You cannot have both tensorflow and tensorflow-gpu. Install only tensorflow GPU (if you're using conda, give a try at installing keras-gpu) – Daniel Möller Nov 27 '18 at 18:24
  • 1
    And defined all CuDNN environment vars properly? Notice that the instructions say that you might need to copy some files to some folders when installing CuDNN (The same concerns with exact version applies with CuDNN as well) – Daniel Möller Nov 27 '18 at 18:33

2 Answers2

2

The problem was with CUDA's version. I had installed CUDA v10.0 but Tensorflow seems to work only with v9.0. Installed it and it works like a charm.

Alireza
  • 410
  • 3
  • 17
1

It might not be the case but, installing and importing the same librariy can sometimes be confusing. More clearly, my guess is your pycharm environment is different than the default python environment, youre installing packages in the default environment and importing from the pycharm's environment. To ensure you have a package installed in pycharm's environment,

you can try the following, from pycharm's python console

!python -m pip install --upgrade tensorflow, keras

this shall properly install the packages, and you can be sure of that installation isn't at fault

dragonLOLz
  • 78
  • 7
  • I assume you mean tensorflow-gpu. but I have tried installing packages from PyCharm project interpreter also. No change. – Alireza Dec 01 '18 at 19:11
  • You've followed the official installation guide? maybe checking out the steps again can help [link](https://www.tensorflow.org/install/gpu#windows_setup) – dragonLOLz Dec 02 '18 at 07:35