-
Notifications
You must be signed in to change notification settings - Fork 0
/
torch_model.py
270 lines (188 loc) · 7.7 KB
/
torch_model.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from torch.nn import Conv2d
from torch.nn import BatchNorm2d
from torch.nn import ZeroPad2d
from torch.nn import Module
import torch.nn.functional as F
class ResBlock(Module):
def __init__(self, in_f, filters):
super(ResBlock, self).__init__()
f1, f2 = filters
self.conv_1 = Conv2d(in_f, f1, 1, padding=1, bias=False)
self.bnorm_1 = BatchNorm2d(f1, eps=1e-03)
self.conv_2 = Conv2d(f1, f2, 3, bias=False)
self.bnorm_2 = BatchNorm2d(f2, eps=1e-03)
def forward(self, input_x):
x = self.conv_1(input_x)
x = self.bnorm_1(x)
x = F.leaky_relu(x, negative_slope=0.1)
x = self.conv_2(x)
x = self.bnorm_2(x)
x = F.leaky_relu(x, negative_slope=0.1)
x = input_x + x
return x
class ConvStride2NormRelu(Module):
def __init__(self, in_f, f):
super(ConvStride2NormRelu, self).__init__()
self.zero = ZeroPad2d((1, 0, 1, 0))
self.conv = Conv2d(in_f, f, 3, stride=2, bias=False)
self.bnorm = BatchNorm2d(f, eps=1e-03)
def forward(self, input_x):
x = self.zero(input_x)
x = self.conv(x)
x = self.bnorm(x)
x = F.leaky_relu(x, negative_slope=0.1)
return x
class ConvNormRelu(Module):
def __init__(self, in_f, f, k=3):
super(ConvNormRelu, self).__init__()
self.conv = Conv2d(in_f, f, k,padding=1, bias=False)
self.bnorm = BatchNorm2d(f, eps=1e-03)
def forward(self, input_x):
x = self.conv(input_x)
x = self.bnorm(x)
x = F.leaky_relu(x, negative_slope=0.1)
return x
class YOLO(Module):
def __init__(self):
super(YOLO, self).__init__()
self.add_module('conv_100', ConvNormRelu(3, 32))
self.conv_1 = ConvStride2NormRelu(32, 64)
self.res_2_3 = ResBlock(64,(32,64))
self.conv_4 = ConvStride2NormRelu(64, 128)
self.res_5_6 = ResBlock(128,(64,128))
self.res_7_8 = ResBlock(128,(64,128))
self.conv_9 = ConvStride2NormRelu(128, 256)
self.res_10_11 = ResBlock(256,(128,256))
self.res_12_13 = ResBlock(256,(128,256))
self.res_14_15 = ResBlock(256,(128,256))
self.res_16_17 = ResBlock(256,(128,256))
self.res_18_19 = ResBlock(256,(128,256))
self.res_20_21 = ResBlock(256,(128,256))
self.res_22_23 = ResBlock(256,(128,256))
self.res_24_25 = ResBlock(256,(128,256))
self.conv_26 = ConvStride2NormRelu(256, 512)
def forward(self, input_x):
x = input_x
for i, m in enumerate(self.named_children()):
name, module = m
print(name)
# x = m(x)
return x
# def load_weights():
# self.wd = WeightDecoder(weight_file)
# self.wd.load_weights(self)
def compile_model():
input_x = Input(shape=(None, None, 3))
#1, 416
x = conv_norm_relu(input_x,32,0)
#2, 208
x = conv_stride_2_norm_relu(x,64,1)
#3,4, 208
x = res_block(x,(32,64),(1,3),2)
#5, 104
x = conv_stride_2_norm_relu(x,128,4)
#6-9, 104
for i in range(2):
x = res_block(x,(64,128),(1,3),i*2 + 5)
#10, 52
x = conv_stride_2_norm_relu(x,256,9)
#11-26, 52
for i in range(8):
x = res_block(x,(128,256),(1,3), i*2+10)
#saving for future conc for scale 1, 52x52x256
scale_1_conc_1 = x
#27, 26
x = conv_stride_2_norm_relu(x,512,26)
#28-44, 26
for i in range(8):
x = res_block(x,(256,512),(1,3), i*2+27)
#saving for future conc for scale 2, 26x26x512
scale_2_conc_1 = x
#45, 13
x = conv_stride_2_norm_relu(x,1024,43)
#46-53, 13
for i in range(4):
x = res_block(x,(512,1024),(1,3), i*2 + 44)
#Darknet-53 ended here
#scale 3 detection, 13
for i in range(2):
x = conv_norm_relu(x, 512, i*2 + 52, size=1)
x = conv_norm_relu(x, 1024, i*2 + 53)
x = conv_norm_relu(x, 512, 56, size=1)
z = conv_norm_relu(x, 1024, 57)
#13x13x255
scale_3_det = Conv2D(255,1, padding='same', name='conv_58', use_bias=True)(z)
#saving for future conc for scale 2
#conv + upsample for scale 2
#13
x = conv_norm_relu(x, 256, 59, size=1)
#26
x = UpSampling2D(2)(x)
#26x26x(256+512)
x = concatenate([x, scale_2_conc_1])
for i in range(2):
x = conv_norm_relu(x, 256, i*2 + 60, size=1)
x = conv_norm_relu(x, 512, i*2 + 61)
x = conv_norm_relu(x, 256, 64, size=1)
z = conv_norm_relu(x, 512, 65)
#saving for future conc for scale 1
#conv + upsample for scale 1
#scale 2 detection
scale_2_det = Conv2D(255,1, padding='same', name='conv_66', use_bias=True)(z)
#26
x = conv_norm_relu(x, 128, 67, size=1)
#52
x = UpSampling2D(2)(x)
#52x52x(128 + 256)
x = concatenate([x, scale_1_conc_1])
for i in range(3):
x = conv_norm_relu(x, 128, i*2 + 68, size=1)
x = conv_norm_relu(x, 256, i*2 + 69)
scale_1_det = Conv2D(255,1, padding='same', name='conv_74', use_bias=True)(x)
model = Model(input_x, [scale_3_det, scale_2_det, scale_1_det])
return model
import struct
import numpy as np
class WeightDecoder:
def __init__(self, weight_file):
with open(weight_file, 'rb') as w_f:
major, = struct.unpack('i', w_f.read(4))
minor, = struct.unpack('i', w_f.read(4))
revision, = struct.unpack('i', w_f.read(4))
if (major*10 + minor) >= 2 and major < 1000 and minor < 1000:
w_f.read(8)
else:
w_f.read(4)
transpose = (major > 1000) or (minor > 1000)
binary = w_f.read()
self.offset = 0
self.all_weights = np.frombuffer(binary, dtype='float32')
def read_bytes(self, size):
self.offset = self.offset + size
return self.all_weights[self.offset-size:self.offset]
def load_weights(self, model):
for i, layer in enumerate(model.children()):
print("loading weights of convolution #" + str(i))
if i not in [58, 66, 74]:
norm_layer = model.get_layer('bnorm_' + str(i))
size = np.prod(norm_layer.get_weights()[0].shape)
beta = self.read_bytes(size) # bias
gamma = self.read_bytes(size) # scale
mean = self.read_bytes(size) # mean
var = self.read_bytes(size) # variance
weights = norm_layer.set_weights([gamma, beta, mean, var])
if len(conv_layer.get_weights()) > 1:
bias = self.read_bytes(np.prod(conv_layer.get_weights()[1].shape))
print('filter weights = ', conv_layer.get_weights()[0].shape)
kernel = self.read_bytes(np.prod(conv_layer.get_weights()[0].shape))
kernel = kernel.reshape(list(reversed(conv_layer.get_weights()[0].shape)))
kernel = kernel.transpose([2,3,1,0])
conv_layer.set_weights([kernel, bias])
else:
kernel = self.read_bytes(np.prod(conv_layer.get_weights()[0].shape))
kernel = kernel.reshape(list(reversed(conv_layer.get_weights()[0].shape)))
kernel = kernel.transpose([2,3,1,0])
conv_layer.set_weights([kernel])
print(self.offset, len(self.all_weights))
def reset(self):
self.offset = 0