Skip to content Skip to sidebar Skip to footer

Tensorflow Vs Tensorflow Js Different Results For Floating Point Arithmetic Computations

I have converted a Tensorflow model to Tensorflow JS and tried using in the browser. There are some preprocessing steps which are to be executed on the inout before feeding it to t

Solution 1:

There might be a number of possibilities that can lead to the issue.

1- The ops used in python are not used in the same manner in both js and python. If that is the case, using exactly the same ops will get rid of the issue.

2- The tensors image might be read differently by the python library and the browser canvas. Actually, accross browsers the canvas pixel don't always have the same value due to some operations like anti-aliasing, etc ... as explained in this answer. So there might be some slight differences in the result of the operations. To make sure that this is the root cause of the issue, first try to print the python and the js array image and see if they are alike. It is likely that the 3d tensor is different in js and python.

tensor3d = tf.tensor3d(image,[height,width,1],'float32')

In this case, instead of reading directly the image in the browser, one can use the python library to convert image to array of tensor. And use tfjs to read directly this array instead of the image. That way, the input tensors will be the same both for in js and in python.

3 - it is a float32 precision issue. tensor3d is created with the dtype float32 and depending on the operations used, there might be a precision issue. Consider this operation:

tf.scalar(12045, 'int32').mul(tf.scalar(12045, 'int32')).print(); // 145082032 instead of 145082025

The same precision issue will be encountered in python with the following:

a = tf.constant([12045], dtype='float32') * tf.constant([12045], dtype='float32')
tf.print(a) // 145082032

In python this can be solved by using int32 dtype. However because of the webgl float32 limitation the same thing can't be done using the webgl backend on tfjs. In neural networks, this precision issue is not a great deal. To get rid of it, one can change the backend using setBackend('cpu') for instance which is much slower.

Post a Comment for "Tensorflow Vs Tensorflow Js Different Results For Floating Point Arithmetic Computations"