C3 in yolov5, CSP Bottleneck with 3 convolutions
| 145 | |
| 146 | |
| 147 | class CSPLayer(nn.Module): |
| 148 | """C3 in yolov5, CSP Bottleneck with 3 convolutions""" |
| 149 | |
| 150 | def __init__( |
| 151 | self, |
| 152 | in_channels, |
| 153 | out_channels, |
| 154 | n=1, |
| 155 | shortcut=True, |
| 156 | expansion=0.5, |
| 157 | depthwise=False, |
| 158 | act="silu", |
| 159 | ): |
| 160 | """ |
| 161 | Args: |
| 162 | in_channels (int): input channels. |
| 163 | out_channels (int): output channels. |
| 164 | n (int): number of Bottlenecks. Default value: 1. |
| 165 | """ |
| 166 | # ch_in, ch_out, number, shortcut, groups, expansion |
| 167 | super().__init__() |
| 168 | hidden_channels = int(out_channels * expansion) # hidden channels |
| 169 | self.conv1 = BaseConv(in_channels, hidden_channels, 1, stride=1, act=act) |
| 170 | self.conv2 = BaseConv(in_channels, hidden_channels, 1, stride=1, act=act) |
| 171 | self.conv3 = BaseConv(2 * hidden_channels, out_channels, 1, stride=1, act=act) |
| 172 | module_list = [ |
| 173 | Bottleneck( |
| 174 | hidden_channels, hidden_channels, shortcut, 1.0, depthwise, act=act |
| 175 | ) |
| 176 | for _ in range(n) |
| 177 | ] |
| 178 | self.m = nn.Sequential(*module_list) |
| 179 | |
| 180 | def forward(self, x): |
| 181 | x_1 = self.conv1(x) |
| 182 | x_2 = self.conv2(x) |
| 183 | x_1 = self.m(x_1) |
| 184 | x = torch.cat((x_1, x_2), dim=1) |
| 185 | return self.conv3(x) |
| 186 | |
| 187 | |
| 188 | class Focus(nn.Module): |