Training with keras+tensorflow in python and predict in C/C++

Viewed 3206

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.

4 Answers

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.

You can use cv::dnn module in opencv3.2. See example in the opencv samples.

In simple steps you can do this -

  1. Save Keras model into text files in python
  2. In C++/C, read those text files, and run the model in a multithreading environment.

Above Steps in detail

  1. 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) '''

  2. In c++/c projects, include keras.h /keras.cpp files.

  3. 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++;
            
          }
         
       }

'''

  1. 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);
    

    '''

  2. 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.

'''

Related