When using the official TensorFlow Docker image tensorflow/tensorflow:2.15.0, the environment variable TF_USE_LEGACY_KERAS is preset to 1 (True). This causes tensorflow-model-optimization (tfmot) to fail with an ImportError because it expects tf_keras to be installed.
Attempting to install tf_keras leads to an automatic upgrade of TensorFlow to 2.21+, which breaks compatibility with tfmot (which is designed for TensorFlow 2.15).
- Pull the official image:
docker pull tensorflow/tensorflow:2.15.0
docker run -it tensorflow/tensorflow:2.15.0 bash
pip install tensorflow-model-optimization==0.8.0
python -c "import tensorflow_model_optimization as tfmot"
The import fails with:
ImportError: Keras cannot be imported. Check that it is installed.