It is possible to make a fast model with keras with tensor flow as backend and use it to predict in C or C++?
I need to do the prediction inside of a C++ program, but I feel much more comfortable doing the model and the training in keras.
It is possible to make a fast model with keras with tensor flow as backend and use it to predict in C or C++?
I need to do the prediction inside of a C++ program, but I feel much more comfortable doing the model and the training in keras.
Yes, it is possible. TensorFlow provides a stable C API as well as a C++ one.
For more details, you probably want to ask a more specific question.
In simple steps you can do this -
Above Steps in detail
For first step, call save_keras_model_as_text from keras2cpp.py on Github-
''' save_keras_model_as_text(model, open('/content/modeltext.txt', 'w') ) save_keras_model_as_text(model, open('/content/modellayersonlytext.txt', 'w') , noweight=True) '''
In c++/c projects, include keras.h /keras.cpp files.
convert input image into Keras format or read input image into Keras format from a file.
'''
//assume dst is pointer to an image object. This
//can be of CxImage / CImg /OpenCV or of any other
//library.
int h = dst->GetHeight();
int w= dst->GetWidth();;
int *img = new int[h*w*3];
int *img_r = img, *img_g =&img[h*w], *img_b=&img[2*h*w];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w;x++) {
RGBQUAD rgb= dst->GetPixelColor(x,y);
*img_r= rgb.rgbRed; img_r++;
*img_g= rgb.rgbGreen; img_g++;
*img_b = rgb.rgbBlue; img_b++;
}
}
'''
Call ExecuteKerasSegmentation.
'''
//text files in which keras model is saved.
char *modelfile ="modellayersonlytext.txt";
char *weightfile="modeltext.txt";
int *result = ExecuteKerasSegmentation(img, h, w, 3, modelfile, weightfile);
'''
result contains the segmentation result. You may save it into pgm file or convert it into your image library object and use it further.
'''
save_image_pgm("segmentation_map.pgm",result,h,w,127); //127 is scaling factor for binary images.
'''