def recon_eval(op_shapes, pre_j3ds, gt_j3ds, visual, key): pose0 = torch.eye(3).repeat(1, 16, 1, 1) mano = manolayer.ManoLayer(flat_hand_mean=True, side="right", mano_root='mano/models', use_pca=False, root_rot_mode='rotmat', joint_rot_mode='rotmat') j3d_recons = [] evaluator = EvalUtil() for i in tqdm(range(pre_j3ds.shape[0])): j3d_pre = pre_j3ds[i] op_shape = torch.tensor(op_shapes[i]).float().unsqueeze(0) _, j3d_p0_ops = mano(pose0, op_shape) template = j3d_p0_ops.cpu().numpy().squeeze() / 1000.0 # template, m ratio = np.linalg.norm(template[9] - template[0]) / np.linalg.norm(j3d_pre[9] - j3d_pre[0]) j3d_pre_process = j3d_pre * ratio # template, m j3d_pre_process = j3d_pre_process - j3d_pre_process[0] + template[0] pose_R = AIK.adaptive_IK(template, j3d_pre_process) pose_R = torch.from_numpy(pose_R).float() # reconstruction hand_verts, j3d_recon = mano(pose_R, op_shape.float()) # visualization if visual: demo.display_hand( { 'verts': hand_verts.cpu(), 'joints': j3d_recon.cpu() }, mano_faces=mano.th_faces) j3d_recon = j3d_recon.cpu().numpy().squeeze() / 1000. j3d_recons.append(j3d_recon) # visualization if visual: vis.multi_plot3d([j3d_recon, j3d_pre_process], title=["recon", "pre"]) j3d_recons = np.array(j3d_recons) gt_joint, j3d_recon_align_gt = align.global_align(gt_j3ds, j3d_recons, key=key) for targj, predj_a in zip(gt_joint, j3d_recon_align_gt): evaluator.feed(targj * 1000.0, predj_a * 1000.0) (_1, _2, _3, auc_all, pck_curve_all, thresholds) = evaluator.get_measures(20, 50, 15) print("Reconstruction AUC all of {}_test_set is : {}".format(key, auc_all))
# print("grad is {}".format(random_pose.grad)) with torch.no_grad(): random_shape -= learning_rate * random_shape.grad random_pose -= learning_rate * random_pose.grad # Manually zero the gradients after updating weights random_shape.grad.zero_() random_pose.grad.zero_() if np.abs(loss.item() - last_loss) < learning_rate or i > 1000: break else: last_loss = loss.item() writeObj( os.path.join( "D:\\pycharm_project\\Fit_hands\\manopth\\hand_output", "hand-{}.obj".format(j)), hand_verts.detach().numpy(), face) # animation_hands(h_joints, ax, color='r') animation_hands(np.array(cors), ax, color='y') # plt.show() # print(hand_joints.numpy()[0, :, :]) # writeObj("hand-900.obj", hand_verts.detach().numpy(), ) demo.display_hand( { 'verts': hand_verts.detach().numpy(), 'joints': hand_joints.detach().numpy() }, ax=ax, mano_faces=mano_layer.th_faces)
import torch from manopth.manolayer import ManoLayer from manopth.manolayer2 import ManoLayer as ManoLayer2 from manopth import demo torch.random.manual_seed(4) batch_size = 10 # Select number of principal components for pose space ncomps = 6 # Initialize MANO layer mano_layer = ManoLayer(mano_root='mano/models', use_pca=True, ncomps=ncomps, flat_hand_mean=False) # Generate random shape parameters random_shape = torch.rand(batch_size, 10) # Generate random pose parameters, including 3 values for global axis-angle rotation random_pose = torch.rand(batch_size, ncomps + 3) # Forward pass through MANO layer hand_verts, hand_joints = mano_layer(random_pose, random_shape) demo.display_hand({ 'verts': hand_verts, 'joints': hand_joints }, mano_faces=mano_layer.th_faces)
def main(args): batch_size = args.batch_size if args.use_pca: ncomps = 12 else: ncomps = 45 # Initialize MANO layer mano_layer = ManoLayer( mano_root="mano/models", use_pca=args.use_pca, ncomps=ncomps, flat_hand_mean=args.flat_hand_mean, center_idx=9, return_transf=True, ) faces = np.array(mano_layer.th_faces).astype(np.long) # Generate random shape parameters random_shape = torch.rand(batch_size, 10) # Generate random pose parameters, including 3 values for global axis-angle rotation if args.use_pca: random_pose = torch.rand(batch_size, ncomps + 3) else: random_pose = torch.zeros(batch_size, ncomps + 3) # Forward pass through MANO layer vertices, joints, transf = mano_layer(random_pose, random_shape) if args.render == "plt": demo.display_hand( { "verts": vertices, "joints": joints }, mano_faces=mano_layer.th_faces, ) elif args.render == "pyrender": # =========================== Viewer Options >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> scene = pyrender.Scene() cam = pyrender.PerspectiveCamera(yfov=np.pi / 3.0, aspectRatio=1.414) node_cam = pyrender.Node(camera=cam, matrix=np.eye(4)) scene.add_node(node_cam) scene.set_pose(node_cam, pose=np.eye(4)) vertex_colors = np.array([200, 200, 200, 150]) joint_colors = np.array([10, 73, 233, 255]) # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< transl = np.array([0, 0, -200.0]) transl = transl[np.newaxis, :] joints = np.array(joints[0]) vertices = np.array(vertices[0]) transf = np.array(transf[0]) joints = joints * 1000.0 + transl vertices = vertices * 1000.0 + transl transf[:, :3, 3] = transf[:, :3, 3] * 1000.0 + transl tri_mesh = trimesh.Trimesh(vertices, faces, vertex_colors=vertex_colors) mesh = pyrender.Mesh.from_trimesh(tri_mesh) scene.add(mesh) # Add Joints for j in range(21): sm = trimesh.creation.uv_sphere(radius=2) sm.visual.vertex_colors = joint_colors tfs = np.tile(np.eye(4), (1, 1, 1)) tfs[0, :3, 3] = joints[j] joints_pcl = pyrender.Mesh.from_trimesh(sm, poses=tfs) scene.add(joints_pcl) # Add Transformation for tf in range(16): axis = trimesh.creation.axis(transform=transf[tf], origin_size=3, axis_length=15) axis = pyrender.Mesh.from_trimesh(axis, smooth=False) scene.add(axis) pyrender.Viewer(scene, use_raymond_lighting=True) else: raise ValueError(f"Unknown renderer: {args.render}") return 0
-0.5, 0.5, 1.5, 0.0, 0.5, 0.0, 0.0, 1.0, -0.5, ] ) pose = pose.unsqueeze(dim=0) shape = torch.rand(batch_size, 10) min_val = 0 max_val = 15 values = np.arange(min_val, max_val, 1) for value in values: pose[0, 3] = value hand_verts, hand_joints = mano_layer(pose, shape) display_hand( {"verts": hand_verts, "joints": hand_joints}, mano_faces=mano_layer.th_faces ) # %% rot_max_list = [ 10.0, 10.0, 10.0, 0.25, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5, 0.25, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.7, 0.25, 0.5, 1.5, 1.0, 0.0, 1.0, 0.0, 0.0, 1.5, 0.25, 0.5, 1.0,
# Generate random pose coefficients pose_params = torch.rand(args.batch_size, n_components + rot) pose_params.requires_grad = True if args.random_shape: shape = torch.rand(args.batch_size, 10) else: shape = torch.zeros(1) # Hack to act like None for PyTorch JIT if args.cuda: pose_params = pose_params.cuda() # 注意写法的区别 shape = shape.cuda() layer.cuda() # Loop for forward/backward quick profiling # for idx in tqdm(range(args.iters)): # Forward pass # verts, Jtr = layer(pose_params, th_betas=shape) # 这里执行forward函数 # Backward pass # loss = torch.norm(verts) # loss.backward() if not args.no_display: verts, Jtr = layer(pose_params, th_betas=shape) joints = Jtr.cpu().detach() verts = verts.cpu().detach() # Draw obtained vertices and joints display_hand({ 'verts': verts, 'joints': joints }, mano_faces=layer.th_faces)
ncomps=ncomps, flat_hand_mean=True, center_idx=0) mano_layer2 = ManoLayer2(template_root='mano/models', ncomps=ncomps) # Generate random shape parameters random_shape = torch.rand(batch_size, 10) # Generate random pose parameters, including 3 values for global axis-angle rotation random_pose = torch.rand(batch_size, ncomps + 3) # Forward pass through MANO layer hand_verts, hand_joints = mano_layer(random_pose, random_shape) hand_verts2, hand_joints2 = mano_layer2(random_pose, random_shape) print(hand_verts.shape) print(hand_joints.shape) print(torch.equal(hand_verts, hand_verts2)) print(torch.equal(hand_joints, hand_joints2)) demo.display_hand({ 'verts': hand_verts, 'joints': hand_joints }, mano_faces=mano_layer.th_faces, savename="1") demo.display_hand({ 'verts': hand_verts2, 'joints': hand_joints2 }, mano_faces=mano_layer.th_faces, savename="2") print(hand_joints[0, 0]) print(hand_joints2[0, 0])