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