-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathyolov2_darknet_parser.py
103 lines (88 loc) · 3.03 KB
/
yolov2_darknet_parser.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
import numpy as np
import chainer
from chainer import cuda, Function, gradient_check, Variable, optimizers, serializers, utils
from chainer import Link, Chain, ChainList
import chainer.functions as F
import chainer.links as L
from chainer import training
from chainer.training import extensions
import argparse
from lib.utils import *
from lib.image_generator import *
from yolov2 import *
parser = argparse.ArgumentParser(description="指定したパスのweightsファイルを読み込み、chainerモデルへ変換する")
parser.add_argument('file', help="オリジナルのyolov2のweightsファイルへのパスを指定")
args = parser.parse_args()
print("loading", args.file)
file = open(args.file, "rb")
dat=np.fromfile(file, dtype=np.float32)[4:] # skip header(4xint)
# load model
print("loading initial model...")
n_classes = 80
n_boxes = 5
last_out = (n_classes + 5) * n_boxes
yolov2 = YOLOv2(n_classes=n_classes, n_boxes=n_boxes)
yolov2.train = True
yolov2.finetune = False
layers=[
[3, 32, 3],
[32, 64, 3],
[64, 128, 3],
[128, 64, 1],
[64, 128, 3],
[128, 256, 3],
[256, 128, 1],
[128, 256, 3],
[256, 512, 3],
[512, 256, 1],
[256, 512, 3],
[512, 256, 1],
[256, 512, 3],
[512, 1024, 3],
[1024, 512, 1],
[512, 1024, 3],
[1024, 512, 1],
[512, 1024, 3],
[1024, 1024, 3],
[1024, 1024, 3],
[3072, 1024, 3],
]
offset=0
for i, l in enumerate(layers):
in_ch = l[0]
out_ch = l[1]
ksize = l[2]
# load bias(Bias.bはout_chと同じサイズ)
txt = "yolov2.bias%d.b.data = dat[%d:%d]" % (i+1, offset, offset+out_ch)
offset+=out_ch
exec(txt)
# load bn(BatchNormalization.gammaはout_chと同じサイズ)
txt = "yolov2.bn%d.gamma.data = dat[%d:%d]" % (i+1, offset, offset+out_ch)
offset+=out_ch
exec(txt)
# (BatchNormalization.avg_meanはout_chと同じサイズ)
txt = "yolov2.bn%d.avg_mean = dat[%d:%d]" % (i+1, offset, offset+out_ch)
offset+=out_ch
exec(txt)
# (BatchNormalization.avg_varはout_chと同じサイズ)
txt = "yolov2.bn%d.avg_var = dat[%d:%d]" % (i+1, offset, offset+out_ch)
offset+=out_ch
exec(txt)
# load convolution weight(Convolution2D.Wは、outch * in_ch * フィルタサイズ。これを(out_ch, in_ch, 3, 3)にreshapeする)
txt = "yolov2.conv%d.W.data = dat[%d:%d].reshape(%d, %d, %d, %d)" % (i+1, offset, offset+(out_ch*in_ch*ksize*ksize), out_ch, in_ch, ksize, ksize)
offset+= (out_ch*in_ch*ksize*ksize)
exec(txt)
print(i+1, offset)
# load last convolution weight(BiasとConvolution2Dのみロードする)
in_ch = 1024
out_ch = last_out
ksize = 1
txt = "yolov2.bias%d.b.data = dat[%d:%d]" % (i+2, offset, offset+out_ch)
offset+=out_ch
exec(txt)
txt = "yolov2.conv%d.W.data = dat[%d:%d].reshape(%d, %d, %d, %d)" % (i+2, offset, offset+(out_ch*in_ch*ksize*ksize), out_ch, in_ch, ksize, ksize)
offset+=out_ch*in_ch*ksize*ksize
exec(txt)
print(i+2, offset)
print("save weights file to yolov2_darknet.model")
serializers.save_hdf5("yolov2_darknet.model", yolov2)