MCPcopy Index your code
hub / github.com/ActiveVisionLab/DFNet / train_on_feature_batch

Function train_on_feature_batch

script/feature/direct_feature_matching.py:235–320  ·  view source on GitHub ↗

Perform 1 step of training using scheme1

(args, data, model, feat_model, pose, img_idx, hwf, optimizer, device, world_setup_dict, **render_kwargs_test)

Source from the content-addressed store, hash-verified

233 return total_loss_mean, total_psnr_mean
234
235def train_on_feature_batch(args, data, model, feat_model, pose, img_idx, hwf, optimizer, device, world_setup_dict, **render_kwargs_test):
236 ''' Perform 1 step of training using scheme1 '''
237 batch_size_iter = data.shape[0]
238
239 H, W, focal = hwf
240 data = data.to(device) # [1, 3, 240, 427]
241
242 # pose regression module
243 _, pose_ = inference_pose_regression(args, data, device, model, retFeature=False) # here returns predicted pose [1, 3, 4] # real img features and predicted pose # features: (1, [3, 1, 128, 240, 427]), predict_pose: [1, 3, 4]
244 pose_nerf = pose_.clone()
245
246 # rescale the predicted pose to nerf scales
247 pose_nerf = fix_coord_supp(args, pose_nerf, world_setup_dict, device=device)
248
249 pose = pose.to(device)
250 img_idx = img_idx.to(device)
251 # every new tensor from onward is in GPU, here memory cost is a bottleneck
252 torch.set_default_tensor_type('torch.cuda.FloatTensor')
253
254 # here is single frame
255 target = data.permute(0,2,3,1) # [B,H,W,C]
256 rays_o_list=[]
257 rays_d_list=[]
258 img_idx_list=[]
259 N_rand = args.N_rand
260 for i in range(pose_nerf.shape[0]):
261 rays_o, rays_d = get_rays(H, W, focal, pose_nerf[i]) # (H, W, 3), (H, W, 3)
262 rays_o_list.append(rays_o)
263 rays_d_list.append(rays_d)
264 img_idx_list.append(img_idx[i].repeat(N_rand,1))
265 rays_o_batch = torch.stack(rays_o_list)
266 rays_d_batch = torch.stack(rays_d_list)
267 img_idx_batch = torch.cat(img_idx_list)
268
269 # randomly select coords
270 coords = torch.stack(torch.meshgrid(torch.linspace(0, H-1, H), torch.linspace(0, W-1, W), indexing='ij'), -1) # (H, W, 2)
271 coords = torch.reshape(coords, [-1,2]) # (H * W, 2)
272 select_inds = np.random.choice(coords.shape[0], size=[N_rand], replace=False) # (N_rand,)
273 select_coords = coords[select_inds].long() # (N_rand, 2)
274
275 # fetch from coords
276 rays_o = rays_o_batch[:, select_coords[:, 0], select_coords[:, 1]]
277 rays_d = rays_d_batch[:, select_coords[:, 0], select_coords[:, 1]]
278 rays_o = rays_o.reshape(rays_o.shape[0]*rays_o.shape[1], 3) # (B*N_rand, 3)
279 rays_d = rays_d.reshape(rays_d.shape[0]*rays_d.shape[1], 3) # (B*N_rand, 3)
280 batch_rays = torch.stack([rays_o, rays_d], 0)
281 target_s = target[:,select_coords[:, 0], select_coords[:, 1]].reshape(batch_size_iter*N_rand,3) # (B*N_rand, 3)
282
283 rgb_feature, disp, acc, extras = render(H, W, focal, chunk=args.chunk, rays=batch_rays, img_idx=img_idx_batch, **render_kwargs_test)
284 # rgb_feature is rgb 3 + features 128
285 rgb = rgb_feature[...,:3] # [B*N_rand, 3]
286 feature = rgb_feature[...,3:].reshape(batch_size_iter, N_rand, args.out_channel_size-3)[None, ...].permute(0,1,3,2) # [lvl, B, C, N_rand] assuming lvl size = 1
287
288 # inference featurenet
289 target_in = target.permute(0,3,1,2)
290 features, _ = feat_model(target_in, True, True, H, W) # features: (1, [3,B,C,H,W])
291
292 # get features_target, # now choose 1st level feature only

Callers

nothing calls this directly

Calls 7

fix_coord_suppFunction · 0.90
get_raysFunction · 0.90
renderFunction · 0.90
feature_lossFunction · 0.85
rgb_lossFunction · 0.70
PoseLossFunction · 0.70

Tested by

no test coverage detected