-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdarknet.py
406 lines (292 loc) · 13.6 KB
/
darknet.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
from __future__ import division
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import cv2
import matplotlib.pyplot as plt
from util import count_parameters as count
from util import convert2cpu as cpu
class test_net(nn.Module):
def __init__(self, num_layers, input_size):
super(test_net, self).__init__()
self.num_layers= num_layers
self.linear_1 = nn.Linear(input_size, 5)
self.middle = nn.ModuleList([nn.Linear(5,5) for x in range(num_layers)])
self.output = nn.Linear(5,2)
def forward(self, x):
x = x.view(-1)
fwd = nn.Sequential(self.linear_1, *self.middle, self.output)
return fwd(x)
def get_test_input():
img = cv2.imread("dog-cycle-car.png")
img = cv2.resize(img, (416,416))
img_ = img[:,:,::-1].transpose((2,0,1))
img_ = img_[np.newaxis,:,:,:]/255.0
img_ = torch.from_numpy(img_).float()
img_ = Variable(img_)
return img_
def parse_cfg(cfgfile):
"""
Takes a configuration file
Returns a list of blocks. Each blocks describes a block in the neural
network to be built. Block is represented as a dictionary in the list
"""
file = open(cfgfile, 'r')
lines = file.read().split('\n') #store the lines in a list
lines = [x for x in lines if len(x) > 0] #get read of the empty lines
lines = [x for x in lines if x[0] != '#']
lines = [x.rstrip().lstrip() for x in lines]
block = {}
blocks = []
for line in lines:
if line[0] == "[": #This marks the start of a new block
if len(block) != 0:
blocks.append(block)
block = {}
block["type"] = line[1:-1].rstrip()
else:
key,value = line.split("=")
block[key.rstrip()] = value.lstrip()
blocks.append(block)
return blocks
# print('\n\n'.join([repr(x) for x in blocks]))
class MaxPoolStride1(nn.Module):
def __init__(self, kernel_size):
super(MaxPoolStride1, self).__init__()
self.kernel_size = kernel_size
self.pad = kernel_size - 1
def forward(self, x):
padded_x = F.pad(x, (0, self.pad, 0, self.pad), mode = "replicate")
pooled_x = F.max_pool2d(padded_x, self.kernel_size, padding = self.pad)
return pooled_x
class RouteLayer(nn.Module):
def __init__(self, start, end):
super(RouteLayer, self).__init__()
self.start = start
self.end = end
#
class ReOrgLayer(nn.Module):
def __init__(self, stride = 2):
super(ReOrgLayer, self).__init__()
self.stride= stride
def forward(self,x):
assert(x.data.dim() == 4)
B,C,H,W = x.data.shape
hs = self.stride
ws = self.stride
assert(H % hs == 0), "The stride " + str(self.stride) + " is not a proper divisor of height " + str(H)
assert(W % ws == 0), "The stride " + str(self.stride) + " is not a proper divisor of height " + str(W)
x = x.view(B,C, H // hs, hs, W // ws, ws).transpose(-2,-3).contiguous()
x = x.view(B,C, H // hs * W // ws, hs, ws)
x = x.view(B,C, H // hs * W // ws, hs*ws).transpose(-1,-2).contiguous()
x = x.view(B, C, ws*hs, H // ws, W // ws).transpose(1,2).contiguous()
x = x.view(B, C*ws*hs, H // ws, W // ws)
return x
def create_modules(blocks):
inp_info = blocks[0] #Captures the information about the input and pre-processing
modules = blocks[1:-1] #The layers of the neural network
loss = blocks[-1] # Loss function
module_list = nn.ModuleList()
index = 0 #indexing blocks helps with implementing route layers (skip connections)
prev_filters = 3
output_filters = []
for x in modules:
module = nn.Sequential()
#If it's a convolutional layer
if (x["type"] == "convolutional"):
#Get the info about the layer
activation = x["activation"]
try:
batch_normalize = int(x["batch_normalize"])
bias = False
except:
batch_normalize = 0
bias = True
filters= int(x["filters"])
padding = int(x["pad"])
kernel_size = int(x["size"])
stride = int(x["stride"])
if padding:
pad = (kernel_size - 1) // 2
else:
pad = 0
#Add the convolutional layer
conv = nn.Conv2d(prev_filters, filters, kernel_size, stride, pad, bias = bias)
module.add_module("conv_{0}".format(index), conv)
#Add the Batch Norm Layer
if batch_normalize:
bn = nn.BatchNorm2d(filters)
module.add_module("batch_norm_{0}".format(index), bn)
#Check the activation.
#It is either Linear or a Leaky ReLU for YOLO
if activation == "leaky":
activn = nn.LeakyReLU(0.1, inplace = True)
module.add_module("leaky_{0}".format(index), activn)
elif (x["type"] == "maxpool"): #if it is a max pooling layer
#Both YOLO f/ PASCAL and COCO don't use 2X2 pooling with stride 1
#Tiny-YOLO does use it
kernel_size = int(x["size"])
stride = int(x["stride"])
if stride > 1:
pool = nn.MaxPool2d(kernel_size, stride)
else:
pool = MaxPoolStride1(kernel_size)
module.add_module("pool_{0}".format(index), pool)
#If it is a route layer
elif (x["type"] == "route"):
x["layers"] = x["layers"].split(',')
start = int(x["layers"][0])
try:
end = int(x["layers"][1])
except:
end = 0
route = RouteLayer(start, end)
module.add_module("route_{0}".format(index), route)
if end < 0:
filters = output_filters[index + start] + output_filters[index + end]
else:
output_filters[index + start]
filters= output_filters[index + start]
#If it's a reorganisation layer (Identity mappings in ResNet)
elif (x["type"] == "reorg"):
stride = int(x["stride"])
reorg = ReOrgLayer(stride)
module.add_module("reorg_{0}".format(index), reorg)
filters = filters*stride*stride
module_list.append(module)
prev_filters = filters
output_filters.append(filters)
index += 1
return (inp_info, module_list, loss)
class Darknet(nn.Module):
def __init__(self, cfgfile):
super(Darknet, self).__init__()
self.blocks = parse_cfg(cfgfile)
self.inp, self.module_list, self.loss = create_modules(self.blocks)
self.header = torch.IntTensor([0,0,0,0])
self.seen = 0
anchors = self.loss["anchors"]
anchors = anchors.split(',')
anchors = [float(x.lstrip()) for x in anchors]
anchors = [[anchors[i], anchors[i+1]] for i in range(0, len(anchors), 2)]
self.anchors = anchors
def get_blocks(self):
return self.blocks
def get_module_list(self):
return self.module_list
def forward(self, x):
outputs = {} #We cache the outputs for the route layer
for i in range(len(self.module_list)):
module_type = (self.blocks[i + 1]["type"])
if module_type == "convolutional" or module_type == "maxpool" or module_type=="reorg":
x = self.module_list[i](x)
outputs[i] = x
elif module_type == "route":
layers = self.blocks[i+1]["layers"]
if len(layers) == 1:
x = outputs[i + int(layers[0])]
else:
start = outputs[i + int(layers[0])]
end = outputs[i + int(layers[1])]
x = torch.cat((start, end), 1)
outputs[i] = x
return x
def load_weights(self, weightfile):
#Open the weights file
fp = open(weightfile, "rb")
#The first 4 values are header information
# 1. Major version number
# 2. Minor Version Number
# 3. Subversion number
# 4. IMages seen
header = np.fromfile(fp, dtype = np.int32, count = 4)
self.header = torch.from_numpy(header)
self.seen = self.header[3]
#The rest of the values are the weights
# Let's load them up
weights = np.fromfile(fp, dtype = np.float32)
ptr = 0
for i in range(len(self.module_list)):
module_type = self.blocks[i + 1]["type"]
if module_type == "convolutional":
model = self.module_list[i]
try:
batch_normalize = int(self.blocks[i+1]["batch_normalize"])
except:
batch_normalize = 0
conv = model[0]
if (batch_normalize):
bn = model[1]
#Get the number of weights of Batch Norm Layer
num_bn_biases = bn.bias.numel()
#Load the weights
bn_biases = torch.from_numpy(weights[ptr:ptr + num_bn_biases])
ptr += num_bn_biases
bn_weights = torch.from_numpy(weights[ptr: ptr + num_bn_biases])
ptr += num_bn_biases
bn_running_mean = torch.from_numpy(weights[ptr: ptr + num_bn_biases])
ptr += num_bn_biases
bn_running_var = torch.from_numpy(weights[ptr: ptr + num_bn_biases])
ptr += num_bn_biases
#Cast the loaded weights into dims of model weights.
bn_biases = bn_biases.view_as(bn.bias.data)
bn_weights = bn_weights.view_as(bn.weight.data)
bn_running_mean = bn_running_mean.view_as(bn.running_mean)
bn_running_var = bn_running_var.view_as(bn.running_var)
#Copy the data to model
bn.bias.data.copy_(bn_biases)
bn.weight.data.copy_(bn_weights)
bn.running_mean.copy_(bn_running_mean)
bn.running_var.copy_(bn_running_var)
else:
#Number of biases
num_biases = conv.bias.numel()
#Load the weights
conv_biases = torch.from_numpy(weights[ptr: ptr + num_biases])
ptr = ptr + num_biases
#reshape the loaded weights according to the dims of the model weights
conv_biases = conv_biases.view_as(conv.bias.data)
#Finally copy the data
conv.bias.data.copy_(conv_biases)
#Let us load the weights for the Convolutional layers
num_weights = conv.weight.numel()
#Do the same as above for weights
conv_weights = torch.from_numpy(weights[ptr:ptr+num_weights])
ptr = ptr + num_weights
conv_weights = conv_weights.view_as(conv.weight.data)
conv.weight.data.copy_(conv_weights)
def save_weights(self, savedfile, cutoff = 0):
if cutoff <= 0:
cutoff = len(self.blocks) - 1
fp = open(savedfile, 'wb')
# Attach the header at the top of the file
self.header[3] = self.seen
header = self.header
header = header.numpy()
header.tofile(fp)
# Now, let us save the weights
for i in range(len(self.module_list)):
module_type = self.blocks[i+1]["type"]
if (module_type) == "convolutional":
model = self.module_list[i]
try:
batch_normalize = int(self.blocks[i+1]["batch_normalize"])
except:
batch_normalize = 0
conv = model[0]
if (batch_normalize):
bn = model[1]
#If the parameters are on GPU, convert them back to CPU
#We don't convert the parameter to GPU
#Instead. we copy the parameter and then convert it to CPU
#This is done as weight are need to be saved during training
cpu(bn.bias.data).numpy().tofile(fp)
cpu(bn.weight.data).numpy().tofile(fp)
cpu(bn.running_mean).numpy().tofile(fp)
cpu(bn.running_var).numpy().tofile(fp)
else:
cpu(conv.bias.data).numpy().tofile(fp)
#Let us save the weights for the Convolutional layers
cpu(conv.weight.data).numpy().tofile(fp)