tensorflow / tensorflow/model-optimization

tensorflow-model-optimization fails to import in official tensorflow:2.15.0 Docker image due to TF_USE_LEGACY_KERAS=1 and missing tf_keras

Open
#1,272 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.6k
Forks
349
Avg merge
3d 2h
Merged PRs (30d)
1

Description

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).

  1. 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the import failure in the official tensorflow/tensorflow:2.15.0 image using tensorflow-model-optimization==0.8.0 and the shown Python command. Inspect how TF_USE_LEGACY_KERAS=1 and the tf_keras dependency interact; done means tfmot imports successfully without upgrading TensorFlow beyond 2.15.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python, tensorflow
Domain
devops, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.