MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / get_single_mapping_ind

Function get_single_mapping_ind

diffusers/examples/community/fresco_v2v.py:641–693  ·  view source on GitHub ↗

FLATTEN: Optical fLow-guided attention (Temoporal-guided attention) Find the correspondence between every pixels in a pair of frames [input] bwd_flow: 1*2*H*W bwd_occ: 1*H*W i.e., f2 = warp(f1, bwd_flow) * bwd_occ imgs: 2*3*H*W i.e., [f1,f2] [output] map

(bwd_flow, bwd_occ, imgs, scale=1.0)

Source from the content-addressed store, hash-verified

639
640@torch.no_grad()
641def get_single_mapping_ind(bwd_flow, bwd_occ, imgs, scale=1.0):
642 """
643 FLATTEN: Optical fLow-guided attention (Temoporal-guided attention)
644 Find the correspondence between every pixels in a pair of frames
645
646 [input]
647 bwd_flow: 1*2*H*W
648 bwd_occ: 1*H*W i.e., f2 = warp(f1, bwd_flow) * bwd_occ
649 imgs: 2*3*H*W i.e., [f1,f2]
650
651 [output]
652 mapping_ind: pixel index correspondence
653 unlinkedmask: indicate whether a pixel has no correspondence
654 i.e., f2 = f1[mapping_ind] * unlinkedmask
655 """
656 flows = F.interpolate(bwd_flow, scale_factor=1.0 / scale, mode="bilinear")[0][[1, 0]] / scale # 2*H*W
657 _, H, W = flows.shape
658 masks = torch.logical_not(F.interpolate(bwd_occ[None], scale_factor=1.0 / scale, mode="bilinear") > 0.5)[
659 0
660 ] # 1*H*W
661 frames = F.interpolate(imgs, scale_factor=1.0 / scale, mode="bilinear").view(2, 3, -1) # 2*3*HW
662 grid = torch.stack(torch.meshgrid([torch.arange(H), torch.arange(W)]), dim=0).to(flows.device) # 2*H*W
663 warp_grid = torch.round(grid + flows)
664 mask = torch.logical_and(
665 torch.logical_and(
666 torch.logical_and(torch.logical_and(warp_grid[0] >= 0, warp_grid[0] < H), warp_grid[1] >= 0),
667 warp_grid[1] < W,
668 ),
669 masks[0],
670 ).view(-1) # HW
671 warp_grid = warp_grid.view(2, -1) # 2*HW
672 warp_ind = (warp_grid[0] * W + warp_grid[1]).to(torch.long) # HW
673 mapping_ind = torch.zeros_like(warp_ind) - 1 # HW
674
675 for f0ind, f1ind in enumerate(warp_ind):
676 if mask[f0ind]:
677 if mapping_ind[f1ind] == -1:
678 mapping_ind[f1ind] = f0ind
679 else:
680 targetv = frames[0, :, f1ind]
681 pref0ind = mapping_ind[f1ind]
682 prev = frames[1, :, pref0ind]
683 v = frames[1, :, f0ind]
684 if ((prev - targetv) ** 2).mean() > ((v - targetv) ** 2).mean():
685 mask[pref0ind] = False
686 mapping_ind[f1ind] = f0ind
687 else:
688 mask[f0ind] = False
689
690 unusedind = torch.arange(len(mask)).to(mask.device)[~mask]
691 unlinkedmask = mapping_ind == -1
692 mapping_ind[unlinkedmask] = unusedind
693 return mapping_ind, unlinkedmask
694
695
696@torch.no_grad()

Callers 1

get_mapping_indFunction · 0.85

Calls 2

interpolateMethod · 0.45
toMethod · 0.45

Tested by

no test coverage detected