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

yolox_unit_test.py 4.3 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
  1. import os
  2. import tempfile
  3. import unittest
  4. import torch
  5. from super_gradients.common.object_names import Models
  6. from super_gradients.training import models
  7. from super_gradients.training.losses import YoloXDetectionLoss, YoloXFastDetectionLoss
  8. from super_gradients.training.models.detection_models.yolox import YoloX_N, YoloX_T, YoloX_S, YoloX_M, YoloX_L, YoloX_X
  9. from super_gradients.training.utils.detection_utils import DetectionCollateFN
  10. from super_gradients.training.utils.utils import HpmStruct
  11. class TestYOLOX(unittest.TestCase):
  12. def setUp(self) -> None:
  13. self.arch_params = HpmStruct(num_classes=10)
  14. self.yolo_classes = [YoloX_N, YoloX_T, YoloX_S, YoloX_M, YoloX_L, YoloX_X]
  15. self.devices = ["cpu", "cuda"] if torch.cuda.is_available() else ["cpu"]
  16. def test_yolox_creation(self):
  17. """
  18. test_yolox_creation - Tests the creation of the models
  19. :return:
  20. """
  21. for device in self.devices:
  22. dummy_input = torch.randn(1, 3, 320, 320).to(device)
  23. with torch.no_grad():
  24. for yolo_cls in self.yolo_classes:
  25. yolo_model = yolo_cls(self.arch_params).to(device)
  26. # THIS SHOULD RUN THE FORWARD ONCE
  27. yolo_model.eval()
  28. output_standard = yolo_model(dummy_input)
  29. self.assertIsNotNone(output_standard)
  30. # THIS SHOULD RUN A TRAINING FORWARD
  31. yolo_model.train()
  32. output_train = yolo_model(dummy_input)
  33. self.assertIsNotNone(output_train)
  34. # THIS SHOULD RUN THE FORWARD AUGMENT
  35. yolo_model.eval()
  36. yolo_model.augmented_inference = True
  37. output_augment = yolo_model(dummy_input)
  38. self.assertIsNotNone(output_augment)
  39. def test_yolox_loss(self):
  40. samples = [
  41. (torch.zeros((3, 256, 256)), torch.zeros((100, 5))),
  42. (torch.zeros((3, 256, 256)), torch.zeros((100, 5))),
  43. (torch.zeros((3, 256, 256)), torch.zeros((100, 5))),
  44. (torch.zeros((3, 256, 256)), torch.zeros((100, 5))),
  45. (torch.zeros((3, 256, 256)), torch.zeros((100, 5))),
  46. ]
  47. collate = DetectionCollateFN()
  48. _, targets = collate(samples)
  49. for device in self.devices:
  50. predictions = [
  51. torch.randn((5, 1, 256 // 8, 256 // 8, 4 + 1 + 10)).to(device),
  52. torch.randn((5, 1, 256 // 16, 256 // 16, 4 + 1 + 10)).to(device),
  53. torch.randn((5, 1, 256 // 32, 256 // 32, 4 + 1 + 10)).to(device),
  54. ]
  55. for loss in [
  56. YoloXDetectionLoss(strides=[8, 16, 32], num_classes=10, use_l1=True, iou_type="giou"),
  57. YoloXDetectionLoss(strides=[8, 16, 32], num_classes=10, use_l1=True, iou_type="iou"),
  58. YoloXDetectionLoss(strides=[8, 16, 32], num_classes=10, use_l1=False),
  59. YoloXFastDetectionLoss(strides=[8, 16, 32], num_classes=10, use_l1=True),
  60. YoloXFastDetectionLoss(strides=[8, 16, 32], num_classes=10, use_l1=False),
  61. ]:
  62. result = loss(predictions, targets.to(device))
  63. print(result)
  64. def test_yolo_x_checkpoint_solver(self):
  65. """
  66. This test checks whether we can:
  67. 1. load an old pretrained weights for YoloX that has non-matching keys (Using custom solver under the hood).
  68. 2. load a regular checkpoint (As if one would train a model from scratch).
  69. 3. that both models produce the same output.
  70. :return:
  71. """
  72. model_variant = [Models.YOLOX_S, Models.YOLOX_M, Models.YOLOX_L, Models.YOLOX_T, Models.YOLOX_N]
  73. for model_name in model_variant:
  74. model = models.get(model_name, pretrained_weights="coco").eval()
  75. input = torch.randn((1, 3, 320, 320))
  76. output1 = model(input)
  77. sd = model.state_dict()
  78. with tempfile.TemporaryDirectory() as tmp_dirname:
  79. path = os.path.join(tmp_dirname, f"{model_name}_coco.pth")
  80. torch.save({"net": sd}, path)
  81. model = models.get(model_name, num_classes=80, checkpoint_path=path).eval()
  82. output2 = model(input)
  83. assert torch.allclose(output1[0], output2[0], atol=1e-4)
  84. if __name__ == "__main__":
  85. unittest.main()
Tip!

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

Comments

Loading...