/
main.py
executable file
·129 lines (104 loc) · 3.42 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 8 17:13:10 2018
@author: quinn
"""
import h5py
import json
import numpy as np
from model import model
## Section on reading weights
def read_weights(weights):
out = {}
if isinstance(weights, h5py.Dataset):
return np.asarray(weights)
for k in weights.keys():
out[k] = read_weights(weights[k])
return out
## Section on reading config
def read_config(config):
out = [[],[]]
con = json.loads(config.decode('utf-8'))
if isinstance(con['config'],list):
return read_layers(con['config'])
m = con['config']['layers']
for layer in m:
a, b = read_model(layer)
out[0] += a
out[1] += b
return out
def read_model(m):
out = [[],[]]
a, b = read_layers(m['config'])
out[0] += a
out[1] += b
return out
def read_layers(layers):
out = [[],[]]
if isinstance(layers,dict):
return read_layer(layers)
elif isinstance(layers,list):
for l in layers:
a, b = read_layers(l)
out[0] += a
out[1] += b
return out
def read_layer(layer):
if 'config' in layer.keys():
return [ [layer['config']['name'] ], [layer['config']] ]
elif 'name' in layer.keys():
return [ [layer['name'] ], [layer] ]
raise Exception(" unable to parse layer ")
## Section on building model
def build_model(filename, name='NONE'):
m = model(name)
file = h5py.File(filename)
w_index, layers, c_index = None, None, None
for k in file.keys():
if 'model_weights' in k:
w_index = read_weights(file[k])
if w_index is None:
raise Exception("no model weights read")
if 'model_config' in file.attrs.keys():
layers, c_index = read_config(file.attrs['model_config'])
for i,n in enumerate(layers):
if n in w_index.keys():
print(n)
m.add_layer(n, c_index[i], w_index[n])
else:
m.add_layer(n,c_index[i],None)
file.close()
return m
def convert_model(filename, name='NONE', path='./', verbose=True):
m = build_model(filename, name)
if verbose:
print(m.p_def())
print(m.p_func_call())
print(m.p_header())
file = open( path + name + '.c','w')
file.write("""/**This file was auto generated using the NN 2 CMSIS library
* More information can be found at github.com/quinnabrvau/Keras_2_CMSIS
**/\n""")
file.write('#include "' + name + '.h"\n')
file.write(m.p_def())
file.write(m.p_func_call())
file.close()
file = open( path + name + '.h','w')
file.write(m.p_header())
file.close()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description="program to takes keras files and converts them to strive C, H files")
parser.add_argument('model', help="path to source model")
parser.add_argument('out_name', default='_no_name_', help="prefix for function calls and file name")
parser.add_argument('out_path', default='./', help="path to put files")
# parser.add_argument('-V','--verbose')
# parser.add_argument('-t','--test')
args = parser.parse_args()
print("saving output in",args.out_path + args.out_name + '.c',
' and ', args.out_path + args.out_name + '.h')
convert_model(args.model,
path = args.out_path,
name = args.out_name,
verbose = True)