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.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 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 YoloPostPredictionCallback
  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})
  21. trainer = Trainer("visualization_test", device=self.device)
  22. post_prediction_callback = YoloPostPredictionCallback()
  23. # Simulate one iteration of validation subset
  24. batch_i, (imgs, targets) = 0, next(iter(valid_loader))
  25. imgs = core_utils.tensor_container_to_device(imgs, self.device)
  26. targets = core_utils.tensor_container_to_device(targets, self.device)
  27. output = self.model(imgs)
  28. output = post_prediction_callback(output)
  29. # Visualize the batch
  30. DetectionVisualization.visualize_batch(imgs, output, targets, batch_i, COCO_DETECTION_CLASSES_LIST, trainer.checkpoints_dir_path)
  31. # Assert images ware created and delete them
  32. img_name = "{}/{}_{}.jpg"
  33. for i in range(4):
  34. img_path = img_name.format(trainer.checkpoints_dir_path, batch_i, i)
  35. self.assertTrue(os.path.exists(img_path))
  36. os.remove(img_path)
  37. @unittest.skipIf(not is_data_available(), "run only when /data is available")
  38. def test_detection_metrics(self):
  39. valid_loader = coco2017_val(dataloader_params={"batch_size": 16})
  40. metrics = [
  41. DetectionMetrics(num_cls=80, post_prediction_callback=YoloPostPredictionCallback(), normalize_targets=True),
  42. DetectionMetrics_050(num_cls=80, post_prediction_callback=YoloPostPredictionCallback(), normalize_targets=True),
  43. DetectionMetrics(num_cls=80, post_prediction_callback=YoloPostPredictionCallback(conf=2), normalize_targets=True),
  44. ]
  45. ref_values = [
  46. np.array([0.24662896, 0.4024832, 0.34590888, 0.28435066]),
  47. np.array([0.34606069, 0.56745648, 0.50594932, 0.40323338]),
  48. np.array([0.0, 0.0, 0.0, 0.0]),
  49. ]
  50. for met, ref_val in zip(metrics, ref_values):
  51. met.reset()
  52. for i, (imgs, targets) in enumerate(valid_loader):
  53. if i > 5:
  54. break
  55. imgs = core_utils.tensor_container_to_device(imgs, self.device)
  56. targets = core_utils.tensor_container_to_device(targets, self.device)
  57. output = self.model(imgs)
  58. met.update(output, targets, device=self.device, inputs=imgs)
  59. results = met.compute()
  60. values = np.array([x.item() for x in list(results.values())])
  61. self.assertTrue(np.allclose(values, ref_val))
  62. if __name__ == "__main__":
  63. unittest.main()
Tip!

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

Comments

Loading...