bluestyle97 commited on
Commit
b770527
1 Parent(s): 8e25beb

Update freesplatter/webui/runner.py

Browse files
Files changed (1) hide show
  1. freesplatter/webui/runner.py +68 -37
freesplatter/webui/runner.py CHANGED
@@ -144,12 +144,12 @@ class FreeSplatterRunner:
144
  self.freesplatter_scene = model.eval().to(device)
145
 
146
  # mesh optimizer
147
- self.mesh_renderer = MeshRenderer(
148
- near=0.01,
149
- far=100,
150
- ssaa=1,
151
- texture_filter='linear-mipmap-linear',
152
- device=device).to(device)
153
 
154
  @torch.inference_mode()
155
  def run_segmentation(
@@ -375,45 +375,76 @@ class FreeSplatterRunner:
375
  t4 = time.time()
376
 
377
  # optimize texture
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
  cam_pos = c2ws_fusion[:, :3, 3].cpu().numpy()
379
  cam_inds = torch.from_numpy(fpsample.fps_sampling(cam_pos, 16).astype(int)).to(device=device)
380
 
381
  alphas_bake = alphas_fusion[cam_inds]
382
  images_bake = (images_fusion[cam_inds] - (1 - alphas_bake)) / alphas_bake.clamp(min=1e-6)
383
 
 
 
 
 
 
 
 
384
  out_mesh = Mesh.load(str(mesh_path), auto_uv=False, device='cpu')
385
- max_faces = 50000
386
- mesh_reduction = max(1 - max_faces / out_mesh.f.shape[0], mesh_reduction)
387
- mesh_verts_, mesh_faces_ = fast_simplification.simplify(
388
- out_mesh.v.numpy(), out_mesh.f.numpy(), target_reduction=mesh_reduction)
389
- mesh_verts = out_mesh.v.new_tensor(mesh_verts_, dtype=torch.float32).requires_grad_(False)
390
- mesh_faces = out_mesh.f.new_tensor(mesh_faces_).requires_grad_(False)
391
- out_mesh = Mesh(v=mesh_verts, f=mesh_faces)
392
- out_mesh.auto_normal()
393
- out_mesh.auto_uv()
394
- out_mesh = out_mesh.to(device)
395
-
396
- intrinsics = fxfycxcy_fusion[0:1].clone()
397
- intrinsics[..., [0, 2]] *= images_bake.shape[-2]
398
- intrinsics[..., [1, 3]] *= images_bake.shape[-3]
399
-
400
- out_mesh = self.mesh_renderer.bake_multiview(
401
- [out_mesh],
402
- images_bake.unsqueeze(0),
403
- alphas_bake.unsqueeze(0),
404
- c2ws_fusion[cam_inds].unsqueeze(0),
405
- intrinsics.unsqueeze(0),
406
- )[0]
407
  mesh_fine_path = os.path.join(self.output_dir, 'mesh.glb')
408
- # align mesh orientation
409
- out_mesh.v = out_mesh.v.clone()
410
- out_mesh.vn = out_mesh.vn.clone()
411
- out_mesh.v[..., 0] = -out_mesh.v[..., 0]
412
- out_mesh.vn[..., 0] = -out_mesh.vn[..., 0]
413
- out_mesh.v[..., [1, 2]] = out_mesh.v[..., [2, 1]]
414
- out_mesh.vn[..., [1, 2]] = out_mesh.vn[..., [2, 1]]
415
-
416
- out_mesh.write(mesh_fine_path, flip_yz=False)
417
  print(f"Save optimized mesh at {mesh_fine_path}")
418
  t5 = time.time()
419
 
 
144
  self.freesplatter_scene = model.eval().to(device)
145
 
146
  # mesh optimizer
147
+ # self.mesh_renderer = MeshRenderer(
148
+ # near=0.01,
149
+ # far=100,
150
+ # ssaa=1,
151
+ # texture_filter='linear-mipmap-linear',
152
+ # device=device).to(device)
153
 
154
  @torch.inference_mode()
155
  def run_segmentation(
 
375
  t4 = time.time()
376
 
377
  # optimize texture
378
+ # cam_pos = c2ws_fusion[:, :3, 3].cpu().numpy()
379
+ # cam_inds = torch.from_numpy(fpsample.fps_sampling(cam_pos, 16).astype(int)).to(device=device)
380
+
381
+ # alphas_bake = alphas_fusion[cam_inds]
382
+ # images_bake = (images_fusion[cam_inds] - (1 - alphas_bake)) / alphas_bake.clamp(min=1e-6)
383
+
384
+ # out_mesh = Mesh.load(str(mesh_path), auto_uv=False, device='cpu')
385
+ # max_faces = 50000
386
+ # mesh_reduction = max(1 - max_faces / out_mesh.f.shape[0], mesh_reduction)
387
+ # mesh_verts_, mesh_faces_ = fast_simplification.simplify(
388
+ # out_mesh.v.numpy(), out_mesh.f.numpy(), target_reduction=mesh_reduction)
389
+ # mesh_verts = out_mesh.v.new_tensor(mesh_verts_, dtype=torch.float32).requires_grad_(False)
390
+ # mesh_faces = out_mesh.f.new_tensor(mesh_faces_).requires_grad_(False)
391
+ # out_mesh = Mesh(v=mesh_verts, f=mesh_faces)
392
+ # out_mesh.auto_normal()
393
+ # out_mesh.auto_uv()
394
+ # out_mesh = out_mesh.to(device)
395
+
396
+ # intrinsics = fxfycxcy_fusion[0:1].clone()
397
+ # intrinsics[..., [0, 2]] *= images_bake.shape[-2]
398
+ # intrinsics[..., [1, 3]] *= images_bake.shape[-3]
399
+
400
+ # out_mesh = self.mesh_renderer.bake_multiview(
401
+ # [out_mesh],
402
+ # images_bake.unsqueeze(0),
403
+ # alphas_bake.unsqueeze(0),
404
+ # c2ws_fusion[cam_inds].unsqueeze(0),
405
+ # intrinsics.unsqueeze(0),
406
+ # )[0]
407
+ # mesh_fine_path = os.path.join(self.output_dir, 'mesh.glb')
408
+ # # align mesh orientation
409
+ # out_mesh.v = out_mesh.v.clone()
410
+ # out_mesh.vn = out_mesh.vn.clone()
411
+ # out_mesh.v[..., 0] = -out_mesh.v[..., 0]
412
+ # out_mesh.vn[..., 0] = -out_mesh.vn[..., 0]
413
+ # out_mesh.v[..., [1, 2]] = out_mesh.v[..., [2, 1]]
414
+ # out_mesh.vn[..., [1, 2]] = out_mesh.vn[..., [2, 1]]
415
+
416
+ # out_mesh.write(mesh_fine_path, flip_yz=False)
417
+ # print(f"Save optimized mesh at {mesh_fine_path}")
418
+ # t5 = time.time()
419
+
420
+ # optimize texture
421
+ from freesplatter.utils.mesh_optim import optimize_mesh
422
  cam_pos = c2ws_fusion[:, :3, 3].cpu().numpy()
423
  cam_inds = torch.from_numpy(fpsample.fps_sampling(cam_pos, 16).astype(int)).to(device=device)
424
 
425
  alphas_bake = alphas_fusion[cam_inds]
426
  images_bake = (images_fusion[cam_inds] - (1 - alphas_bake)) / alphas_bake.clamp(min=1e-6)
427
 
428
+ fxfycxcy = fxfycxcy_fusion[cam_inds].clone()
429
+ intrinsics = torch.eye(3).unsqueeze(0).repeat(len(cam_inds), 1, 1).to(fxfycxcy)
430
+ intrinsics[:, 0, 0] = fxfycxcy[:, 0]
431
+ intrinsics[:, 0, 2] = fxfycxcy[:, 2]
432
+ intrinsics[:, 1, 1] = fxfycxcy[:, 1]
433
+ intrinsics[:, 1, 2] = fxfycxcy[:, 3]
434
+
435
  out_mesh = Mesh.load(str(mesh_path), auto_uv=False, device='cpu')
436
+ out_mesh = optimize_mesh(
437
+ out_mesh,
438
+ images_bake,
439
+ alphas_bake.squeeze(-1),
440
+ c2ws_fusion[cam_inds].inverse(),
441
+ intrinsics,
442
+ simplify=mesh_reduction,
443
+ verbose=False
444
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
445
  mesh_fine_path = os.path.join(self.output_dir, 'mesh.glb')
446
+
447
+ out_mesh.export(mesh_fine_path)
 
 
 
 
 
 
 
448
  print(f"Save optimized mesh at {mesh_fine_path}")
449
  t5 = time.time()
450