简体   繁体   中英

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.

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.

Try Keras.NET. That uses C# numpy and calls the installed Keras and backend.

https://github.com/SciSharp/Keras.NET

I have used it succesfully to train and predict with Keras directly from C#. Not ideal but it works.

it does not work like that, since you wont even be able to install tensorflow in a C# project. Abandon C# stack and learn framework in python stack instead, ex. if if you need to consume the prediction result in a web app, learn Flask or Django

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM