def abs_to_hiprel(poses, joint_set): """ Converts an absolute pose into [hi]+relative_pose. """ assert_shape(poses, (None, joint_set.NUM_JOINTS, 3)) if isinstance(poses, torch.Tensor): root = poses[:, [joint_set.index_of('hip')]] rel = remove_root(poses, joint_set.index_of('hip')) return torch.cat([root, rel], dim=-2) else: root = poses[:, [joint_set.index_of('hip')]].copy() rel = remove_root(poses, joint_set.index_of('hip')) return np.concatenate([root, rel], axis=-2)
def eval(self, model=None): losses, preds = self.pred_and_calc_loss(model) losses = np.concatenate([losses[seq] for seq in self.sequences]) self.val_loss = np.nanmean(losses) self.losses_to_log = {self.prefix + '_loss': self.val_loss} self.losses = losses self.preds = preds # Assuming hip is the last component if self.is_absolute: self.losses_to_log[self.prefix + '_abs_loss'] = np.nanmean( losses[:, -3:]) self.losses_to_log[self.prefix + '_rel_loss'] = np.nanmean( losses[:, :-3]) else: self.losses_to_log[self.prefix + '_rel_loss'] = self.val_loss assert self.pctiles[ -1] == 99, "Currently the last percentile is hardcoded to be 99 for printing" sequence_mpjpes, sequence_pcks, sequence_pctiles, joint_means, joint_pctiles = \ eval_results(preds, self.data_3d_mm, self.joint_set, pctiles=self.pctiles, verbose=True) # Calculate relative error if self.is_absolute: rel_pred = {} rel_gt = {} for seq in preds: rel_pred[seq] = remove_root(preds[seq], self.joint_set.index_of('hip')) rel_gt[seq] = remove_root(self.data_3d_mm[seq], self.joint_set.index_of('hip')) rel_mean_error, _, _, _, _ = eval_results(rel_pred, rel_gt, self.joint_set, verbose=False) rel_mean_error = np.mean(list(rel_mean_error.values())) print("Root relative error: %.2f mm" % rel_mean_error) self.rel_mean_error = rel_mean_error self.losses_to_log[self.prefix + '_rel_error'] = rel_mean_error self.mean_sequence_mpjpe = np.mean(list(sequence_mpjpes.values())) self.mean_sequence_pck = np.mean(list(sequence_pcks.values())) self.losses_to_log[self.prefix + '_err'] = self.mean_sequence_mpjpe self.losses_to_log[self.prefix + '_pck'] = self.mean_sequence_pck return sequence_mpjpes, sequence_pcks, sequence_pctiles, joint_means, joint_pctiles
def preprocess_3d(data, add_root, log_root_z, joint_set, root_name): """ 3D preprocessing: 1. Removes the root joint 2. If add_root is True, append the root joint at the end of the pose. The The logarithm of the z coordinate of the root is taken. 3. Flattens the data. :param data: ndarray(nFrames, [nPoses], nJoints, 3[x, y, z]) 3D coordinates in MuPoTS order :param add_root: True if the absolute coordinates of the hip should be included in the output :param log_root_z:if true, the log of the z coordinate of the root is used :param root_name: name of the root joint, must be a MuPoTS joint :return: ndarray(nPoses, 3*nJoints|3*(nJoints-1)), 3*nJoints if add_root is true otherwise 3*(nJoints-1) """ assert_shape(data, ("*", joint_set.NUM_JOINTS, 3)) root_ind = joint_set.index_of(root_name) root3d = data[..., root_ind, :].copy() if log_root_z: root3d[..., 2] = np.log(root3d[..., 2]) data = remove_root(data, root_ind) # (nFrames, [nPoses], nJoints-1, 3) data = data.reshape(data.shape[:-2] + (-1,)) # (nFrames, [nPoses], (nJoints-1)*3) if add_root: data = np.concatenate([data, root3d], axis=-1) # (nFrames, [nPoses], nJoints*3) return data.astype('float32')
def abs_to_hiprel(poses, joint_set): """ Converts an absolute pose into [hi]+relative_pose. """ assert_shape(poses, (None, joint_set.NUM_JOINTS, 3)) root = poses[:, [joint_set.index_of('hip')]].copy() rel = remove_root(poses, joint_set.index_of('hip')) return np.concatenate([root, rel], axis=-2)
def eval(self, model=None, calculate_scale_free=False, verbose=False): """ :param model: the evaluator can use this model, if self.model is nor provided :param calculate_scale_free: if True, also calculates N-MRPE and N_RMPJPE :return: """ losses, preds = self.pred_and_calc_loss(model) losses = np.concatenate([losses[seq] for seq in self.sequences]) self.val_loss = np.nanmean(losses) self.losses_to_log = {self.prefix + '_loss': self.val_loss} self.losses = losses self.preds = preds # Assuming hip is the last component if self.is_absolute: self.losses_to_log[self.prefix + '_abs_loss'] = np.nanmean( losses[:, -3:]) self.losses_to_log[self.prefix + '_rel_loss'] = np.nanmean( losses[:, :-3]) else: self.losses_to_log[self.prefix + '_rel_loss'] = self.val_loss assert self.pctiles[ -1] == 99, "Currently the last percentile is hardcoded to be 99 for printing" sequence_mpjpes, sequence_pcks, sequence_pctiles, joint_means, joint_pctiles = \ eval_results(preds, self.data_3d_mm, self.joint_set, pctiles=self.pctiles, verbose=verbose) self.losses_to_log[self.prefix + '_mrpe'] = np.mean([ mrpe(preds[s], self.data_3d_mm[s], self.joint_set) for s in preds ]) # Calculate relative error if self.is_absolute: rel_pred = {} rel_gt = {} for seq in preds: rel_pred[seq] = remove_root(preds[seq], self.joint_set.index_of('hip')) rel_gt[seq] = remove_root(self.data_3d_mm[seq], self.joint_set.index_of('hip')) rel_mean_error, _, _, _, _ = eval_results(rel_pred, rel_gt, self.joint_set, verbose=False) rel_mean_error = np.mean( np.asarray(list(rel_mean_error.values()), dtype=np.float32)) if verbose: print("Root relative error (MPJPE): %.2f" % rel_mean_error) self.rel_mean_error = rel_mean_error self.losses_to_log[self.prefix + '_rel_error'] = rel_mean_error self.mean_sequence_mpjpe = np.mean( np.asarray(list(sequence_mpjpes.values()), dtype=np.float32)) self.mean_sequence_pck = np.mean( np.asarray(list(sequence_pcks.values()), dtype=np.float32)) self.losses_to_log[self.prefix + '_err'] = self.mean_sequence_mpjpe self.losses_to_log[self.prefix + '_pck'] = self.mean_sequence_pck if calculate_scale_free: scaled_preds = {} for seq in preds: # predict a single scale for the full video pred_points = preds[seq].reshape(1, -1, 3) gt_points = self.data_3d_mm[seq].reshape(1, -1, 3) s = optimal_scaling(pred_points, gt_points) scaled_preds[seq] = preds[seq] * s n_mrpe = np.mean([ mrpe(scaled_preds[s], self.data_3d_mm[s], self.joint_set) for s in scaled_preds ]) n_rmpjpe = np.mean([ r_mpjpe(scaled_preds[s], self.data_3d_mm[s], self.joint_set) for s in scaled_preds ]) if verbose: print('N-MRPE: %.1f' % n_mrpe) print('N-MPJPE: %.1f' % n_rmpjpe) self.losses_to_log[self.prefix + '_n_mrpe'] = n_mrpe self.losses_to_log[self.prefix + '_n_rel_err'] = n_rmpjpe return sequence_mpjpes, sequence_pcks, sequence_pctiles, joint_means, joint_pctiles