Skip to content

[Bug, Update]: tf.keras.Sequential error, uv, ruff, python 3.10, 3.11, 3.12, 3.13#178

Merged
Agustin-Picard merged 5 commits intodeel-ai:masterfrom
quentinpossamai:master
Mar 31, 2026
Merged

[Bug, Update]: tf.keras.Sequential error, uv, ruff, python 3.10, 3.11, 3.12, 3.13#178
Agustin-Picard merged 5 commits intodeel-ai:masterfrom
quentinpossamai:master

Conversation

@quentinpossamai
Copy link
Copy Markdown

@quentinpossamai quentinpossamai commented Jan 29, 2026

Trying to solve #177

As described in the issue, tf.keras.Sequential don't have the model.input and model.output defined even after a call.

These attributes are instead present in model.inputs[0] and model.output[0]. So I have added an elif condition in xplique.attributions.base.py BlackBoxExplainer.__init__ to use the right attributes in case of a Sequential to build the caching of models.

TLDR:
Using id(model.inputs[0]) and id(model.outputs[0]) which contains the symbolic tensors (dtype and shape) of the input and output for a tf.keras.Sequential instead of id(model.input) and id(model.output).

@Agustin-Picard Agustin-Picard added the bug Something isn't working label Jan 29, 2026
Copy link
Copy Markdown
Member

@Agustin-Picard Agustin-Picard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! LGTM

@quentinpossamai quentinpossamai force-pushed the master branch 4 times, most recently from 27fe28b to 4726eda Compare February 23, 2026 09:18
@quentinpossamai quentinpossamai changed the title [Bug]: tf.keras.Sequential error #177 [Bug, Update]: tf.keras.Sequential error, uv, ruff, python 3.10, 3.11, 3.12, 3.13 #177 Feb 23, 2026
@quentinpossamai
Copy link
Copy Markdown
Author

quentinpossamai commented Feb 23, 2026

Changes:

  • pre-commit hook to use ruff check and ruff format
  • pyproject.toml replacing requirements.txt
  • updated tox.ini to test on python 3.10, 3.11, 3.12, 3.13
  • fix bugs when computing gradients from a tf.keras.Sequential, especially for tcav and gradcam *1
  • updated .github/*.yml file for linting, publishing, docs building and testing
  • updated contributing.md, integrating uv use

*1
In TCAV (very similary for GradCam):

self.multi_head = tf.keras.Model(model.input, [target_layer.output, model.output]) was used to retrieve the gradient of the score w.r.t the taget_layer.output:

        with tf.GradientTape() as tape:
            tape.watch(inputs)

            activations, y_pred = self.multi_head(inputs)
            score = y_pred[:, label]

        gradients = tape.gradient(score, activations)

This failed when self.model is a tf.keras.Sequential (as model.output and model.input are never defined, even after a call), so I have created watch_layer and end_watch_layer to retrieve the activations:

        with tf.GradientTape(watch_accessed_variables=False) as tape:
            watch_layer(self.target_layer, tape)
            y_pred = self.model(inputs)
            activations = self.target_layer.result
            score = y_pred[:, label]

        gradients = tape.gradient(score, activations)
        end_watch_layer(self.target_layer)

We just need to keep the self.target_layer and do not need to build a second model (self.multi_head) now so I have deleted the multi_head attribute.

@quentinpossamai quentinpossamai changed the title [Bug, Update]: tf.keras.Sequential error, uv, ruff, python 3.10, 3.11, 3.12, 3.13 #177 [Bug, Update]: tf.keras.Sequential error, uv, ruff, python 3.10, 3.11, 3.12, 3.13 Feb 27, 2026
@Agustin-Picard Agustin-Picard self-requested a review March 2, 2026 10:09
uv for virtual env managment
Compatibility with 3.10 3.11 3.12 3.13
New tox matrix
@quentinpossamai quentinpossamai force-pushed the master branch 3 times, most recently from 59e7ecf to 9f029b8 Compare March 2, 2026 16:36
@Agustin-Picard Agustin-Picard added the continuous-integration Improvement relative to the CI pipeline label Mar 2, 2026
@quentinpossamai quentinpossamai force-pushed the master branch 11 times, most recently from fe3ec13 to 6b5ddf2 Compare March 4, 2026 09:28
Copy link
Copy Markdown
Collaborator

@lucashervier lucashervier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very substantial PR — great work overall. There are many improvements and it’s clear a lot of effort went into it.

For future changes of this size, it might be beneficial to split them into several smaller PRs. In particular, quite a few diffs appear to be formatting-only changes. While those are useful, mixing them with functional updates makes the review a bit harder, as it becomes more difficult to quickly identify the core changes to focus on. I hope I didn’t miss anything important while going through it.

Overall, the direction looks very good 👍 I left a few questions and comments that I think should be clarified before merging, especially around the parts that modify method signatures or behavior.

I also assumed that the workflows were tested and that the existing tests were run successfully.

Comment thread .github/workflows/python-lints.yml Outdated
Comment thread .github/workflows/python-torch.yml
Comment thread .github/workflows/python-torch.yml
Comment thread docs/contributing.md Outdated
Comment thread docs/contributing.md Outdated
Comment thread Makefile Outdated
Comment thread package_python.py Outdated
Comment thread package_python.py Outdated
Comment thread pyproject.toml Outdated
Comment thread pyproject.toml
Copy link
Copy Markdown
Member

@Agustin-Picard Agustin-Picard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, you've implemented a lot of improvements that needed to be done (a long time ago for some of them)!

However, as @lucashervier said, maybe we should have split the PR into 2 or 3, as there were some important changes amidst all the formatting changes. No worries, but just keep it in mind for next time.

I see that you propose a ruff check and ruff format but only fix on PEP violations instead of doing both. We could technically fix both in the pre-commit (or only check in CI). What are your thoughts on this?

In this PR, you also propose changes in the way we perform GradCAM and TCAV, but they haven't been explained in the description. We should try to keep a trace of these changes somewhere as they seem out of the blue.

Finally, we should make sure that the docs build locally before merging. You wouldn't want to be like me and merge to find out you now need to make a second patch PR to fix the documentation ^^

Comment thread .github/workflows/deploy-docs.yml Outdated
Comment thread .github/workflows/python-tests.yml
Comment thread tests/attributions/test_online_statistics.py Outdated
Comment thread tests/commons/test_segmentation_operator.py
Comment thread tests/example_based/test_datasets_harmonization.py Outdated
Comment thread xplique/attributions/object_detector.py Outdated
Comment thread xplique/concepts/tcav.py
Comment thread .pre-commit-config.yaml
Comment thread package_python.py Outdated
Comment thread uv.lock Outdated
Comment thread tests/attributions/test_object_detector.py Outdated
Comment thread xplique/attributions/object_detector.py Outdated
Copy link
Copy Markdown
Member

@Agustin-Picard Agustin-Picard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. The requested changes have been made as far as I'm concerned.

Copy link
Copy Markdown
Collaborator

@lucashervier lucashervier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ;)

@Agustin-Picard Agustin-Picard merged commit fa5d65a into deel-ai:master Mar 31, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working continuous-integration Improvement relative to the CI pipeline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants