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

#609 Ci fix

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:bugfix/infra-000_ci
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
  1. """
  2. Repvgg Pytorch Implementation. This model trains a vgg with residual blocks
  3. but during inference (in deployment mode) will convert the model to vgg model.
  4. Pretrained models: https://drive.google.com/drive/folders/1Avome4KvNp0Lqh2QwhXO6L5URQjzCjUq
  5. Refrerences:
  6. [1] https://github.com/DingXiaoH/RepVGG
  7. [2] https://arxiv.org/pdf/2101.03697.pdf
  8. Based on https://github.com/DingXiaoH/RepVGG
  9. """
  10. from typing import Union
  11. import torch.nn as nn
  12. from super_gradients.modules import RepVGGBlock, SEBlock
  13. from super_gradients.training.models.sg_module import SgModule
  14. from super_gradients.training.utils.module_utils import fuse_repvgg_blocks_residual_branches
  15. from super_gradients.training.utils.utils import get_param
  16. class RepVGG(SgModule):
  17. def __init__(
  18. self,
  19. struct,
  20. num_classes=1000,
  21. width_multiplier=None,
  22. build_residual_branches=True,
  23. use_se=False,
  24. backbone_mode=False,
  25. in_channels=3,
  26. ):
  27. """
  28. :param struct: list containing number of blocks per repvgg stage
  29. :param num_classes: number of classes if nut in backbone mode
  30. :param width_multiplier: list of per stage width multiplier or float if using single value for all stages
  31. :param build_residual_branches: whether to add residual connections or not
  32. :param use_se: use squeeze and excitation layers
  33. :param backbone_mode: if true, dropping the final linear layer
  34. :param in_channels: input channels
  35. """
  36. super(RepVGG, self).__init__()
  37. if isinstance(width_multiplier, float):
  38. width_multiplier = [width_multiplier] * 4
  39. else:
  40. assert len(width_multiplier) == 4
  41. self.build_residual_branches = build_residual_branches
  42. self.use_se = use_se
  43. self.backbone_mode = backbone_mode
  44. self.in_planes = int(64 * width_multiplier[0])
  45. self.stem = RepVGGBlock(
  46. in_channels=in_channels,
  47. out_channels=self.in_planes,
  48. stride=2,
  49. build_residual_branches=build_residual_branches,
  50. activation_type=nn.ReLU,
  51. activation_kwargs=dict(inplace=True),
  52. se_type=SEBlock if self.use_se else nn.Identity,
  53. se_kwargs=dict(in_channels=self.in_planes, internal_neurons=self.in_planes // 16) if self.use_se else None,
  54. )
  55. self.cur_layer_idx = 1
  56. self.stage1 = self._make_stage(int(64 * width_multiplier[0]), struct[0], stride=2)
  57. self.stage2 = self._make_stage(int(128 * width_multiplier[1]), struct[1], stride=2)
  58. self.stage3 = self._make_stage(int(256 * width_multiplier[2]), struct[2], stride=2)
  59. self.stage4 = self._make_stage(int(512 * width_multiplier[3]), struct[3], stride=2)
  60. if not self.backbone_mode:
  61. self.avgpool = nn.AdaptiveAvgPool2d(output_size=1)
  62. self.linear = nn.Linear(int(512 * width_multiplier[3]), num_classes)
  63. if not build_residual_branches:
  64. self.eval() # fusing has to be made in eval mode. When called in init, model will be built in eval mode
  65. fuse_repvgg_blocks_residual_branches(self)
  66. self.final_width_mult = width_multiplier[3]
  67. def _make_stage(self, planes, struct, stride):
  68. strides = [stride] + [1] * (struct - 1)
  69. blocks = []
  70. for stride in strides:
  71. blocks.append(
  72. RepVGGBlock(
  73. in_channels=self.in_planes,
  74. out_channels=planes,
  75. stride=stride,
  76. groups=1,
  77. build_residual_branches=self.build_residual_branches,
  78. activation_type=nn.ReLU,
  79. activation_kwargs=dict(inplace=True),
  80. se_type=SEBlock if self.use_se else nn.Identity,
  81. se_kwargs=dict(in_channels=self.in_planes, internal_neurons=self.in_planes // 16) if self.use_se else None,
  82. )
  83. )
  84. self.in_planes = planes
  85. self.cur_layer_idx += 1
  86. return nn.Sequential(*blocks)
  87. def forward(self, x):
  88. out = self.stem(x)
  89. out = self.stage1(out)
  90. out = self.stage2(out)
  91. out = self.stage3(out)
  92. out = self.stage4(out)
  93. if not self.backbone_mode:
  94. out = self.avgpool(out)
  95. out = out.view(out.size(0), -1)
  96. out = self.linear(out)
  97. return out
  98. def prep_model_for_conversion(self, input_size: Union[tuple, list] = None, **kwargs):
  99. if self.build_residual_branches:
  100. fuse_repvgg_blocks_residual_branches(self)
  101. def train(self, mode: bool = True):
  102. assert (
  103. not mode or self.build_residual_branches
  104. ), "Trying to train a model without residual branches, set arch_params.build_residual_branches to True and retrain the model"
  105. super(RepVGG, self).train(mode=mode)
  106. def replace_head(self, new_num_classes=None, new_head=None):
  107. if new_num_classes is None and new_head is None:
  108. raise ValueError("At least one of new_num_classes, new_head must be given to replace output layer.")
  109. if new_head is not None:
  110. self.linear = new_head
  111. else:
  112. self.linear = nn.Linear(int(512 * self.final_width_mult), new_num_classes)
  113. class RepVggCustom(RepVGG):
  114. def __init__(self, arch_params):
  115. super().__init__(
  116. struct=arch_params.struct,
  117. num_classes=arch_params.num_classes,
  118. width_multiplier=arch_params.width_multiplier,
  119. build_residual_branches=arch_params.build_residual_branches,
  120. use_se=get_param(arch_params, "use_se", False),
  121. backbone_mode=get_param(arch_params, "backbone_mode", False),
  122. in_channels=get_param(arch_params, "in_channels", 3),
  123. )
  124. class RepVggA0(RepVggCustom):
  125. def __init__(self, arch_params):
  126. arch_params.override(struct=[2, 4, 14, 1], width_multiplier=[0.75, 0.75, 0.75, 2.5])
  127. super().__init__(arch_params=arch_params)
  128. class RepVggA1(RepVggCustom):
  129. def __init__(self, arch_params):
  130. arch_params.override(struct=[2, 4, 14, 1], width_multiplier=[1, 1, 1, 2.5])
  131. super().__init__(arch_params=arch_params)
  132. class RepVggA2(RepVggCustom):
  133. def __init__(self, arch_params):
  134. arch_params.override(struct=[2, 4, 14, 1], width_multiplier=[1.5, 1.5, 1.5, 2.75])
  135. super().__init__(arch_params=arch_params)
  136. class RepVggB0(RepVggCustom):
  137. def __init__(self, arch_params):
  138. arch_params.override(struct=[4, 6, 16, 1], width_multiplier=[1, 1, 1, 2.5])
  139. super().__init__(arch_params=arch_params)
  140. class RepVggB1(RepVggCustom):
  141. def __init__(self, arch_params):
  142. arch_params.override(struct=[4, 6, 16, 1], width_multiplier=[2, 2, 2, 4])
  143. super().__init__(arch_params=arch_params)
  144. class RepVggB2(RepVggCustom):
  145. def __init__(self, arch_params):
  146. arch_params.override(struct=[4, 6, 16, 1], width_multiplier=[2.5, 2.5, 2.5, 5])
  147. super().__init__(arch_params=arch_params)
  148. class RepVggB3(RepVggCustom):
  149. def __init__(self, arch_params):
  150. arch_params.override(struct=[4, 6, 16, 1], width_multiplier=[3, 3, 3, 5])
  151. super().__init__(arch_params=arch_params)
  152. class RepVggD2SE(RepVggCustom):
  153. def __init__(self, arch_params):
  154. arch_params.override(struct=[8, 14, 24, 1], width_multiplier=[2.5, 2.5, 2.5, 5])
  155. super().__init__(arch_params=arch_params)
Discard
Tip!

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