keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.89k stars 19.45k forks source link

Keras v3 equivalent for `get_output_at(num)` #20267

Open felixdittrich92 opened 1 month ago

felixdittrich92 commented 1 month ago

Hi :wave:,

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 commented 1 month ago

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.

felixdittrich92 commented 1 month ago

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 😅 (https://github.com/mindee/doctr/pull/1724)

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

Thanks again 🤗

fchollet commented 4 weeks ago

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!

felixdittrich92 commented 3 weeks ago

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 :+1:
  2. I'm maintainer of docTR and OnnxTR in my spare time, so time is limited :sweat_smile: But sure i think i could reserve some time, it's more the question about the implementation

Backend: