Closed
Description
Describe the bug
The current implementation of LNCC loss https://github.com/Project-MONAI/MONAI/blob/master/monai/losses/image_dissimilarity.py comes from DeepReg.
However, recently in DeepReg, we encountered INF loss, due to a negative variance at certain voxels. The variance should be zero, but somehow it became around -1e-5 and sometimes it eventually caused INF.
The fix in DeepReg is
- First normalize the kernel so that the sum of the kernel weights equals 1. For some reason, this makes the variance be around machine error (-1e-11) and by adding an EPS, we will not encounter INF anymore.
- We've increased the EPS from 1e-7 to 1e-5, as under mixed-precision, we think 1e-5 is safer.
- Further, we clip the variance to zero, so that the variance must be >=0 in following calculation.
Unfortunately, I could not provide some simple tests to reproduce the inf error (due to the difficult debugging in TensorFlow).
More details are in