LSTM cudnn test
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#include<iostream>
|
||||
#include "tkdnn.h"
|
||||
|
||||
const char *i0_bin = "../tests/imuodom/layers/input0.bin";
|
||||
const char *i1_bin = "../tests/imuodom/layers/input1.bin";
|
||||
const char *i2_bin = "../tests/imuodom/layers/input2.bin";
|
||||
const char *o0_bin = "../tests/imuodom/layers/output0.bin";
|
||||
const char *o1_bin = "../tests/imuodom/layers/output1.bin";
|
||||
const char *output_bin = "../tests/imuodom/layers/output.bin";
|
||||
|
||||
const char *c0_bin = "../tests/imuodom/layers/conv1d_7.bin";
|
||||
const char *c1_bin = "../tests/imuodom/layers/conv1d_8.bin";
|
||||
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";
|
||||
|
||||
int main() {
|
||||
|
||||
// Network layout
|
||||
tk::dnn::dataDim_t dim0(1, 4, 1, 100);
|
||||
tk::dnn::dataDim_t dim1(1, 3, 1, 100);
|
||||
tk::dnn::dataDim_t dim2(1, 3, 1, 100);
|
||||
|
||||
// Load input
|
||||
dnnType *i0_d, *i1_d, *i2_d;
|
||||
dnnType *i0_h, *i1_h, *i2_h;
|
||||
readBinaryFile(i0_bin, dim0.tot(), &i0_h, &i0_d);
|
||||
readBinaryFile(i1_bin, dim1.tot(), &i1_h, &i1_d);
|
||||
readBinaryFile(i2_bin, dim2.tot(), &i2_h, &i2_d);
|
||||
|
||||
tk::dnn::Network net(dim0);
|
||||
tk::dnn::Input x0 (&net, dim0, i0_d);
|
||||
tk::dnn::Conv2d x0_0(&net, 128, 1, 11, 1, 1, 0, 0, c0_bin);
|
||||
tk::dnn::Conv2d x0_1(&net, 128, 1, 11, 1, 1, 0, 0, c1_bin);
|
||||
tk::dnn::Pooling x0_2(&net, 1, 3, 1, 3, tk::dnn::tkdnnPoolingMode_t::POOLING_MAX);
|
||||
|
||||
tk::dnn::Input x1 (&net, dim1, i1_d);
|
||||
tk::dnn::Conv2d x1_0(&net, 128, 1, 11, 1, 1, 0, 0, c2_bin);
|
||||
tk::dnn::Conv2d x1_1(&net, 128, 1, 11, 1, 1, 0, 0, c3_bin);
|
||||
tk::dnn::Pooling x1_2(&net, 1, 3, 1, 3, tk::dnn::tkdnnPoolingMode_t::POOLING_MAX);
|
||||
|
||||
tk::dnn::Input x2 (&net, dim2, i2_d);
|
||||
tk::dnn::Conv2d x2_0(&net, 128, 1, 11, 1, 1, 0, 0, c4_bin);
|
||||
tk::dnn::Conv2d x2_1(&net, 128, 1, 11, 1, 1, 0, 0, c5_bin);
|
||||
tk::dnn::Pooling x2_2(&net, 1, 3, 1, 3, tk::dnn::tkdnnPoolingMode_t::POOLING_MAX);
|
||||
|
||||
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, "ciao");
|
||||
|
||||
net.print();
|
||||
|
||||
dnnType *data;
|
||||
tk::dnn::dataDim_t dim;
|
||||
|
||||
TIMER_START
|
||||
// Inference
|
||||
data = net.infer(dim, data); dim.print();
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import keras
|
||||
from keras.models import load_model
|
||||
import keras.backend.tensorflow_backend as KTF
|
||||
import numpy as np
|
||||
import argparse
|
||||
import tensorflow as tf
|
||||
import os
|
||||
import random
|
||||
import struct
|
||||
from keras.models import Sequential, Model
|
||||
|
||||
def bin_write(f, data):
|
||||
data = data.flatten()
|
||||
fmt = 'f'*len(data)
|
||||
bin = struct.pack(fmt, *data)
|
||||
f.write(bin)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
|
||||
print("DATA FORMAT: ", keras.backend.image_data_format())
|
||||
|
||||
print("Load model: ", "ferrariS1.hdf5")
|
||||
model = load_model("ferrariS1.hdf5")
|
||||
model.summary()
|
||||
|
||||
weights = model.get_weights()
|
||||
|
||||
x_angle = np.random.rand(1,100,4)
|
||||
x_gyro = np.random.rand(1,100,3)
|
||||
x_acc = np.random.rand(1,100,3)
|
||||
|
||||
[yhat_delta_p, yhat_delta_q] = model.predict([x_angle, x_gyro, x_acc], batch_size=1, verbose=1)
|
||||
|
||||
layer_name = 'bidirectional_3'
|
||||
intermediate_layer_model = Model(inputs=model.input,
|
||||
outputs=model.get_layer(layer_name).output)
|
||||
intermediate_output = intermediate_layer_model.predict([x_angle, x_gyro, x_acc])
|
||||
|
||||
x_angle = np.array([x_angle])
|
||||
x_gyro = np.array([x_gyro])
|
||||
x_acc = np.array([x_acc])
|
||||
intermediate_output = np.array([intermediate_output])
|
||||
|
||||
x_angle = x_angle.transpose(0, 3, 1, 2)
|
||||
x_gyro = x_gyro.transpose(0, 3, 1, 2)
|
||||
x_acc = x_acc.transpose(0, 3, 1, 2)
|
||||
intermediate_output = intermediate_output.transpose(0, 3, 1, 2)
|
||||
|
||||
print("x0: ", np.shape(x_angle))
|
||||
print("out: ",np.shape(intermediate_output))
|
||||
|
||||
x_angle = np.array(x_angle.flatten(), dtype=np.float32)
|
||||
x_gyro = np.array(x_gyro.flatten(), dtype=np.float32)
|
||||
x_acc = np.array(x_acc.flatten(), dtype=np.float32)
|
||||
yhat_delta_p = np.array(yhat_delta_p.flatten(), dtype=np.float32)
|
||||
yhat_delta_q = np.array(yhat_delta_q.flatten(), dtype=np.float32)
|
||||
intermediate_output = np.array(intermediate_output.flatten(), dtype=np.float32)
|
||||
|
||||
|
||||
f = open("layers/input0.bin", mode='wb')
|
||||
bin_write(f, x_angle)
|
||||
f = open("layers/input1.bin", mode='wb')
|
||||
bin_write(f, x_gyro)
|
||||
f = open("layers/input2.bin", mode='wb')
|
||||
bin_write(f, x_acc)
|
||||
f = open("layers/output0.bin", mode='wb')
|
||||
bin_write(f, yhat_delta_p)
|
||||
f = open("layers/output1.bin", mode='wb')
|
||||
bin_write(f, yhat_delta_q)
|
||||
f = open("layers/output.bin", mode='wb')
|
||||
bin_write(f, intermediate_output)
|
||||
|
||||
Reference in New Issue
Block a user