o :Ng@s4ddlZddZddZddZdd Zd d ZdS) NcCs0|dd}|d|d}tj||gddS)a Args: xx_spans: tensor, (#windows, 2) or (..., 2), each row is a window of format (st, ed) Returns: cxw_spans: tensor, (#windows, 2), each row is a window of format (center=(st+ed)/2, width=(ed-st)) >>> spans = torch.Tensor([[0, 1], [0.2, 0.4]]) >>> span_xx_to_cxw(spans) tensor([[0.5000, 1.0000], [0.3000, 0.2000]]) >>> spans = torch.Tensor([[[0, 1], [0.2, 0.4]]]) >>> span_xx_to_cxw(spans) tensor([[[0.5000, 1.0000], [0.3000, 0.2000]]]) ?..rdim)sumtorchstack)Zxx_spanscenterwidthrM/cpfs01/user/heyinan/code/reasoning2/third_party/cgdetr/cg_detr/span_utils.pyspan_xx_to_cxwsrcCs:|dd|d}|dd|d}tj||gddS)a Args: cxw_spans: tensor, (#windows, 2) or (..., 2), the last dim is a row denoting a window of format (center, width) >>> spans = torch.Tensor([[0.5000, 1.0000], [0.3000, 0.2000]]) >>> span_cxw_to_xx(spans) tensor([[0.0000, 1.0000], [0.2000, 0.4000]]) >>> spans = torch.Tensor([[[0.5000, 1.0000], [0.3000, 0.2000]]]) >>> span_cxw_to_xx(spans) tensor([[[0.0000, 1.0000], [0.2000, 0.4000]]]) rrrrr)r r )Z cxw_spansx1x2rrrspan_cxw_to_xxsrc Cs|dddf|dddf}|dddf|dddf}t|ddddf|dddf}t|ddddf|dddf}||jdd}|dddf||}||}||fS)a Args: spans1: (N, 2) torch.Tensor, each row defines a span [st, ed] spans2: (M, 2) torch.Tensor, ... Returns: iou: (N, M) torch.Tensor union: (N, M) torch.Tensor >>> test_spans1 = torch.Tensor([[0, 0.2], [0.5, 1.0]]) >>> test_spans2 = torch.Tensor([[0, 0.3], [0., 1.0]]) >>> temporal_iou(test_spans1, test_spans2) (tensor([[0.6667, 0.2000], [0.0000, 0.5000]]), tensor([[0.3000, 1.0000], [0.8000, 1.0000]])) Nrrminr maxrclamp) spans1spans2Zareas1Zareas2leftrightinterunioniourrr temporal_iou,s  &&r cCst|ddddf|dddf}t|ddddf|dddf}||jdd}||dddf|dddf}|S)z| intersection over the second input spans Args: gt_spans: (N, 2), pred_spans: (M, 2) Returns: Nrrrr)Zgt_spans pred_spansrrrZinter_over_predrrrtemporal_intersection_over_predJs & &$r"cCs8|}|}|dddf|dddfkr3t||dd|dddfd7<t||dddf|dddfksKJ|dddf|dddfks_Jt||\}}t|ddddf|dddf}t|ddddf|dddf}||j dd}||||S)al Generalized IoU from https://giou.stanford.edu/ Also reference to DETR implementation of generalized_box_iou https://github.com/facebookresearch/detr/blob/master/util/box_ops.py#L40 Args: spans1: (N, 2) torch.Tensor, each row defines a span in xx format [st, ed] spans2: (M, 2) torch.Tensor, ... Returns: giou: (N, M) torch.Tensor >>> test_spans1 = torch.Tensor([[0, 0.2], [0.5, 1.0]]) >>> test_spans2 = torch.Tensor([[0, 0.3], [0., 1.0]]) >>> generalized_temporal_iou(test_spans1, test_spans2) tensor([[ 0.6667, 0.2000], [-0.2000, 0.5000]]) Nrr)rrz test_spans.ptg-C6?r) floatallr savecpuprintr rrr)rrrrrrZenclosing_arearrrgeneralized_temporal_iou[s$((&&r()r rrr r"r(rrrrs