shreshthtuli / keras-py2cpp

A simple tool to convert trained python keras models to cpp for forward pass
2 stars 0 forks source link

Work with CNN/LSTM? #1

Open sserg94 opened 5 years ago

sserg94 commented 5 years ago

Hello Shreshthtuli! Great Work! I need to create simple neural Network for one word recognition from wav/mfcc with dimension(20 frames per 11 numbers: 20х11) (on microcontroller board). Can i using your library with one-two CNN/LSTM layers network (i think it prefer for my task). Or your recommendation ?

I try with CNN layers:

model = Sequential() model.add(Convolution2D(4, 2, 2, border_mode='valid', input_shape=(20, 11, 1))) model.add(Activation('relu')) model.add(Convolution2D(6, 2, 2, border_mode='valid')) model.add(Activation('relu')) model.add(Convolution2D(12, 2, 2, border_mode='valid')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(1, 1))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(6)) model.add(Activation('relu')) model.add(Dropout(0.25)) model.add(Dense(2)) model.add(Activation('relu')) model.add(Dropout(0.4)) model.add(Dense(num_classes)) model.add(Activation('softplus')) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])

test.cc: // Create a 1D Tensor on length 10 for input data. Tensor in(11, 20); in.data_ = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}};

// Run prediction.
Tensor out;
model.Apply(&in, &out);

bash-3.2$ ./a.out KASSERT: keras_model.cc(235): Input 'depth' doesn't match kernel 'depth' KASSERT: keras_model.cc(678): Failed to apply layer 0 KASSERT: test.cc(39): Failed to apply

and with LSTM:

n_timesteps = 10

define LSTM

model = Sequential() model.add(LSTM(20, input_shape=(n_timesteps, 1), return_sequences=True)) model.add(TimeDistributed(Dense(1, activation='sigmoid'))) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])

kerasify.py returns: Traceback (most recent call last): File "test_model.py", line 71, in export_model(model, 'yinsh.model') File "/kerasify.py", line 133, in export_model inner_activation = layer.get_config()['inner_activation'] KeyError: 'inner_activation'

Can you please me to build right NN for convert to C++ with your library.

Thank you!

shreshthtuli commented 5 years ago

Hi sserg94! Thanks for the feedback! I have not tested this for LSTM yet. Let me look in more detail, and get back soon.