MCPcopy Create free account
hub / github.com/drinkingcoder/NeuralMarker / RAFT

Class RAFT

core/raft.py:25–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 2

__init__Method · 0.90
evaluation_FM.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected