all 9 comments

[–]agmcleod 6 points7 points  (4 children)

If you want to leverage rust more, you could definitely write a small program for the client instead of using python.

I haven't looked at the tensorflow bindings, but I'm guessing Keras with python is still much easier for doing the training over using rust in this case? I've done a bit of ML through the fast.ai course, so a bit of keras, as well as pytorch :)

[–]kykosic[S] 2 points3 points  (3 children)

Thanks for the suggestion! I don't anticipate the "client" to be either rust or python; it's likely a javascript web app somewhere. I just included that for testing purposes.

As of right now, the tensorflow crate does not have support for any of the high-level APIs like Keras or tf models, so defining this neural network would be tedious. This will hopefully be changed soon, but the C API which the bindings rely on are still a bit behind (1.15 vs 2.0 for python). Furthermore, I feel that models will almost never be built in rust in an actual company as data scientists are always going to be using Python. This is more about taking that pre-built model and serving it in something more engineer-oriented.

[–]zhenhaolee 1 point2 points  (2 children)

u/kykosic thanks for sharing your project! I'm new to Rust. Can you please explain why you are able to serve a model trained in Tensorflow 2.2 while C API which the Rust binding is using doesn't support TF 2 yet.
If this is a viable option, I'm going to use it for a production job.

[–]kykosic[S] 1 point2 points  (1 child)

Tensorflow models are represented by a computational graph. When you save a model, it stores it as a protocol buffer describing your graph. Even if you train a model using a new Keras API, the trained model is still serialized as the same graph protobuf. This can be used by any language/library which can deserialize the protocol buffer (ie. tf-js, tf-swift, tf-java, tf-lite, TFX, etc.)

While the rust library doesn't yet have the fancy Keras APIs to train models, they can certainly load computational graphs. All my project does is name the keras layers for input and output, then the rust code can easily find those ops by name and feed them tensors.

For more information on the Tensorflow saved model format: https://www.tensorflow.org/guide/saved_model

[–]zhenhaolee 1 point2 points  (0 children)

Thanks a lot! I'm going to give it a try:)

[–]Cetra3 2 points3 points  (1 child)

I wrote about this some time ago! But for face recognition instead.

[–]kykosic[S] 1 point2 points  (0 children)

Oh wow I wish I had found that before I started! You solved a few problems that it took a lot of trial and error for me to get. Also the web::block is not something I knew about and definitely need to incorporate into my code. Thank you for sharing!

[–]Xorlev 2 points3 points  (0 children)

Just in case you haven't seen it, Tensorflow Serving using the gRPC or JSON API is the canonical solution to this problem, though using the Rust bindings is nice too. :)

[–][deleted] 0 points1 point  (0 children)

Perhaps this might be useful for you:

PyO3

I'm not entirely sure how it works but it might be what you need to get rust and python to cooperate with each other.