import torch


def to_mini_mask(rois, boxes):
    """
    Transform ROI coordinates from normalized image space
    to normalized mini-mask space.
    """
    y1, x1, y2, x2 = rois.chunk(4, dim=1)
    gt_y1, gt_x1, gt_y2, gt_x2 = boxes.chunk(4, dim=1)
    gt_h = gt_y2 - gt_y1
    gt_w = gt_x2 - gt_x1
    y1 = (y1 - gt_y1) / gt_h
    x1 = (x1 - gt_x1) / gt_w
    y2 = (y2 - gt_y1) / gt_h
    x2 = (x2 - gt_x1) / gt_w
    return torch.cat([y1, x1, y2, x2], dim=1)