LSTM params

This commit is contained in:
Francesco Gatti
2020-02-15 20:37:08 +01:00
parent 4fa5d2c231
commit 4746121d43
5 changed files with 111 additions and 25 deletions
+13 -7
View File
@@ -14,6 +14,9 @@ const char *c2_bin = "../tests/imuodom/layers/conv1d_9.bin";
const char *c3_bin = "../tests/imuodom/layers/conv1d_10.bin";
const char *c4_bin = "../tests/imuodom/layers/conv1d_11.bin";
const char *c5_bin = "../tests/imuodom/layers/conv1d_12.bin";
const char *l0_bin = "../tests/imuodom/layers/bidirectional_3.bin";
const char *l1_bin = "../tests/imuodom/layers/bidirectional_4.bin";
const char *d0_bin = "../tests/imuodom/layers/dense_3.bin";
int main() {
@@ -48,8 +51,9 @@ int main() {
tk::dnn::Layer *concat_l[3] = { &x0_2, &x1_2, &x2_2 };
tk::dnn::Route concat (&net, concat_l, 3);
tk::dnn::LSTM lstm0(&net, 128, true, "ciao");
tk::dnn::LSTM lstm1(&net, 128, false, "ciao");
//tk::dnn::LSTM lstm0(&net, 128, true, l0_bin);
//tk::dnn::LSTM lstm1(&net, 128, false, l1_bin);
//tk::dnn::Dense d0 (&net, 3, d0_bin);
net.print();
@@ -62,10 +66,12 @@ int main() {
TIMER_STOP
// Print real test
std::cout<<"\n==== CHECK RESULT ====\n";
dnnType *out;
dnnType *out_h;
readBinaryFile(output_bin, dim.tot(), &out_h, &out);
checkResult(dim.tot(), data, out);
//std::cout<<"\n==== CHECK RESULT ====\n";
//dnnType *out;
//dnnType *out_h;
//readBinaryFile(output_bin, dim.tot(), &out_h, &out);
//checkResult(dim.tot(), data, out);
printDeviceVector(100, data);
return 0;
}
+1
View File
@@ -27,6 +27,7 @@ if __name__ == '__main__':
weights = model.get_weights()
np.random.seed(2)
x_angle = np.random.rand(1,100,4)
x_gyro = np.random.rand(1,100,3)
x_acc = np.random.rand(1,100,3)
+13 -3
View File
@@ -24,6 +24,8 @@ def export_layer(name, weights, bias):
weights = weights.transpose(3,2,0,1)
elif(weights.ndim == 3):
weights = weights.transpose(2,1,0)
elif(weights.ndim == 2):
weights = weights.transpose(1,0)
else:
print("Ndim", weights.ndim)
raise("not implemented with dim" )
@@ -40,13 +42,19 @@ def export_layer(name, weights, bias):
bin_write(f, bias)
print ("WEIGHTS saved\n")
def export_bidir(name, weights):
def export_bidir(name, params):
print ("######## EXPORT", name, "LAYER ########")
for w in weights:
print(np.shape(w))
f = open(name + ".bin", mode='wb')
for w in params:
#w = w.transpose()
print(np.shape(w))
bin_write(f, w)
print("WEIGHTS saved\n")
#https://github.com/fchollet/keras/wiki/Converting-convolution-kernels-from-Theano-to-TensorFlow-and-vice-versa
if __name__ == '__main__':
print("DATA FORMAT: ", keras.backend.image_data_format())
@@ -64,6 +72,7 @@ if __name__ == '__main__':
model = load_model(args.model)
model.summary()
weights = model.get_weights()
ws = np.shape(weights)
@@ -90,6 +99,7 @@ if __name__ == '__main__':
elif name.startswith("dense"):
export_layer(args.output + "/" + name, wgs[0], wgs[1])
elif name.startswith("bidirectional"):
wgs = l.forward_layer.get_weights()
export_bidir(args.output + "/" + name, wgs)
else:
print ("skip:", name, "has no weights")