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

Keras v3 equivalent for get_output_at(num) #20267

Open
felixdittrich92 opened this issue Sep 18, 2024 · 4 comments
Open

Keras v3 equivalent for get_output_at(num) #20267

felixdittrich92 opened this issue Sep 18, 2024 · 4 comments
Assignees
Labels
layers stat:contributions welcome A pull request to fix this issue would be welcome. type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@felixdittrich92
Copy link

Hi 👋,

A quick question is there a equivalent in keras v3 for model.get_layer(layer_name).get_output_at(0) from keras v2 ?

context:

class IntermediateLayerGetter(Model):
    """Implements an intermediate layer getter

    >>> from keras.applications import ResNet50
    >>> from doctr.models import IntermediateLayerGetter
    >>> target_layers = ["conv2_block3_out", "conv3_block4_out", "conv4_block6_out", "conv5_block3_out"]
    >>> feat_extractor = IntermediateLayerGetter(ResNet50(include_top=False, pooling=False), target_layers)

    Args:
    ----
        model: the model to extract feature maps from
        layer_names: the list of layers to retrieve the feature map from
    """

    def __init__(self, model: Model, layer_names: List[str]) -> None:
        intermediate_fmaps = [model.get_layer(layer_name).get_output_at(0) for layer_name in layer_names]
        super().__init__(model.input, outputs=intermediate_fmaps)

    def __repr__(self) -> str:
        return f"{self.__class__.__name__}()"

Thanks :)

@fchollet
Copy link
Member

Yeah, something like layer._inbound_nodes[i].outputs. Note that you'd be using a private API so there's no strong guarantee that it will always work.

@sachinprasadhs sachinprasadhs added type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. stat:awaiting response from contributor layers labels Sep 18, 2024
@felixdittrich92
Copy link
Author

Thanks @fchollet 👍
Any recommendations for a "safer" way if you mention that it could change at any time to extract specific layer outputs from a backbone model ?

As context:
I started to bring https://github.com/mindee/doctr step by step to Keras v3 - otherwise the docTR + Tensorflow backend would die over the time 😅 (mindee/doctr#1724)

Btw. i saw it's planned to bring Onnx support to keras v3 is there already any progress ?

Thanks again 🤗

@fchollet
Copy link
Member

Any recommendations for a "safer" way if you mention that it could change at any time to extract specific layer outputs from a backbone model ?

I would recommend just using the code snippet -- it is private, but very stable in practice (hasn't changed since the original Keras 3 release). We just don't think this is common enough to need an official public API. Very little chance it would actually ever break, but make sure to have CI running just in case a so you can catch it.

If we make it a public API it would probably be .inbound_nodes.

Btw. i saw it's planned to bring Onnx support to keras v3 is there already any progress ?

Would you like to contribute something? We haven't staffed it yet but we could guide you if you're interested!

@sachinprasadhs sachinprasadhs added stat:contributions welcome A pull request to fix this issue would be welcome. and removed stat:awaiting keras-eng Awaiting response from Keras engineer labels Sep 21, 2024
@felixdittrich92
Copy link
Author

felixdittrich92 commented Sep 23, 2024

Any recommendations for a "safer" way if you mention that it could change at any time to extract specific layer outputs from a backbone model ?

I would recommend just using the code snippet -- it is private, but very stable in practice (hasn't changed since the original Keras 3 release). We just don't think this is common enough to need an official public API. Very little chance it would actually ever break, but make sure to have CI running just in case a so you can catch it.

If we make it a public API it would probably be .inbound_nodes.

Btw. i saw it's planned to bring Onnx support to keras v3 is there already any progress ?

Would you like to contribute something? We haven't staffed it yet but we could guide you if you're interested!

  1. Thanks a lot again yeah CI would catch this already 👍
  2. I'm maintainer of docTR and OnnxTR in my spare time, so time is limited 😅
    But sure i think i could reserve some time, it's more the question about the implementation

Backend:

  • PyTorch: Should not be to hard: wrapper around torch.onnx.export ?
  • TensorFlow: Depends on third party tf2onnx (which is not yet 100% compatible with keras v3) or "own" integration (looots of work) ?
  • JAX: From my knowledge only possible via JAX -> TF -> Onnx actually !?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
layers stat:contributions welcome A pull request to fix this issue would be welcome. type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

3 participants