-
Notifications
You must be signed in to change notification settings - Fork 326
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
Support for nested tf.keras.layers.Layer
#638
Comments
Seems reasonable -- we are planning on adding this soon. |
@daverim is there any update on this? I saw that there's a new blog from TensorFlow saying that Subclassed models are now supported, but not much information is given on how this is done exactly. Is there a more in-depth link for this? Thank you! |
We still not fully supports subclass model yet. The model we mentioned on that blog post means we wrote some code to supports a kind of subclass model, which implemented in tensorflow official (https://www.tensorflow.org/tfmodels#official) vision model. To support that, we also export some APIs to enable them. but still we need to implement model specific code to support them. Here is a model specific code for QAT: https://github.com/tensorflow/models/tree/master/official/projects/qat/vision |
Thank you for the reply @Xhark! Is there an ETA on when Subclassed models will be supported? Thanks! |
Just noting that subclassed layers don't work with tf.function, nor do subclassed models. |
System information
Ubuntu 16.04
TensorFlow version (you are using):
Tried both
2.3.0
andtf-nightly
(==2.5.0-dev20210223
)Are you willing to contribute it (Yes/No):
I have tried, but to no avail - maybe with some help?
Motivation
Distantly related to #155 , but instead of nested models this is a request for supporting nested layers. Perhaps the necessary steps for developing the feature might be quite similar, but potentially with less constraints that are imposed by the requirement of
tf.keras.Model
.As documented in the official tensorflow guide, one of the recommended ways of building a TF model is via recursive composition of layers. A lot of architectures have natural such groups, and it would be benificial if
tfmot
could natively support this kind of a design pattern.If it helps, the specific application that I was considering is applying
tfmot
to an optical flow network in the style ofPWCNet
. This would entail multiple levels of hierarchical blocks and reuse of such blocks in an hourglass architecture. Unfortunately, this undertaking has proved to be quite an awkward fit with the current design oftfmot
. For reference, the quantization part of the project can be seen here.Describe the feature
As a first milestone, perhaps layers that are pure compositions of
tf.keras.layers.Layer
can be supported, i.e.I'm aware that the above layer, if not written as a subclassed layer of layers, can be quantized - please take it as an example that, despite its simplicity and straightforward implementation that complies to the general guidelines of development with tf2/keras, nonetheless is not supported as-is in the current state without rewriting the layer.
Describe how the feature helps achieve the use case
The above example, if supported as-is, would serve as a first-pass (and even just pure compositions of existing keras layers would already bring significant expressive freedom) for supporting generally nested
Layer
s.Describe how existing APIs don't satisfy your use case (optional if obvious)
I have tried a couple of approaches, but I think the difficulty in supporting nested layers comes in multiple pieces.
model.layers
which does not provide introspection into nested layers;QuantizeConfig
andModelTransformer
are not compatible;ModelTransformer
does not support reused layers (which are common in e.g. optical flow networks with shared feature extractors).I have attempted something like the following:
QuantizeConfig
Which tries to address this via extending the
QuantizeConfig
API. Unfortunately, layers such asBatchNormalization
are only quantizable viaModelTransformer
pattern-matching, and cannot be supported with this approach.ModelTransformer
approach to flatten nested layers, which ended up not being possible due to the absence of support for layers with multiple connections (e.g. reused feature extractors). Copying the relevant line below frommodel_transformer.py#L138
:Moreover, it can be quite unintuitive to try to replicate exactly the underlying operation of the subclassed nested layer with the flat analogue.
TensorFlowOpLayer
s).Hopefully that grounds the picture to some extent - whereas I think the current
tfmot
architecture is already very well architected, I think being able to support this feature would make it a lot more powerful than it already is. While my 4-or-so attempts have all ended up being futile, I was wondering if there is a roadmap in thetfmot
dev team to support nested layers. I haven't seen explicit mentions of it in the issues (while some were similar), so I figured it would be worth bringing it up.Thank you!
Yoonyoung (Jamie) Cho
The text was updated successfully, but these errors were encountered: