MCPcopy Create free account
hub / github.com/commaai/research / to_json

Function to_json

models/utils.py:69–130  ·  view source on GitHub ↗
(output_path, *layers)

Source from the content-addressed store, hash-verified

67
68
69def to_json(output_path, *layers):
70 with open(output_path, "w") as layer_f:
71 lines = ""
72 for w, b, bn in layers:
73 layer_idx = w.name.split('/')[0].split('h')[1]
74
75 B = b.eval()
76
77 if "lin/" in w.name:
78 W = w.eval()
79 depth = W.shape[1]
80 else:
81 W = np.rollaxis(w.eval(), 2, 0)
82 depth = W.shape[0]
83
84 biases = {"sy": 1, "sx": 1, "depth": depth, "w": ['%.2f' % elem for elem in list(B)]}
85 if bn != None:
86 gamma = bn.gamma.eval()
87 beta = bn.beta.eval()
88
89 gamma = {"sy": 1, "sx": 1, "depth": depth, "w": ['%.2f' % elem for elem in list(gamma)]}
90 beta = {"sy": 1, "sx": 1, "depth": depth, "w": ['%.2f' % elem for elem in list(beta)]}
91 else:
92 gamma = {"sy": 1, "sx": 1, "depth": 0, "w": []}
93 beta = {"sy": 1, "sx": 1, "depth": 0, "w": []}
94
95 if "lin/" in w.name:
96 fs = []
97 for w in W.T:
98 fs.append({"sy": 1, "sx": 1, "depth": W.shape[0], "w": ['%.2f' % elem for elem in list(w)]})
99
100 lines += """
101 var layer_%s = {
102 "layer_type": "fc",
103 "sy": 1, "sx": 1,
104 "out_sx": 1, "out_sy": 1,
105 "stride": 1, "pad": 0,
106 "out_depth": %s, "in_depth": %s,
107 "biases": %s,
108 "gamma": %s,
109 "beta": %s,
110 "filters": %s
111 };""" % (layer_idx.split('_')[0], W.shape[1], W.shape[0], biases, gamma, beta, fs)
112 else:
113 fs = []
114 for w_ in W:
115 fs.append({"sy": 5, "sx": 5, "depth": W.shape[3], "w": ['%.2f' % elem for elem in list(w_.flatten())]})
116
117 lines += """
118 var layer_%s = {
119 "layer_type": "deconv",
120 "sy": 5, "sx": 5,
121 "out_sx": %s, "out_sy": %s,
122 "stride": 2, "pad": 1,
123 "out_depth": %s, "in_depth": %s,
124 "biases": %s,
125 "gamma": %s,
126 "beta": %s,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected