def fit_pose(frame, last_smpl, frustum, nohands, viz_rn): if nohands: faces = faces_no_hands(frame.smpl.f) else: faces = frame.smpl.f dst_type = cv2.cv.CV_DIST_L2 if cv2.__version__[0] == '2' else cv2.DIST_L2 dist_i = cv2.distanceTransform(np.uint8(frame.mask * 255), dst_type, 5) - 1 dist_i[dist_i < 0] = 0 dist_i[dist_i > 50] = 50 dist_o = cv2.distanceTransform(255 - np.uint8(frame.mask * 255), dst_type, 5) dist_o[dist_o > 50] = 50 rn_m = ColoredRenderer(camera=frame.camera, v=frame.smpl, f=faces, vc=np.ones_like(frame.smpl), frustum=frustum, bgcolor=0, num_channels=1) E = { 'mask': gaussian_pyramid(rn_m * dist_o * 100. + (1 - rn_m) * dist_i, n_levels=4, normalization='size') * 80., '2dpose': GMOf(frame.pose_obj, 100), 'prior': frame.pose_prior_obj * 4., 'sp': frame.collision_obj * 1e3, } if last_smpl is not None: E['last_pose'] = GMOf(frame.smpl.pose - last_smpl.pose, 0.05) * 50. E['last_trans'] = GMOf(frame.smpl.trans - last_smpl.trans, 0.05) * 50. if nohands: x0 = [ frame.smpl.pose[range(21) + range(27, 30) + range(36, 60)], frame.smpl.trans ] else: x0 = [ frame.smpl.pose[range(21) + range(27, 30) + range(36, 72)], frame.smpl.trans ] ch.minimize(E, x0, method='dogleg', options={ 'e_3': .01, }, callback=get_cb(viz_rn, frame))
def test_earth(): m = get_earthmesh(trans=ch.array([0, 0, 0]), rotation=ch.zeros(3)) # Create V, A, U, f: geometry, brightness, camera, renderer V = ch.array(m.v) A = SphericalHarmonics(vn=VertNormals(v=V, f=m.f), components=[3., 2., 0., 0., 0., 0., 0., 0., 0.], light_color=ch.ones(3)) # camera U = ProjectPoints(v=V, f=[w, w], c=[w / 2., h / 2.], k=ch.zeros(5), t=ch.zeros(3), rt=ch.zeros(3)) f = TexturedRenderer(vc=A, camera=U, f=m.f, bgcolor=[0., 0., 0.], texture_image=m.texture_image, vt=m.vt, ft=m.ft, frustum={ 'width': w, 'height': h, 'near': 1, 'far': 20 }) # Parameterize the vertices translation, rotation = ch.array([0, 0, 8]), ch.zeros(3) f.v = translation + V.dot(Rodrigues(rotation)) observed = f.r np.random.seed(1) # this is reactive # in the sense that changes to values will affect function which depend on them. translation[:] = translation.r + np.random.rand(3) rotation[:] = rotation.r + np.random.rand(3) * .2 # Create the energy E_raw = f - observed E_pyr = gaussian_pyramid(E_raw, n_levels=6, normalization='size') Image.fromarray((observed * 255).astype(np.uint8)).save( os.path.join(save_dir, "reference.png")) step = 0 Image.fromarray((f.r * 255).astype(np.uint8)).save( os.path.join(save_dir, "step_{:05d}.png".format(step))) print('OPTIMIZING TRANSLATION, ROTATION, AND LIGHT PARMS') free_variables = [translation, rotation] ch.minimize({'pyr': E_pyr}, x0=free_variables, callback=create_callback(f)) ch.minimize({'raw': E_raw}, x0=free_variables, callback=create_callback(f))
def final_fit( opt, part_mesh, v, v_offset, dist_o, dist_i, smpl_h_ref, rn_m, debug_rn, dif_mask, v_ids_template, faces_template, v_ids_side, faces_side, max_y, proj_cam, ref_joint_list_coup, ): if opt.disp_mesh_side: mv = MeshViewer() else: mv = None if opt.disp_mesh_whl: mv2 = MeshViewer() else: mv2 = None import scipy.sparse as sp sparse_solver = lambda A, x: sp.linalg.cg(A, x, maxiter=500)[0] tgt_pose = get_pose_prior(init_pose_path=opt.init_pose_path, gar_type=opt.gar_type) E = { 'mask': gaussian_pyramid(rn_m * dist_o * opt.ref_wt_dist_o + (1 - rn_m) * dist_i, n_levels=4, normalization='size') * opt.ref_wt_mask } x0 = [v_offset] if opt.ref_wt_coup: # x0 = [smpl_h_ref.trans, smpl_h_ref.betas, v_offset] E['coupling'] = (v + v_offset - smpl_h_ref[v_ids_template]) * opt.ref_wt_coup if opt.ref_wt_shp: E['beta_prior'] = ch.linalg.norm(smpl_h_ref.betas) * opt.ref_wt_shp if opt.ref_wt_pose: E['pose'] = (smpl_h_ref.pose - tgt_pose) * opt.ref_wt_pose if ref_joint_list_coup != None: range_joint = [] for item in ref_joint_list_coup: range_joint.append(3 * int(item)) range_joint.append(3 * int(item) + 1) range_joint.append(3 * int(item) + 2) x0 = x0 + [smpl_h_ref.pose[range_joint]] if opt.ref_use_betas: x0 = x0 + [smpl_h_ref.betas] if opt.ref_wt_proj: error_bd = get_rings_error(proj_cam, max_y) E['proj_bd'] = error_bd * opt.ref_wt_proj if opt.ref_wt_bd: gar_rings = compute_boundaries(v + v_offset, faces_template) error = smooth_rings(gar_rings, v + v_offset) E['boundary'] = error * opt.ref_wt_bd if opt.ref_wt_lap: lap_op = np.asarray(laplacian(part_mesh).todense()) lap_err = ch.dot(lap_op, v + v_offset) E['laplacian'] = lap_err * opt.ref_wt_lap ch.minimize(E, x0, method='dogleg', options={ 'e_3': .000001, 'sparse_solver': sparse_solver }, callback=get_callback_ref(rend=debug_rn, mask=dif_mask, vertices=v + v_offset, display=opt.display, v_ids_sides=v_ids_side, faces_template=faces_template, faces_side=faces_side, disp_mesh_side=opt.disp_mesh_side, disp_mesh_whl=opt.disp_mesh_whl, save_dir=opt.save_opt_images, mv=mv, mv2=mv2, show=opt.show)) final_mask = rn_m.r mask = dif_mask.copy() mask[dif_mask == 0.5] = 1 final_iou = compute_iou(mask, final_mask) return v + v_offset, final_iou
def init_fit(opt, dist_o, dist_i, dif_mask, rn_m, smpl_h, v_ids_template, faces_template, debug_rn, v_ids_side, faces_side, joints_list): range_joint = [] for item in joints_list: range_joint.append(3 * int(item)) range_joint.append(3 * int(item) + 1) range_joint.append(3 * int(item) + 2) tgt_pose = get_pose_prior(init_pose_path=opt.init_pose_path, gar_type=opt.gar_type) # ============================================ # FIRST STAGE # ============================================ from psbody.mesh import MeshViewer if opt.disp_mesh_side: mv = MeshViewer() else: mv = None if opt.disp_mesh_whl: mv2 = MeshViewer() else: mv2 = None if opt.init_first_stage == "Trans": x0 = [smpl_h.trans] elif opt.init_first_stage == 'Pose': x0 = [smpl_h.trans, smpl_h.pose[range_joint]] # x0 = [smpl_h.trans] elif opt.init_first_stage == 'Shape': x0 = [smpl_h.trans, smpl_h.betas] elif opt.init_first_stage == 'Both': x0 = [smpl_h.trans, smpl_h.betas, smpl_h.pose[range_joint]] E = { 'mask': gaussian_pyramid(rn_m * dist_o * opt.init_fst_wt_dist_o + (1 - rn_m) * dist_i, n_levels=4, normalization='size') * opt.init_fst_wt_mask } if opt.init_fst_wt_betas: E['beta_prior'] = ch.linalg.norm(smpl_h.betas) * opt.init_fst_wt_betas if opt.init_fst_wt_pose: E['pose'] = (smpl_h.pose - tgt_pose) * opt.init_fst_wt_pose ch.minimize(E, x0, method='dogleg', options={ 'e_3': .0001, 'disp': True }, callback=get_callback(rend=debug_rn, mask=dif_mask, smpl=smpl_h, v_ids_template=v_ids_template, v_ids_sides=v_ids_side, faces_template=faces_template, faces_side=faces_side, display=opt.display, disp_mesh_side=opt.disp_mesh_side, disp_mesh_whl=opt.disp_mesh_whl, mv=mv, mv2=mv2, save_dir=opt.save_opt_images, show=opt.show)) # =============================================== # SECOND STAGE # =============================================== if opt.init_second_stage != "None": if opt.init_second_stage == 'Pose': x0 = [smpl_h.trans, smpl_h.pose[range_joint]] elif opt.init_second_stage == 'Shape': x0 = [smpl_h.trans, smpl_h.betas] elif opt.init_second_stage == 'Both': x0 = [smpl_h.trans, smpl_h.betas, smpl_h.pose[range_joint]] E = { 'mask': gaussian_pyramid(rn_m * dist_o * opt.init_sec_wt_dist_o + (1 - rn_m) * dist_i, n_levels=4, normalization='size') * opt.init_sec_wt_mask } if opt.init_sec_wt_betas: E['beta_prior'] = ch.linalg.norm( smpl_h.betas) * opt.init_sec_wt_betas if opt.init_sec_wt_pose: E['pose'] = (smpl_h.pose - tgt_pose) * opt.init_sec_wt_pose ch.minimize(E, x0, method='dogleg', options={'e_3': .0001}, callback=get_callback(rend=debug_rn, mask=dif_mask, smpl=smpl_h, v_ids_template=v_ids_template, v_ids_sides=v_ids_side, faces_template=faces_template, faces_side=faces_side, display=opt.display, disp_mesh_side=opt.disp_mesh_side, disp_mesh_whl=opt.disp_mesh_whl, mv=mv, mv2=mv2, save_dir=opt.save_opt_images, show=opt.show)) temp_params = { 'pose': smpl_h.pose.r, 'betas': smpl_h.betas.r, 'trans': smpl_h.trans.r, 'v_personal': smpl_h.v_personal.r } part_mesh = Mesh(smpl_h.r[v_ids_template], faces_template) return part_mesh, temp_params
#chPositionGT[1] = 0.01 #plt.figure() #plt.title('Init object') _x0 = renderer.r #plt.imshow(_x0) #plt.show(0.1) #sys.exit('Alles Gut!!!') difference = renderer - rendererGT print ('=======================>','difference', type(difference)) gpModel = gaussian_pyramid(difference).sum() #sys.exit('Alles Gut') #plt.title('Init object') global iter iter = 0 def cb(_): pass global method methods = ['dogleg', 'minimize', 'BFGS', 'L-BFGS-B', 'Nelder-Mead', 'SGDMom'] method = 1 options = {'disp': True, 'maxiter': 1000, 'lr':2e-4, 'momentum' : 0.4, 'decay' :0.9, 'tol' : 1e-7}