import torch


def occupancy_iou(gt, pred):
    """Compute occupancy iou of (batched) torch tensors."""
    intersection = ((gt == pred) & (gt > 0)).sum((-1,-2,-3)).float()
    union = gt.sum((-1,-2,-3)) + pred.sum((-1,-2,-3)) - intersection
    union[union == 0] = 1
    intersection[union == 0] = 1
    return torch.mean((intersection / union))