question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16

github_iconTop GitHub Comments

1reaction
caisqcommented, Nov 8, 2019

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

1reaction
caisqcommented, Nov 7, 2019

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

layer_flatten - TensorFlow for R - RStudio
Typically a Sequential model or a Tensor (e.g., as returned by layer_input() ). ... weight ordering when switching a model from one data...
Read more >
Flatten Layer with channel first and channel last experiments ...
In my model I am specifying I am using channels first on every layer. So let's say output from previous maxpool's layer is...
Read more >
A Gentle Introduction to Channels-First and Channels-Last ...
In this tutorial, you will discover channel ordering formats, how to prepare and manipulate image data to meet formats, and how to configure...
Read more >
Tensorflow.js tf.layers.flatten() Function - GeeksforGeeks
Tensorflow.js tf.layers.flatten() Function · dataFormat ('channelsFirst' or 'channelsLast'): It is the image data format. · inputShape ((null | ...
Read more >
tf.keras.layers.Layer | TensorFlow v2.11.0
A layer is a callable object that takes as input one or more tensors and ... of another Layer, the outer layer will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found