-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCSPDarknet53.py
140 lines (99 loc) · 4.01 KB
/
CSPDarknet53.py
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class Mish(nn.Module):
def __init__(self):
super(Mish, self).__init__()
def forward(self, x):
return x * torch.tanh(F.softplus(x))
ACTIVATIONS = {
'mish': Mish(),
'linear': nn.Identity()
}
class Conv(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, activation='mish'):
super(Conv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size, stride, kernel_size//2, bias=False),
nn.BatchNorm2d(out_channels),
ACTIVATIONS[activation]
)
def forward(self, x):
return self.conv(x)
class CSPBlock(nn.Module):
def __init__(self, in_channels, out_channels, hidden_channels=None, residual_activation='linear'):
super(CSPBlock, self).__init__()
if hidden_channels is None:
hidden_channels = out_channels
self.block = nn.Sequential(
Conv(in_channels, hidden_channels, 1),
Conv(hidden_channels, out_channels, 3)
)
self.activation = ACTIVATIONS[residual_activation]
def forward(self, x):
return self.activation(x+self.block(x))
class CSPFirstStage(nn.Module):
def __init__(self, in_channels, out_channels):
super(CSPFirstStage, self).__init__()
self.downsample_conv = Conv(in_channels, out_channels, 3, stride=2)
self.split_conv0 = Conv(out_channels, out_channels, 1)
self.split_conv1 = Conv(out_channels, out_channels, 1)
self.blocks_conv = nn.Sequential(
CSPBlock(out_channels, out_channels, in_channels),
Conv(out_channels, out_channels, 1)
)
self.concat_conv = Conv(out_channels*2, out_channels, 1)
def forward(self, x):
x = self.downsample_conv(x)
x0 = self.split_conv0(x)
x1 = self.split_conv1(x)
x1 = self.blocks_conv(x1)
x = torch.cat([x0, x1], dim=1)
x = self.concat_conv(x)
return x
class CSPStage(nn.Module):
def __init__(self, in_channels, out_channels, num_blocks):
super(CSPStage, self).__init__()
self.downsample_conv = Conv(in_channels, out_channels, 3, stride=2)
self.split_conv0 = Conv(out_channels, out_channels//2, 1)
self.split_conv1 = Conv(out_channels, out_channels//2, 1)
self.blocks_conv = nn.Sequential(
*[CSPBlock(out_channels//2, out_channels//2) for _ in range(num_blocks)],
Conv(out_channels//2, out_channels//2, 1)
)
self.concat_conv = Conv(out_channels, out_channels, 1)
def forward(self, x):
x = self.downsample_conv(x)
x0 = self.split_conv0(x)
x1 = self.split_conv1(x)
x1 = self.blocks_conv(x1)
x = torch.cat([x0, x1], dim=1)
x = self.concat_conv(x)
return x
class CSPDarknet53(nn.Module):
def __init__(self, stem_channels=32, feature_channels=[64, 128, 256, 512, 1024], num_features=1):
super(CSPDarknet53, self).__init__()
self.stem_conv = Conv(3, stem_channels, 3)
self.stages = nn.ModuleList([
CSPFirstStage(stem_channels, feature_channels[0]),
CSPStage(feature_channels[0], feature_channels[1], 2),
CSPStage(feature_channels[1], feature_channels[2], 8),
CSPStage(feature_channels[2], feature_channels[3], 8),
CSPStage(feature_channels[3], feature_channels[4], 4)
])
self.feature_channels = feature_channels
self.num_features = num_features
def forward(self, x):
x = self.stem_conv(x)
features = []
for stage in self.stages:
x = stage(x)
features.append(x)
return features[-self.num_features:]
def _BuildCSPDarknet53(num_features=3):
model = CSPDarknet53(num_features=num_features)
return model, model.feature_channels[-num_features:]
if __name__ == '__main__':
model = CSPDarknet53()
x = torch.randn(1, 3, 224, 224)
y = model(x)