Surya Sharma

Machine Learning Applications | Computer Vision

Inference in C using tensorflow

This guide is a part of a series of articles that show you how to save a Keras model and use it for predictions in a Visual Studio C program.

Assuming you’ve set up your Visual Studio project correctly, we are now ready to use the frozen model (.pb file) to make a prediction in C.

If you do not have the .pb file, you can download it here.

This code is adapted from Patrick Wieschollek‘s inference code, and is similar, except for some parts removed (his code does not use a frozen graph, and thus also loads a checkpoint.)

Change your Visual Studio project created in the last guide to this code, and run it. You should see the value of 0.6984, which is the same as the output for the keras model.

Removing log messages

If things worked as expected, your program will print a lot of messages (above) that you don’t really need.

To change this, go back to the environment variable settings in Windows, and set the TF_CPP_MIN_LOG_LEVEL flag to 3. (You might need to restart visual studio or windows to see an effect).

Moving on

And that’s it! You are now ready to work with more complicated models in keras, or run inferences on multiple samples at a time. The libtensorflow C API and Patwie’s github should have more examples if you need them.

Next Post

Previous Post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2020 Surya Sharma

Theme by Anders Norén