-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
TF implementation of RegNets #17554
TF implementation of RegNets #17554
Conversation
Copied the torch implementation of regnets and porting the code to tf step by step. Also introduced an output layer which was needed for regnets.
The documentation is not available anymore as the PR was closed or merged. |
did not change the documentation yet, yet to try the playground on the model
* fix: code structure in few cases. * fix: code structure to align tf models. * fix: layer naming, bn layer still remains. * chore: change default epsilon and momentum in bn.
If we run the following:
First, it complains the We tested your solution in #17571. With that, we're running into mismatches of Do you have any thoughts? |
Hi @sayakpaul Could you give a bit more information about the mismatches i.e. the printouts you're currently getting? Regarding |
I tried debugging this today but no luck yet. But here's some information for all of us to navigate this through:
All these mismatches seem to be stemming from the The test used to gather this information is the same one as mentioned in #17554 (comment). |
@sayakpaul Thanks for your detailed update. Comments below:
|
@amyeroberts a quick update:
|
@sayakpaul Thanks for the update!
|
Thanks for the suggestions. Will try them out and update. |
I had to do a few minor modifications to your snippet in #17554 (comment):
With this change, the result of
(Full output here). I have gone over the |
Also an oversight on my end in reporting
|
@amyeroberts we were able to rectify the model implementation and make it work. The integration test (mentioned in #17554 (comment)) is passing now. The tests, however, are failing for a weird reason:
Weird because we tested a couple of things in isolation: from transformers import RegNetConfig
config_class = RegNetConfig()
print(f"RegNet Config class type: {type(config_class)}.")
print(f"RegNet Config is an instance of PretrainedConfig: {isinstance(config_class, PretrainedConfig)}") The final print statement gives from src.transformers.models.regnet.modeling_tf_regnet import TFRegNetForImageClassification, TFRegNetModel
class_from_config = TFRegNetModel(config_class)
print("Model class from config was initialized.") it complains:
Do you have any suggestions for this? |
Feat/tf regnets
@sgugger @Rocketknight1 the PR is now ready for review. This particular model actually has the largest vision model checkpoint available to date: https://huggingface.co/facebook/regnet-y-10b-seer. It's still in PyTorch and the corresponding model makes use of the I had a chat with @Rocketknight1 a few days back on the possibility of supporting this checkpoint in TensorFlow too. This will require tweaks and they will be contributed in a separate PR. |
def call(self, hidden_state): | ||
num_channels = shape_list(hidden_state)[-1] | ||
if tf.executing_eagerly() and num_channels != self.num_channels: | ||
raise ValueError( | ||
"Make sure that the channel dimension of the pixel values match with the one set in the configuration." | ||
) | ||
hidden_state = self.embedder(hidden_state) | ||
return hidden_state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def call(self, hidden_state): | |
num_channels = shape_list(hidden_state)[-1] | |
if tf.executing_eagerly() and num_channels != self.num_channels: | |
raise ValueError( | |
"Make sure that the channel dimension of the pixel values match with the one set in the configuration." | |
) | |
hidden_state = self.embedder(hidden_state) | |
return hidden_state | |
def call(self, pixel_values): | |
num_channels = shape_list(pixel_values)[-1] | |
if tf.executing_eagerly() and num_channels != self.num_channels: | |
raise ValueError( | |
"Make sure that the channel dimension of the pixel values match with the one set in the configuration." | |
) | |
hidden_state = self.embedder(pixel_values) | |
return hidden_state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
# When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. | ||
# So change the input format from `NCHW` to `NHWC`. | ||
# shape = (batch_size, in_height, in_width, in_channels=num_channels) | ||
pixel_values = tf.transpose(pixel_values, perm=(0, 2, 3, 1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with other models, it's better to do this in the embedding (stem) class rather than here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
if pixel_values is None: | ||
raise ValueError("You have to specify pixel_values") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check doesn't seem useful as pixel_values
is a required argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
last_hidden_state = encoder_outputs[0] | ||
pooled_output = self.pooler(last_hidden_state) | ||
pooled_output = tf.transpose(pooled_output, perm=(0, 3, 1, 2)) | ||
|
||
# Change to NCHW output format have uniformity in the modules | ||
last_hidden_state = tf.transpose(last_hidden_state, perm=(0, 3, 1, 2)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last_hidden_state = encoder_outputs[0] | |
pooled_output = self.pooler(last_hidden_state) | |
pooled_output = tf.transpose(pooled_output, perm=(0, 3, 1, 2)) | |
# Change to NCHW output format have uniformity in the modules | |
last_hidden_state = tf.transpose(last_hidden_state, perm=(0, 3, 1, 2)) | |
last_hidden_state = encoder_outputs[0] | |
pooled_output = self.pooler(last_hidden_state) | |
# Change to NCHW output format have uniformity in the modules | |
last_hidden_state = tf.transpose(last_hidden_state, perm=(0, 3, 1, 2)) | |
pooled_output = tf.transpose(pooled_output, perm=(0, 3, 1, 2)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored. Thanks.
# Image classification docstring | ||
_IMAGE_CLASS_CHECKPOINT = "facebook/regnet-y-040" | ||
_IMAGE_CLASS_EXPECTED_OUTPUT = "'tabby, tabby cat'" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add TFRegNet to the doc tests, making sure all code examples are tested.
Details here: https://github.com/huggingface/transformers/tree/main/docs#testing-documentation-examples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
@sgugger @Rocketknight1 anything pending on my end to move this ahead? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good on my side!
Thank you!
This part is pending then. |
As soon as @ariG23498 performs a rebase the errors should go away (I can't since the forked repo's owner can only do that I guess). |
Hey folks! The hub PRs for most models are open (the exception is the 10b models, which I'm having a look at), you should be able to remove the |
Thanks, @gante! Keep us posted. |
@gante thanks for your hard work on getting the TF parameters of this model to Hub (there are a total of 32 checkpoints in case anyone's curious). I really appreciate this help! I have removed the occurrences of |
The failing tests seem to be unrelated to this PR? |
Yes, pytorch v1.12 was released a few hours ago and broke a few things here. We have pinned pytorch to <1.12 -- rebasing with |
@gante we performed a rebase but the |
That error seems unrelated to this PR, so I think you could probably merge anyway. |
Over to you @gante then since I can't merge :D |
The error is on |
* chore: initial commit Copied the torch implementation of regnets and porting the code to tf step by step. Also introduced an output layer which was needed for regnets. * chore: porting the rest of the modules to tensorflow did not change the documentation yet, yet to try the playground on the model * Fix initilizations (huggingface#1) * fix: code structure in few cases. * fix: code structure to align tf models. * fix: layer naming, bn layer still remains. * chore: change default epsilon and momentum in bn. * chore: styling nits. * fix: cross-loading bn params. * fix: regnet tf model, integration passing. * add: tests for TF regnet. * fix: code quality related issues. * chore: added rest of the files. * minor additions.. * fix: repo consistency. * fix: regnet tf tests. * chore: reorganize dummy_tf_objects for regnet. * chore: remove checkpoint var. * chore: remov unnecessary files. * chore: run make style. * Update docs/source/en/model_doc/regnet.mdx Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * chore: PR feedback I. * fix: pt test. thanks to @ydshieh. * New adaptive pooler (huggingface#3) * feat: new adaptive pooler Co-authored-by: @Rocketknight1 * chore: remove image_size argument. Co-authored-by: matt <rocketknight1@gmail.com> Co-authored-by: matt <rocketknight1@gmail.com> * Empty-Commit * chore: remove image_size comment. * chore: remove playground_tf.py * chore: minor changes related to spacing. * chore: make style. * Update src/transformers/models/regnet/modeling_tf_regnet.py Co-authored-by: amyeroberts <aeroberts4444@gmail.com> * Update src/transformers/models/regnet/modeling_tf_regnet.py Co-authored-by: amyeroberts <aeroberts4444@gmail.com> * chore: refactored __init__. * chore: copied from -> taken from./g * adaptive pool -> global avg pool, channel check. * chore: move channel check to stem. * pr comments - minor refactor and add regnets to doc tests. * Update src/transformers/models/regnet/modeling_tf_regnet.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * minor fix in the xlayer. * Empty-Commit * chore: removed from_pt=True. Co-authored-by: Sayak Paul <spsayakpaul@gmail.com> Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> Co-authored-by: matt <rocketknight1@gmail.com> Co-authored-by: amyeroberts <aeroberts4444@gmail.com> Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
In this PR in which we (/w @sayakpaul) are proting the RegNets model into TensorFlow.