tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Unable to access pre-trained frozen model layers and nodes in tensorflow-js

Open Raza25 opened this issue 8 years ago • 6 comments

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

TensorFlow.js version 0.13.0

Browser version Google chrome 70.0.3538.102 (Official Build) (64-bit)

Describe the problem or feature request

I trained a keras Seq2Seq model with tensorflow backend. Froze the model, then used GTT (google transform tool) for quantization and weights rounding. I did this to reduce the model size from 100 mb to 25 mb. The GTT also produces a resultant .pb model with quantized weights. After I load the aforementioned model in tensorflow-js, I am unable to access its layers and other nodes. Also there is not much help over the internet regarding inspecting the frozen model in tfjs.

  1. Any steps for accessing the model layers
  2. Way to convert this quantized .pb model into json

Raza25 avatar Nov 27 '18 04:11 Raza25

@Raza25 have you used the tfjs-converter to convert the frozen model to tfjs format? Here is a tutorial on how to do that.

pyu10055 avatar Nov 28 '18 22:11 pyu10055

@pyu10055 yes, I have converted the model via tfjs-converter. My real concern is, how to inspect/diagnose or use the .pb model in tfjs. Json model architecture is easily accessible, however it is not the case with frozen model. I suppose the layers information is removed at the time of freezing a graph.

Note: I am working with RNN, thereby require layers information for inference model in tfjs.

Raza25 avatar Nov 29 '18 03:11 Raza25

@Raza25 Got it, I have created a pb to json script https://github.com/tensorflow/tfjs-converter/pull/256. You can try that with your model.

pyu10055 avatar Nov 29 '18 17:11 pyu10055

@pyu10055 this script flawlessly converts .pb to .json, but in my case the quantized.pb file with size 18 MB was converted to quantized.json with an imprint of 247 MB. This really penalizes the entire struggle for reducing the size.
As any model >50 mb doesn't load in tfjs and the web console dies. Kindly look into it, would you!

Raza25 avatar Nov 30 '18 10:11 Raza25

@Raza25 the script converts the pb file to json, but the json file cannot be loaded by tfjs, since we don't support loading json frozen model format. The script is created to allow viewing what nodes the converter generates. If your goal is to inspect layer information of the converted graph in run-time, unfortunately, the layer information are removed when you freeze the model, but the node name still has layer name prefixes.

To view the nodes of the graph in run-time you can do following:

const model = tf.loadFrozenModel(modelUrl, manifestUrl);
console.log(model.executor.graph.nodes); 

pyu10055 avatar Dec 01 '18 00:12 pyu10055

@pyu10055 my very basic question or you can say 'concern' is how can I simply reduce the size of my keras RNN for seq2seq with footprint of 120 MB to less than 30 MB.

All of the quantization techniques available accept and produces .pb file to work on. The aforementioned .pb file isn't easy to work on in tfjs.

I just require a way to reduce the model.h5 size, convert the reduced model.h5 to (model.json + group-shard) using tfjs converter and finally loading it in tjfs via loadModel. Thats about it, really!

Raza25 avatar Dec 01 '18 08:12 Raza25