-
Notifications
You must be signed in to change notification settings - Fork 666
Add the ORTModelForSemanticSegmentation class #539
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
Changes from 2 commits
27e62e6
b2e6fc6
e59fa8a
36b4c6b
1dcf8c1
ad6300d
ff63f5a
29ef614
4bc1bef
47e8546
f7dc114
e21e721
f144ae3
f765bfc
c6420da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ | |
| ORTModelForCustomTasks, | ||
| ORTModelForFeatureExtraction, | ||
| ORTModelForImageClassification, | ||
| ORTModelForImageSegmentation, | ||
| ORTModelForMultipleChoice, | ||
| ORTModelForQuestionAnswering, | ||
| ORTModelForSeq2SeqLM, | ||
|
|
@@ -1277,6 +1278,117 @@ def test_compare_to_io_binding(self, *args, **kwargs): | |
| gc.collect() | ||
|
|
||
|
|
||
| class ORTModelForImageSegmentationIntegrationTest(unittest.TestCase): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to be similar to |
||
| SUPPORTED_ARCHITECTURES_WITH_MODEL_ID = { | ||
| "vit": "hf-internal-testing/tiny-random-vit", # Probably have to modify to an onnx segmentation model | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to modify
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
|
|
||
| def test_load_vanilla_transformers_which_is_not_supported(self): | ||
| with self.assertRaises(Exception) as context: | ||
| _ = ORTModelForImageSegmentation.from_pretrained(MODEL_NAMES["t5"], from_transformers=True) | ||
|
|
||
| self.assertIn("Unrecognized configuration class", str(context.exception)) | ||
|
|
||
| @parameterized.expand(SUPPORTED_ARCHITECTURES_WITH_MODEL_ID.items()) | ||
| def test_compare_to_transformers(self, *args, **kwargs): | ||
| model_arch, model_id = args | ||
| set_seed(SEED) | ||
| onnx_model = ORTModelForImageSegmentation.from_pretrained(model_id, from_transformers=True) | ||
|
|
||
| self.assertIsInstance(onnx_model.model, onnxruntime.capi.onnxruntime_inference_collection.InferenceSession) | ||
| self.assertIsInstance(onnx_model.config, PretrainedConfig) | ||
|
|
||
| set_seed(SEED) | ||
| trfs_model = AutoModelForImageClassification.from_pretrained(model_id) | ||
|
TheoMrc marked this conversation as resolved.
Outdated
|
||
| preprocessor = get_preprocessor(model_id) | ||
| url = "http://images.cocodataset.org/val2017/000000039769.jpg" | ||
| image = Image.open(requests.get(url, stream=True).raw) | ||
| inputs = preprocessor(images=image, return_tensors="pt") | ||
| onnx_outputs = onnx_model(**inputs) | ||
|
|
||
| self.assertTrue("logits" in onnx_outputs) | ||
| self.assertTrue(isinstance(onnx_outputs.logits, torch.Tensor)) | ||
|
|
||
| with torch.no_grad(): | ||
| trtfs_outputs = trfs_model(**inputs) | ||
|
|
||
| # compare tensor outputs | ||
| self.assertTrue(torch.allclose(onnx_outputs.logits, trtfs_outputs.logits, atol=1e-4)) | ||
|
|
||
| gc.collect() | ||
|
|
||
| @parameterized.expand(SUPPORTED_ARCHITECTURES_WITH_MODEL_ID.items()) | ||
| def test_pipeline_ort_model(self, *args, **kwargs): | ||
| model_arch, model_id = args | ||
| onnx_model = ORTModelForImageSegmentation.from_pretrained(model_id, from_transformers=True) | ||
| preprocessor = get_preprocessor(model_id) | ||
| pipe = pipeline("image-classification", model=onnx_model, feature_extractor=preprocessor) | ||
|
TheoMrc marked this conversation as resolved.
Outdated
|
||
| url = "http://images.cocodataset.org/val2017/000000039769.jpg" | ||
| outputs = pipe(url) | ||
|
|
||
| self.assertEqual(pipe.device, onnx_model.device) | ||
| self.assertGreaterEqual(outputs[0]["score"], 0.0) | ||
| self.assertTrue(isinstance(outputs[0]["label"], str)) | ||
|
|
||
| gc.collect() | ||
|
|
||
| @pytest.mark.run_in_series | ||
| def test_pipeline_model_is_none(self): | ||
| pipe = pipeline("image-segmentation") | ||
| url = "http://images.cocodataset.org/val2017/000000039769.jpg" | ||
| outputs = pipe(url) | ||
|
|
||
| # compare model output class | ||
| self.assertGreaterEqual(outputs[0]["score"], 0.0) | ||
| self.assertTrue(isinstance(outputs[0]["label"], str)) | ||
|
|
||
| @parameterized.expand(SUPPORTED_ARCHITECTURES_WITH_MODEL_ID.items()) | ||
| @require_torch_gpu | ||
| def test_pipeline_on_gpu(self, *args, **kwargs): | ||
| model_arch, model_id = args | ||
| onnx_model = ORTModelForImageSegmentation.from_pretrained(model_id, from_transformers=True) | ||
| preprocessor = get_preprocessor(model_id) | ||
| pipe = pipeline("image-classification", model=onnx_model, feature_extractor=preprocessor, device=0) | ||
|
TheoMrc marked this conversation as resolved.
Outdated
|
||
| url = "http://images.cocodataset.org/val2017/000000039769.jpg" | ||
| outputs = pipe(url) | ||
| # check model device | ||
| self.assertEqual(pipe.model.device.type.lower(), "cuda") | ||
|
|
||
| # compare model output class | ||
| self.assertGreaterEqual(outputs[0]["score"], 0.0) | ||
| self.assertTrue(isinstance(outputs[0]["label"], str)) | ||
|
|
||
| gc.collect() | ||
|
|
||
| @parameterized.expand(SUPPORTED_ARCHITECTURES_WITH_MODEL_ID.items()) | ||
| @require_torch_gpu | ||
| def test_compare_to_io_binding(self, *args, **kwargs): | ||
| model_arch, model_id = args | ||
| set_seed(SEED) | ||
| onnx_model = ORTModelForImageSegmentation.from_pretrained( | ||
| model_id, from_transformers=True, use_io_binding=False | ||
| ) | ||
| set_seed(SEED) | ||
| io_model = ORTModelForImageSegmentation.from_pretrained( | ||
| model_id, from_transformers=True, use_io_binding=True | ||
| ) | ||
|
|
||
| preprocessor = get_preprocessor(model_id) | ||
| url = "http://images.cocodataset.org/val2017/000000039769.jpg" | ||
| image = Image.open(requests.get(url, stream=True).raw) | ||
| inputs = preprocessor(images=[image] * 2, return_tensors="pt") | ||
| onnx_outputs = onnx_model(**inputs) | ||
| io_outputs = io_model(**inputs) | ||
|
|
||
| self.assertTrue("logits" in io_outputs) | ||
| self.assertIsInstance(io_outputs.logits, torch.Tensor) | ||
|
|
||
| # compare tensor outputs | ||
| self.assertTrue(torch.equal(onnx_outputs.logits, io_outputs.logits)) | ||
|
|
||
| gc.collect() | ||
|
|
||
|
|
||
| class ORTModelForSeq2SeqLMIntegrationTest(unittest.TestCase): | ||
| SUPPORTED_ARCHITECTURES = ( | ||
| "t5", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.