-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcfg.py
560 lines (505 loc) · 21.2 KB
/
cfg.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import torch
from utils import convert2cpu
import numpy as np
from easydict import EasyDict as edict
from os import path
__C = edict()
cfg = __C
def load_classes(data='voc'):
fname = path.dirname(path.abspath(__file__))
fname = path.join(fname, 'data/{}.names'.format(data))
print(fname)
with open(fname) as f:
classes = [l.strip() for l in f.readlines()]
return classes
__C.dota_classes = ['bridge',
'small-vehicle',
'large-vehicle',
'helicopter',
'roundabout',
'soccer-ball-field',
'swimming-pool',
'plane',
'ship',
'storage-tank',
'baseball-diamond',
'tennis-court',
'basketball-court',
'ground-track-field',
'harbor']
__C.nwpu_classes = ['airplane',
'ship',
'storage-tank',
'baseball-diamond',
'tennis-court',
'basketball-court',
'ground-track-field',
'harbor',
'bridge',
'vehicle']
__C.dior_classes = ['airplane',
'airport',
'baseballfield',
'basketballcourt',
'bridge',
'chimney',
'dam',
'Expressway-Service-area',
'Expressway-toll-station',
'golffield',
'groundtrackfield',
'harbor',
'overpass',
'ship',
'stadium',
'storagetank',
'tenniscourt',
'trainstation',
'vehicle',
'windmill']
# Maximum number of bboxes per category
__C.max_boxes = 50
__C.neg_ratio = 'full'
__C.tuning = False
__C.metayolo = True
__C.repeat = 1
__C.save_interval = 10
__C.multiscale = True
# '1' for image only, '2' for image + mask, '3' for image + mask + croped obj
__C.metain_type = 2
def get_ids(root):
lines = []
with open(root, 'r') as f:
# files = [line.rstrip().split()[-1] for line in f.readlines()]
files = [line.rstrip().split() for line in f.readlines()]
files = [line[-1] for line in files if line[0] in cfg.base_classes]
for file in files:
with open(file, 'r') as f:
lines.extend(f.readlines())
lines = sorted(list(set(lines)))
ids = [l.split('/')[-1].split('.')[0] for l in lines]
# print(ids)
return ids
def get_novels(root, id=None):
if root.endswith('txt'):
if id == 'None':
return []
with open(root, 'r') as f:
novels = f.readlines()
out_novels = novels[int(id)].strip()
if out_novels == '':
return []
else:
return out_novels.split(',')
else:
return root.split(',')
def add_backup(backup, addon):
strs = backup.split('_')
strs[0] += addon
return '_'.join(strs)
def __configure_data(dataopt):
__C.data = dataopt['data']
if dataopt['data'] == 'dota':
__C.classes = __C.dota_classes
__C.save_interval = 100
elif dataopt['data'] == 'nwpu':
__C.classes = __C.nwpu_classes
__C.save_interval = 100
elif dataopt['data'] == 'dior':
__C.classes = __C.dior_classes
__C.save_interval = 100
if 'scale' in dataopt:
__C.multiscale = int(dataopt['scale'])
if 'metain_type' in dataopt:
__C.metain_type = int(dataopt['metain_type'])
if 'tuning' in dataopt:
__C.tuning = bool(int(dataopt['tuning']))
__C.max_epoch = int(dataopt['max_epoch']) if 'max_epoch' in dataopt else 500
__C.repeat = int(dataopt['repeat']) if 'repeat' in dataopt else 100
if __C.max_epoch / __C.repeat <= 20:
__C.save_interval = 1
elif __C.max_epoch / __C.repeat <= 50:
__C.save_interval = 2
elif __C.max_epoch / __C.repeat <= 100:
__C.save_interval = 5
else:
__C.save_interval = 10
__C.shot = 0 if not __C.tuning else int(dataopt['meta'].split('.')[0].split('_')[-1].replace('shot', ''))
print('save_interval', __C.save_interval)
__C.novelid = novelid = dataopt['novelid'] if 'novelid' in dataopt else 'None'
__C.novel_classes = get_novels(dataopt['novel'], novelid)
print('novel classes: {}'.format(__C.novel_classes))
if __C.tuning:
if dataopt['data'] in ['dota', 'nwpu', 'dior']:
__C.base_classes = __C.classes
else:
raise NotImplementedError('Data type {} not found'.format(dataopt['data']))
else:
__C.base_classes = [c for c in __C.classes if c not in __C.novel_classes]
__C.base_ids = [__C.classes.index(c) for c in __C.base_classes]
__C.novel_ids = [__C.classes.index(c) for c in __C.novel_classes]
__C._real_base_ids = [i for i in range(len(__C.classes)) if i not in __C.novel_ids]
print('base_ids', __C.base_ids)
__C.num_gpus = len(dataopt['gpus'].split(','))
__C.neg_ratio = dataopt['neg'] if 'neg' in dataopt else __C.neg_ratio
__C.randmeta = bool(int(dataopt['rand'])) if 'rand' in dataopt else False
__C.metayolo = bool(int(dataopt['metayolo']))
if __C.neg_ratio.isdigit():
__C.neg_ratio = float(__C.neg_ratio)
if __C.neg_ratio.is_integer():
__C.neg_ratio = int(__C.neg_ratio)
# Set up backup dir
__C.backup = dataopt['backup']
if not __C.multiscale:
__C.backup += 'fix'
if __C.metain_type != 2:
__C.backup = add_backup(__C.backup, 'in{}'.format(__C.metain_type))
__C.backup += '_novel{}'.format(novelid)
if cfg.metayolo:
__C.backup = __C.backup + '_neg{}'.format(cfg.neg_ratio)
if cfg.randmeta:
__C.backup += '_rand'
# Get few-shot image ids
cfg.yolo_joint = int(dataopt['joint']) if 'joint' in dataopt else False
if cfg.yolo_joint:
cfg.metaids = get_ids(dataopt['meta'])
shot = int(dataopt['meta'].split('.')[0].split('_')[-1].replace('shot', ''))
__C.backup += '_joint{}'.format(shot)
def __configure_net(netopt):
__C.height = int(netopt['height'])
__C.width = int(netopt['width'])
__C.batch_size = int(netopt['batch'])
def __configure_meta(metaopt):
__C.meta_height = int(metaopt['height'])
__C.meta_width = int(metaopt['width'])
factor = int(metaopt['feat_layer'])
if factor == 0:
__C.mask_height = __C.meta_height
__C.mask_width = __C.meta_width
else:
__C.mask_height = __C.meta_height // factor
__C.mask_width = __C.meta_width // factor
# meta input type
# if factor == 0:
# if __C.metain_type == 1:
# metaopt['channels'] = 3
# elif __C.metain_type == 2:
# metaopt['channels'] = 3
# ##############################
# #metaopt['channels'] = 4
# ##############################
# elif __C.metain_type == 3:
# metaopt['channels'] = 7
# elif __C.metain_type == 4:
# metaopt['channels'] = 6
# else:
# raise NotImplementedError('Meta input type not found: {}'.format(__C.metain_type))
# elif factor == 4:
# if __C.metain_type == 1:
# metaopt['channels'] = 64
# elif __C.metain_type == 2:
# metaopt['channels'] = 65
# elif __C.metain_type == 3:
# metaopt['channels'] = 64*2 + 1
# elif __C.metain_type == 4:
# metaopt['channels'] = 64*2
# else:
# raise NotImplementedError('Meta input type not found: {}'.format(__C.metain_type))
# else:
# raise NotImplementedError('Feat layer not found{}'.format(factor))
__C.config_data = __configure_data
__C.config_meta = __configure_meta
__C.config_net = __configure_net
def parse_cfg(cfgfile):
blocks = []
fp = open(cfgfile, 'r')
block = None
line = fp.readline()
while line != '':
line = line.rstrip()
if line == '' or line[0] == '#':
line = fp.readline()
continue
elif line[0] == '[':
if block:
blocks.append(block)
block = dict()
block['type'] = line.lstrip('[').rstrip(']')
# set default value
if block['type'] == 'convolutional':
block['batch_normalize'] = 0
else:
key,value = line.split('=')
key = key.strip()
if key == 'type':
key = '_type'
value = value.strip()
block[key] = value
line = fp.readline()
if block:
blocks.append(block)
fp.close()
return blocks
def print_cfg(blocks):
print('layer filters size input output');
prev_width = 416
prev_height = 416
prev_filters = 3
out_filters =[]
out_widths =[]
out_heights =[]
ind = -2
for block in blocks:
ind = ind + 1
if block['type'] == 'net' or block['type'] == 'learnet':
prev_width = int(block['width'])
prev_height = int(block['height'])
prev_filters = int(block['channels'])
if block['type'] == 'learnet':
factor = int(block['feat_layer'])
if factor != 0:
prev_width = prev_width // factor
prev_height = prev_height // factor
continue
elif block['type'] == 'convolutional':
filters = int(block['filters'])
kernel_size = int(block['size'])
stride = int(block['stride'])
is_pad = int(block['pad'])
pad = (kernel_size-1)/2 if is_pad else 0
width = (prev_width + 2*pad - kernel_size)/stride + 1
height = (prev_height + 2*pad - kernel_size)/stride + 1
if 'dynamic' in block and int(block['dynamic']) == 1:
name = 'dconv'
else:
name = 'conv'
if 'mask_in' in block and int(block['mask_in']) == 1:
prev_filters += 1
print('%5d %-6s %4d %d x %d / %d %3d x %3d x%4d -> %3d x %3d x%4d' % (ind, name, filters, kernel_size, kernel_size, stride, prev_width, prev_height, prev_filters, width, height, filters))
prev_width = width
prev_height = height
prev_filters = filters
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'maxpool':
pool_size = int(block['size'])
stride = int(block['stride'])
width = prev_width/stride
height = prev_height/stride
print('%5d %-6s %d x %d / %d %3d x %3d x%4d -> %3d x %3d x%4d' % (ind, 'max', pool_size, pool_size, stride, prev_width, prev_height, prev_filters, width, height, filters))
prev_width = width
prev_height = height
prev_filters = filters
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'globalmax':
pool_size = prev_width
stride = 1
width = prev_width/pool_size
height = prev_height/pool_size
filters = prev_filters
print('%5d %-6s %d x %d / %d %3d x %3d x%4d -> %3d x %3d x%4d' % (ind, 'glomax', pool_size, pool_size, stride, prev_width, prev_height, prev_filters, width, height, filters))
prev_width = width
prev_height = height
prev_filters = filters
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'globalavg':
pool_size = prev_width
stride = 1
width = prev_width/pool_size
height = prev_height/pool_size
filters = prev_filters
print('%5d %-6s %d x %d / %d %3d x %3d x%4d -> %3d x %3d x%4d' % (ind, 'gloavg', pool_size, pool_size, stride, prev_width, prev_height, prev_filters, width, height, filters))
prev_width = width
prev_height = height
prev_filters = filters
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'avgpool':
width = 1
height = 1
print('%5d %-6s %3d x %3d x%4d -> %3d' % (ind, 'avg', prev_width, prev_height, prev_filters, prev_filters))
prev_width = width
prev_height = height
prev_filters = filters
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'split':
splits = [int(sz) for sz in block['splits'].split(',')]
filters = splits[-1]
print(('%5d %-6s %3d -> {}' % (ind, 'split', prev_filters)).format(splits))
prev_filters = filters
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'softmax':
print('%5d %-6s -> %3d' % (ind, 'softmax', prev_filters))
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'cost':
print('%5d %-6s -> %3d' % (ind, 'cost', prev_filters))
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'reorg':
stride = int(block['stride'])
filters = stride * stride * prev_filters
width = prev_width/stride
height = prev_height/stride
print('%5d %-6s / %d %3d x %3d x%4d -> %3d x %3d x%4d' % (ind, 'reorg', stride, prev_width, prev_height, prev_filters, width, height, filters))
prev_width = width
prev_height = height
prev_filters = filters
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'route':
layers = block['layers'].split(',')
layers = [int(i) if int(i) > 0 else int(i)+ind for i in layers]
if len(layers) == 1:
print('%5d %-6s %d' % (ind, 'route', layers[0]))
prev_width = out_widths[layers[0]]
prev_height = out_heights[layers[0]]
prev_filters = out_filters[layers[0]]
elif len(layers) == 2:
print('%5d %-6s %d %d' % (ind, 'route', layers[0], layers[1]))
prev_width = out_widths[layers[0]]
prev_height = out_heights[layers[0]]
assert(prev_width == out_widths[layers[1]])
assert(prev_height == out_heights[layers[1]])
prev_filters = out_filters[layers[0]] + out_filters[layers[1]]
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'region':
print('%5d %-6s' % (ind, 'detection'))
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'shortcut':
from_id = int(block['from'])
from_id = from_id if from_id > 0 else from_id+ind
print('%5d %-6s %d' % (ind, 'shortcut', from_id))
prev_width = out_widths[from_id]
prev_height = out_heights[from_id]
prev_filters = out_filters[from_id]
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
elif block['type'] == 'connected':
filters = int(block['output'])
print('%5d %-6s %d -> %3d' % (ind, 'connected', prev_filters, filters))
prev_filters = filters
out_widths.append(1)
out_heights.append(1)
out_filters.append(prev_filters)
elif block['type'] == 'reshape':
inshape = [int(i) for i in block['inshape'].split(',')]
outshape = [int(i) for i in block['outshape'].split(',')]
if outshape[0] == -1:
assert np.prod(inshape) % np.prod(outshape[1:]) == 0
prev_filters = int(np.prod(inshape) / np.prod(outshape[1:]))
else:
prev_filters = outshape[0]
outshape[0] = prev_filters
print('%5d %-6s %s -> %s' % (ind, 'reshape', inshape, outshape))
if len(outshape) == 1:
out_widths.append(1)
out_heights.append(1)
elif len(outshape) == 3:
out_widths.append(outshape[1])
out_heights.append(outshape[2])
else:
raise NotImplementedError()
out_filters.append(prev_filters)
elif block['type'] == 'upsample':
stride = int(block['stride'])
width = prev_width * stride
height = prev_height * stride
print('%5d %-6s %d %3d x %3d x%4d -> %3d x %3d x%4d' % (
ind, 'up', stride, prev_width, prev_height, prev_filters, width, height, filters))
prev_width = width
prev_height = height
prev_filters = filters
out_widths.append(prev_width)
out_heights.append(prev_height)
out_filters.append(prev_filters)
else:
print('unknown type %s' % (block['type']))
def load_conv(buf, start, conv_model):
num_w = conv_model.weight.numel()
if conv_model.bias is not None:
num_b = conv_model.bias.numel()
conv_model.bias.data.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b
conv_model.weight.data.copy_(torch.from_numpy(buf[start:start+num_w])); start = start + num_w
return start
def load_convfromcoco(buf, start, conv_model):
print('------ loading coco to voc ----------')
tmpb = torch.zeros(425)
tmpw = torch.zeros(425, 1024, 1, 1)
num_w = tmpw.numel()
inds = np.concatenate([
np.asarray([i for i in range(5)]),
np.asarray(__C.vocids_in_coco) + 5]
)
allinds = np.concatenate([inds + i * 85 for i in range(5)])
if conv_model.bias is not None:
num_b = tmpb.numel()
tmpb.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b
conv_model.bias.data.copy_(tmpb[allinds]);
tmpw.copy_(torch.from_numpy(buf[start:start+num_w])); start = start + num_w
conv_model.weight.data.copy_(tmpw[allinds]);
return start
def save_conv(fp, conv_model):
if conv_model.weight.is_cuda:
if conv_model.bias is not None:
convert2cpu(conv_model.bias.data).numpy().tofile(fp)
convert2cpu(conv_model.weight.data).numpy().tofile(fp)
else:
if conv_model.bias is not None:
conv_model.bias.data.numpy().tofile(fp)
conv_model.weight.data.numpy().tofile(fp)
def load_conv_bn(buf, start, conv_model, bn_model):
num_w = conv_model.weight.numel()
num_b = bn_model.bias.numel()
bn_model.bias.data.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b
bn_model.weight.data.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b
bn_model.running_mean.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b
bn_model.running_var.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b
conv_model.weight.data.copy_(torch.from_numpy(buf[start:start+num_w])); start = start + num_w
return start
def save_conv_bn(fp, conv_model, bn_model):
if bn_model.bias.is_cuda:
convert2cpu(bn_model.bias.data).numpy().tofile(fp)
convert2cpu(bn_model.weight.data).numpy().tofile(fp)
convert2cpu(bn_model.running_mean).numpy().tofile(fp)
convert2cpu(bn_model.running_var).numpy().tofile(fp)
convert2cpu(conv_model.weight.data).numpy().tofile(fp)
else:
bn_model.bias.data.numpy().tofile(fp)
bn_model.weight.data.numpy().tofile(fp)
bn_model.running_mean.numpy().tofile(fp)
bn_model.running_var.numpy().tofile(fp)
conv_model.weight.data.numpy().tofile(fp)
def load_fc(buf, start, fc_model):
num_w = fc_model.weight.numel()
num_b = fc_model.bias.numel()
fc_model.bias.data.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b
fc_model.weight.data.copy_(torch.from_numpy(buf[start:start+num_w])); start = start + num_w
return start
def save_fc(fp, fc_model):
fc_model.bias.data.numpy().tofile(fp)
fc_model.weight.data.numpy().tofile(fp)
if __name__ == '__main__':
import sys
blocks = parse_cfg('cfg/yolo.cfg')
if len(sys.argv) == 2:
blocks = parse_cfg(sys.argv[1])
print_cfg(blocks)