34

I just installed Visual Studio 2019 and Tensorflow, but I cannot import Keras because I get the following error message:

Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow

The problem is that I had no choice but to install Tensorflow 1.15, because I have the following setup:

  • Visual Studio 2019
  • Python 3.7
  • CPU i7 920 (no avs, only SSE)
  • OS Windows 7 64
  • Nvidia GPU
  • CUDA 10.1

I had to download and install a wheel for that Python version, my CPU, and that CUDA version named "tensorflow-1.15.0-cp37-cp37m-win_amd64".

Tensorflow seems to work (it detects my GPU and prints a "hello world" message) but the problem is that Visual Studio installs the newest version of Keras.

How can I specify an older, compatible version, and what is the newer version compatible?

rickvdbosch
  • 14,105
  • 2
  • 40
  • 53
tutizeri
  • 503
  • 1
  • 5
  • 10
  • **NOT AN ANSWER** The same thing happened to me. I am using docker for my MTCNN, it was working good couple of days ago now that I create a new docker image, this error is appearing. there was a new release in Keras 2 days ago, this might be the issue https://github.com/keras-team/keras/releases – sudharshan rao Jun 19 '20 at 22:16
  • Does this answer your question? [Error while importing keras and tensorflow](https://stackoverflow.com/questions/62482404/error-while-importing-keras-and-tensorflow) – Peter O. Jul 13 '20 at 07:15

6 Answers6

60

I had the same issue caused by last keras release,what i remember did():

1-Upgrade tensorflow:

  pip install --user --upgrade tensorflow-gpu

(there might be some missing packages, just pip install them)

2-Upgrade Tensorboard

pip install --user --upgrade tensorboard

(there might be some missing packages, just pip install them)

3-Downgrade Keras

pip install keras==2.3.1

(latest version working for me)

4-Downgrade tensorflow-gpu

pip install --user --upgrade tensorflow-gpu==1.14.0

(latest version working for me)

Let me know if worked!


Anaconda 2020.02

Python 3.7

CPU i3 8100

OS Windows 10 64

Nvidia GPU GTX1050TI

CUDA 10.1

Gustavo Zantut
  • 751
  • 6
  • 8
  • Thank you. I had to run VS2019 as administrator, becasue it was impossible to run the commands with admin privileges otherwise – tutizeri Jun 21 '20 at 01:28
  • 1
    downgrading keras to 2.3.1 did the trick for me for cpu based tensorflow installation and downgrading tensorflow to 2.0 – Jijo Jun 23 '20 at 10:31
  • 1
    Insisting a little more to uso TF 2.2 i came up that conda install tensorflow-gpu installed cuda10.0 toolkit,. If you want to use tf 2.2 you have to dowload and install cuda 10.1 and cudnn manually, than TF 2.2 will work with latest keras version – Gustavo Zantut Jun 27 '20 at 13:32
  • I've ended up with specifying `Keras>=2.0.6,<2.4.0` :-) – Martin Thoma Jul 19 '20 at 13:39
  • `pip install --user --upgrade tensorboard==2.2.0` instead of `pip install --user --upgrade tensorboard` avoided an error – YoussefDir Jul 24 '20 at 07:42
  • Ok, this worked like a charm but can you explain what the last upgrade is for? – nooshinha Mar 29 '21 at 12:51
  • Well, there are some things that happens in life that i just have one explanation: chaos. By the way I got pissed off by those beautiful dependencies, requirements and god knows why errors and just started using docker with tensorflow oficial image and it's amazing, never ever had a problem with tensorflow and those CUDA CUDNN and other CU's again. – Gustavo Zantut May 09 '21 at 22:55
20

Following the advice given here, downgrading Keras did the trick for me without having to touch any other packages. Just do:

pip install keras==2.3.0

I hope this only remains a temporary issue and will be fixed in future versions of TensorFlow and Keras.

Other possible solutions, are discussed here.

Hagbard
  • 3,430
  • 5
  • 28
  • 64
4

you need to first upgrade pip:

#TensorFlow 2 packages require a pip version >19.0
pip install --upgrade pip

then you can install tensorflow=2.2 with:

pip install tensorflow==2.2

then it worked for me.

user2348209
  • 136
  • 11
  • I had to install tensorflow for gpu, that didn't work – Gustavo Zantut Jun 24 '20 at 00:30
  • I see, but the latest version of keras requires tensorflow 2.2 and above. You can install the corresponding tensorflow for gpu similarly with: pip install tensorflow-gpu==2.2.0rc2 or any 2.2 that you want. – user2348209 Jun 24 '20 at 17:06
  • I get it but a could not find a CUDA and cudll config for tensorflow gpu 2.2, neither conda install is updated to use tensorflow 2.2... – Gustavo Zantut Jun 25 '20 at 18:27
  • I see, thank you for the clarifications, this is a reason that I don't like using anaconda. I was a fan of anaconda but later on, I understood that it limits a lot when you need to have some packages that are not supported by conda. BTW, maybe using a virtual environment for your project is not a bad idea that let you to install and try the packages in your local space before you decide to install them as the default libraries. It is much easier than it seems and it's explained very well in https://docs.python.org/3/tutorial/venv.html – user2348209 Jun 25 '20 at 23:48
  • I've tried configure the enviroment without conda to use tf with gpu but i had no sucess, despite this, i can't use me GPU from a virtual enviroment. But I'm beggining in ML and certanly will look for. – Gustavo Zantut Jun 26 '20 at 03:34
  • The problem is to find a wheel for my configuration. That version of Tensorflow was the first one I installed, but it refused to work. Then I found on Internet, that it is broken and particular for each version of visual studio, type of processor, OS, Python and CUDA version, so the user needs to get a tensorflow version pre compiled for that configuration. – tutizeri Jun 27 '20 at 01:23
  • I tried everything and nothing works. I have python 64, win 64, keras 2.4.3, tensor 2.2, C++. I've tried all the combinations and still end up with keras doesn't support tensor 2.2. What's going on? – Jelani Aug 01 '20 at 20:13
3

For me the issue was solved by replacing the library includes from this:

import keras
from keras.models import Sequential, load_model

To This

from tensorflow import keras
from tensorflow.keras.models import Sequential, load_model
Anubha
  • 1,345
  • 6
  • 23
  • 35
1

I was facing same issue just downgraded keras version to 2.3.1 and it was working

pip install keras==2.3.1

0

Believe me, I suffered form the same error and the only solution is -->

If this problem occurs in Anaconda Navigator, the only way to solve this problem is create a new environment in anaconda with python 3.7 version.

The new environment can be created by clicking bottom left new button on the Anaconda navigator dashboard and the individually add the libraries which is requires for the project.