import torch


def MIoU(pred, gt):
    """
    Description:
        Mean Intersection over Union
    Params:
        pred -- the predicted label, tensor of shape (N,H,W,C)
        gt -- the ground_truth label, tensor of shape (N,H,W,C)
    Tips:
        gt needs to be transformed to one_hot form
    """
    intersection = torch.sum(pred * gt, (1,2,3))
    union = torch.sum(pred, (1,2,3)) + torch.sum(gt, (1,2,3)) - intersection
    miou = torch.mean(intersection / union)

    return miou