tfjs: ChannelsFirst dataFormat in Flatten Layer Causes Different results for TFJS-Web vs Keras on same model weights and tensor

TensorFlow.js version

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.1.2/dist/tf.min.js"></script>

Browser version

chrome 70.0.3538.77 (Official Build) (64-bit) Python: 3.6.7 keras 2.2.4 tensorflowjs convert 1.2.10.1 and 0.5.7 tensorflow-gpu 1.10.0, channel first

Describe the problem or feature request

I wanted to use a Keras-trained model in a JS application (Cordova/Ionic) so I used TensorFlow.js Python conversion however my test in a webpage shows different results from my Python predictions.

I tried a fixed input tensor by [1,1,18,5] into python and JS script to make a prediction, But I got diffent results: python 1.3110151, but for JS I’ve got -1.028530240058899

I’ve used model.getWeights()[0].print() to checkout the first and last weights in converted json model and it returns

[[ [[-0.4403104, -0.4608164, 0.0136627, ..., 0.1289152, -0.0397321, 0.3372021 ],],

       [[0.5465862 , 0.4188564 , 0.0245932, ..., 0.4540363, -0.0008038, -0.6032839],]]]

the python weights also returns

[[[[-0.44031048 -0.4608164   0.01366271     ....  0.1289152  -0.03973224  0.33720216]]
 [[ 0.54658633  0.41885644  0.0245933  ....   0.45403644 -0.00080385 -0.60328406]]]]

So they have exactly the same weigths for the first layer, except a small precision convertion problem.

I’m pretty sure that’s a bug, right?

Code to reproduce the bug / link to feature request

I tried these actions below to make sure if it was caused by the error sequence of JS weights

  1. As I find in the last python layer is dense/bias. but in JS, last layer is a 40 nums parameter.

This picture shows the last layer in JS converted model, and the first row shows the last layer weights value, second row shows the last layer weights shape image

In Python, the last layer is ‘dense/bias’, and I’m sure it’s right. Here shows the python dense/bias layer weights value and shape.

image

In fact the dense/bias layer of JS appears in the model.getWeights()[6].shape;

I want to use the code to get the dense/bias parameter,but it failed with the error attached below. I want to know does this matters? And could you tell me the right way/code to get the dense/bias layers in JS?

let t=model.getLayers('dense/bias');        
print(t);

image

  1. Then I use below code to get the last layer weights data and all the sequence weights shape in JS
       model.getWeights()[8].print();  //the last layer weights data
       console.log(model.getWeights()[0].shape);  //first layer shape, JS and python the same
       console.log(model.getWeights()[1].shape);
       console.log(model.getWeights()[2].shape);
       console.log(model.getWeights()[3].shape);
       console.log(model.getWeights()[4].shape);
       console.log(model.getWeights()[5].shape);
       console.log(model.getWeights()[6].shape);
       console.log(model.getWeights()[7].shape);
      //last layer shape, JS and python different, 40 for JS and 1 for dense layers in Python
       console.log(model.getWeights()[8].shape); 

the layer weights shape sequence is

image

image

vs python layer weights shape sequence is image

  1. Then I checked the tfjs convert json model information, and it’s right image

So does this matters the predict results of keras and its converted tfjs models? Thanks

git_issue.tar.gz

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16

Commits related to this issue

Most upvoted comments

@wingman-jr-addon The fix will become available in the npm package when the next version of tensorflow.js is released

@joyicejin @wingman-jr-addon I’ll address the issue that TF.js’ Flatten Layer doesn’t respect data_format === 'channel_first' soon (before EOW), under this bug. Thank you for your patience and for your detailed analysis of this issue.