LSTM cudnn test
This commit is contained in:
+33
-27
@@ -1,43 +1,49 @@
|
||||
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.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((6, 16), name='x1')
|
||||
conv = Conv1D(4, 2)(x1)
|
||||
model = Model([x1], [conv])
|
||||
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
|
||||
x = np.random.rand(1,1,6,16)
|
||||
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))
|
||||
|
||||
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")
|
||||
|
||||
@@ -2,23 +2,15 @@
|
||||
#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 *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, 16, 1, 6);
|
||||
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::Flatten l4(&net);
|
||||
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);
|
||||
|
||||
// Load input
|
||||
dnnType *data;
|
||||
|
||||
Reference in New Issue
Block a user