I am using a Keras for object detection and converting the model into CoreML for mobile deployment.
On the python side, the predict method is run on the same image. The preprocessing ( mean removal ) is handled on python side for keras , and by coreML ( red_bias, green_bias etc ) by the coreML.
The prediction outputs are same upto first decimal place, however the remainder of the decimals places are all different.
I check with numpy.testing.assert_array_almost_equal to get between 4-10% difference in the output arrays.
The output array is of the size (1,10000,45).
the result is different prediction and object detection output.
any insights into why this may be happening ?
how can i find the source of the issue and fix it ?
Assuming you did everything correctly, and the model runs on the GPU, then these kinds of precision errors are par for the course. The GPU uses 16-bit floats, which are precise only to about 3 decimals, and if your model has a lot of layers then such precision errors will accumulate.
Also, for certain types of models, the last layer will have large weights and a large (negative) bias (in order to force predictions to be very confident) and this will amplify the precision errors.
Try running the Core ML model on the CPU instead of the GPU (see MLPredictionOptions
). If the precision is now better, then it was indeed the 16-bit floats.
Generally this issue is nothing to worry about, since (convolutional) neural networks are quite robust against such precision issues, and you'll probably still get the proper results from the model.
My rule of thumb is that errors smaller than 5e-2 are OK, and if there is only a handful of errors larger than 5e-2 then the model is fine.