-
Notifications
You must be signed in to change notification settings - Fork 4
/
dump_to_cpp.py
75 lines (62 loc) · 3.12 KB
/
dump_to_cpp.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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
import numpy as np
np.random.seed(1337)
from keras.models import Sequential, model_from_json
import json
import argparse
np.set_printoptions(threshold=np.inf)
parser = argparse.ArgumentParser(description='This is a simple script to dump Keras model into \
simple format suitable for porting into pure C++ model')
parser.add_argument('-a', '--architecture', help="JSON with model architecture", required=True)
parser.add_argument('-w', '--weights', help="Model weights in HDF5 format", required=True)
parser.add_argument('-o', '--output', help="Ouput file name", required=True)
parser.add_argument('-v', '--verbose', help="Verbose", required=False)
args = parser.parse_args()
print('Read architecture from', args.architecture)
print('Read weights from', args.weights)
print('Writing to', args.output)
arch = open(args.architecture).read()
model = model_from_json(arch)
model.load_weights(args.weights)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
arch = json.loads(arch)
with open(args.output, 'w') as fout:
fout.write('layers ' + str(len(model.layers)) + '\n')
layers = []
for ind, l in enumerate(arch["config"]):
if args.verbose:
print(ind, l)
fout.write('layer ' + str(ind) + ' ' + l['class_name'] + '\n')
if args.verbose:
print(str(ind), l['class_name'])
layers += [l['class_name']]
if l['class_name'] == 'Convolution2D':
#fout.write(str(l['config']['nb_filter']) + ' ' + str(l['config']['nb_col']) + ' ' + str(l['config']['nb_row']) + ' ')
#if 'batch_input_shape' in l['config']:
# fout.write(str(l['config']['batch_input_shape'][1]) + ' ' + str(l['config']['batch_input_shape'][2]) + ' ' + str(l['config']['batch_input_shape'][3]))
#fout.write('\n')
W = model.layers[ind].get_weights()[0]
if args.verbose:
print(W.shape)
fout.write(str(W.shape[0]) + ' ' + str(W.shape[1]) + ' ' + str(W.shape[2]) + ' ' + str(W.shape[3]) + ' ' + l['config']['border_mode'] + '\n')
for i in range(W.shape[0]):
for j in range(W.shape[1]):
for k in range(W.shape[2]):
fout.write(str(W[i,j,k]) + '\n')
fout.write(str(model.layers[ind].get_weights()[1]) + '\n')
if l['class_name'] == 'Activation':
fout.write(l['config']['activation'] + '\n')
if l['class_name'] == 'MaxPooling2D':
fout.write(str(l['config']['pool_size'][0]) + ' ' + str(l['config']['pool_size'][1]) + '\n')
#if l['class_name'] == 'Flatten':
# print(l['config']['name'])
if l['class_name'] == 'Dense':
#fout.write(str(l['config']['output_dim']) + '\n')
W = model.layers[ind].get_weights()[0]
if args.verbose:
print(W.shape)
fout.write(str(W.shape[0]) + ' ' + str(W.shape[1]) + '\n')
for w in W:
fout.write(str(w) + '\n')
fout.write(str(model.layers[ind].get_weights()[1]) + '\n')