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

Method forward

script/feature/efficientnet.py:211–271  ·  view source on GitHub ↗

inference DFNet. 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 isSingleStream

(self, x, return_feature=False, isSingleStream=False, return_pose=False, upsampleH=120, upsampleW=213)

Source from the content-addressed store, hash-verified

209 self.fc_pose = nn.Linear(1280, feat_dim)
210
211 def forward(self, x, return_feature=False, isSingleStream=False, return_pose=False, upsampleH=120, upsampleW=213):
212 '''
213 inference DFNet. It can regress camera pose as well as extract intermediate layer features.
214 :param x: image blob (2B x C x H x W) two stream or (B x C x H x W) single stream
215 :param return_feature: whether to return features as output
216 :param isSingleStream: whether it's an single stream inference or siamese network inference
217 :param return_pose: TODO: if only return_pose, we don't need to compute return_feature part
218 :param upsampleH: feature upsample size H
219 :param upsampleW: feature upsample size W
220 :return feature_maps: (2, [B, C, H, W]) or (1, [B, C, H, W]) or None
221 :return predict: [2B, 12] or [B, 12]
222 '''
223 # normalize input data
224 mean, std = x.new_tensor(self.mean), x.new_tensor(self.std)
225 x = (x - mean[:, None, None]) / std[:, None, None]
226
227 ### encoder ###
228 feature_maps = []
229 list_x = self.feature_extractor(x)
230
231 x = list_x['reduction_6'] # features to save
232 for i in self.feature_block_index:
233 fe = list_x['reduction_'+str(i)].clone()
234 feature_maps.append(fe)
235
236 ### extract and process intermediate features ###
237 if return_feature:
238 feature_maps = self.adaptation_layers(feature_maps) # (3, [B, C, H', W']), H', W' are different in each layer
239
240 if isSingleStream: # not siamese network style inference
241 feature_stacks = []
242
243 for f in feature_maps:
244 feature_stacks.append(torch.nn.UpsamplingBilinear2d(size=(upsampleH, upsampleW))(f))
245 feature_maps = [torch.stack(feature_stacks)] # (1, [3, B, C, H, W])
246 else: # siamese network style inference
247 feature_stacks_t = []
248 feature_stacks_r = []
249
250 for f in feature_maps:
251
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
263 else:
264 feature_maps = None
265
266 ### pose regression head ###
267 x = self.avgpool(x)
268 x = x.reshape(x.size(0), -1)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected