-
Notifications
You must be signed in to change notification settings - Fork 502
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
Add the ORTModelForSemanticSegmentation class #539
Add the ORTModelForSemanticSegmentation class #539
Conversation
The documentation is not available anymore as the PR was closed or merged. |
Hi @TheoMrc, Thanks for opening the PR, it looks great!! Just some questions for other team members before jumping into a more detailed review:
(@TheoMrc for the IOBinding, ONNX Runtime supports binding with And for your question on the GPU test, if your CUDA env is correctly set, ensure that you have uninstalled |
I think the class it is supporting is |
Hi, We'll deprecate the xxxForImageSegmentation class in Transformers, as we now split them up into The pipeline however is just called "image segmentation", as it unifies all 3 into a single abstraction. |
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.
LGTM, great work @TheoMrc !
Should wait for @NielsRogge and @JingyaHuang approvals as well.
tests/onnxruntime/test_modeling.py
Outdated
@@ -1277,6 +1278,117 @@ def test_compare_to_io_binding(self, *args, **kwargs): | |||
gc.collect() | |||
|
|||
|
|||
class ORTModelForImageSegmentationIntegrationTest(unittest.TestCase): | |||
SUPPORTED_ARCHITECTURES_WITH_MODEL_ID = { | |||
"vit": "hf-internal-testing/tiny-random-vit", # Probably have to modify to an onnx segmentation model |
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.
No need to modify
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.
Hmmm
I'm wondering why though ?? Tests are dying on my pc
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.
VIT doesn't have segmentation model implemented, let's replace it by Segformer
tests/onnxruntime/test_modeling.py
Outdated
@@ -1277,6 +1278,117 @@ def test_compare_to_io_binding(self, *args, **kwargs): | |||
gc.collect() | |||
|
|||
|
|||
class ORTModelForImageSegmentationIntegrationTest(unittest.TestCase): |
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.
It seems to be similar to ORTModelForImageClassification
, maybe we could abstract all of these test with a base test class, can be done in another PR
Thanks for your feedbacks !
Actually, last time I tried, loading my segformer with AutoModel only worked with AutoModelForSemanticSegmentation and not with AutoModelForImageSegmentation. I don't know how relevant this is. That is why I put AutoModelForSemanticSegmentation as auto_model_class in ORTModelForImageSegmentation. Seems weird to me that there are two classes, that are supposed to do the same thing but don't behave the same, maybe XXXForImageSegmentation is for binary outputs ? |
I think yes, to align with Transformers. |
I believe we should probably change the model name to a segmentation model in the test class. And I will remove comments from the code soon |
Seeing this for the output shape, it seems that the output logits' shape would be |
@JingyaHuang |
Hi @TheoMrc, I just merged the support for dynamic shape output IOBinding. Can you update the IOBinding part in your PR like the way done in optimum/optimum/onnxruntime/modeling_ort.py Line 1559 in 8b559db
And maybe try to fix the style with
to pass the code quality check, thank you! |
Hi @JingyaHuang, yes that's correct. |
Hi @JingyaHuang, Gonna do it soon. To clarify what I should do, you're asking me to copy-paste all functions linked to io_binding from ORTModelForCustomTasks ? Shouldn't we just make ORTForSemanticSegmentation inherit from ORTModelForCustomTasks instead of ORTModel ? We should then also add a class attribute which stores in cls.OutputClass = SemanticSegmenterOutput.
Not sure how to do such a thing, this looks like a linux command (although I couldn't find anything about it) |
Hi @TheoMrc, It is a great point to avoid copying the method for preparing dynamic shape IOBinding. For structure consistency, what I suggest to do, is to move the I will open a PR now to make the move. And for the styling, alternatively, you can pip install black and isort to do the formatting. |
Hi @TheoMrc, the dynamic IOBinding preparation is moved to the
You can use it for the I/O binding of ORTModelForSemanticSegmentation , feel free to ping me if you need help.
|
e19918b
to
ff63f5a
Compare
Hi again @JingyaHuang !
Once again, despite a working CUDA v11.7 env for torch, I did not manage to make
Additional information about GPU ORT not working:
I think this has to do with the fact that I don't have a typical optimum installation but just cloned the branch locally, installed dependencies with pycharm and venv, which do weird stuff from time to time.
Obviously, we should test GPU inference.
What do you think is left to do ? |
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.
Hey @TheoMrc, thanks for the update.
Just left some nits.
tests/onnxruntime/test_modeling.py
Outdated
@@ -1277,6 +1278,117 @@ def test_compare_to_io_binding(self, *args, **kwargs): | |||
gc.collect() | |||
|
|||
|
|||
class ORTModelForImageSegmentationIntegrationTest(unittest.TestCase): | |||
SUPPORTED_ARCHITECTURES_WITH_MODEL_ID = { | |||
"vit": "hf-internal-testing/tiny-random-vit", # Probably have to modify to an onnx segmentation model |
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.
VIT doesn't have segmentation model implemented, let's replace it by Segformer
@TheoMrc, thanks for updating the code! I just tested on my end with the following snippet: import torch
from transformers import SegformerImageProcessor, pipeline
from PIL import Image
import requests
from optimum.onnxruntime import ORTModelForSemanticSegmentation
device = torch.device("cuda:0")
image_processor = SegformerImageProcessor.from_pretrained("optimum/segformer-b0-finetuned-ade-512-512")
model = ORTModelForSemanticSegmentation.from_pretrained("optimum/segformer-b0-finetuned-ade-512-512")
model.to(device)
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
inputs = image_processor(images=image, return_tensors="pt").to(device)
out = model(**inputs)
pipe = pipeline("image-segmentation", model=model, feature_extractor=image_processor)
pred = pipe(url) It is working decently both on CPU and on GPU. For failing tests:
|
Co-authored-by: Jingya HUANG <44135271+JingyaHuang@users.noreply.github.com>
Hi @JingyaHuang, Thanks for the code review ! I believe I did all the requested changes. I had to add All tests are running smooth locally (appart from the GPU one). Hopefully everything will be good with the next PR run :) I'm glad I could contribute and learn a bunch of things on the way ! See you, Theo |
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.
Hi @TheoMrc, the PR looks great, thanks for iterating and contributing to Optimum!
The exporter currently has some breaking changes, the tests for the exporter are irrelevant to this PR, we can merge it after all other tests pass.
Hi @TheoMrc, thank you for your contribution! I am running into the same issue when running GPU inference.
I made sure that:
Did you manage to make it work on GPU? System information
|
Hi @allankouidri, I did not get to try setting up a clean venv to try GPU inference with ONNX. Since it works fine for @JingyaHuang, I think maybe it could be a matter of OS, since we are both on windows ? Our issue probably has to do with the method used to detect onnxruntime vs onnxruntime-gpu. I'll try a few things when I have the time and come back to you |
Hi @allankouidri, Try doing this: pip uninstall onnxruntime
pip uninstall onnxruntime-gpu
pip install onnxruntime-gpu |
What does this PR do?
This PR aims to implement the ORTModelForImageSegmentation class to provide support for image segmentation .onnx models, and full integration of such models through transformers pipelines for CPU or GPU onnxruntime inference (see Issue #382)
Implementation details
The ORTModelForImageSegmentation was based on the already implemented ORTModelForImageClassification in
optimum/onnxruntime/modeling_ort.py
with several modifications:optimum/onnxruntime/__init__.py
self.forward
method returns aSemanticSegmenterOutput
instead ofImageClassifierOutput
auto_model_class
andexport_feature
referencedORTModelForImageClassificationIntegrationTest
intests/onnxruntime/test_modeling.py
ORTModelForImageSegmentation.prepare_logits_buffer
to return a 4 dimensional tensor shape 2D of shape(input_batch_size, self.config.num_labels, output_height, output_width)
.The issue is that I did not find a way to get model output size, which is different from input size from config.json, or any other attribute of ORTModelForImageSegmentation or ORTModelForImageSegmentation.model.
CPU inference works as following:
I could not test GPU inference because I could not manage to make onnxruntime-gpu work:
Might be because of local venv setup issues on my side.
My CUDA installation is working for transformers with torch models.
Still, it probably would not work properly yet because of the wrong output size in
prepare_logits_buffer
Remaining tasks
IMAGE_SEGMENTATION_EXAMPLE
checkpoint name and image url to appropriate example. (See two comments at optimum/onnxruntime/modeling_ort.py lines 1463 and 1533)@michaelbenayoun @JingyaHuang your help would be appreciated 👍