MCPcopy Create free account
hub / github.com/drinkingcoder/FlowFormer-Official / RAFT

Class RAFT

core/raft.py:24–144  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22
23
24class RAFT(nn.Module):
25 def __init__(self, args):
26 super(RAFT, self).__init__()
27 self.args = args
28
29 if args.small:
30 self.hidden_dim = hdim = 96
31 self.context_dim = cdim = 64
32 args.corr_levels = 4
33 args.corr_radius = 3
34
35 else:
36 self.hidden_dim = hdim = 128
37 self.context_dim = cdim = 128
38 args.corr_levels = 4
39 args.corr_radius = 4
40
41 if 'dropout' not in self.args:
42 self.args.dropout = 0
43
44 if 'alternate_corr' not in self.args:
45 self.args.alternate_corr = False
46
47 # feature network, context network, and update block
48 if args.small:
49 self.fnet = SmallEncoder(output_dim=128, norm_fn='instance', dropout=args.dropout)
50 self.cnet = SmallEncoder(output_dim=hdim+cdim, norm_fn='none', dropout=args.dropout)
51 self.update_block = SmallUpdateBlock(self.args, hidden_dim=hdim)
52
53 else:
54 self.fnet = BasicEncoder(output_dim=256, norm_fn='instance', dropout=args.dropout)
55 self.cnet = BasicEncoder(output_dim=hdim+cdim, norm_fn='batch', dropout=args.dropout)
56 self.update_block = BasicUpdateBlock(self.args, hidden_dim=hdim)
57
58 def freeze_bn(self):
59 for m in self.modules():
60 if isinstance(m, nn.BatchNorm2d):
61 m.eval()
62
63 def initialize_flow(self, img):
64 """ Flow is represented as difference between two coordinate grids flow = coords1 - coords0"""
65 N, C, H, W = img.shape
66 coords0 = coords_grid(N, H//8, W//8).to(img.device)
67 coords1 = coords_grid(N, H//8, W//8).to(img.device)
68
69 # optical flow computed as difference: flow = coords1 - coords0
70 return coords0, coords1
71
72 def upsample_flow(self, flow, mask):
73 """ Upsample flow field [H/8, W/8, 2] -> [H, W, 2] using convex combination """
74 N, _, H, W = flow.shape
75 mask = mask.view(N, 1, 9, 8, 8, H, W)
76 mask = torch.softmax(mask, dim=2)
77
78 up_flow = F.unfold(8 * flow, [3,3], padding=1)
79 up_flow = up_flow.view(N, 2, 9, 1, 1, H, W)
80
81 up_flow = torch.sum(mask * up_flow, dim=2)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected