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

detection_utils_test.py 3.6 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 numpy as np
  4. import torch.cuda
  5. from super_gradients.common.object_names import Models
  6. from super_gradients.training import Trainer, utils as core_utils, models
  7. from super_gradients.training.dataloaders.dataloaders import coco2017_val
  8. from super_gradients.training.datasets.datasets_conf import COCO_DETECTION_CLASSES_LIST
  9. from super_gradients.training.metrics import DetectionMetrics, DetectionMetrics_050
  10. from super_gradients.training.models.detection_models.yolo_base import YoloXPostPredictionCallback
  11. from super_gradients.training.utils.detection_utils import DetectionVisualization
  12. from tests.core_test_utils import is_data_available
  13. class TestDetectionUtils(unittest.TestCase):
  14. def setUp(self):
  15. self.device = "cuda" if torch.cuda.is_available() else "cpu"
  16. self.model = models.get(Models.YOLOX_N, pretrained_weights="coco").to(self.device)
  17. self.model.eval()
  18. @unittest.skipIf(not is_data_available(), "run only when /data is available")
  19. def test_visualization(self):
  20. valid_loader = coco2017_val(dataloader_params={"batch_size": 16, "num_workers": 0})
  21. trainer = Trainer("visualization_test")
  22. post_prediction_callback = YoloXPostPredictionCallback()
  23. # Simulate one iteration of validation subset
  24. batch_i, batch = 0, next(iter(valid_loader))
  25. imgs, targets = batch[:2]
  26. imgs = core_utils.tensor_container_to_device(imgs, self.device)
  27. targets = core_utils.tensor_container_to_device(targets, self.device)
  28. output = self.model(imgs)
  29. output = post_prediction_callback(output)
  30. # Visualize the batch
  31. DetectionVisualization.visualize_batch(imgs, output, targets, batch_i, COCO_DETECTION_CLASSES_LIST, trainer.checkpoints_dir_path)
  32. # Assert images ware created and delete them
  33. img_name = "{}/{}_{}.jpg"
  34. for i in range(4):
  35. img_path = img_name.format(trainer.checkpoints_dir_path, batch_i, i)
  36. self.assertTrue(os.path.exists(img_path))
  37. os.remove(img_path)
  38. @unittest.skipIf(not is_data_available(), "run only when /data is available")
  39. def test_detection_metrics(self):
  40. valid_loader = coco2017_val(dataloader_params={"batch_size": 16, "num_workers": 0})
  41. metrics = [
  42. DetectionMetrics(num_cls=80, post_prediction_callback=YoloXPostPredictionCallback(), normalize_targets=True),
  43. DetectionMetrics_050(num_cls=80, post_prediction_callback=YoloXPostPredictionCallback(), normalize_targets=True),
  44. DetectionMetrics(num_cls=80, post_prediction_callback=YoloXPostPredictionCallback(conf=2), normalize_targets=True),
  45. ]
  46. ref_values = [
  47. np.array([0.24701539, 0.40294355, 0.34654024, 0.28485271]),
  48. np.array([0.34666198, 0.56854934, 0.5079478, 0.40414381]),
  49. np.array([0.0, 0.0, 0.0, 0.0]),
  50. ]
  51. for met, ref_val in zip(metrics, ref_values):
  52. met.reset()
  53. for i, (imgs, targets, extras) in enumerate(valid_loader):
  54. if i > 5:
  55. break
  56. imgs = core_utils.tensor_container_to_device(imgs, self.device)
  57. targets = core_utils.tensor_container_to_device(targets, self.device)
  58. output = self.model(imgs)
  59. met.update(output, targets, device=self.device, inputs=imgs)
  60. results = met.compute()
  61. values = np.array([x.item() for x in list(results.values())])
  62. self.assertTrue(np.allclose(values, ref_val, rtol=1e-3, atol=1e-4))
  63. if __name__ == "__main__":
  64. unittest.main()
Tip!

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

Comments

Loading...