lutzroeder / netron

Visualizer for neural network, deep learning and machine learning models
https://netron.app
MIT License
28.37k stars 2.8k forks source link

TensorFlow.js weights support #683

Closed TheNewSound closed 3 years ago

TheNewSound commented 3 years ago

Netron does support loading the model.json, but does not seem to recognize the weights.bin uploaded with it.

TheNewSound commented 3 years ago

No repro?

Is this a feature that is already added? It is not to my knowledge...

lutzroeder commented 3 years ago

Can you share a sample file, steps how to load the file and what isn't working or submit a PR to fix the issue.

TheNewSound commented 3 years ago

Sure!

2 files: model.json (works). weights.bin (is not recognized, missing feature likely, not a bug I assume)

Have to upload it as .zip to github, see attachment.

Just extract the 2 files and upload them to netron. model.zip

The weights.bin is just a bunch of float32's with no special structure. The model.json specifies which float32's indices belong to which layer.

TheNewSound commented 3 years ago

weights.bin can be read out with the following small piece of javascript:

Extract weights for consistent float32 Tensorflowjs saved model binary url <br>
<input type="file" id="files" name="files[]" /><br><br>
<script type="text/javascript">
    function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    var reader = new FileReader();
    reader.onload = function(){
      const dv = new DataView(reader.result);
      var f32 = new Float32Array(reader.result.byteLength / 4);
      const littleEndian = true;

     for (let i = 0; i < f32.length; i++) {
        f32[i] = dv.getFloat32(i*4, littleEndian);
     }   

     const myWeightsJSON = JSON.stringify(f32,null,4)     
     document.getElementById('myWeights').value =  myWeightsJSON 
    };
    reader.readAsArrayBuffer(files[0]);
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
<br />
<textarea id="myWeights" rows=15 cols="50"></textarea><br>

Demo: https://jsfiddle.net/8nmhboa1/