MLBazaar/MLPrimitives

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

csala opened this issue · 1 comments

csala commented
  • MLPrimitives version: 0.2.5.dev0
  • Python version: 3.6
  • Operating System: Ubuntu

Description

Keras 2.4 is incompatible with tensorflow < 2.2, but mlprimitives requires tensorflow < 2 as well as Keras<3.

As a result, after Keras 2.4 was released, when using Keras-based primitives in MLPrimitives a dependency error is raised.

What I Did

Steps to reproduce the error: Install the latest version of mlprimitives and create an instance of any pipeline that uses Keras:

$ python
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from mlblocks import MLPipeline
>>> pipeline = MLPipeline('text.classification.lstm')
Exception caught building MLBlock keras.preprocessing.text.Tokenizer
Traceback (most recent call last):
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/keras/__init__.py", line 3, in <module>
    from tensorflow.keras.layers.experimental.preprocessing import RandomRotation
ModuleNotFoundError: No module named 'tensorflow.keras.layers.experimental.preprocessing'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/mlblocks/mlblock.py", line 20, in import_object
    parent = importlib.import_module(parent_name)
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/keras/__init__.py", line 6, in <module>
    'Keras requires TensorFlow 2.2 or higher. '
ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/keras/__init__.py", line 3, in <module>
    from tensorflow.keras.layers.experimental.preprocessing import RandomRotation
ModuleNotFoundError: No module named 'tensorflow.keras.layers.experimental.preprocessing'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/mlblocks/mlpipeline.py", line 113, in _build_blocks
    block = MLBlock(primitive, **block_params)
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/mlblocks/mlblock.py", line 164, in __init__
    self.primitive = import_object(self.metadata['primitive'])
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/mlblocks/mlblock.py", line 23, in import_object
    grand_parent = importlib.import_module(grand_parent_name)
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/keras/__init__.py", line 6, in <module>
    'Keras requires TensorFlow 2.2 or higher. '
ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`
Traceback (most recent call last):
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/keras/__init__.py", line 3, in <module>
    from tensorflow.keras.layers.experimental.preprocessing import RandomRotation
ModuleNotFoundError: No module named 'tensorflow.keras.layers.experimental.preprocessing'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/mlblocks/mlblock.py", line 20, in import_object
    parent = importlib.import_module(parent_name)
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/keras/__init__.py", line 6, in <module>
    'Keras requires TensorFlow 2.2 or higher. '
ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/keras/__init__.py", line 3, in <module>
    from tensorflow.keras.layers.experimental.preprocessing import RandomRotation
ModuleNotFoundError: No module named 'tensorflow.keras.layers.experimental.preprocessing'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/mlblocks/mlpipeline.py", line 183, in __init__
    self.blocks = self._build_blocks()
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/mlblocks/mlpipeline.py", line 113, in _build_blocks
    block = MLBlock(primitive, **block_params)
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/mlblocks/mlblock.py", line 164, in __init__
    self.primitive = import_object(self.metadata['primitive'])
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/mlblocks/mlblock.py", line 23, in import_object
    grand_parent = importlib.import_module(grand_parent_name)
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/xals/.virtualenvs/MLPrimitives.clean/lib/python3.6/site-packages/keras/__init__.py", line 6, in <module>
    'Keras requires TensorFlow 2.2 or higher. '
ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`

Solution

Either ensure tensorflow>2 support or cap Keras version to <2.4

To note, Keras is now the default API in TensorFlow 2.0 and above. Therefore, you should not need to import it separately. The version of Keras on my end at least is 2.3.0 (see below).

To upgrade TensorFlow to the latest version (and given you are using Python 3.6.9), you can upgrade TF in the terminal as follows:

pip3 install --upgrade tensorflow --user

My TF version is 2.2, and I was able to import tensorflow.keras.layers.experimental.preprocessing without any issues.

To check your TensorFlow version:

>>> import tensorflow as tf
>>> print(tf.__version__)
2.2.0

Additionally, the indicated version of Keras on my end under TF v2.2 is as follows:

>>> tf.keras.__version__
'2.3.0-tf'

Hope this helps.