GeoPOI / runtime /span_proposal.py
Tiga233's picture
Release complete GeoPOI model with shared regional basis and 500-entity public catalog
49d63ea verified
Raw History Blame Contribute Delete
1.3 kB
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