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

test_predict.py 3.5 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  1. import os
  2. import unittest
  3. import tempfile
  4. from pathlib import Path
  5. from super_gradients.common.object_names import Models
  6. from super_gradients.training import models
  7. from super_gradients.training.datasets import COCODetectionDataset
  8. class TestModelPredict(unittest.TestCase):
  9. def setUp(self) -> None:
  10. rootdir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
  11. self.images = [
  12. os.path.join(rootdir, "documentation", "source", "images", "examples", "countryside.jpg"),
  13. os.path.join(rootdir, "documentation", "source", "images", "examples", "street_busy.jpg"),
  14. "https://deci-datasets-research.s3.amazonaws.com/image_samples/beatles-abbeyroad.jpg",
  15. ]
  16. self._set_images_with_targets()
  17. def _set_images_with_targets(self):
  18. mini_coco_data_dir = str(Path(__file__).parent.parent / "data" / "tinycoco")
  19. dataset = COCODetectionDataset(
  20. data_dir=mini_coco_data_dir, subdir="images/val2017", json_file="instances_val2017.json", input_dim=None, transforms=[], cache_annotations=False
  21. )
  22. # x's are np.ndarrays images of shape (H,W,3)
  23. # y's are np.ndarrays of shape (num_boxes,x1,y1,x2,y2,class_id)
  24. x1, y1, _ = dataset[0]
  25. x2, y2, _ = dataset[1]
  26. # images from COCODetectionDataset are RGB and images as np.ndarrays are expected to be BGR
  27. x2 = x2[:, :, ::-1]
  28. x1 = x1[:, :, ::-1]
  29. self.np_array_images = [x1, x2]
  30. self.np_array_target_bboxes = [y1[:, :4], y2[:, :4]]
  31. self.np_array_target_class_ids = [y1[:, 4], y2[:, 4]]
  32. def test_classification_models(self):
  33. with tempfile.TemporaryDirectory() as tmp_dirname:
  34. for model_name in {Models.RESNET18, Models.EFFICIENTNET_B0, Models.MOBILENET_V2, Models.REGNETY200}:
  35. model = models.get(model_name, pretrained_weights="imagenet")
  36. predictions = model.predict(self.images)
  37. predictions.show()
  38. predictions.save(output_folder=tmp_dirname)
  39. def test_pose_estimation_models(self):
  40. model = models.get(Models.DEKR_W32_NO_DC, pretrained_weights="coco_pose")
  41. with tempfile.TemporaryDirectory() as tmp_dirname:
  42. predictions = model.predict(self.images)
  43. predictions.show()
  44. predictions.save(output_folder=tmp_dirname)
  45. def test_detection_models(self):
  46. for model_name in [Models.YOLO_NAS_S, Models.YOLOX_S, Models.PP_YOLOE_S]:
  47. model = models.get(model_name, pretrained_weights="coco")
  48. with tempfile.TemporaryDirectory() as tmp_dirname:
  49. predictions = model.predict(self.images)
  50. predictions.show()
  51. predictions.save(output_folder=tmp_dirname)
  52. def test_detection_models_with_targets(self):
  53. for model_name in [Models.YOLO_NAS_S, Models.YOLOX_S, Models.PP_YOLOE_S]:
  54. model = models.get(model_name, pretrained_weights="coco")
  55. with tempfile.TemporaryDirectory() as tmp_dirname:
  56. predictions = model.predict(self.np_array_images)
  57. predictions.show(target_bboxes=self.np_array_target_bboxes, target_class_ids=self.np_array_target_class_ids, target_bboxes_format="xyxy")
  58. predictions.save(
  59. output_folder=tmp_dirname,
  60. target_bboxes=self.np_array_target_bboxes,
  61. target_class_ids=self.np_array_target_class_ids,
  62. target_bboxes_format="xyxy",
  63. )
  64. if __name__ == "__main__":
  65. unittest.main()
Tip!

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

Comments

Loading...