Skip to content

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 #1272

@Lao666-bot

Description

@Lao666-bot

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions