Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Keras trained model predictions in c#

As title states I'm trying to use my Keras (tf backend) pretrained model for predicitions in c#. What's the best approach?

I've tried IronPython but it gave me errors, after search I found it isn't supported. Simply calling python script won't work since target Windows devices won't have python interpreters installed.

like image 533
temo Avatar asked Nov 19 '22 02:11

temo


1 Answers

This question is somewhat vague, but I'll try to answer it the best I can:

Tensorflow core is written in c++ and exposes an interface, which can be consumed by a client written in whichever language (although yes, the Python one is the most developed so far).

What could you do:

  • You could use a python script to train the keras model and save its computation graph in a file. Then use TensorFlowSharp --- which is the project that contracts with the tensorflow API using c# -- to reload the computation graph and generate predictions with the trained network.
  • If your devices are phones, tablets and such, Tensorflow for mobile could help you too. It would work similarly to the option above. I'm not sure if they offer support to windows phones, though.
  • You mentioned:

    [...] devices won't have python interpreters installed.

    Well, okay. You could, however, define an boot process that would cascade the installation of the necessary dependencies (cpython included). Some applications do that to set databases, for example. In this case, you could create a microservice that boots tensorflow and answers to prediction requests through a file, pipe or socket. Your c# application would then connect to your microservice and request predictions.

like image 138
ldavid Avatar answered Dec 17 '22 22:12

ldavid