Merge with master works

Signed-off-by:  Micaela Verucchi <micaelaverucchi@gmail.com>
		Davide Sapienza <sapienza.dav@gmail.com>
This commit is contained in:
Micaela Verucchi
2020-04-08 14:59:42 +02:00
16 changed files with 948 additions and 190 deletions
+77
View File
@@ -0,0 +1,77 @@
#include<iostream>
#include "tkDNN/ImuOdom.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 *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";
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";
const char *d1_bin = "../tests/imuodom/layers/dense_4.bin";
int main() {
tk::dnn::ImuOdom ImuNet;
ImuNet.init("../tests/imuodom/layers/");
const int N = 19513;
// 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()*N, &i0_h, &i0_d);
readBinaryFile(i1_bin, dim1.tot()*N, &i1_h, &i1_d);
readBinaryFile(i2_bin, dim2.tot()*N, &i2_h, &i2_d);
dnnType *data;
tk::dnn::dataDim_t dim;
dnnType *out0, *out1;
dnnType *out0_h, *out1_h;
readBinaryFile(o0_bin, ImuNet.odim0.tot()*N, &out0_h, &out0);
readBinaryFile(o1_bin, ImuNet.odim1.tot()*N, &out1_h, &out1);
std::ofstream path("path.txt");
for(int i=0; i<N; i++) {
std::cout<<"i: "<<i<<"\n";
//TIMER_START
// Inference
ImuNet.update(i0_h, i1_h, i2_h);
//TIMER_STOP
// log path
path<<ImuNet.odomPOS(0)<<" "<<ImuNet.odomPOS(1)<<" "<< ImuNet.odomPOS(2)<<"\n";
path.flush();
// Print real test
//printCenteredTitle( (std::string(" CHECK RESULT ") + std::to_string(i) + " ").c_str() , '=');
//ImuNet.odim0.print();
//checkResult(ImuNet.odim0.tot(), out0, ImuNet.o0_d);
//ImuNet.odim1.print();
//checkResult(ImuNet.odim0.tot(), out1, ImuNet.o1_d);
i0_h += ImuNet.dim0.tot();
i1_h += ImuNet.dim1.tot();
i2_h += ImuNet.dim2.tot();
out0 += ImuNet.odim0.tot();
out1 += ImuNet.odim1.tot();
}
return 0;
}
+87
View File
@@ -0,0 +1,87 @@
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
import pickle
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()
indata = pickle.load(open("input.pk", 'rb'))
outdata = pickle.load(open("output.pk", 'rb'))
x_angle = indata[0]
x_gyro = indata[1]
x_acc = indata[2]
[yhat_delta_p, yhat_delta_q] = model.predict(indata, batch_size=1, verbose=1)
predictdata = [yhat_delta_p, yhat_delta_q]
error = outdata[0] - predictdata[0]
print("error delta_p: ", error.sum())
error = outdata[1] - predictdata[1]
print("error delta_q: ", error.sum())
#layer_name = 'dense_4'
#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(1, 3, 0, 2)
x_gyro = x_gyro.transpose(1, 3, 0, 2)
x_acc = x_acc.transpose(1, 3, 0, 2)
#intermediate_output = intermediate_output.transpose(0, 3, 1, 2)
#print("Aggregate:")
#print(intermediate_output.tolist())
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)
+1 -1
View File
@@ -134,7 +134,7 @@ const char *regression_header5 = "../tests/mobilenetv2ssd/layers/regression_head
int main()
{
downloadWeightsifDoNotExist(input_bin, "../tests/mobilenetv2ssd", "https://cloud.hipert.unimore.it/s/B6mj33k7beECXsY/download");
downloadWeightsifDoNotExist(input_bin, "../tests/mobilenetv2ssd", "https://cloud.hipert.unimore.it/s/x4ZfxBKN23zAJQp/download");
int classes = 21;
+38 -27
View File
@@ -1,43 +1,54 @@
import keras
import numpy as np
from keras.models import Sequential
from keras.layers import Input, Dense, Activation, Flatten, Dropout, ELU, Reshape, Lambda
from keras.layers import Input, Dense, Activation, Flatten, Dropout, ELU, Reshape, Lambda, Conv1D
from keras.layers import Bidirectional, CuDNNLSTM
from keras.layers.convolutional import Convolution2D, Convolution3D
from keras.layers.pooling import MaxPooling2D, MaxPooling3D, AveragePooling3D
from keras.models import Sequential, Model
from keras.layers import Cropping2D
import keras.backend.tensorflow_backend as KTF
import struct
from keras.models import Sequential, Model
def dense_model():
model = Sequential()
def bin_write(f, data):
data = data.flatten()
fmt = 'f'*len(data)
bin = struct.pack(fmt, *data)
f.write(bin)
def create_model():
x1 = Input((3, 8), name='x1')
conv = Conv1D(4, 2)(x1)
lstm = Bidirectional(CuDNNLSTM(5, return_sequences=True))(conv)
lstm2 = Bidirectional(CuDNNLSTM(5, return_sequences=False))(lstm)
model = Model([x1], [lstm2])
model.summary()
model.add(Reshape((10, 10, 1), input_shape=(10, 10)))
model.add(Convolution2D(2, (4, 4), subsample=(2, 2),
bias_initializer='random_uniform', activation="relu"))
model.add(Convolution2D(4, (2, 2), subsample=(1, 1),
bias_initializer='random_uniform', activation="relu"))
model.add(Flatten())
model.add(Dense(4, bias_initializer='random_uniform', activation="relu"))
sgd = keras.optimizers.Adam(lr=1e-4, decay=1e-8)
model.compile(optimizer=sgd, loss="mse")
return model
if __name__ == '__main__':
print "DATA FORMAT: ", keras.backend.image_data_format()
print ("DATA FORMAT: ", keras.backend.image_data_format())
model = dense_model()
model.save("net.h5")
model = create_model()
model.save("net.hdf5")
grid = np.random.rand(10,10)
X = grid[None,:,:]
i = np.array(grid.flatten(), dtype=np.float32)
print i
i.tofile("input.bin", format="f")
print "Input: ", X
np.random.seed(2)
x = np.random.rand(1,1,3,8)
r = model.predict( x[0], batch_size=1)
r = np.array([r])
x = x.transpose(0, 3, 1, 2)
#r = r.transpose(0, 3, 1, 2)
print("in: ", np.shape(x))
print("out: ", np.shape(r))
print("output: ", r.tolist())
x = np.array(x.flatten(), dtype=np.float32)
f = open("input.bin", mode='wb')
bin_write(f, x)
r = np.array(r.flatten(), dtype=np.float32)
f = open("output.bin", mode='wb')
bin_write(f, r)
r = model.predict( X, batch_size=1)
print np.shape(r)
print "Result: ", r
print "Result shape: ", np.shape(r)
r.tofile("output.bin", format="f")
+9 -10
View File
@@ -2,22 +2,21 @@
#include "tkdnn.h"
const char *input_bin = "../tests/simple/input.bin";
const char *c0_bin = "../tests/simple/layers/c0.bin";
const char *c1_bin = "../tests/simple/layers/c1.bin";
const char *d2_bin = "../tests/simple/layers/d2.bin";
const char *c0_bin = "../tests/simple/layers/conv1d_1.bin";
const char *l1_bin = "../tests/simple/layers/bidirectional_1.bin";
const char *l2_bin = "../tests/simple/layers/bidirectional_2.bin";
const char *output_bin = "../tests/simple/output.bin";
int main() {
// Network layout
tk::dnn::dataDim_t dim(1, 1, 10, 10, 1);
tk::dnn::dataDim_t dim(1, 8, 1, 3);
tk::dnn::Network net(dim);
tk::dnn::Conv2d l0(&net, 2, 4, 4, 2, 2, 0, 0, c0_bin);
tk::dnn::Activation l1(&net, CUDNN_ACTIVATION_RELU);
tk::dnn::Conv2d l2(&net, 4, 2, 2, 1, 1, 0, 0, c1_bin);
tk::dnn::Activation l3(&net, CUDNN_ACTIVATION_RELU);
tk::dnn::Dense l5(&net, 4, d2_bin);
tk::dnn::Activation l6(&net, CUDNN_ACTIVATION_RELU);
tk::dnn::Conv2d l0(&net, 4, 1, 2, 1, 1, 0, 0, c0_bin);
tk::dnn::LSTM l1(&net, 5, true, l1_bin);
tk::dnn::LSTM l2(&net, 5, false, l2_bin);
net.print();
net.print();
+98 -93
View File
@@ -5,96 +5,89 @@ import numpy as np
import argparse
import tensorflow as tf
import os
import msgpack
import lmdb
import random
import struct
from keras.models import Sequential, Model
def export_dense(name, weights, bias):
print "######## EXPORT", name, "LAYER ########"
print "Original weighs:"
print weights
print bias, "\n"
def bin_write(f, data):
data = data.flatten()
fmt = 'f'*len(data)
bin = struct.pack(fmt, *data)
f.write(bin)
#input, filters
I, C = np.shape(weights)
B = np.shape(bias)
print "w shape: ", I, C
print "b shape: ", B
def export_layer(name, weights, bias):
print ("######## EXPORT", name, "LAYER ########")
wgs = [ [ j[i] for j in weights ] for i in xrange(C) ]
wgs = np.array(wgs, dtype=np.float32)
print "REPOSITIONED WEIGHTS:"
print wgs
print("wgs pretranpose: ", np.shape(weights))
# convert NHWC to NCHW
if(weights.ndim == 4):
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" )
print("weights: ", np.shape(weights))
print("bias: ", np.shape(bias))
weights = np.array(weights.flatten(), dtype=np.float32)
bias = np.array(bias, dtype=np.float32)
wgs.tofile(name + ".bin", format="f")
bias.tofile(name + ".bias.bin", format="f")
print "WEIGHTS saved\n"
print(len(weights) + len(bias))
def export_conv2d(name, weights, bias):
print "######## EXPORT", name, "LAYER ########"
print "Original weighs:"
print weights
print bias, "\n"
# height, width, input, filters
H, W, N, C = np.shape(weights)
B = np.shape(bias)
print "w shape: ", N, C, H, W
print "b shape: ", B
f = open(name + ".bin", mode='wb')
bin_write(f, weights)
bin_write(f, bias)
print ("WEIGHTS saved\n")
wgs = weights.transpose()
wgs = wgs.transpose(0, 1, 3, 2)
print "Final shape:", np.shape(wgs)
wgs = np.array(wgs.flatten(), dtype=np.float32)
print "REPOSITIONED WEIGHTS:"
print wgs
def export_bidir(name, params, paramsb):
print ("######## EXPORT", name, "LAYER ########")
f = open(name + ".bin", mode='wb')
bias = np.array(bias, dtype=np.float32)
wgs.tofile(name + ".bin", format="f")
bias.tofile(name + ".bias.bin", format="f")
print "WEIGHTS saved\n"
def export_conv3d(name, weights, bias):
print "######## EXPORT", name, "LAYER ########"
print "Original weighs:"
print weights
print bias, "\n"
print np.shape(weights)
# height, width, input, thickness, filters
H, W, T, N, C = np.shape(weights)
B = np.shape(bias)
print "w shape: ", T, C, H, W #thickness is number of images for cudnn
print "b shape: ", B
wgs = weights.transpose()
wgs = wgs.transpose(0, 1, 4, 3, 2)
print "Final shape:", np.shape(wgs)
wgs = np.array(wgs.flatten(), dtype=np.float32)
print "REPOSITIONED WEIGHTS:"
print wgs
bias = np.array(bias, dtype=np.float32)
wgs.tofile(name + ".bin", format="f")
bias.tofile(name + ".bias.bin", format="f")
print "WEIGHTS saved\n"
def get_session(gpu_fraction=0.5):
gpu_options = tf.GPUOptions(allow_growth=True)
#per_process_gpu_memory_fraction=gpu_fraction)
return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
print("FORWARD")
ker = params[0]
rec_ker = params[1]
bias = params[2]
print ("export kernels: ", np.shape(ker))
units = np.shape(ker)[1] // 4
bin_write(f, ker[:,:units])
bin_write(f, ker[:,units:units*2])
bin_write(f, ker[:,units*2:units*3])
bin_write(f, ker[:,units*3:])
print ("export recurrent kernels: ", np.shape(rec_ker))
bin_write(f, rec_ker[:,:units])
bin_write(f, rec_ker[:,units:units*2])
bin_write(f, rec_ker[:,units*2:units*3])
bin_write(f, rec_ker[:,units*3:])
print ("export kernels: ", np.shape(ker))
bin_write(f, bias)
print("WEIGHTS saved\n")
print("BACKWARD")
ker = paramsb[0]
rec_ker = paramsb[1]
bias = paramsb[2]
print ("export kernels: ", np.shape(ker))
units = np.shape(ker)[1] // 4
bin_write(f, ker[:,:units])
bin_write(f, ker[:,units:units*2])
bin_write(f, ker[:,units*2:units*3])
bin_write(f, ker[:,units*3:])
print ("export recurrent kernels: ", np.shape(rec_ker))
bin_write(f, rec_ker[:,:units])
bin_write(f, rec_ker[:,units:units*2])
bin_write(f, rec_ker[:,units*2:units*3])
bin_write(f, rec_ker[:,units*3:])
print ("export kernels: ", np.shape(ker))
bin_write(f, bias)
print("WEIGHTS saved\n")
#https://github.com/fchollet/keras/wiki/Converting-convolution-kernels-from-Theano-to-TensorFlow-and-vice-versa
if __name__ == '__main__':
KTF.set_session(get_session())
print("DATA FORMAT: ", keras.backend.image_data_format())
parser = argparse.ArgumentParser(description='KERAS WEIGHTS EXPORTER TO CUDNN')
parser.add_argument('model',type=str,
@@ -103,31 +96,43 @@ if __name__ == '__main__':
args = parser.parse_args()
print "DATA FORMAT: ", keras.backend.image_data_format()
print("DATA FORMAT: ", keras.backend.image_data_format())
print "Load model: ", args.model
print("Load model: ", args.model)
model = load_model(args.model)
model.summary()
weights = model.get_weights()
ws = np.shape(weights)
print "Weights shape:", ws
print("Weights shape:", ws)
if not os.path.exists(args.output):
os.makedirs(args.output)
num = 0
name_num = 0
for l in model.layers:
name = l.name
if name.startswith("conv3d"):
export_conv3d(args.output + "/conv" + str(name_num), weights[num], weights[num+1])
elif name.startswith("conv2d"):
export_conv2d(args.output + "/conv" + str(name_num), weights[num], weights[num+1])
elif name.startswith("dense"):
export_dense(args.output + "/dense" + str(name_num), weights[num], weights[num+1])
else:
print "skip:", name, "has no weights"
continue
name_num += 1
num += 2
print("\n\nNAME: ", l.name)
print("input: ", l.input_shape, " output: ", l.output_shape)
wgs = l.get_weights()
print("wgs num: ", len(wgs))
name = l.name
if name.startswith("conv3d"):
export_layer(args.output + "/" + name, wgs[0], wgs[1])
elif name.startswith("conv2d"):
export_layer(args.output + "/" + name, wgs[0], wgs[1])
elif name.startswith("conv1d"):
export_layer(args.output + "/" + name, wgs[0], wgs[1])
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, l.forward_layer.get_weights(), l.backward_layer.get_weights())
else:
print ("skip:", name, "has no weights")
continue