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

added deit onnx config #16887

Merged
merged 5 commits into from
Apr 25, 2022

Conversation

0xrushi
Copy link
Contributor

@0xrushi 0xrushi commented Apr 22, 2022

What does this PR do?

Added DeiT OnnxConfig to make this model available for conversion

@chainyo

#16308

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Apr 22, 2022

The documentation is not available anymore as the PR was closed or merged.

@@ -286,6 +287,7 @@ class FeaturesManager:
),
"vit": supported_features_mapping("default", "image-classification", onnx_config_cls=ViTOnnxConfig),
"beit": supported_features_mapping("default", "image-classification", onnx_config_cls=BeitOnnxConfig),
"deit": supported_features_mapping("default", "image-classification", onnx_config_cls=DeiTOnnxConfig),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that DeiT also have a DeiTForMaskedImageModeling implementation, is it a feature also available in onnx ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BeiT also has BeiTForMaskedImageModeling, but there isn't anything for that in features.py

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add something like

"masked-im": AutoModelForMaskedImageModeling

to the _TASKS_TO_AUTOMODELS attribute of the FeaturesManager. I don't think we currently support this feature for TensorFlow, so no need to include this in __TASKS_TO_TF_AUTOMODELS.

Feel free to then include this feature as part of vit, deit, and beit

@chainyo
Copy link
Contributor

chainyo commented Apr 22, 2022

What does this PR do?

Added DeiT OnnxConfig to make this model available for conversion

@chainyo

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR.

Also pinging @LysandreJik, @lewtun and @NielsRogge because he did the implementation of DeiT.

Btw Did you try to convert one DeiT model with your add ?
If so you could add the converted model to the ONNXConfig for all organization, it would be awesome!

Copy link
Member

@lewtun lewtun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding DEIT to the ONNX exporter @rushic24 ! Overall this looks great and I think it would be neat if we can also include the masked image modelling feature :)

@@ -286,6 +287,7 @@ class FeaturesManager:
),
"vit": supported_features_mapping("default", "image-classification", onnx_config_cls=ViTOnnxConfig),
"beit": supported_features_mapping("default", "image-classification", onnx_config_cls=BeitOnnxConfig),
"deit": supported_features_mapping("default", "image-classification", onnx_config_cls=DeiTOnnxConfig),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add something like

"masked-im": AutoModelForMaskedImageModeling

to the _TASKS_TO_AUTOMODELS attribute of the FeaturesManager. I don't think we currently support this feature for TensorFlow, so no need to include this in __TASKS_TO_TF_AUTOMODELS.

Feel free to then include this feature as part of vit, deit, and beit

@0xrushi
Copy link
Contributor Author

0xrushi commented Apr 22, 2022

Thanks @lewtun , I just added those.

I'm trying to add a README to ONNXConfigForAll,

How does onnx work in ViT?
I tried the code below, but its not working

from transformers import ViTFeatureExtractor, ViTModel
import torch
from datasets import load_dataset
from onnxruntime import InferenceSession


dataset = load_dataset("huggingface/cats-image")
image = dataset["test"]["image"][0]

feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224-in21k")
model = ViTModel.from_pretrained("google/vit-base-patch16-224-in21k")

inputs = feature_extractor(image, return_tensors="pt")

session = InferenceSession("onnx/model.onnx")

# ONNX Runtime expects NumPy arrays as input
outputs = session.run(output_names=["last_hidden_state"], input_feed=list(inputs))

@chainyo
Copy link
Contributor

chainyo commented Apr 22, 2022

How does onnx work in ViT?
I tried the code below, but its not working

Could you check on netron.app what are the inputs of the onnx converted model and then check if the input_feed is correct. Btw it should be a Dict not a List.

@0xrushi
Copy link
Contributor Author

0xrushi commented Apr 22, 2022

How does onnx work in ViT?
I tried the code below, but its not working

Could you check on netron.app what are the inputs of the onnx converted model and then check if the input_feed is correct. Btw it should be a Dict not a List.

And inputs itself is a dictionary
image

The input as seen from netron is pixel_values, I did try dict(inputs), inputs, list(inputs)

image

@0xrushi
Copy link
Contributor Author

0xrushi commented Apr 23, 2022

I just figured it out. You can find it here https://huggingface.co/OWG/DeiT :-)

@chainyo
Copy link
Contributor

chainyo commented Apr 25, 2022

I just figured it out. You can find it here https://huggingface.co/OWG/DeiT :-)

It seems really good! 🤗

Copy link
Member

@lewtun lewtun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for iterating on this @rushic24 and great job on pushing a model to the Hub 😍 !

This LGTM so gently pinging @sgugger for final approval

@@ -102,6 +104,7 @@ class FeaturesManager:
"multiple-choice": AutoModelForMultipleChoice,
"question-answering": AutoModelForQuestionAnswering,
"image-classification": AutoModelForImageClassification,
"masked-im": AutoModelForMaskedImageModeling,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: we picked masked-im as the name to match the pre-existing masked-lm used for language modeling

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. It all looks good but there is a datasets submodule added in this PR that should be removed before it can be merged.

@lewtun
Copy link
Member

lewtun commented Apr 25, 2022

Thanks for the fix!

@lewtun lewtun merged commit 8246caf into huggingface:main Apr 25, 2022
@0xrushi 0xrushi deleted the Add-DeiT-OnnxConfig-to-Transformers branch April 26, 2022 03:55
elusenji pushed a commit to elusenji/transformers that referenced this pull request Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants