-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarknet53.py
224 lines (169 loc) · 6.61 KB
/
darknet53.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import torch
class UpsampleLayer(torch.nn.Module):
def __init__(self):
super(UpsampleLayer, self).__init__()
def forward(self, x):
return torch.nn.functional.interpolate(x, scale_factor=2, mode='nearest')
class ConvolutionalLayer(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, bias=False):
super(ConvolutionalLayer, self).__init__()
self.sub_module = torch.nn.Sequential(
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, bias=bias),
torch.nn.BatchNorm2d(out_channels),
torch.nn.LeakyReLU(0.1)
)
def forward(self, x):
return self.sub_module(x)
class ResidualLayer(torch.nn.Module):
def __init__(self, in_channels):
super(ResidualLayer, self).__init__()
self.sub_module = torch.nn.Sequential(
ConvolutionalLayer(in_channels, in_channels // 2, 1, 1, 0),
ConvolutionalLayer(in_channels // 2, in_channels, 3, 1, 1),
)
def forward(self, x):
return x + self.sub_module(x)
class DownsamplingLayer(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(DownsamplingLayer, self).__init__()
self.sub_module = torch.nn.Sequential(
ConvolutionalLayer(in_channels, out_channels, 3, 2, 1)
)
def forward(self, x):
return self.sub_module(x)
class ConvolutionalSet(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(ConvolutionalSet, self).__init__()
self.sub_module = torch.nn.Sequential(
ConvolutionalLayer(in_channels, out_channels, 1, 1, 0),
ConvolutionalLayer(out_channels, in_channels, 3, 1, 1),
ConvolutionalLayer(in_channels, out_channels, 1, 1, 0),
ConvolutionalLayer(out_channels, in_channels, 3, 1, 1),
ConvolutionalLayer(in_channels, out_channels, 1, 1, 0),
)
# print(in_channels, out_channels)
def forward(self, x):
return self.sub_module(x)
class ConvolutionalSets(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(ConvolutionalSets, self).__init__()
self.sub_module = torch.nn.Sequential(
ConvolutionalLayer(in_channels, out_channels, 3, 1, 1),
ConvolutionalLayer(out_channels, in_channels, 1, 1, 0),
ConvolutionalLayer(in_channels, out_channels, 3, 1, 1),
ConvolutionalLayer(out_channels, in_channels, 1, 1, 0),
)
# print(in_channels, out_channels)
def forward(self, x):
return self.sub_module(x)
class MainNet(torch.nn.Module):
def __init__(self, cls_num):
super(MainNet, self).__init__()
self.trunk_52 = torch.nn.Sequential(
ConvolutionalLayer(3, 32, 3, 1, 1),
ConvolutionalLayer(32, 64, 3, 2, 1),
ResidualLayer(64),
DownsamplingLayer(64, 128),
ResidualLayer(128),
ResidualLayer(128),
DownsamplingLayer(128, 256),
ResidualLayer(256),
ResidualLayer(256),
ResidualLayer(256),
ResidualLayer(256),
ResidualLayer(256),
ResidualLayer(256),
ResidualLayer(256),
ResidualLayer(256),
)
self.trunk_26 = torch.nn.Sequential(
DownsamplingLayer(256, 512),
ResidualLayer(512),
ResidualLayer(512),
ResidualLayer(512),
ResidualLayer(512),
ResidualLayer(512),
ResidualLayer(512),
ResidualLayer(512),
ResidualLayer(512),
)
self.trunk_13 = torch.nn.Sequential(
DownsamplingLayer(512, 1024),
ResidualLayer(1024),
ResidualLayer(1024),
ResidualLayer(1024),
ResidualLayer(1024)
)
self.convset_13 = torch.nn.Sequential(
ConvolutionalSet(1024, 512)
)
self.detetion_13 = torch.nn.Sequential(
ConvolutionalLayer(512, 1024, 3, 1, 1),
torch.nn.Conv2d(1024, 3 * (5 + cls_num), 1, 1, 0)
)
self.up_26 = torch.nn.Sequential(
ConvolutionalLayer(512, 256, 1, 1, 0),#
UpsampleLayer()
)
self.convset_26 = torch.nn.Sequential(
ConvolutionalLayer(768, 256, 1, 1, 0),
ConvolutionalSets(256, 512)
)
self.detetion_26 = torch.nn.Sequential(
ConvolutionalLayer(256, 512, 3, 1, 1),
torch.nn.Conv2d(512, 3 * (5 + cls_num), 1, 1, 0)
)
self.up_52 = torch.nn.Sequential(
ConvolutionalLayer(256, 128, 1, 1, 0),#
UpsampleLayer()
)
self.convset_52 = torch.nn.Sequential(
ConvolutionalLayer(384, 128, 1, 1, 0),
ConvolutionalSets(128, 256)
)
self.detetion_52 = torch.nn.Sequential(
ConvolutionalLayer(128, 256, 3, 1, 1),
torch.nn.Conv2d(256, 3 * (5 + cls_num), 1, 1, 0)
)
def forward(self, x):
h_52 = self.trunk_52(x)
h_26 = self.trunk_26(h_52)
h_13 = self.trunk_13(h_26)
convset_out_13 = self.convset_13(h_13)
detetion_out_13 = self.detetion_13(convset_out_13)
up_out_26 = self.up_26(convset_out_13)
route_out_26 = torch.cat((up_out_26, h_26), dim=1)
convset_out_26 = self.convset_26(route_out_26)
detetion_out_26 = self.detetion_26(convset_out_26)
up_out_52 = self.up_52(convset_out_26)
route_out_52 = torch.cat((up_out_52, h_52), dim=1)
convset_out_52 = self.convset_52(route_out_52)
detetion_out_52 = self.detetion_52(convset_out_52)
return detetion_out_13, detetion_out_26, detetion_out_52
if __name__ == '__main__':
net = MainNet(80)
# layer89_conv_89 = net.convset_26[0].sub_module[2].sub_module[0]
# print(layer89_conv_89)
print(net)
import cv2
net.cuda().half()
x = torch.cuda.HalfTensor(2, 3, 416, 416)
#
y_13, y_26, y_52 = net(x)
print(y_13.shape)
# # print(y_26.shape)
# # print(y_52.shape)
# print(y_13.view(-1, 3, 5, 13, 13).shape)
# torch.save(net.state_dict(),
# './darknet.pt')
#
# weights = torch.load("yolov3.pt")['model']
# layer0 = net.trunk_52[0].sub_module[0]
# # print(layer0)
# layer0.weight.data = weights['module_list.0.conv_0.weight']
# # print(layer0.weight.data)
# layer1 = net.trunk_52[0].sub_module[1]
# # print(layer1)
# layer1.weight.data = weights['module_list.0.batch_norm_0.bias']
# # print(layer1.weight.data )
# cv2.waitKey(0)