import torch


def calc_IOU(seg_omg1: torch.BoolTensor, seg_omg2: torch.BoolTensor, eps: float = 1.e-6) -> float:
    """
    calculate intersection over union between 2 boolean segmentation masks
    :param seg_omg1: first segmentation mask
    :param seg_omg2: second segmentation mask
    :param eps: eps for numerical stability
    :return: IOU
    """
    dim = [1, 2, 3] if len(seg_omg1.shape) == 4 else [1, 2]
    intersection = (seg_omg1 & seg_omg2).sum(dim=dim)
    union = (seg_omg1 | seg_omg2).sum(dim=dim)
    return (intersection.float() / (union.float() + eps)).mean().item()