Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

export_coreml_test.py 2.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  1. import os
  2. import tempfile
  3. import unittest
  4. from torchvision.transforms import Compose, Normalize, Resize
  5. from super_gradients.common.object_names import Models
  6. from super_gradients.training import models
  7. from super_gradients.training.transforms import Standardize
  8. class TestModelsCoreMLExport(unittest.TestCase):
  9. def test_models_onnx_export_with_explicit_input_size(self):
  10. pretrained_model = models.get(Models.RESNET18, num_classes=1000, pretrained_weights="imagenet")
  11. preprocess = Compose([Resize(224), Standardize(), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
  12. with tempfile.TemporaryDirectory() as tmpdirname:
  13. out_path = os.path.join(tmpdirname, "resnet18.mlmodel")
  14. models.convert_to_coreml(model=pretrained_model, out_path=out_path, input_size=(3, 256, 256), pre_process=preprocess)
  15. self.assertTrue(os.path.isfile(out_path))
  16. def test_models_onnx_export_without_explicit_input_size_raises_error(self):
  17. pretrained_model = models.get(Models.RESNET18, num_classes=1000, pretrained_weights="imagenet")
  18. preprocess = Compose([Resize(224), Standardize(), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
  19. with self.assertRaises(KeyError):
  20. models.convert_to_coreml(model=pretrained_model, out_path="some-output-path.coreml", pre_process=preprocess)
  21. def test_models_coreml_export(self, **export_kwargs):
  22. pretrained_model = models.get(Models.YOLO_NAS_S, num_classes=1000, pretrained_weights="coco")
  23. # Just for the sake of testing, not really COCO preprocessing
  24. preprocess = Compose([Resize(224), Standardize(), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
  25. with tempfile.TemporaryDirectory() as tmpdirname:
  26. out_path = os.path.join(tmpdirname, "yolo_nas_s")
  27. model_path = models.convert_to_coreml(
  28. model=pretrained_model,
  29. out_path=out_path,
  30. pre_process=preprocess,
  31. prep_model_for_conversion_kwargs=dict(input_size=(1, 3, 640, 640)),
  32. **export_kwargs,
  33. )
  34. if export_kwargs.get("export_as_ml_program"):
  35. # Expecting a directory
  36. self.assertTrue(os.path.isdir(model_path))
  37. self.assertTrue(model_path.endswith(".mlpackage"))
  38. else:
  39. # Expecting a single file
  40. self.assertTrue(os.path.isfile(model_path))
  41. self.assertTrue(model_path.endswith(".mlmodel"))
  42. def test_models_coreml_export_as_mlprogram(self):
  43. self.test_models_coreml_export(export_as_ml_program=True)
  44. if __name__ == "__main__":
  45. unittest.main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...