FLATTEN: Optical fLow-guided attention (Temoporal-guided attention) Find pixel correspondence between every consecutive frames in a batch [input] bwd_flow: (N-1)*2*H*W bwd_occ: (N-1)*H*W imgs: N*3*H*W [output] fwd_mappings: N*1*HW bwd_mappings: N*1*HW flatt
(bwd_flows, bwd_occs, imgs, scale=1.0)
| 695 | |
| 696 | @torch.no_grad() |
| 697 | def get_mapping_ind(bwd_flows, bwd_occs, imgs, scale=1.0): |
| 698 | """ |
| 699 | FLATTEN: Optical fLow-guided attention (Temoporal-guided attention) |
| 700 | Find pixel correspondence between every consecutive frames in a batch |
| 701 | |
| 702 | [input] |
| 703 | bwd_flow: (N-1)*2*H*W |
| 704 | bwd_occ: (N-1)*H*W |
| 705 | imgs: N*3*H*W |
| 706 | |
| 707 | [output] |
| 708 | fwd_mappings: N*1*HW |
| 709 | bwd_mappings: N*1*HW |
| 710 | flattn_mask: HW*1*N*N |
| 711 | i.e., imgs[i,:,fwd_mappings[i]] corresponds to imgs[0] |
| 712 | i.e., imgs[i,:,fwd_mappings[i]][:,bwd_mappings[i]] restore the original imgs[i] |
| 713 | """ |
| 714 | N, H, W = imgs.shape[0], int(imgs.shape[2] // scale), int(imgs.shape[3] // scale) |
| 715 | iterattn_mask = torch.ones(H * W, N, N, dtype=torch.bool).to(imgs.device) |
| 716 | for i in range(len(imgs) - 1): |
| 717 | one_mask = torch.ones(N, N, dtype=torch.bool).to(imgs.device) |
| 718 | one_mask[: i + 1, i + 1 :] = False |
| 719 | one_mask[i + 1 :, : i + 1] = False |
| 720 | mapping_ind, unlinkedmask = get_single_mapping_ind( |
| 721 | bwd_flows[i : i + 1], bwd_occs[i : i + 1], imgs[i : i + 2], scale |
| 722 | ) |
| 723 | if i == 0: |
| 724 | fwd_mapping = [torch.arange(len(mapping_ind)).to(mapping_ind.device)] |
| 725 | bwd_mapping = [torch.arange(len(mapping_ind)).to(mapping_ind.device)] |
| 726 | iterattn_mask[unlinkedmask[fwd_mapping[-1]]] = torch.logical_and( |
| 727 | iterattn_mask[unlinkedmask[fwd_mapping[-1]]], one_mask |
| 728 | ) |
| 729 | fwd_mapping += [mapping_ind[fwd_mapping[-1]]] |
| 730 | bwd_mapping += [torch.sort(fwd_mapping[-1])[1]] |
| 731 | fwd_mappings = torch.stack(fwd_mapping, dim=0).unsqueeze(1) |
| 732 | bwd_mappings = torch.stack(bwd_mapping, dim=0).unsqueeze(1) |
| 733 | return fwd_mappings, bwd_mappings, iterattn_mask.unsqueeze(1) |
| 734 | |
| 735 | |
| 736 | def apply_FRESCO_opt( |
no test coverage detected