2

A friend and I have been trying to run a model in the browser built with Keras using Tensorflow JS. We followed the official tutorial to do so but the predictions are different in the browser compared to Python.

Specifically, the model is an 1D CNN and its purpose is to classify 1D arrays (segments) of voltage recordings into a seizure or no seizure class. In the browser the score predictions of the model are consistently centered around 0.93 for the no seizure class, across all voltage segments, which is incorrect.


We used the following methods to export the model from Keras to a Tensorflow JS-compatible format:

With the tensorflowjs_converter CLI tool:

tensorflowjs_converter --input_format keras \
                       path/to/my_model.h5 \
                       path/to/tfjs_target_dir

Directly from Python:


tfjs.converters.save_keras_model(model, tfjs_target_dir)


The 1D CNN model requires 3D data, so we have to reshape them on the fly.

In Python we simply use:

data = data.reshape(data.shape[0], data.shape[1], 1) # data = tables instance
prediction = model.predict(data) 

This yields the correct predictions as expected.

Here is how we load the model, reshape the data and run the model in the browser:

let toPredict = tf.tensor2d(inputData, [inputData.length, inputData[0].length])
toPredict = toPredict.reshape([inputData.length, inputData[0].length, 1])

const model = await tf.loadLayersModel('/model/model.json');
predictions = await model.predict(toPredict).array();

This does not return any error, but yields incorrect predictions as described above.


Approaches we tried to troubleshoot this:

1) Verified that the data were correctly loaded in the appropriate format. Exporting this data from the browser and passing them into the Python keras model resulted in correct predictions.

2) Verified that the model weights and architecture are the same in Python and in the browser.

3) Disabled WebGL.

4) Converted the model using two different techniques (see above).

However, none of this worked for us. Does anyone have any advice or recommendations on how to fix this issue?

Frontend stack:

  • Latest Google Chrome / latest Firefox on Mac OS
  • Tensorflow JS 1.7.2

Thanks in advance,

  • Are you making some data processing in js ? Have you tried to print the tensors just before calling the prediction method in order to be sure that you exactly have the same both in js and in python ? – edkeveked Apr 14 '20 at 19:55
  • Thanks for you comment :). - We don't process data in JS, we just load it. - We double / triple checked at every stage that data and weights were the same Thank you. – Matteo Cargnelutti Apr 15 '20 at 20:53
  • When you said that there are major discrepancies, can you maybe be more precise like how far off are the predicitions in js compared to that of python? Additionnally which versions are you using ? – edkeveked Apr 16 '20 at 14:56

0 Answers0