tensorFlow/tfjs adds 50 MB to my JS app, can I reduce it somehow?

Hi!
I am using tfjs for a simple prediction in my application but the 50 MBs it adds (upon building it with “npm run build”) seems quite a lot given my app size. I assume there is no way to reduce that but still wanted to ask, just to be sure.
Thank you

1 Like

You can try by using tensorflowjs-core and only the backends you need. If you want to include more than one though, it basically ends up being the same than the full package.

And an example:

Thank you! What do you mean by “only backends I need”? I only use it in my Meteor app, on the server side.

The front end in this case is the tensorflow api / model. The backend is the engine performing the calculations, and it’s tight to hardware.

So you have cpu backend, gpu backend, and also some flavours for each.

Thanks, though it seems models (e.g. sequential, which I use) are not part of the core package :frowning: So I assume there is no other way than including tfjs :confused:

Well, you can still create your models with tf or pytorch in python and then convert your models to onnx, which can be used in both web and nodeJS, for example with onnx-runtime web to load them.

But you may require pre and post-processing steps that still need full blown tf, so it’s non-obvious without trying.

So this isn’t simple path if you are getting started, since there are several layers of complexity. I’ve done that though, and in some cases it’s worth, in others it’s not.

Thank you, I naively thought I could maybe import just the model, but even when I import {tensor2d,sequential, layers}, it adds 50 MBs anyway :confused:

if you upload everything to github i (or others) may test some options, but can’t do much otherwise. it’s quite a tough problem (many are facing it, so don’t feel alone.)

Thank you. I cannot really do that (it is part of my work, though “nice to have” that I develop for fun). But basically, all I use is sequential, tensor2d and layers.

1 Like