Estimate optical flow between pair of frames
(self, image1, image2, iters=12, flow_init=None, upsample=True, test_mode=False)
| 84 | |
| 85 | |
| 86 | def forward(self, image1, image2, iters=12, flow_init=None, upsample=True, test_mode=False): |
| 87 | """ Estimate optical flow between pair of frames """ |
| 88 | |
| 89 | image1 = 2 * (image1 / 255.0) - 1.0 |
| 90 | image2 = 2 * (image2 / 255.0) - 1.0 |
| 91 | |
| 92 | image1 = image1.contiguous() |
| 93 | image2 = image2.contiguous() |
| 94 | |
| 95 | hdim = self.hidden_dim |
| 96 | cdim = self.context_dim |
| 97 | |
| 98 | # run the feature network |
| 99 | with autocast(enabled=self.args.mixed_precision): |
| 100 | fmap1, fmap2 = self.fnet([image1, image2]) |
| 101 | |
| 102 | fmap1 = fmap1.float() |
| 103 | fmap2 = fmap2.float() |
| 104 | if self.args.alternate_corr: |
| 105 | corr_fn = AlternateCorrBlock(fmap1, fmap2, radius=self.args.corr_radius) |
| 106 | else: |
| 107 | corr_fn = CorrBlock(fmap1, fmap2, radius=self.args.corr_radius) |
| 108 | |
| 109 | # run the context network |
| 110 | with autocast(enabled=self.args.mixed_precision): |
| 111 | cnet = self.cnet(image1) |
| 112 | net, inp = torch.split(cnet, [hdim, cdim], dim=1) |
| 113 | net = torch.tanh(net) |
| 114 | inp = torch.relu(inp) |
| 115 | |
| 116 | coords0, coords1 = self.initialize_flow(image1) |
| 117 | |
| 118 | if flow_init is not None: |
| 119 | coords1 = coords1 + flow_init |
| 120 | |
| 121 | flow_predictions = [] |
| 122 | for itr in range(iters): |
| 123 | coords1 = coords1.detach() |
| 124 | corr = corr_fn(coords1) # index correlation volume |
| 125 | |
| 126 | flow = coords1 - coords0 |
| 127 | with autocast(enabled=self.args.mixed_precision): |
| 128 | net, up_mask, delta_flow = self.update_block(net, inp, corr, flow) |
| 129 | |
| 130 | # F(t+1) = F(t) + \Delta(t) |
| 131 | coords1 = coords1 + delta_flow |
| 132 | |
| 133 | # upsample predictions |
| 134 | if up_mask is None: |
| 135 | flow_up = upflow8(coords1 - coords0) |
| 136 | else: |
| 137 | flow_up = self.upsample_flow(coords1 - coords0, up_mask) |
| 138 | |
| 139 | flow_predictions.append(flow_up) |
| 140 | |
| 141 | if test_mode: |
| 142 | return coords1 - coords0, flow_up |
| 143 |
no test coverage detected