pplonski / keras2cpp

This is a bunch of code to port Keras neural network model into pure C++.
MIT License
679 stars 153 forks source link

Support for keras2 #30

Closed Tsieki closed 6 years ago

Tsieki commented 6 years ago

hello! I am using Keras2 and there is a compatibility problem due to conv2d layer which was changed in current version of Keras. Actually, the way it is stored in json file has also changed and as a result the current c++ code does not work as it is.

I made some changes in the code that I attach and now it works, however c++ and python do not provide the same results when conv2d is used. (If these two conv2D layers are omitted the results are the same) Even though the two outcomes are comparable, there are not the same so I would like to ask you if there is something more that needs to be changed.

The main change is the following: in keras1.x convolutiond2D layer's weights are stored and their shape is [nb_filter][depth][row][cols], which gives for the first layer of the example the numbers [4][1][3][3] In contrary, in Keras 2.x conv2D layer's weights are stored and their shape is now [row][cols][depth][nb_filters], which gives the numbers [3][3][1][4] (you can see this in the dumped file created). So, in order to use current cpp I created m_rowsVec() in class keras::LayerConv2D : public Layer. Then, I read the weights from dumped file and i store them there. Afterwards I make the transition to the old format and I store them in m_kernels(). (see lines 71 - 111 in keras_model.cc). The following code remains the same.

The code attached works fine for new Keras2.0.6 but the prediction results are not the same as when I run them in python. Any suggestions? Should they be the same? keras2cpp(keras2 version).zip

wu-ruijie commented 6 years ago

I test your code; with the Theano backend, your code work well python: 0.09988655 0.10504469 0.10044335 0.10128921 0.09823733 0.095519 0.09941912 0.10130119 0.09886824 0.09999127 c++: 0.0998865515 0.105044685 0.100443356 0.101289213 0.0982373282 0.0955190063 0.0994191244 0.101301186 0.0988682434 0.0999912620

with the Tensorflow backend,sorry, it's incorrect python: 0.0998288 0.10554188 0.09975836 0.10162295 0.09870712 0.09481214 0.0988815 0.10205296 0.09908297 0.0997113 c++: 0.0998295173 0.105537765 0.0997576788 0.101601161 0.0986871123 0.0948245302 0.0989128724 0.102043636 0.0990825072 0.0997231528

if train more epochs, the difference greater with the Tensorflow backend

thanks your codes

pplonski commented 6 years ago

It is working with theano backend only - sorry!

wu-ruijie commented 6 years ago

thanks your reply; will you plan to add Tensorflow backend ? thanks

pplonski commented 6 years ago

no, for tensorflow please use tensorflow c++ api directly.

wu-ruijie commented 6 years ago

please show me some " tensorflow c++ api" link; thank you very much!

pplonski commented 6 years ago

take a look at this post https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f