Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility between tensorboard and keras3/keras-core to display functional model graphs #6686

Open
nhuet opened this issue Nov 20, 2023 · 6 comments

Comments

@nhuet
Copy link

nhuet commented Nov 20, 2023

When using keras-nightly which is now a preview of the keras 3.0 to come, tensorboard fails to represent the conceptual graph of a functional keras model.

I know that tensorboard is intended to work with the keras of same version, so i put this as a feature request instead of a bug. I tried also with tensorboard-nightly with the same result: sequential models are well represented but not functional models, probably because of a change in json representation. The error comes from "inbound_nodes" which can now be a list of dictionaries/lists instead of a list of lists.

Here is a minimal example showing the bug:

import json

from keras import Input, Model, Sequential
from keras.layers import Dense

from tensorboard.plugins.graph.keras_util import keras_model_to_graph_def


input = Input((1,))
layer = Dense(3)

## sequential: working
model_seq = Sequential(layers=[input, layer])
graph_def = keras_model_to_graph_def(json.loads(model_seq.to_json()))

## functional: not working  (with keras-nightly aka keras3)
output = layer(input)
model_func = Model(input, output)
graph_def = keras_model_to_graph_def(json.loads(model_func.to_json()))

which results in

Traceback (most recent call last):
File "/home/nolwen/Projects/decomon/tensorboard/minimal_example.py", line 19, in
graph_def = keras_model_to_graph_def(json.loads(model_func.to_json()))
File "/home/nolwen/Projects/decomon/tensorboard/tb-keras3-venv/lib/python3.9/site-packages/tensorboard/plugins/graph/keras_util.py", line 248, in keras_model_to_graph_def
inbound_nodes = _norm_to_list_of_layers(maybe_inbound_node)
File "/home/nolwen/Projects/decomon/tensorboard/tb-keras3-venv/lib/python3.9/site-packages/tensorboard/plugins/graph/keras_util.py", line 116, in _norm_to_list_of_layers
maybe_layers if isinstance(maybe_layers[0], (list,)) else [maybe_layers]
KeyError: 0

Here is an extract of the json reprenstation showing that inbound_nodes members are not always lists:

{

    "inbound_nodes": [
          {
            "args": [
              {
                "class_name": "__keras_tensor__",
                "config": {
                  "shape": [
                    null,
                    1
                  ],
                  "dtype": "float32",
                  "keras_history": [
                    "input_layer",
                    0,
                    0
                  ]
                }
              }
            ],
            "kwargs": {}
          }
        ]
}

You have to run it in an environment with tensorboard (potentially tb-nightly) and keras-nightly. For instance with:

python -m venv tb-keras3-venv
. tb-keras3-venv/bin/activate
pip install tf-nightly
pip uninstall tf-keras-nightly keras-nightly -y  # be sure to avoid a mix of both keras
pip install keras-nightly
@JamesHollyer
Copy link
Contributor

Thank you for this detailed issue! I have added this to out internal hotlist. I hope we can get to it soon!

@yatbear
Copy link
Member

yatbear commented Nov 22, 2023

More context: we recently pinned to Keras 2 before fully migrating our codebase to Keras 3: #6684

Apologies for the inconvenience!

@nhuet
Copy link
Author

nhuet commented Nov 27, 2023

Ok, i understand, thanks for the explanation.

nhuet added a commit to nhuet/decomon that referenced this issue Dec 1, 2023
nhuet added a commit to nhuet/decomon that referenced this issue Dec 4, 2023
ducoffeM pushed a commit to airbus/decomon that referenced this issue Dec 13, 2023
ducoffeM pushed a commit to ducoffeM/decomon that referenced this issue Jan 17, 2024
ducoffeM pushed a commit to airbus/decomon that referenced this issue Jan 17, 2024
bmd3k added a commit that referenced this issue Feb 16, 2024
This adjusts *_demo.py files to work with Keras 3.

The hparams_demo is fully backward compatible with Keras 2 and forward
compatible with Keras 3.

Unfortunately the graphs_demo is not backward compatible with Keras 2.
Users attempting to run it with Keras 2 will get the following error:

```
  File "/usr/local/google/home/bdubois/.cache/bazel/_bazel_bdubois/079646a57be11faea0b2bfefccb2a81a/execroot/org_tensorflow_tensorboard/bazel-out/k8-fastbuild/bin/tensorboard/plugins/graph/graphs_demo.runfiles/org_tensorflow_tensorboard/tensorboard/plugins/graph/graphs_demo.py", line 128, in profile
    tf.summary.trace_on(profiler=True, profiler_outdir=logdir)
TypeError: trace_on() got an unexpected keyword argument 'profiler_outdir'
```

Amazingly, though, the graph that the demo generates with Keras 3 can be
successfully loaded in the Graph dashboard. This makes me optimistic to
get the Graph plugin fully Keras 3 compatible after addressing the
user-reported error in #6686.

Old Keras 2 Graph: 

![image](https://github.com/tensorflow/tensorboard/assets/17152369/b8745739-ac06-4171-a7bc-c97135b2dec7)
New Keras 3 Graph:

![image](https://github.com/tensorflow/tensorboard/assets/17152369/04dcaf14-3464-47dc-b4a5-373cccd8370b)
@nhuet
Copy link
Author

nhuet commented Mar 18, 2024

Hi, is there any progress on this issue? Since 2.16, tensorflow and tensorboard are supposed to be keras 3 fully compatible since this is now the default keras installed. It seems that functional keras models cannot still be displayed in graph plugin. (Same error, inbound_nodes list is made of dicts instead of a lists, thus missing the key 0.

@yatbear
Copy link
Member

yatbear commented Mar 21, 2024

Hi @nhuet,

Apologies since we haven't got any bandwidth to work on this issue, will post an update here as soon as there's any update.

yatbear added a commit that referenced this issue Apr 8, 2024
…d tf-keras-nightly (#6823)

This should fix the issue described in #6686. Compared the graph content with the one parsed from Keras 3, there are some changes in the structure and naming, this PR modifies the graph parsing accordingly.

Googlers, see b/325451531 and b/312739672.

Tested with `tensorboard/plugins/graph:graphs_demo`:

![image](https://github.com/tensorflow/tensorboard/assets/15273931/2e13205a-5ea2-437c-9fa7-3198f0eed1c5)

#keras3 #oncall
@yatbear
Copy link
Member

yatbear commented Apr 8, 2024

Hi @nhuet,

Keras 3 compatibility issue for graph plugin should be fixed at #6823 and #6826. This change will be available in tb-nightly in the next one or two days, please try again and let us know if this resolves your issue. Thanks for your patience!

AnuarTB pushed a commit to AnuarTB/tensorboard that referenced this issue May 3, 2024
…d tf-keras-nightly (tensorflow#6823)

This should fix the issue described in tensorflow#6686. Compared the graph content with the one parsed from Keras 3, there are some changes in the structure and naming, this PR modifies the graph parsing accordingly.

Googlers, see b/325451531 and b/312739672.

Tested with `tensorboard/plugins/graph:graphs_demo`:

![image](https://github.com/tensorflow/tensorboard/assets/15273931/2e13205a-5ea2-437c-9fa7-3198f0eed1c5)

#keras3 #oncall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants