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

#669 Hotfix/sg 645 regression tests essential fixes

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:hotfix/SG-645_limit_tests_forward_passes
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  1. import torch
  2. import torch.nn as nn
  3. import unittest
  4. from super_gradients.training.models.classification_models.regnet import (
  5. CustomRegNet,
  6. NASRegNet,
  7. RegNetY200,
  8. RegNetY400,
  9. RegNetY600,
  10. RegNetY800,
  11. Stem,
  12. Stage,
  13. XBlock,
  14. )
  15. from super_gradients.training.utils.utils import HpmStruct
  16. class TestRegnet(unittest.TestCase):
  17. @classmethod
  18. def setUp(cls):
  19. cls.arch_params = HpmStruct(**{"num_classes": 1000})
  20. @staticmethod
  21. def verify_2_archs_are_identical(model_1: nn.Module, model_2: nn.Module):
  22. state_dict_1 = model_1.state_dict()
  23. model_2.load_state_dict(state_dict_1, strict=False)
  24. def test_custom_and_nas_regnet_can_build_regnetY200(self):
  25. """Test that when build Nas Regnet and Custom Regnet with the correct params - they build RegnetY200"""
  26. regnet_y_200 = RegNetY200(arch_params=self.arch_params)
  27. # Parameters identical to regnet_y_200
  28. nas_regnet = NASRegNet(arch_params=HpmStruct(**{"structure": [24, 36, 2.5, 13, 1, 8, 2, 4], "num_classes": 1000}))
  29. regnet_y_200_arch_params = {
  30. "initial_width": 24,
  31. "slope": 36,
  32. "quantized_param": 2.5,
  33. "network_depth": 13,
  34. "bottleneck_ratio": 1,
  35. "group_width": 8,
  36. "stride": 2,
  37. "num_classes": 1000,
  38. }
  39. custom_regnet = CustomRegNet(arch_params=HpmStruct(**regnet_y_200_arch_params))
  40. self.verify_2_archs_are_identical(regnet_y_200, nas_regnet)
  41. self.verify_2_archs_are_identical(regnet_y_200, custom_regnet)
  42. def test_regnet_model_creation(self):
  43. """
  44. Tests that the basic Regnets can be created
  45. """
  46. dummy_input = torch.randn(1, 3, 224, 224)
  47. regnet_y_200 = RegNetY200(arch_params=self.arch_params)
  48. regnet_y_400 = RegNetY400(arch_params=self.arch_params)
  49. regnet_y_600 = RegNetY600(arch_params=self.arch_params)
  50. regnet_y_800 = RegNetY800(arch_params=self.arch_params)
  51. with torch.no_grad():
  52. for model in [regnet_y_200, regnet_y_400, regnet_y_600, regnet_y_800]:
  53. output = model(dummy_input)
  54. self.assertIsNotNone(output)
  55. def test_dropout_forward_backward(self):
  56. """
  57. Test that output is stochastic in training and is fixed in eval with Dropout.
  58. """
  59. arch_params = HpmStruct(**{"num_classes": 1000, "dropout_prob": 0.3})
  60. model = RegNetY200(arch_params=arch_params)
  61. dummy_input = torch.randn(1, 3, 224, 224)
  62. model.train()
  63. self.assertFalse(torch.equal(model(dummy_input), model(dummy_input)))
  64. model.eval()
  65. self.assertTrue(torch.equal(model(dummy_input), model(dummy_input)))
  66. def test_droppath_forward_backward(self):
  67. """
  68. Test that output is stochastic in training and is fixed in eval with DropPath.
  69. """
  70. arch_params = HpmStruct(**{"num_classes": 1000, "droppath_prob": 0.2})
  71. model = RegNetY200(arch_params=arch_params)
  72. dummy_input = torch.randn(1, 3, 224, 224)
  73. model.train()
  74. self.assertFalse(torch.equal(model(dummy_input), model(dummy_input)))
  75. model.eval()
  76. self.assertTrue(torch.equal(model(dummy_input), model(dummy_input)))
  77. def test_nas_regnet_logic_is_backward_competible(self):
  78. """
  79. Runs several configurations of CustomRegnet models and validates that the logic wasn't change in the Regnet class
  80. This is important in order to reproduce previous Deci models and be backward competible
  81. """
  82. # THE LIST CONSISTS SEVERAL CUSTOM REGNET "ENCODINGS" AND THE CORRESPONDING XBLOCK STRUCTURE OF THE MODEL
  83. selected_arch_and_corresponding_configs = [
  84. {"struct": [56, 10, 2.2, 8, 2, 8, 2, 0], "expected_config": [3, 32, 32, 32, 16, 16, 16, 2, (2, 2), None, 32, 32, 32, 32, 4, (2, 2), None]},
  85. {"struct": [56, 10, 2.3, 11, 1, 1, 3, 0], "expected_config": [3, 32, 32, 32, 56, 56, 56, 56, (3, 3), None, 56, 128, 128, 128, 128, (3, 3), None]},
  86. {
  87. "struct": [70, 20, 2.6, 13, 0.5, 16, 2, 4],
  88. "expected_config": [
  89. 3,
  90. 32,
  91. 32,
  92. 32,
  93. 288,
  94. 288,
  95. 288,
  96. 18,
  97. (2, 2),
  98. nn.Module,
  99. 144,
  100. 736,
  101. 736,
  102. 736,
  103. 46,
  104. (2, 2),
  105. nn.Module,
  106. 368,
  107. 1888,
  108. 1888,
  109. 1888,
  110. 118,
  111. (2, 2),
  112. nn.Module,
  113. ],
  114. },
  115. {
  116. "struct": [8, 20, 2.3, 13, 0.16666666666666666, 1, 2, 2],
  117. "expected_config": [
  118. 3,
  119. 32,
  120. 32,
  121. 32,
  122. 288,
  123. 288,
  124. 288,
  125. 288,
  126. (2, 2),
  127. nn.Module,
  128. 48,
  129. 1440,
  130. 1440,
  131. 1440,
  132. 1440,
  133. (2, 2),
  134. nn.Module,
  135. 240,
  136. 3456,
  137. 3456,
  138. 3456,
  139. 3456,
  140. (2, 2),
  141. nn.Module,
  142. 576,
  143. 8064,
  144. 8064,
  145. 8064,
  146. 8064,
  147. (2, 2),
  148. nn.Module,
  149. ],
  150. },
  151. {"struct": [56, 10, 2.4, 13, 2, 8, 1, 0], "expected_config": [3, 32, 32, 32, 16, 16, 16, 2, (1, 1), None, 32, 32, 32, 32, 4, (1, 1), None]},
  152. ]
  153. for arch_conf_pair in selected_arch_and_corresponding_configs:
  154. expected_config = iter(arch_conf_pair["expected_config"])
  155. model = NASRegNet(HpmStruct(**{"structure": arch_conf_pair["struct"], "num_classes": 1000}))
  156. for stage in model.net.children():
  157. # CHECK CORRECTNESS OF THE STEM
  158. if isinstance(stage, Stem):
  159. assert stage.conv.in_channels == next(expected_config)
  160. assert stage.conv.out_channels == next(expected_config)
  161. assert stage.bn.num_features == next(expected_config)
  162. # CHECK THE CORRECTNESS OF THE FIRST XBlock IN EACH STAGE
  163. if isinstance(stage, Stage):
  164. for block in stage.blocks.children():
  165. if isinstance(block, XBlock):
  166. assert block.conv_block_1[0].in_channels == next(expected_config)
  167. assert block.conv_block_1[0].out_channels == next(expected_config)
  168. assert block.conv_block_2[0].in_channels == next(expected_config)
  169. assert block.conv_block_2[0].out_channels == next(expected_config)
  170. assert block.conv_block_2[0].groups == next(expected_config)
  171. assert block.conv_block_2[0].stride == next(expected_config)
  172. se_block = next(expected_config)
  173. assert block.se is None if se_block is None else isinstance(block, se_block)
  174. # SKIP TO THE NEXT STAGE
  175. break
  176. if __name__ == "__main__":
  177. unittest.main()
Discard
Tip!

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