Hi,
The problem you met, I have also met a few days ago in my Accuracy function. In my opinion, the outputs of the model(we call it logits) and the targets are both Tensors, so there are two ways to calculate metrics, in numpy or in tensor. I have tried convert tensors to numpy array, it is okay but it cost so much time to calculate metrics at the end of each iteration. So I tired to calculate them in Tensor, you can try to print you union and intersection they are not 0, but IoU is zero may be caused by type error. In my code, I try to calculate sum of valid pixels and correct pixels and convert them to .long() and IoU calculated by .long() / .long() , then get value 0. Otherwise, you can also use .item() to get python scalar correspond to tensor.
Hope this helps.