Some operations such as this one:
masks_pred.eq(true_masks).sum().item()

might use broadcasting, if you don’t use tensors in the right shape.
I would recommend to print the shapes of all tensors, which are used to compute the accuracy and make sure they have matching shapes.