Skip to content

Update TensorFlow backend for Python 3.12 compatibility (Keras 2 via tf-keras)#3087

Merged
AlexEMG merged 27 commits intomainfrom
maxim/relax_dependencies
Oct 1, 2025
Merged

Update TensorFlow backend for Python 3.12 compatibility (Keras 2 via tf-keras)#3087
AlexEMG merged 27 commits intomainfrom
maxim/relax_dependencies

Conversation

@maximpavliv
Copy link
Contributor

@maximpavliv maximpavliv commented Sep 4, 2025

Motivation

Google Colab recently switched from Python 3.11 to Python 3.12, which introduced several dependency issues, particularly affecting our TensorFlow backend.

The main issue was that we had pinned TensorFlow to version 2.12.0 (tensorflow>=2.0,<=2.12 for non-Windows, <=2.10 for Windows). These old versions are no longer available for Python 3.12, where the earliest installable TensorFlow release is 2.16.0.

However, since TensorFlow 2.16.0, Keras 3 is used by default, which is incompatible with our current TensorFlow code (e.g., BatchNormalization is not available with Keras 3 errors due to tf.compat.v1). This incompatibility was the reason for pinning TensorFlow in the first place.


Changes Introduced

To resolve these issues, this PR introduces the following changes:

1. TensorFlow & Keras compatibility (link to TF documentation)

  • Unpinned TensorFlow, allowing installation of recent versions.
  • Forced the use of Keras 2 (tf-keras) by installing tf-keras and setting the environment variable TF_USE_LEGACY_KERAS=1.
  • Introduced a new module _tf_legacy.py to ensure these environment variables are declared when using the TensorFlow backend.

2. Additional environment variables

  • Added WRAPT_DISABLE_EXTENSIONS=1 to resolve the error:
    TypeError: this __dict__ descriptor does not support '_DictWrapper' objects
    (related to wrapt, a TensorFlow dependency, the solution has been suggested here).

3. Compatibility shim for tf-keras namespace changes

  • Newer versions of tf-keras reorganized legacy layers under a src/ namespace.
  • Added a shim in _tf_legacy.py to handle both layouts:
try:
    import tf_keras.src.legacy_tf_layers as legacy_tf_layers
    sys.modules["tf_keras.legacy_tf_layers"] = legacy_tf_layers
except ImportError:
    # Older tf-keras didn’t use src/, so nothing to do
    pass

4. Relaxed other package requirements

  • Relaxed pinned versions for tables and PySide6, since the exact pinned versions were unavailable on Python 3.12.

5. CI/CD improvements

  • Extended the test matrix to include Python 3.11 and Python 3.12 on Linux, MacOS, and Windows.
  • On MacOS + Python 3.12, functional tests were hanging due to a known PyTorch DataLoader bug when num_workers > 0. Set num_workers=0 explicitly in these cases.
  • Attempted to include Python 3.13, but encountered multiple unrelated issues across OSs. Deferred this for later (may become necessary once Colab updates to 3.13).

6. Colab notebooks update


Testing

  • Verified unit and functional tests pass across the updated CI/CD matrix.
  • Ran the 3 TensorFlow-based notebooks manually — all executed successfully.
  • ⚠️ MacOS with tensorflow-metal: only tested via CI/CD (no tensorflow-metal), not on a real machine. This should be validated before merging.

Notes / Future Work

  • Monitor TensorFlow and Colab’s transition to Python 3.13 and plan for compatibility.
  • Ensure MacOS + tensorflow-metal users validate installation and training pipelines.

@maximpavliv maximpavliv added the WORK IN PROGRESS! developers are currently working on this feature... stay tuned. label Sep 4, 2025
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch 2 times, most recently from 1c1fd02 to 08b5d3c Compare September 4, 2025 15:10
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch from 08b5d3c to 79e54a9 Compare September 6, 2025 10:33
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch from 2657cdf to 40d159a Compare September 6, 2025 11:03
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch 2 times, most recently from aa0fca8 to 0e8c3d2 Compare September 8, 2025 13:10
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch from 0e8c3d2 to b4aca6e Compare September 8, 2025 14:00
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch from 4efcbda to 48201fa Compare September 8, 2025 14:44
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch from 48201fa to 01c3008 Compare September 8, 2025 15:21
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch from 7c151af to d08878e Compare September 8, 2025 16:31
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch 2 times, most recently from 7e88a68 to 1023a14 Compare September 9, 2025 08:10
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch 4 times, most recently from 5a4df95 to e778557 Compare September 9, 2025 10:29
@maximpavliv maximpavliv force-pushed the maxim/relax_dependencies branch from 6833c8a to 8b8ec8c Compare September 9, 2025 11:21
@maximpavliv maximpavliv added Installation Tensorflow 2 COLAB dependencies Pull requests that update a dependency file and removed WORK IN PROGRESS! developers are currently working on this feature... stay tuned. labels Sep 10, 2025
@maximpavliv maximpavliv changed the title [WIP] Fix dependencies for installation Fix dependencies for installation Sep 10, 2025
@maximpavliv maximpavliv changed the title Fix dependencies for installation Update TensorFlow backend for Python 3.12 compatibility (Keras 2 via tf-keras) Sep 10, 2025
@AlexEMG AlexEMG merged commit d0806b7 into main Oct 1, 2025
26 of 29 checks passed
deruyter92 added a commit to deruyter92/DeepLabCut that referenced this pull request Dec 5, 2025
Useful for investigating compatibility with different tensorflow versions (See PR DeepLabCut#3087 and DeepLabCut#3091)
deruyter92 added a commit to deruyter92/DeepLabCut that referenced this pull request Dec 5, 2025
Useful for investigating compatibility with different tensorflow versions (See PR DeepLabCut#3087 and DeepLabCut#3091)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

COLAB dependencies Pull requests that update a dependency file Installation Tensorflow 2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants