ofxKerasify
openFrameworks addon for Running trained Keras Deep Learning models in C++.
based on moof2k's wonderful kerasify project. https://github.com/moof2k/kerasify
Disclaimer
-
kerasify supports only a subset of keras layers. I'm working on more layers!
-
not thread safe
-
oF-iOS compatible
Example 1: MNIST MLP (example-mnist_mlp)
-
Train MLP model for MNIST using Keras example https://github.com/fchollet/keras/blob/master/examples/mnist_mlp.py
-
Save trained model
model.save('mnist_mlp.h5')
-
"Kerasify" the saved model using python script
$ python convert_keras_model.py mnist_mlp.h5 mnist_mlp.model
-
Copy the converted model into "data" folder of ofApp
-
Load model
kerasify.loadModel("mnist_mlp.model");
- Inference
// Image -> vector
ofPixels pix = img.getPixels();
std::vector<float> input;
for (int i = 0; i < pix.size(); i++){
input.push_back(pix.getData()[i]/255.0);
}
// INFERENCE
output.resize(10); // output vector should be [10]. because we have 10 digits! ;-)
kerasify.predict(input, output);
- Voila!
Example 2: Image Regression for iOS (example-ios-imageregression)
This sample project is for oF iOS
"Image Regression" - "treats the pixels of an image as a learning problem: it takes the (x,y) position on a grid and learns to predict the color at that point using regression to (r,g,b). It's a bit like compression, since the image information is encoded in the weights of the network, but almost certainly not of practical kind :)"" by @karpathy.
Here is his great online demo.
I trained a simple fully-connected 8-layer network for this image of dog.
https://github.com/naotokui/ChainerPainter/blob/master/ChainerPainter.ipynb
You can run the trained model on iOS now!
(Don't forget to use convert_keras_model.py
)
Let's see what happens when you randomly disable one of trained layers.