Hi Shubham!



 towardsautonomy:

“rendered_silhouette” and “original_silhouette” are binary images, each containing a blob.


The short answer is to compute the center of mass (the “weighted
centroid”) of  rendered_silhouette before thresholding.
Let me speculate as to what your use case might be:
Your original_silhouette plays the role of some kind of “ground-truth”
target.  Perhaps your network is trying to reconstruct it somehow.
rendered_silhouette is generated by a network and you want to
train the weights of the network so that rendered_silhouette
approximates original_silhouette.  Therefore you want a differentiable
loss function so that you can use backpropagation for training.
In all probability your network generates an image that is not binary.
That is, the value of each pixel in the generated image is the probability
(or perhaps the logit) for that pixel being 1 (rather than 0) in the final
binary image.  You then threshold this “probability” image to get your
final rendered_silhouette binary image.
This thresholding is not (usefully) differentiable, so you can’t
backpropagate through it.
What you can do instead is calculate the center of mass of the (blob
in the) probability image.  This will be a properly-differentiable version
of the (non-differentiable) centroid of the thresholded image.  Use it
to compute centroid_dist returned by your CentroidLoss function.
You can now use this differentiable loss to backpropagate and train
the weights in your network.
Best.
K. Frank