Skip to content

Commit

Permalink
Merge pull request #77 from kaseris/losses/angle-loss
Browse files Browse the repository at this point in the history
Losses/angle loss
  • Loading branch information
kaseris committed Jan 31, 2024
2 parents d0d0ac5 + 2e69aa1 commit dd74009
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/skelcast/losses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

LOSSES = Registry()

from .logloss import LogLoss
from .logloss import LogLoss
from .euler_angle_loss import EulerAngleLoss
29 changes: 29 additions & 0 deletions src/skelcast/losses/euler_angle_loss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np
import torch
import torch.nn as nn

from skelcast.data.human36m.quaternion import qeuler
from skelcast.losses import LOSSES


@LOSSES.register_module()
class EulerAngleLoss(nn.Module):
def __init__(self, order="xyz", reduction="mean"):
super(EulerAngleLoss, self).__init__()
self._order = order
self._reduction = reduction

def forward(self, predictions: torch.Tensor, targets: torch.Tensor):
# Check the shape of predictions and targets
assert (
predictions.shape == targets.shape
), f"Predictions and targets must have the same shape."
assert (
predictions.shape[-1] == 3
), f"Predictions and targets must have 3 channels in the last dimension."

predicted_euler = qeuler(predictions, self._order, epsilon=1e-6)
angle_distance = (
torch.remainder(predicted_euler - targets + np.pi, 2 * np.pi) - np.pi
)
return torch.mean(torch.abs(angle_distance))

0 comments on commit dd74009

Please sign in to comment.