File size: 1,110 Bytes
f877698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
import torch


def mask_for_images(mask, images):
    b, s, _, h, w = images.shape
    if mask.ndim == 3 and mask.shape == (b, h, w):
        mask = mask[:, None].expand(b, s, h, w)
    if mask.shape != (b, s, h, w):
        raise ValueError(f'occluder mask {tuple(mask.shape)} != {(b, s, h, w)}')
    if mask.dtype != torch.bool and not bool(((mask == 0) | (mask == 1)).all()):
        raise ValueError('occluder mask must be binary 0/1')
    return mask.to(device=images.device, dtype=torch.bool)


def gate_predictions(predictions, mask):
    gs = predictions['gs_map']
    if gs.ndim != 5 or gs.shape[:4] != mask.shape or gs.shape[-1] < 4:
        raise ValueError(f'gs_map {tuple(gs.shape)} / mask {tuple(mask.shape)}')
    result = dict(predictions)
    gated = gs.clone()
    gated[..., 3] = gs[..., 3].masked_fill(mask, 0.)
    result['gs_map'] = gated
    return result


def cap_scales(predictions, cap):
    if cap <= 0:
        return predictions
    gs = predictions['gs_map']
    capped = gs.clone()
    capped[..., 4:7] = gs[..., 4:7].clamp(max=cap)
    return {**predictions, 'gs_map': capped}