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

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

Comments

Loading...