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
198
199
200
201
202
203
204
205
206
207
208
209
|
- import collections
- from typing import List, Type, Tuple
- import torch
- from super_gradients.common.decorators.factory_decorator import resolve_param
- from super_gradients.common.factories.activations_type_factory import ActivationsTypeFactory
- from torch import nn, Tensor
- from super_gradients.modules import RepVGGBlock, EffectiveSEBlock, ConvBNAct
- __all__ = ["CSPResNet"]
- class CSPResNetBasicBlock(nn.Module):
- def __init__(self, in_channels: int, out_channels: int, activation_type: Type[nn.Module], use_residual_connection: bool = True, use_alpha=False):
- """
- :param in_channels:
- :param out_channels:
- :param activation_type:
- :param use_residual_connection: Whether to add input x to the output
- :param use_alpha: If True, enables additional learnable weighting parameter for 1x1 branch in RepVGGBlock
- """
- super().__init__()
- if use_residual_connection and in_channels != out_channels:
- raise RuntimeError(
- f"Number of input channels (got {in_channels}) must be equal to the "
- f"number of output channels (got {out_channels}) when use_residual_connection=True"
- )
- self.conv1 = ConvBNAct(in_channels, out_channels, kernel_size=3, stride=1, padding=1, activation_type=activation_type, bias=False)
- self.conv2 = RepVGGBlock(
- out_channels, out_channels, activation_type=activation_type, se_type=nn.Identity, use_residual_connection=False, use_alpha=use_alpha
- )
- self.use_residual_connection = use_residual_connection
- def forward(self, x):
- y = self.conv1(x)
- y = self.conv2(y)
- if self.use_residual_connection:
- return x + y
- else:
- return y
- class CSPResStage(nn.Module):
- def __init__(
- self,
- in_channels: int,
- out_channels: int,
- num_blocks,
- stride: int,
- activation_type: Type[nn.Module],
- use_attention: bool = True,
- use_alpha: bool = False,
- ):
- """
- :param in_channels: Number of input channels
- :param out_channels: Number of output channels
- :param num_blocks: Number of blocks in stage
- :param stride: Desired down-sampling for the stage (Usually 2)
- :param activation_type: Non-linearity type used in child modules.
- :param use_attention: If True, adds EffectiveSEBlock at the end of each stage
- :param use_alpha: If True, enables additional learnable weighting parameter for 1x1 branch in underlying RepVGG blocks (PP-Yolo-E Plus)
- """
- super().__init__()
- mid_channels = (in_channels + out_channels) // 2
- half_mid_channels = mid_channels // 2
- mid_channels = 2 * half_mid_channels
- if stride != 1:
- self.conv_down = ConvBNAct(in_channels, mid_channels, 3, stride=stride, padding=1, activation_type=activation_type, bias=False)
- else:
- self.conv_down = None
- self.conv1 = ConvBNAct(mid_channels, half_mid_channels, kernel_size=1, stride=1, padding=0, activation_type=activation_type, bias=False)
- self.conv2 = ConvBNAct(mid_channels, half_mid_channels, kernel_size=1, stride=1, padding=0, activation_type=activation_type, bias=False)
- self.blocks = nn.Sequential(
- *[
- CSPResNetBasicBlock(
- in_channels=half_mid_channels,
- out_channels=half_mid_channels,
- activation_type=activation_type,
- use_alpha=use_alpha,
- )
- for _ in range(num_blocks)
- ]
- )
- if use_attention:
- self.attn = EffectiveSEBlock(mid_channels)
- else:
- self.attn = nn.Identity()
- self.conv3 = ConvBNAct(mid_channels, out_channels, kernel_size=1, stride=1, padding=0, activation_type=activation_type, bias=False)
- def forward(self, x):
- if self.conv_down is not None:
- x = self.conv_down(x)
- y1 = self.conv1(x)
- y2 = self.blocks(self.conv2(x))
- y = torch.concat([y1, y2], dim=1)
- y = self.attn(y)
- y = self.conv3(y)
- return y
- class CSPResNet(nn.Module):
- """
- CSPResNet backbone
- """
- @resolve_param("activation", ActivationsTypeFactory())
- def __init__(
- self,
- layers: Tuple[int, ...],
- channels: Tuple[int, ...],
- activation: Type[nn.Module],
- return_idx: Tuple[int, int, int],
- use_large_stem: bool,
- width_mult: float,
- depth_mult: float,
- use_alpha: bool,
- ):
- """
- :param layers: Number of blocks in each stage
- :param channels: Number of channels [stem, stage 0, stage 1, stage 2, ...]
- :param activation: Used activation type for all child modules.
- :param return_idx: Indexes of returned feature maps
- :param use_large_stem: If True, uses 3 conv+bn+act instead of 2 in stem blocks
- :param width_mult: Scaling factor for a number of channels
- :param depth_mult: Scaling factor for a number of blocks in each stage
- :param use_alpha: If True, enables additional learnable weighting parameter for 1x1 branch in RepVGGBlock
- """
- super().__init__()
- channels = [max(round(num_channels * width_mult), 1) for num_channels in channels]
- layers = [max(round(num_layers * depth_mult), 1) for num_layers in layers]
- if use_large_stem:
- self.stem = nn.Sequential(
- collections.OrderedDict(
- [
- (
- "conv1",
- ConvBNAct(3, channels[0] // 2, 3, stride=2, padding=1, activation_type=activation, bias=False),
- ),
- (
- "conv2",
- ConvBNAct(
- channels[0] // 2,
- channels[0] // 2,
- 3,
- stride=1,
- padding=1,
- activation_type=activation,
- bias=False,
- ),
- ),
- (
- "conv3",
- ConvBNAct(channels[0] // 2, channels[0], 3, stride=1, padding=1, activation_type=activation, bias=False),
- ),
- ]
- )
- )
- else:
- self.stem = nn.Sequential(
- collections.OrderedDict(
- [
- (
- "conv1",
- ConvBNAct(3, channels[0] // 2, 3, stride=2, padding=1, activation_type=activation, bias=False),
- ),
- (
- "conv2",
- ConvBNAct(channels[0] // 2, channels[0], 3, stride=1, padding=1, activation_type=activation, bias=False),
- ),
- ]
- )
- )
- n = len(channels) - 1
- self.stages = nn.ModuleList(
- [
- CSPResStage(
- channels[i],
- channels[i + 1],
- layers[i],
- stride=2,
- activation_type=activation,
- use_alpha=use_alpha,
- )
- for i in range(n)
- ]
- )
- self._out_channels = channels[1:]
- self._out_strides = [4 * 2**i for i in range(n)]
- self.return_idx = return_idx
- def forward(self, x: Tensor) -> List[Tensor]:
- x = self.stem(x)
- outs = []
- for idx, stage in enumerate(self.stages):
- x = stage(x)
- if idx in self.return_idx:
- outs.append(x)
- return outs
|