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

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

Comments

Loading...