Exemple #1
0
    def load_mesh(self, data_dict):

        gender_type = "male" if data_dict["gender"] == -1 else "female"

        with torch.no_grad():
            bm = BodyModel(bm_path=self.bm_path%(gender_type), num_betas=self.num_betas, batch_size=1)
            body = bm.forward(pose_body=data_dict['pose_body'].unsqueeze(0), 
                            betas=data_dict['betas'].unsqueeze(0))

        mesh_ori = trimesh.Trimesh(vertices=c2c(body.v)[0], faces=c2c(body.f))

        # move the mesh to the original
        joints = c2c(body.Jtr)[0]
        root_xyz = joints[0]
        mesh_ori.vertices -= root_xyz

        verts = mesh_ori.vertices
        vert_normals = mesh_ori.vertex_normals
        face_normals = mesh_ori.face_normals
        faces = mesh_ori.faces

        mesh = HoppeMesh(
            verts=verts, 
            faces=faces, 
            vert_normals=vert_normals, 
            face_normals=face_normals)

        return {'mesh': mesh, 
                'A': body.A[0,:self.num_poses+1],
                'weights': body.weights}
    def forward(self, poZ_body=None, **kwargs):

        if self.poser_type == 'vposer':
            if self.model_type in ['smpl', 'smplh', 'smplx']:
                if poZ_body is None:  poZ_body = self.poZ_body

                pose = self.poser_body_pt.decode(poZ_body, output_type='aa').view(self.batch_size, -1)

                if pose.shape[1] > 63:
                    pose_body = pose[:, 3:66]
                    root_orient = pose[:, :3]
                else:
                    pose_body = pose[:, :63]
                    root_orient = None

                if self.use_hands and self.model_type in['smplh', 'smplx']:
                    pose_handL = self.poser_handL_pt.decode(self.poZ_handL, output_type='aa').view(self.batch_size, -1)
                    pose_handR = self.poser_handR_pt.decode(self.poZ_handR, output_type='aa').view(self.batch_size, -1)
                    pose_hand = torch.cat([pose_handL, pose_handR], dim=1)
                else:
                    pose_hand = None

                new_body = super(BodyModelWithPoser, self).forward(pose_body=pose_body, root_orient=root_orient, pose_hand=pose_hand, **kwargs)
                new_body.poZ_body = poZ_body


            if self.model_type in ['mano_left', 'mano_right']:
                pose_hand = self.poser_hand_pt.decode(self.poZ_hand, output_type='aa').view(self.batch_size, -1)
                new_body = super(BodyModelWithPoser, self).forward(pose_hand=pose_hand, **kwargs)

        else:
            new_body = BodyModel.forward(self)

        return new_body
Exemple #3
0
optimizer = torch.optim.Adam([pose_embedding], lr=0.1)
target_locs = torch.Tensor([[0.3, 0, 0.5], [-0.3, 0, 0.5]]).cuda()

wrist_idxs = list(range(20, 22))
for iter_idx in range(100):
    pose_body_off = vp.decode(pose_embedding, output_type="aa").view(-1, 63)
    if mode == "smplx":
        # outputs = model.forward(body_pose=pose_body_off - pose_body)
        # outputs = model.forward(body_pose=pose_body_off - pose_body)
        outputs = model.forward(body_pose=pose_body_off)
        verts = outputs.vertices
        joints = outputs.joints
    else:
        # out = bm.forward(pose_body=pose_body_off - pose_body, return_vertices=True)
        out = bm.forward(pose_body=pose_body_off, return_vertices=True)
        verts = out.v
        joints = out.Jtr
    wrist_joints = joints[:, wrist_idxs]
    diffs = wrist_joints - target_locs
    loss = (diffs ** 2).sum(-1).mean() + 0.001 * (pose_embedding ** 2).sum()
    optimizer.zero_grad()
    loss.backward(retain_graph=True)
    optimizer.step()
    # if (pose_embedding != pose_embedding).sum():
    #     import pdb; pdb.set_trace()

    pts = verts.cpu().detach().numpy()
    wrists = joints[0, wrist_idxs].cpu().detach().numpy()
    targ = target_locs.cpu().detach().numpy()
    plt.clf()