Skip to content

Commit

Permalink
Euler Angle Loss
Browse files Browse the repository at this point in the history
  • Loading branch information
kaseris committed Jan 31, 2024
1 parent d0d0ac5 commit 91f5dd3
Show file tree
Hide file tree
Showing 2 changed files with 29 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
27 changes: 27 additions & 0 deletions src/skelcast/losses/euler_angle_loss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import numpy as np
import torch
import torch.nn as nn

from skelcast.data.human36m.quaternion import qeuler


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 91f5dd3

Please sign in to comment.