tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Save\load model and weights to\from DB

Open Aluman opened this issue 8 years ago • 7 comments

To get help from the community, check out our Google group.

TensorFlow.js version: 0.11.7

Browser version: node.js

Describe the problem or feature request

How can I save\load model and weights to\from DB? Model has method toJSON and I can store json in DB. But in method tf.loadModel I can't use use JSON. How can I create model from JSON? For weights model has methods getWeights, setWeights. But this methods return/use Tensor[]. How can I convert Tensor[] to/from JSON?

Aluman avatar Jul 06 '18 02:07 Aluman

We are starting to look at implementing this feature in layers. We need the ability to pass a JSON object directly into tf.loadModel() instead of loading it over some resource protocol.

nkreeger avatar Jul 06 '18 16:07 nkreeger

@Aluman Can you clarify what you mean by "convert Tensor[] to/from JSON"? Tensors are binary (mostly float) data. Representing the float numbers in JSON is very inefficient, which is why the saving and loading of the weights involve non-JSON types such as ArrayBuffers.

caisq avatar Jul 06 '18 17:07 caisq

Also, by "DB", what do you mean? IndexedDB of the browser?

caisq avatar Jul 06 '18 17:07 caisq

Also, by "DB", what do you mean? IndexedDB of the browser?

I mean SQL/NoSQL DB like MySQL, mongodb etc.

Can you clarify what you mean by "convert Tensor[] to/from JSON"? Tensors are binary (mostly float) data. Representing the float numbers in JSON is very inefficient, which is why the saving and loading of the weights involve non-JSON types such as ArrayBuffers.

I mentioned JSON because it's widely used format in js development, usually array\object can be converted to JSON and model already can be saved as json. JSON can easily stored to db/file or transfer by network it is very handy format. But if JSON is very inefficient any others formats can be used as well. I just looking for way to store model/weights to well known formats(JSON, string, array, buffer etc). And way to load model from it.

Aluman avatar Jul 06 '18 19:07 Aluman

I just prepared tensorflow/tfjs-core#1148, providing "passthrough" IOHandlers that load and save models to a simple in-memory format (a combination of JS objects and ArrayBuffers). That approach may be enough for simple use cases.

For more complex use cases, you can write your own implementation of IOHandler. Then you could write things like tf.loadModel(new MySqlIOHandler(credentials, model_key)) and model.save(new MySqlIOHandler(...)).

Note that IOHandlers (including the "passthrough" ones proposed in the PR) operate in terms of ModelArtifacts, where the weights are packed into a single ArrayBuffer. You could convert that to JSON for storage, but that would be very inefficient; you'd be much better off just storing the binary data as a blob. If for some reason you do need to shard the weight data or extract individual weight variables, the weightSpecs entry provides the information you need to do that.

Hope this helps!

davidsoergel avatar Jul 10 '18 01:07 davidsoergel

Closing: tensorflow/tfjs-core#1148 is now submitted. I think either that or the custom IOHandler approach should resolve this need.

davidsoergel avatar Jul 10 '18 14:07 davidsoergel

@davidsoergel Any docs on usage of the pass-through handler?

From the sources:

 const model = await tf.loadModel(tf.io.fromMemory(
  modelTopology, weightSpecs, weightData));
function handleSave(artifacts) {
  // ... do something with the artifacts ...
   return {modelArtifactsInfo: {...}, ...};
 }

 const saveResult = model.save(tf.io.withSaveHandler(handleSave));

Seems like this should be in the official docs.

toddpi314 avatar Dec 22 '18 14:12 toddpi314