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

#869 Add DagsHub Logger to Super Gradients

Merged
Ghost merged 1 commits into Deci-AI:master from timho102003:dagshub_logger
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
  1. import collections
  2. from typing import Type, Tuple, List
  3. import torch
  4. from torch import nn, Tensor
  5. from super_gradients.common.registry.registry import register_detection_module
  6. from super_gradients.common.decorators.factory_decorator import resolve_param
  7. from super_gradients.common.factories.activations_type_factory import ActivationsTypeFactory
  8. from super_gradients.training.models.detection_models.csp_resnet import CSPResNetBasicBlock
  9. from super_gradients.modules import ConvBNAct
  10. __all__ = ["CustomCSPPAN"]
  11. class SPP(nn.Module):
  12. def __init__(
  13. self,
  14. in_channels: int,
  15. out_channels: int,
  16. kernel_size: int,
  17. pool_size: Tuple[int, ...],
  18. activation_type: Type[nn.Module],
  19. ):
  20. super().__init__()
  21. mid_channels = in_channels * (1 + len(pool_size))
  22. pools = []
  23. for i, size in enumerate(pool_size):
  24. pool = nn.MaxPool2d(kernel_size=size, stride=1, padding=size // 2, ceil_mode=False)
  25. pools.append(pool)
  26. self.pool = nn.ModuleList(pools)
  27. self.conv = ConvBNAct(mid_channels, out_channels, kernel_size, padding=kernel_size // 2, activation_type=activation_type, stride=1, bias=False)
  28. def forward(self, x: Tensor) -> Tensor:
  29. outs = [x]
  30. for pool in self.pool:
  31. outs.append(pool(x))
  32. y = torch.cat(outs, dim=1)
  33. y = self.conv(y)
  34. return y
  35. class CSPStage(nn.Module):
  36. def __init__(self, in_channels: int, out_channels: int, n, activation_type: Type[nn.Module], spp: bool):
  37. super().__init__()
  38. ch_mid = int(out_channels // 2)
  39. self.conv1 = ConvBNAct(in_channels, ch_mid, kernel_size=1, padding=0, activation_type=activation_type, stride=1, bias=False)
  40. self.conv2 = ConvBNAct(in_channels, ch_mid, kernel_size=1, padding=0, activation_type=activation_type, stride=1, bias=False)
  41. convs = []
  42. next_ch_in = ch_mid
  43. for i in range(n):
  44. convs.append((str(i), CSPResNetBasicBlock(next_ch_in, ch_mid, activation_type=activation_type, use_residual_connection=False)))
  45. if i == (n - 1) // 2 and spp:
  46. convs.append(("spp", SPP(ch_mid, ch_mid, 1, (5, 9, 13), activation_type=activation_type)))
  47. next_ch_in = ch_mid
  48. self.convs = nn.Sequential(collections.OrderedDict(convs))
  49. self.conv3 = ConvBNAct(ch_mid * 2, out_channels, kernel_size=1, padding=0, activation_type=activation_type, stride=1, bias=False)
  50. def forward(self, x):
  51. y1 = self.conv1(x)
  52. y2 = self.conv2(x)
  53. y2 = self.convs(y2)
  54. y = torch.cat([y1, y2], dim=1)
  55. y = self.conv3(y)
  56. return y
  57. @register_detection_module()
  58. class CustomCSPPAN(nn.Module):
  59. @resolve_param("activation", ActivationsTypeFactory())
  60. def __init__(
  61. self,
  62. in_channels: Tuple[int, ...],
  63. out_channels: Tuple[int, ...],
  64. activation: Type[nn.Module],
  65. stage_num: int,
  66. block_num: int,
  67. spp: bool,
  68. width_mult: float,
  69. depth_mult: float,
  70. ):
  71. super().__init__()
  72. in_channels = [max(round(c * width_mult), 1) for c in in_channels]
  73. out_channels = [max(round(c * width_mult), 1) for c in out_channels]
  74. if len(in_channels) != len(out_channels):
  75. raise ValueError("in_channels and out_channels must have the same length")
  76. block_num = max(round(block_num * depth_mult), 1)
  77. self.num_blocks = len(in_channels)
  78. self._out_channels = out_channels
  79. in_channels = in_channels[::-1]
  80. fpn_stages = []
  81. fpn_routes = []
  82. ch_pre = None
  83. for i, (ch_in, ch_out) in enumerate(zip(in_channels, out_channels)):
  84. if i > 0:
  85. ch_in += ch_pre // 2
  86. stage = []
  87. for j in range(stage_num):
  88. stage.append(
  89. (
  90. str(j),
  91. CSPStage(
  92. ch_in if j == 0 else ch_out,
  93. ch_out,
  94. block_num,
  95. activation_type=activation,
  96. spp=(spp and i == 0),
  97. ),
  98. ),
  99. )
  100. fpn_stages.append(nn.Sequential(collections.OrderedDict(stage)))
  101. if i < self.num_blocks - 1:
  102. fpn_routes.append(
  103. ConvBNAct(in_channels=ch_out, out_channels=ch_out // 2, kernel_size=1, stride=1, padding=0, activation_type=activation, bias=False)
  104. )
  105. ch_pre = ch_out
  106. self.fpn_stages = nn.ModuleList(fpn_stages)
  107. self.fpn_routes = nn.ModuleList(fpn_routes)
  108. pan_stages = []
  109. pan_routes = []
  110. for i in reversed(range(self.num_blocks - 1)):
  111. pan_routes.append(
  112. ConvBNAct(
  113. in_channels=out_channels[i + 1],
  114. out_channels=out_channels[i + 1],
  115. kernel_size=3,
  116. stride=2,
  117. padding=1,
  118. activation_type=activation,
  119. bias=False,
  120. )
  121. )
  122. ch_in = out_channels[i] + out_channels[i + 1]
  123. ch_out = out_channels[i]
  124. stage = []
  125. for j in range(stage_num):
  126. stage.append(
  127. (
  128. str(j),
  129. CSPStage(
  130. ch_in if j == 0 else ch_out,
  131. ch_out,
  132. block_num,
  133. activation_type=activation,
  134. spp=False,
  135. ),
  136. ),
  137. )
  138. pan_stages.append(nn.Sequential(collections.OrderedDict(stage)))
  139. self.pan_stages = nn.ModuleList(pan_stages[::-1])
  140. self.pan_routes = nn.ModuleList(pan_routes[::-1])
  141. def forward(self, blocks: List[Tensor]) -> List[Tensor]:
  142. blocks = blocks[::-1]
  143. fpn_feats = []
  144. route = None
  145. for i, block in enumerate(blocks):
  146. if i > 0:
  147. block = torch.cat([route, block], dim=1)
  148. route = self.fpn_stages[i](block)
  149. fpn_feats.append(route)
  150. if i < self.num_blocks - 1:
  151. route = self.fpn_routes[i](route)
  152. route = torch.nn.functional.interpolate(route, scale_factor=2, mode="nearest")
  153. pan_feats = [
  154. fpn_feats[-1],
  155. ]
  156. route = fpn_feats[-1]
  157. for i in reversed(range(self.num_blocks - 1)):
  158. block = fpn_feats[i]
  159. route = self.pan_routes[i](route)
  160. block = torch.cat([route, block], dim=1)
  161. route = self.pan_stages[i](block)
  162. pan_feats.append(route)
  163. return pan_feats[::-1]
  164. @property
  165. def out_channels(self) -> Tuple[int, ...]:
  166. return tuple(self._out_channels)
Discard
Tip!

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