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

#298 Feature/sg 171 make coco inherit from detection dataset

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

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