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 4.8 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  1. import os
  2. import unittest
  3. import tempfile
  4. from pathlib import Path
  5. import numpy as np
  6. from super_gradients.common.object_names import Models
  7. from super_gradients.training import models
  8. from super_gradients.training.datasets import COCODetectionDataset
  9. class TestModelPredict(unittest.TestCase):
  10. def setUp(self) -> None:
  11. rootdir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
  12. self.images = [
  13. os.path.join(rootdir, "documentation", "source", "images", "examples", "countryside.jpg"),
  14. os.path.join(rootdir, "documentation", "source", "images", "examples", "street_busy.jpg"),
  15. "https://deci-datasets-research.s3.amazonaws.com/image_samples/beatles-abbeyroad.jpg",
  16. ]
  17. self._set_images_with_targets()
  18. def _set_images_with_targets(self):
  19. mini_coco_data_dir = str(Path(__file__).parent.parent / "data" / "tinycoco")
  20. dataset = COCODetectionDataset(
  21. data_dir=mini_coco_data_dir, subdir="images/val2017", json_file="instances_val2017.json", input_dim=None, transforms=[], cache_annotations=False
  22. )
  23. # x's are np.ndarrays images of shape (H,W,3)
  24. # y's are np.ndarrays of shape (num_boxes,x1,y1,x2,y2,class_id)
  25. x1, y1, _ = dataset[0]
  26. x2, y2, _ = dataset[1]
  27. # images from COCODetectionDataset are RGB and images as np.ndarrays are expected to be BGR
  28. x2 = x2[:, :, ::-1]
  29. x1 = x1[:, :, ::-1]
  30. self.np_array_images = [x1, x2]
  31. self.np_array_target_bboxes = [y1[:, :4], y2[:, :4]]
  32. self.np_array_target_class_ids = [y1[:, 4], y2[:, 4]]
  33. def test_classification_models(self):
  34. with tempfile.TemporaryDirectory() as tmp_dirname:
  35. for model_name in {Models.RESNET18, Models.EFFICIENTNET_B0, Models.MOBILENET_V2, Models.REGNETY200}:
  36. model = models.get(model_name, pretrained_weights="imagenet")
  37. predictions = model.predict(self.images)
  38. predictions.show()
  39. predictions.save(output_folder=tmp_dirname)
  40. def test_pose_estimation_models(self):
  41. model = models.get(Models.DEKR_W32_NO_DC, pretrained_weights="coco_pose")
  42. with tempfile.TemporaryDirectory() as tmp_dirname:
  43. predictions = model.predict(self.images)
  44. predictions.show()
  45. predictions.save(output_folder=tmp_dirname)
  46. def test_detection_models(self):
  47. for model_name in [Models.YOLO_NAS_S, Models.YOLOX_S, Models.PP_YOLOE_S]:
  48. model = models.get(model_name, pretrained_weights="coco")
  49. with tempfile.TemporaryDirectory() as tmp_dirname:
  50. predictions = model.predict(self.images)
  51. predictions.show()
  52. predictions.save(output_folder=tmp_dirname)
  53. def test_detection_models_with_targets(self):
  54. for model_name in [Models.YOLO_NAS_S, Models.YOLOX_S, Models.PP_YOLOE_S]:
  55. model = models.get(model_name, pretrained_weights="coco")
  56. with tempfile.TemporaryDirectory() as tmp_dirname:
  57. predictions = model.predict(self.np_array_images)
  58. predictions.show(target_bboxes=self.np_array_target_bboxes, target_class_ids=self.np_array_target_class_ids, target_bboxes_format="xyxy")
  59. predictions.save(
  60. output_folder=tmp_dirname,
  61. target_bboxes=self.np_array_target_bboxes,
  62. target_class_ids=self.np_array_target_class_ids,
  63. target_bboxes_format="xyxy",
  64. )
  65. def test_predict_class_names(self):
  66. for model_name in [Models.YOLO_NAS_S, Models.YOLOX_S, Models.PP_YOLOE_S]:
  67. model = models.get(model_name, pretrained_weights="coco")
  68. predictions = model.predict(self.np_array_images)
  69. _ = predictions.show(class_names=["person", "bicycle", "car", "motorcycle", "airplane", "bus"])
  70. with self.assertRaises(ValueError):
  71. _ = predictions.show(class_names=["human"])
  72. def test_predict_detection_skip_resize(self):
  73. for model_name in [Models.YOLO_NAS_S, Models.YOLOX_S, Models.PP_YOLOE_S]:
  74. model = models.get(model_name, pretrained_weights="coco")
  75. pipeline = model._get_pipeline(skip_image_resizing=True)
  76. dummy_images = [np.random.random((21, 21, 3)), np.random.random((21, 32, 3)), np.random.random((640, 640, 3))]
  77. expected_preprocessing_shape = [(3, 32, 32), (3, 32, 32), (3, 640, 640)]
  78. for image, expected_shape in zip(dummy_images, expected_preprocessing_shape):
  79. pred = model.predict(image, skip_image_resizing=True)[0]
  80. self.assertEqual(image.shape, pred.draw().shape)
  81. preprocessed_shape = pipeline.image_processor.preprocess_image(image)[0].shape
  82. self.assertEqual(preprocessed_shape, expected_shape)
  83. if __name__ == "__main__":
  84. unittest.main()
Tip!

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

Comments

Loading...