MCPcopy Create free account
hub / github.com/ActiveVisionLab/DFNet / forward

Method forward

script/feature/dfnet.py:209–273  ·  view source on GitHub ↗

inference DFNet_s. It can regress camera pose as well as extract intermediate layer features. :param x: image blob (2B x C x H x W) two stream or (B x C x H x W) single stream :param return_feature: whether to return features as output :param isSingleStre

(self, x, return_feature=False, isSingleStream=False, return_pose=True, upsampleH=240, upsampleW=427)

Source from the content-addressed store, hash-verified

207 self.fc_pose = nn.Linear(512, feat_dim)
208
209 def forward(self, x, return_feature=False, isSingleStream=False, return_pose=True, upsampleH=240, upsampleW=427):
210 '''
211 inference DFNet_s. It can regress camera pose as well as extract intermediate layer features.
212 :param x: image blob (2B x C x H x W) two stream or (B x C x H x W) single stream
213 :param return_feature: whether to return features as output
214 :param isSingleStream: whether it's an single stream inference or siamese network inference
215 :param upsampleH: feature upsample size H
216 :param upsampleW: feature upsample size W
217 :return feature_maps: (2, [B, C, H, W]) or (1, [B, C, H, W]) or None
218 :return predict: [2B, 12] or [B, 12]
219 '''
220
221 # normalize input data
222 mean, std = x.new_tensor(self.mean), x.new_tensor(self.std)
223 x = (x - mean[:, None, None]) / std[:, None, None]
224
225 ### encoder ###
226 feature_maps = []
227 for i in range(len(self.encoder)):
228 x = self.encoder[i](x)
229
230 if i in self.hypercolumn_indices:
231 feature = x.clone()
232 feature_maps.append(feature)
233
234 if i==self.hypercolumn_indices[-1]:
235 if return_pose==False:
236 predict = None
237 break
238
239 ### extract and process intermediate features ###
240 if return_feature:
241 feature_maps = self.adaptation_layers(feature_maps) # (3, [B, C, H', W']), H', W' are different in each layer
242
243 if isSingleStream: # not siamese network style inference
244 feature_stacks = []
245 for f in feature_maps:
246 feature_stacks.append(torch.nn.UpsamplingBilinear2d(size=(upsampleH, upsampleW))(f))
247 feature_maps = [torch.stack(feature_stacks)] # (1, [3, B, C, H, W])
248 else: # siamese network style inference
249 feature_stacks_t = []
250 feature_stacks_r = []
251 for f in feature_maps:
252 # split real and nerf batches
253 batch = f.shape[0] # should be target batch_size + rgb batch_size
254 feature_t = f[:batch//2]
255 feature_r = f[batch//2:]
256
257 feature_stacks_t.append(torch.nn.UpsamplingBilinear2d(size=(upsampleH, upsampleW))(feature_t)) # GT img
258 feature_stacks_r.append(torch.nn.UpsamplingBilinear2d(size=(upsampleH, upsampleW))(feature_r)) # render img
259 feature_stacks_t = torch.stack(feature_stacks_t) # [3, B, C, H, W]
260 feature_stacks_r = torch.stack(feature_stacks_r) # [3, B, C, H, W]
261 feature_maps = [feature_stacks_t, feature_stacks_r] # (2, [3, B, C, H, W])
262 else:
263 feature_maps = None
264
265 if return_pose==False:
266 return feature_maps, predict

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected