File size: 1,297 Bytes
49d63ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import torch

def propose_spans(frame_probability, mask, threshold=0.5, max_spans=4, padding=2, return_bounds=False):
    if frame_probability.ndim != 2 or frame_probability.shape != (mask.numel(), 3):
        raise ValueError('expected [T,3] membership/start/end probabilities')
    active = ((frame_probability[:, 0] >= threshold) & mask).tolist()
    intervals = []
    start = None
    for index, enabled in enumerate(active + [False]):
        if enabled and start is None:
            start = index
        if not enabled and start is not None:
            intervals.append((start, index))
            start = None
    intervals.sort(key=lambda bounds: float(frame_probability[bounds[0]:bounds[1], 0].max()), reverse=True)
    intervals = sorted(intervals[:max_spans])
    weights = frame_probability.new_zeros((max_spans, mask.numel()))
    bounds = torch.full((max_spans, 2), -1, dtype=torch.long, device=mask.device)
    for slot, (start, stop) in enumerate(intervals):
        bounds[slot] = bounds.new_tensor([start, stop])
        left, right = (max(0, start - padding), min(mask.numel(), stop + padding))
        weights[slot, left:right] = frame_probability[left:right, 0].clamp_min(0.05)
    weights = weights * mask[None]
    return (weights, bounds) if return_bounds else weights