Download runtime/span_proposal.py from guga-guga/GeoPOI: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/guga-guga/GeoPOI/resolve/main/runtime/span_proposal.py
- Command line
-
hf download hf://guga-guga/GeoPOI/runtime/span_proposal.py
-
curl -L -o span_proposal.py https://huggingface.co/guga-guga/GeoPOI/resolve/main/runtime/span_proposal.py
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 | |