Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keras 2.4 release adds dependency on tensorflow>=2.2.0, which breaks earlier installs #14129

Closed
cliffwoolley opened this issue Jun 18, 2020 · 12 comments

Comments

@cliffwoolley
Copy link

Yesterday, June 17, Keras 2.4.0 was released. This is the first numbered release since last October (which was 2.3.1).

Keras 2.4 now brings in tensorflow>=2.2 as a dependency (starting from #14121 , which was merged yesterday). It looks like this is because Keras 2.4 basically now redirects to tf.keras.

The problem, however, is one of compatibility: any user workflow that had pip install keras or packages that have keras in their requirements.txt/setup.py (without pinning to 'keras<2.4' or to some specific earlier version, at least) will now end up replacing existing TensorFlow 1.15 or TensorFlow 2.1 installations (which are the LTS versions as far as I'm aware) with TF 2.2.

I'm not quite sure what to suggest should be done as a corrective action at this point; even if this had been numbered as for example, "Keras 3.0" then still packages that don't pin to a particular maximum Keras version would have fallen into this trap.

Already some NVIDIA customers have started hitting this, so I wanted to open the discussion, at least.

@cliffwoolley
Copy link
Author

/cc @fchollet

@tgaddair
Copy link

Thanks for raising this @cliffwoolley. I can confirm this change is incompatible with tensorflow-gpu and tf-nightly, resulting in multiple overlapping installations of TensorFlow and errors like the following:

AttributeError: module 'tensorflow.python.keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'

@fchollet
Copy link
Collaborator

Incompatibility with tf-nightly is a problem. I will remove the version requirement.

@DEKHTIARJonathan
Copy link
Contributor

DEKHTIARJonathan commented Jun 19, 2020

@fchollet unfortunately your fix won't work to address the issue of tf-nightly and tensorflow-gpu.
You still have that hard dependency on tensorflow: https://github.com/keras-team/keras/blob/master/setup.py#L22

What I would suggest is the following:

  1. Remove completely the dependency on TF from setup.py

  2. Add this script in __init__.py :

from distutils.version import LooseVersion
try:
   import tensorflow as tf
   if LooseVersion(tf.__version__) < LooseVersion("2.2.0"):
       raise RuntimeError("Keras requires a version of Tensorflow >= 2.2. Please upgrade")
except ImportError:
   raise ImportError("Tensorflow >= 2.2 is not installed. Please run `pip install tensorflow>=2.2`")

This will effectively make any installation of Keras to fail on import if the condition on Tensorflow does not a version condition.
This solution also allows to use different tensorflow metapackages.

@tgaddair
Copy link

tgaddair commented Jun 19, 2020

To add on to @DEKHTIARJonathan's suggestion, I think it should be possible to dynamically add tensorflow>=2.2 to the install_requires list only if TensorFlow isn't already installed (so they can get the benefit of auto-installing TensorFlow, consistent with the original PR), so something like:

from distutils.version import LooseVersion

requires_list = ['numpy>=1.9.1',
                        'scipy>=0.14',
                        'pyyaml',
                        'h5py']

try:
   import tensorflow as tf
   if LooseVersion(tf.__version__) < LooseVersion("2.2.0"):
       raise RuntimeError("Keras requires a version of Tensorflow >= 2.2. Please upgrade")
except ImportError:
   requires_list += ['tensorflow>=2.2']

...
setup(install_requires=requires_list, ...)

@DEKHTIARJonathan
Copy link
Contributor

@tgaddair so long as keras is shipped as an sdist that will work. Totally agree with you

@fchollet
Copy link
Collaborator

Updated to remove the TF requirement and instead run an ImportError at runtime.

@sb4267
Copy link

sb4267 commented Jun 19, 2020

ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow'

getting this error after the latest release

@cliffwoolley
Copy link
Author

@sb4267 Yes, that is intentional. If you need a TensorFlow version prior to 2.2, you should pin Keras to keras<2.4 .

@cliffwoolley
Copy link
Author

cliffwoolley commented Jun 19, 2020

7b76bed for Keras 2.4.2 effectively resolves this issue.

Explanation: Keras 2.4.0 installation would silently swap out existing TensorFlow installations with TF 2.2, which may be undesirable as discussed above and is why this issue was opened.

Starting from Keras 2.4.2, rather than automatically swapping TF versions when installing keras, the new behavior is that import keras will throw the runtime error quoted by @sb4267 above if the user has incorrectly mixed the new Keras with an earlier TensorFlow. The user can then choose either upgrading to tensorflow>=2.2 or downgrading to keras<2.4.

Closing.

@sb4267
Copy link

sb4267 commented Jun 19, 2020

thanks for the prompt reply ,
here is my dockerfile can you please comments on it , if this works
DOCKERFILE

FROM tensorflow/tensorflow:latest-py3

ENV PYTHONUNBUFFERED 1
RUN pip uninstall keras
RUN pip install keras==2.1.2
RUN python -m pip install --upgrade pip
RUN pip install tensorflow

RUN mkdir /face_recog

WORKDIR /face_recog

ADD . /face_recog/

RUN apt-get update && apt-get install -y build-essential && apt-get install -y libgtk2.0-dev && pip install cmake

Install any needed packages specified in requirements.txt

RUN pip install -r requirements.txt

#EXPOSE 8000

CMD python manage.py runserver 0.0.0.0:$PORT

@cliffwoolley
Copy link
Author

@sb4267 Let's handle that outside this issue, please. Maybe a StackOverflow post if you find your combination isn't working? (The above isn't actually enough information to determine this.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants