tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Make tflite model run directly with tfjs

Open rajeev-tbrew opened this issue 7 years ago • 31 comments

To get help from the community, check out our Google group. This is not a bug rather a feature request. As TensorFlow library provides TFLite models to run on Android, iOS platform, can we a build a tfjs wrapper to allow tfjs to directly load TFlite model in browser. This will allow same model being used across multiple platforms.

TensorFlow.js version

Browser version

Describe the problem or feature request

Code to reproduce the bug / link to feature request

rajeev-tbrew avatar Dec 13 '18 07:12 rajeev-tbrew

I don't think this is something we'll ever support -- you can convert a savedmodel to tfjs without tf lite.

nsthorat avatar Dec 13 '18 19:12 nsthorat

closing this as there is no support for feature request.

rthadur avatar Feb 06 '19 22:02 rthadur

Question @mbostock and @rthadur, this is my first time working with javascript, so, let me know if I am way off, js is a client-side programming language right and tf.lite works on edge, so, why not have a way to load tf.lite directly via tfjs, seems pretty straightforward.

mukeshmithrakumar avatar Jun 16 '19 07:06 mukeshmithrakumar

Unfortunately it’s not as simple as you’d think. Is there a use case you have a in mind that we can’t solve with TensorFlow.js itself?

nsthorat avatar Jun 16 '19 14:06 nsthorat

oh no, @nsthorat it's more of a architecture & performance decision, we have a voice assistant in tensorflow and to move it to edge there are two ways, convert it to tf.lite and write in java for android or convert it into tf.js and write in js, I read tf.lite has much better performance for ML but writing in js would be convenient, not to mention, having to learn Java to do this, so was wondering if I could convert from tf to tf.lite and then load the model via js. If you have any ideas, it would be really helpful

mukeshmithrakumar avatar Jun 16 '19 17:06 mukeshmithrakumar

Got it.

In the browser, compiling TF Lite will be too large of a binary to run directly and will be much slower than our kernels.

In Node.js, we're working on natively executing SavedModels, and we are also working on headless WebGL bindings.

These two should cover everything you'd need, unless I'm missing something!

nsthorat avatar Jun 17 '19 14:06 nsthorat

Hey @nsthorat thanks a lot man, was going through tf node.js and it looks like that would work great. Appreciate your help 🙂

mukeshmithrakumar avatar Jun 17 '19 15:06 mukeshmithrakumar

No problem :)

nsthorat avatar Jun 17 '19 17:06 nsthorat

@nsthorat Have a use case that doesn't require a wrapper. Let me know if this is possible.

Question: Is it possible to convert a .tflite file to a .pb file then use tfjs-converter to convert to a js model?

Context: I'm using Firebase's AutoML library -- it's pretty sweet, it creates generic .tflite files (TensorFlow lite models) for me I can plug into my native mobile app (I don't have access to a .pb output).

Reading tfjs-converter, maybe I can convert a tf saved model into a js model format offline. However, doesn't seem you can convert .tflite to json.

opiepj avatar Jul 21 '19 20:07 opiepj

Hi @opiepj, we're actively working on getting TensorFlow.js models directly supported in AutoML, stay tuned!

nsthorat avatar Jul 22 '19 15:07 nsthorat

https://github.com/intel/webml-polyfill this is a way of running tflite on web

no-1ne avatar Oct 05 '19 09:10 no-1ne

@startupgurukul unfortunately that requires a custom chromium build.

nsthorat avatar Oct 07 '19 14:10 nsthorat

Hi Nikhil, My understanding is that polyfill is enough to run in any browser and custom chromium build if one needs native performance.

Here are some examples they provide https://intel.github.io/webml-polyfill/examples/

no-1ne avatar Oct 07 '19 15:10 no-1ne

@rthadur I've checked the examples, it looks like i've to write lot of javascript code like this to work with smart reply https://github.com/intel/webml-polyfill/blob/master/examples/face_recognition/utils.js?

bernardoespinosa avatar Jan 08 '20 03:01 bernardoespinosa

@nsthorat what should we do if we want the benefits of a fast tflite model but we want to run it on the web?

It doesn't seem possible to convert a .tflite model back into a Tensorflow model so that we can convert it into a TFjs model.

NathanBWaters avatar Mar 05 '20 19:03 NathanBWaters

@NathanBWaters where did you get the .tflite model? .tflite models come from SavedModels which can be converted to tfjs.

nsthorat avatar Mar 05 '20 19:03 nsthorat

@nsthorat thanks for responding so quickly. We are seeing faster results using a tflite blazeface model (see here) than using the TF model converted into TFjs.

So, though we can convert the original SavedModels into tfjs, we want to use the faster more optimized .tflite version of the model.

NathanBWaters avatar Mar 05 '20 22:03 NathanBWaters

@NathanBWaters We actually already have blazeface converted in tfjs: https://github.com/tensorflow/tfjs-models/tree/master/blazeface

Hope this helps!

nsthorat avatar Mar 06 '20 01:03 nsthorat

@NathanBWaters We actually already have blazeface converted in tfjs: https://github.com/tensorflow/tfjs-models/tree/master/blazeface

@nsthorat Will it be possible for you to share how you did the conversion?

rrjanbiah avatar Mar 06 '20 16:03 rrjanbiah

@rrjanbiah We converted it from the internal SavedModel (not sure why the team didn’t want to release it).

nsthorat avatar Mar 06 '20 17:03 nsthorat