gosha20777 / keras2cpp Goto Github PK
View Code? Open in Web Editor NEWit's a small library for running trained Keras 2 models from a native C++ code.
License: MIT License
it's a small library for running trained Keras 2 models from a native C++ code.
License: MIT License
I have a trained sample model from there that is converted to unknown keras2cpp_cool_format (???) with keras2cpp.export_model
.
Then I'm stuck with simple task of importing the image in input keras2cpp::Tensor
to get the output keras2cpp::Tensor
from the keras2cpp::Model
.
keras2cpp/src/layers/conv2d.cc
Lines 7 to 9 in b574755
Hi,
thanks for this work!
I am using the keras2cpp implementation to convert a trained model with keras_retinanet to c++. I have managed to do everything successfully, the ".model" file is generated, the c++ reads an image and passes it to the model and it compiles and links but when executing the compiled keras2cpp binary I get the following error:
I have been doing some debugging and what fails is the read of the model. It is not a matter of permissions.
The only thing I do different when exporting the model after training and validation is that I use "model.fit_generator" instead of "model.fit" as in the example provided by you, but it should not be an issue right? do you have an idea about why this could be happening? my guess is, the model is being generated wrong somehow.
thanks in advance!
Hey,
Reading the project's README.md
it is mentioned that it implements 2D Convolutional layers, but the CNN
is not implemented.
Therefore, what does CNN
stands for?
Looks promising, but is very fragile at the moment.
I'm trying to port char-rnn-keras to C++. I had to add a script to import the model from h5+json:
from keras.models import model_from_json
from keras2cpp import export_model
model_arch_filepath = '[...]/model/arch.json'
model_weights_filepath = '[...]/model/weights.3.h5'
print('Importing model arch from: %s' % model_arch_filepath)
print('Importing model weights from: %s' % model_weights_filepath)
with open(model_arch_filepath, 'r') as f:
model = model_from_json(f.read())
model.load_weights(model_weights_filepath)
export_model(model, 'example.model')
Then I'm following the instructions and running this script:
$ mkdir build
$ cd build
$ python3 ../h5.py
This creates an example.model file, but throws an error:
File "/home/user1/data/src/keras2cpp/keras2cpp.py", line 213, in export_model
f.write(struct.pack('I', LAYERS.index(type(layer)) + 1))
ValueError: tuple.index(x): x not in tuple
Then I tried to run cmake as described, but got a meaningless error:
user1@u320:~/data/src/keras2cpp$ cmake --build .
Error: could not load cache
P.S. Please fix typos in the readme.
This is the model arch file:
{
"class_name": "Sequential",
"keras_version": "2.2.4",
"config": {
"layers": [
{
"class_name": "Embedding",
"config": {
"embeddings_initializer": {
"class_name": "RandomUniform",
"config": {
"maxval": 0.05,
"seed": null,
"minval": -0.05
}
},
"name": "embedding_1",
"dtype": "float32",
"output_dim": 512,
"trainable": true,
"embeddings_regularizer": null,
"input_dim": 65,
"mask_zero": false,
"embeddings_constraint": null,
"batch_input_shape": [
16,
64
],
"activity_regularizer": null,
"input_length": null
}
},
{
"class_name": "LSTM",
"config": {
"recurrent_activation": "hard_sigmoid",
"trainable": true,
"recurrent_initializer": {
"class_name": "Orthogonal",
"config": {
"seed": null,
"gain": 1.0
}
},
"use_bias": true,
"bias_regularizer": null,
"return_state": false,
"unroll": false,
"activation": "tanh",
"bias_initializer": {
"class_name": "Zeros",
"config": {}
},
"units": 256,
"unit_forget_bias": true,
"activity_regularizer": null,
"recurrent_dropout": 0.0,
"kernel_initializer": {
"class_name": "VarianceScaling",
"config": {
"distribution": "uniform",
"scale": 1.0,
"seed": null,
"mode": "fan_avg"
}
},
"kernel_constraint": null,
"dropout": 0.0,
"stateful": true,
"recurrent_regularizer": null,
"name": "lstm_1",
"bias_constraint": null,
"go_backwards": false,
"implementation": 1,
"kernel_regularizer": null,
"return_sequences": true,
"recurrent_constraint": null
}
},
{
"class_name": "Dropout",
"config": {
"rate": 0.2,
"noise_shape": null,
"trainable": true,
"seed": null,
"name": "dropout_1"
}
},
{
"class_name": "LSTM",
"config": {
"recurrent_activation": "hard_sigmoid",
"trainable": true,
"recurrent_initializer": {
"class_name": "Orthogonal",
"config": {
"seed": null,
"gain": 1.0
}
},
"use_bias": true,
"bias_regularizer": null,
"return_state": false,
"unroll": false,
"activation": "tanh",
"bias_initializer": {
"class_name": "Zeros",
"config": {}
},
"units": 256,
"unit_forget_bias": true,
"activity_regularizer": null,
"recurrent_dropout": 0.0,
"kernel_initializer": {
"class_name": "VarianceScaling",
"config": {
"distribution": "uniform",
"scale": 1.0,
"seed": null,
"mode": "fan_avg"
}
},
"kernel_constraint": null,
"dropout": 0.0,
"stateful": true,
"recurrent_regularizer": null,
"name": "lstm_2",
"bias_constraint": null,
"go_backwards": false,
"implementation": 1,
"kernel_regularizer": null,
"return_sequences": true,
"recurrent_constraint": null
}
},
{
"class_name": "Dropout",
"config": {
"rate": 0.2,
"noise_shape": null,
"trainable": true,
"seed": null,
"name": "dropout_2"
}
},
{
"class_name": "LSTM",
"config": {
"recurrent_activation": "hard_sigmoid",
"trainable": true,
"recurrent_initializer": {
"class_name": "Orthogonal",
"config": {
"seed": null,
"gain": 1.0
}
},
"use_bias": true,
"bias_regularizer": null,
"return_state": false,
"unroll": false,
"activation": "tanh",
"bias_initializer": {
"class_name": "Zeros",
"config": {}
},
"units": 256,
"unit_forget_bias": true,
"activity_regularizer": null,
"recurrent_dropout": 0.0,
"kernel_initializer": {
"class_name": "VarianceScaling",
"config": {
"distribution": "uniform",
"scale": 1.0,
"seed": null,
"mode": "fan_avg"
}
},
"kernel_constraint": null,
"dropout": 0.0,
"stateful": true,
"recurrent_regularizer": null,
"name": "lstm_3",
"bias_constraint": null,
"go_backwards": false,
"implementation": 1,
"kernel_regularizer": null,
"return_sequences": true,
"recurrent_constraint": null
}
},
{
"class_name": "Dropout",
"config": {
"rate": 0.2,
"noise_shape": null,
"trainable": true,
"seed": null,
"name": "dropout_3"
}
},
{
"class_name": "TimeDistributed",
"config": {
"layer": {
"class_name": "Dense",
"config": {
"kernel_initializer": {
"class_name": "VarianceScaling",
"config": {
"distribution": "uniform",
"scale": 1.0,
"seed": null,
"mode": "fan_avg"
}
},
"name": "dense_1",
"kernel_constraint": null,
"bias_regularizer": null,
"bias_constraint": null,
"activation": "linear",
"trainable": true,
"kernel_regularizer": null,
"bias_initializer": {
"class_name": "Zeros",
"config": {}
},
"units": 65,
"use_bias": true,
"activity_regularizer": null
}
},
"trainable": true,
"name": "time_distributed_1"
}
},
{
"class_name": "Activation",
"config": {
"activation": "softmax",
"trainable": true,
"name": "activation_1"
}
}
],
"name": "sequential_1"
},
"backend": "tensorflow"
}
Hi,
your library looks very nice. Are you planning on adding support for multiple inputs?
When load InputLayers I get the error
"keras2cpp.py", line 214, in export_model
f.write(struct.pack('I', LAYERS.index(type(layer)) + 1))
ValueError: tuple.index(x): x not in tuple
I want to deploy keras model to embedded linux system.
I need use arm tool chain to build source code.
Currently, arm tool chain can not support C++17.
Is there c++14 version code?
Thanks for sharing this interesting project!
Ubuntu, Python 3.5.4, Keras 2.2.4
running python3 python_model.py returns this error:
Traceback (most recent call last):
File "python_model.py", line 22, in <module>
from keras2cpp import export_model
File "/usr/local/google/home/erwincoumans/develop/keras2cpp/keras2cpp.py", line 60
f.write(struct.pack(f'={remaining}f', *data[i: i + remaining]))
^
SyntaxError: invalid syntax
Do you have any idea what this is about?
Здравствуйте. Мне раньше не приходилось юзать СMake, поэтому существует вероятность, что я что-то делаю не так.
На хабре вы писали, что эта библиотека создавалась с целью быть использованной в движке UE4. У меня такая же цель.
Во время сборки вот, что происходит в консоли:
`D:\downloads\keras2cpp\build> cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: D:/downloads/keras2cpp/build
PS D:\downloads\keras2cpp\build> cmake --build .
Microsoft (R) Build Engine версии 15.9.21+g9802d43bc3 для .NET Framework
(C) Корпорация Майкрософт (Microsoft Corporation). Все права защищены.cl : командная строка warning D9002: пропуск неизвестного параметра "-g" [D:\downloads\keras2cpp\build\keras2cpp.vcxpro
j]
utils.cc
cl : командная строка warning D9002: пропуск неизвестного параметра "-g" [D:\downloads\keras2cpp\build\keras2cpp.vcxpro
j]
cl : командная строка warning D9002: пропуск неизвестного параметра "-g" [D:\downloads\keras2cpp\build\keras2cpp.vcxpro
j]
D:\downloads\keras2cpp\src\utils.cc(6): warning C4834: отмена возвращаемого значения функции с атрибутом "nodiscard" [D
:\downloads\keras2cpp\build\keras2cpp.vcxproj]
baseLayer.cc
activation.cc
conv1d.cc
conv2d.cc
dense.cc
elu.cc
embedding.cc
flatten.cc
lstm.cc
locally1d.cc
locally2d.cc
maxPooling2d.cc
batchNormalization.cc
model.cc
tensor.cc
cpp_model.cc
Создание кода...
keras2cpp.vcxproj -> D:\downloads\keras2cpp\build\Debug\keras2cpp.exe
PS D:\downloads\keras2cpp\build> ./keras2cpp
./keras2cpp : Имя "./keras2cpp" не распознано как имя командлета, функции, файла сценария или выполняемой программы. Пр
оверьте правильность написания имени, а также наличие и правильность пути, после чего повторите попытку.
строка:1 знак:1
- ./keras2cpp
+ CategoryInfo : ObjectNotFound: (./keras2cpp:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException`
Версия python = 3.7 (x64)
CMake 3.14
Что я делаю не так?
MSVS detects exception in vector 'cannot seek iterator after end' while computing this model
model = keras.Sequential()
model.add(keras.layers.Conv2D(3, 3, input_shape=(8, 8, 1))
Tensor input(8, 8, 1);
input.fill(0.5);
Tensor output = model(x);
Is that possible make VC++ dll from this source?
instead exe file, compile in VC++ dll?
Hello,
Which configs should I use when I compile it with Qt and qmake?
I am getting interesting errors with your default code:
main.cpp:10: error: undefined reference to `keras2cpp::Stream::Stream(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.cpp:11: error: undefined reference to `keras2cpp::Model::Model(keras2cpp::Stream&)'
main.cpp:23: error: undefined reference to `keras2cpp::Tensor::print() const'
Thanks! :)
I am unable to create the program using keras_model_test.cc since it refers to the a number of files in the test directory. Can you please upload all the files so that I may run the benchmark tests.
I am trying to infer a single sample of dimensions (1,28,28,1). Since my dumped model accepts in this format.
I did the following to initialize my data:-
Tensor in{1}{28}{28}{1}; // Creating a 4 dim. test Sample :- Batch_Size = 1, Image_Dim = (28,28), Channel = 1 (for greysacle image)
in.data_ = {{{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{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{1.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{1.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}}};
But I am getting a lot of errors. Which are like these,
Tensor in{1}{28}{28}{1}; // Creating a 4 dim. test Sample :- Batch_Size = 1, Image_Dim = (28,28), Channel = 1 (for greysacle image)
^
/home/ghosh8/project/Demo/keras2cpp/cpp_model.cc:12:25: error: expected ‘;’ before ‘}’ token
Tensor in{1}{28}{28}{1}; // Creating a 4 dim. test Sample :- Batch_Size = 1, Image_Dim = (28,28), Channel = 1 (for greysacle image)
/home/ghosh8/project/Demo/keras2cpp/cpp_model.cc:13:26: error: expected ‘;’ before ‘{’ token
in.data_ = {{{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
/home/ghosh8/project/Demo/keras2cpp/cpp_model.cc:14:20: error: expected unqualified-id before ‘{’ token
{{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}{0.0}}
Could anyone help what I am doing wrong..................?
Привет. Пытаюсь адаптировать keras модель в сишном коде с помощью данной библиотеки. Есть простая модель, на вход подаю картинку 100 на 100.
модель начинается так.
input_shape = (100, 100, 3)
model.add(Conv2D(32, (3, 3), padding="same", input_shape=input_shape))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.25))
гружу картинку из opencv в модель приблизительно таким кодом
keras2cpp::Tensor in{mat.cols , mat.rows , 3};
float *fptr = mat.ptr<float>(0);
in.data_.insert( in.data_.begin() , fptr, fptr + mat.total() );
keras2cpp::Stream mfile("my.model");
keras2cpp::Model _model( mfile );
keras2cpp::Tensor out = _model(in);
out.print();
и получаю
keras2cpp/src/tensor.cc:56] 'dims_ == scale.dims_' failed
на строчке 56 ассерт -- kassert(dims_ == scale.dims_)
print_shape -- (98, 98, 32) для dims_
scale.print_shape -- (32)
bias.print_shape -- (32)
Смотрел в python код пакера модели, там шейпы для тензоров scale и bias должны быть 1d, получается я даю на вход чтото не правильное, но судя по коду Tensor с dim_ = (98, 98, 32) появляется в Conv2D и далее проваливается через Activation без каких либо ошибок. И вот тут происходит непонимание, почему ассерт ясно, а как починить, не очень.
С керасом работаю первую неделю, так что осознаю что оч многого не знаю, прошу подсказки в какую сторону посмотреть
Hi,
I am trying to convert a keras model but getting the following error message.
AssertionError: Unsupported layer type: Conv2D
My code is below and I print the summary of the model.
Regards
Paul
from keras.models import load_model
model = load_model('bayer_1000_BS4_56_56_2.model')
#save model
from keras2cpp import export_model
export_model(model, 'example.model')
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 56, 56, 64) 640
_________________________________________________________________
batch_normalization_1 (Batch (None, 56, 56, 64) 256
_________________________________________________________________
activation_1 (Activation) (None, 56, 56, 64) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 28, 28, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 28, 28, 32) 18464
_________________________________________________________________
batch_normalization_2 (Batch (None, 28, 28, 32) 128
_________________________________________________________________
activation_2 (Activation) (None, 28, 28, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 28, 28, 64) 18496
_________________________________________________________________
batch_normalization_3 (Batch (None, 28, 28, 64) 256
_________________________________________________________________
activation_3 (Activation) (None, 28, 28, 64) 0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 14, 14, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 14, 14, 32) 18464
_________________________________________________________________
batch_normalization_4 (Batch (None, 14, 14, 32) 128
_________________________________________________________________
activation_4 (Activation) (None, 14, 14, 32) 0
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 7, 7, 32) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 7, 7, 16) 4624
_________________________________________________________________
batch_normalization_5 (Batch (None, 7, 7, 16) 64
_________________________________________________________________
activation_5 (Activation) (None, 7, 7, 16) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 7, 7, 32) 4640
_________________________________________________________________
batch_normalization_6 (Batch (None, 7, 7, 32) 128
_________________________________________________________________
activation_6 (Activation) (None, 7, 7, 32) 0
_________________________________________________________________
conv2d_7 (Conv2D) (None, 7, 7, 16) 4624
_________________________________________________________________
batch_normalization_7 (Batch (None, 7, 7, 16) 64
_________________________________________________________________
activation_7 (Activation) (None, 7, 7, 16) 0
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 3, 3, 16) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 144) 0
_________________________________________________________________
dense_1 (Dense) (None, 3) 435
_________________________________________________________________
activation_8 (Activation) (None, 3) 0
=================================================================
Using the sample code with a single Dense layer, the result is different between keras model and c++.
keras model: -0.03101047
c++: -0.0310103893
I would like to know where this decimal difference comes from.
I'm attaching .h5 and model file for easier reproducing.
keras: 2.3.0
tensorflow-gpu: 2.1.0
example.zip
Hello, messaged you before via mail. Following your instructions, I tried the unit test "benchmark" and it gives the same error I got earlier when experimenting with my own model.
Debug Assertion Failed!
Program ...SCCN.exe
File:c:\program files(x86)....\msvc\14.16.27023\include\vector
Line: 122
Expression: cannot seek vector iterator after end
Because I suspected the input dimension being wrong in my model, I managed to make the conv3x3 unit test work just fine. I will run more tests and successively modify the unit test conv3x3 towards the benchmark one and see when it breaks.
EDIT: found the culprit (or at least how to trigger it). I modified the Conv3x3 test to use 1 channel input as follows:
test_x = np.random.rand(10, 3, 3, 1).astype('f').astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential([
Conv2D(1, (3, 3), input_shape=(3, 3, 1)),
Flatten(),
Dense(1)
])
This works. But as soon as I increase the input size to anything larger than the kernel size of the Conv2D layer and load the generated model into the Cpp project.
test_x = np.random.rand(10, 9, 9, 1).astype('f').astype('f')
test_y = np.random.rand(10, 1).astype('f')
model = Sequential([
Conv2D(1, (3, 3), input_shape=(9, 9, 1)),
Flatten(),
Dense(1)
])
This was sufficient for me to trigger the error.
hi there,
I'm trying to run your code with my own model. The input shape for my prediction in python's keras is (1, 96, 114, 1), which works fine.
However when I create the same shaped Tensor in c++ and try to run the prediction I get:
ASSERT [src/layers/conv2d.cc:8] 'in.dims_[2] == weights_.dims_[3]' failed
my c++ code is:
int main() {
auto model = Model::load("example.model");
Tensor in{1, 96, 114, 1};
Tensor out = model(in);
out.print();
return 0;
}
Any ideas?
Hello there,
I want to use this repo in my project. However, I cannot do that. I took src to my include folder. Is that enough? Or may I build this project on the CMAKE?
Thank you.
I have an embedding layer that looks like this:
Embedding( 30, 20, mask_zero=True, weights=wv_matrix, input_length=max_seq_len, trainable=False )
Any input data that contains 0 produces predictions that are different than keras. Input data that does not contain 0 produces same results as keras.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.