What I meant is,  to calculate the IoU,  we can use something like below.
Consider A,  B are two 2D tensors made up of 0 and 1s.
Intersection = (A * B).sum()
Union = (A+B - A*B).sum()
IoU = intersection / Union