triple dense example

This commit is contained in:
Francesco Gatti
2017-06-28 12:37:20 +00:00
parent a86df107be
commit 8bf0b0257e
4 changed files with 202 additions and 33 deletions
+1
View File
@@ -2,3 +2,4 @@
build/ build/
.vscode/ .vscode/
*.bin *.bin
*.pyc
+21 -21
View File
@@ -8,37 +8,37 @@ from keras.layers.pooling import MaxPooling2D, MaxPooling3D
from keras.models import Sequential, Model from keras.models import Sequential, Model
from keras.layers import Cropping2D from keras.layers import Cropping2D
import keras.backend.tensorflow_backend as KTF import keras.backend.tensorflow_backend as KTF
from weights_exporter import *
def dense_model():
def dense_model(inp, out):
model = Sequential() model = Sequential()
model.add(Dense(out, input_shape=(1, inp))) model.add(Dense(256, input_shape=(1, 512)))
model.add(ELU()) model.add(ELU())
model.add(Dense(32))
model.add(ELU())
model.add(Dense(2))
sgd = keras.optimizers.Adam(lr=1e-4, decay=1e-8) sgd = keras.optimizers.Adam(lr=1e-4, decay=1e-8)
model.compile(optimizer=sgd, loss="mse") model.compile(optimizer=sgd, loss="mse")
return model return model
if __name__ == '__main__': if __name__ == '__main__':
model = dense_model(8, 2) model = dense_model()
wg = model.get_weights() wg = model.get_weights()
w = np.squeeze(wg[0])
w = np.array([ i[0] for i in w ] + [ i[1] for i in w ], dtype=np.float32) export_dense("dense0", wg[0], wg[1])
b = np.squeeze(wg[1]) export_dense("dense1", wg[2], wg[3])
export_dense("dense2", wg[4], wg[5])
print "weigths: ", w
print "bias: ", b model.set_weights(wg)
w.tofile("dense.bin", format="f")
b.tofile("dense.bias.bin", format="f")
X = np.array([[[0,1,2,3,4,5,6,7]]], dtype=np.float32) X = np.random.rand(1, 512)
i = np.squeeze(X[0][0]) i = np.array(X, dtype=np.float32)
print "input: ", i
i.tofile("input.bin", format="f") i.tofile("input.bin", format="f")
r = model.predict( X, batch_size=1) print "Input: ", i
r = model.predict( X[None, :], batch_size=1)
print "Result: ", r print "Result: ", r
print "Result shape: ", np.shape(r) print "Result shape: ", np.shape(r)
+28 -12
View File
@@ -1,24 +1,40 @@
#include<iostream> #include<iostream>
#include "Layer.h" #include "Layer.h"
const char *input_bin = "../tests/input.bin";
const char *d0_bin = "../tests/dense0.bin";
const char *d0_bias_bin = "../tests/dense0.bias.bin";
const char *d1_bin = "../tests/dense1.bin";
const char *d1_bias_bin = "../tests/dense1.bias.bin";
const char *d2_bin = "../tests/dense2.bin";
const char *d2_bias_bin = "../tests/dense2.bias.bin";
int main() { int main() {
// Network layout
tkDNN::Network net; tkDNN::Network net;
tkDNN::dataDim_t dim(1, 8, 1, 1); tkDNN::dataDim_t dim(1, 512, 1, 1);
tkDNN::Dense d(&net, dim, 2, "../tests/dense.bin", "../tests/dense.bias.bin"); tkDNN::Dense d0 (&net, dim, 256, d0_bin, d0_bias_bin);
tkDNN::Activation a(&net, d.output_dim, tkDNN::ACTIVATION_ELU); tkDNN::Activation a0 (&net, d0.output_dim, tkDNN::ACTIVATION_ELU);
tkDNN::Dense d1 (&net, a0.output_dim, 32, d1_bin, d1_bias_bin);
tkDNN::Activation a1 (&net, d1.output_dim, tkDNN::ACTIVATION_ELU);
tkDNN::Dense d2 (&net, a1.output_dim, 2, d2_bin, d2_bias_bin);
// Load input
value_type *data; value_type *data;
value_type *input_h; value_type *input_h;
readBinaryFile("../tests/input.bin", 8, &input_h, &data); readBinaryFile(input_bin, dim.tot(), &input_h, &data);
dim.print();
data = d.infer(dim, data);
dim.print();
data = a.infer(dim, data);
dim.print();
dim.print(); //print initial dimension
// Inference
data = d0.infer(dim, data); dim.print();
data = a0.infer(dim, data); dim.print();
data = d1.infer(dim, data); dim.print();
data = a1.infer(dim, data); dim.print();
data = d2.infer(dim, data); dim.print();
// Print result
printDeviceVector(dim.tot(), data); printDeviceVector(dim.tot(), data);
return 0; return 0;
} }
+152
View File
@@ -0,0 +1,152 @@
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 msgpack
import lmdb
import random
def export_dense(name, weights, bias):
print "######## EXPORT", name, "LAYER ########"
print "Original weighs:"
print weights
print bias, "\n"
#input, filters
I, C = np.shape(weights)
B = np.shape(bias)
print "w shape: ", I, C
print "b shape: ", B
wgs = [ [ j[i] for j in weights ] for i in xrange(C) ]
wgs = np.array(wgs, 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 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
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
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))
#https://github.com/fchollet/keras/wiki/Converting-convolution-kernels-from-Theano-to-TensorFlow-and-vice-versa
if __name__ == '__main__':
KTF.set_session(get_session())
parser = argparse.ArgumentParser(description='KERAS WEIGHTS EXPORTER TO CUDNN')
parser.add_argument('model',type=str,
help='Path to model h5 file. Model should be on the same path.')
parser.add_argument('layers', type=str, help="layers list [ dense, conv2d ]", nargs='+')
parser.add_argument('--output', type=str, help="output directory", default="layers")
parser.add_argument('--test_db', type=str, help="input db to test", default=None)
args = parser.parse_args()
print "DATA FORMAT: ", keras.backend.image_data_format()
print "Load model: ", args.model
model = load_model(args.model)
weights = model.get_weights()
ws = np.shape(weights)
print "Weights shape:", ws
if not os.path.exists(args.output):
os.makedirs(args.output)
num = 0
name_num = 0
for i in args.layers:
if i == "conv3d":
export_conv3d(args.output + "/conv" + str(name_num), weights[num], weights[num+1])
elif i == "conv2d":
export_conv2d(args.output + "/conv" + str(name_num), weights[num], weights[num+1])
elif i == "dense":
export_dense(args.output + "/dense" + str(name_num), weights[num], weights[num+1])
else:
print "error: ", i, "is not a layer type"
break
name_num += 1
num += 2
if args.test_db != None:
print "Test on db: ", args.test_db
db = lmdb.open(args.test_db, subdir=False, readonly=True, lock=False)
txn = db.begin()
s = random.randint(0, txn.stat()["entries"]-1)
print "camp number: ", s
s = txn.get(str(s))
c = msgpack.unpackb(s)
print "Steer, throttle: ", c["actuators"]
print "Speed (m/s): ", c["speed"]
grid = np.asarray(c["bitmap"], np.float32)
i = np.array(grid.flatten(), dtype=np.float32)
i.tofile(args.output + "input.bin", format="f")
X = grid[None, :, :]
print "Prediction: ", model.predict(X)