Here's the structured loss function description incorporating your requirements:
Loss Function Components
The loss function combines three main components to penalize mismatches in direction, half-fov, and containment:
1. Direction Loss
Measures the angular difference between the predicted direction and target direction.
- Formula:
[
L_{\text{dir}} = 2 \cdot \left(1 - \mathbf{dir}{\text{pred}} \cdot \mathbf{dir}{\text{target}} \right)
]
- Intuition: Uses the dot product to compute the cosine of the angle between the two directions. Penalizes deviations from alignment.
- Scaling: Always applied, but its weight can be reduced if containment is satisfied (see Containment Penalty).
2. Half-FOV Loss
Measures the angular difference between the predicted half-fov and target half-fov.
- Formula:
[
L_{\text{fov}} = 2 \cdot \left(1 - \mathbf{fov}{\text{pred}} \cdot \mathbf{fov}{\text{target}} \right)
]
- Intuition: Ensures the predicted half-fov angle closely matches the target. Penalizes overestimation/underestimation.
- Scaling: Always applied with full weight.
3. Containment Penalty
Penalizes if the predicted FoV does not contain the target direction.
- Formula:
[
L_{\text{contain}} = \max\left(0, \left(\mathbf{fov}{\text{pred}} \cdot \mathbf{\hat{x}} \right) - \left(\mathbf{dir}{\text{pred}} \cdot \mathbf{dir}{\text{target}} \right) \right) \cdot \lambda{\text{contain}}
]
- Components:
- (\mathbf{\hat{x}} = (1, 0)): Basis vector to extract the cosine of the predicted half-fov angle ((\cos \phi_{\text{pred}} = \mathbf{fov}_{\text{pred}} \cdot \mathbf{\hat{x}})).
- (\mathbf{dir}{\text{pred}} \cdot \mathbf{dir}{\text{target}} = \cos \theta): Cosine of the angle between predicted and target directions.
- Intuition:
- The predicted FoV contains the target if (\cos \theta \geq \cos \phi_{\text{pred}}).
- The penalty triggers if (\cos \theta < \cos \phi_{\text{pred}}), meaning the angle between directions exceeds the half-fov.
- Scaling: The penalty is scaled by (\lambda_{\text{contain}}) (e.g., (\lambda_{\text{contain}} = 100)) to emphasize this constraint.
Total Loss
[
\mathcal{L} = L_{\text{dir}} + L_{\text{fov}} + L_{\text{contain}}
]
Penalty Rules
-
Direction Mismatch:
- Always penalized by (L_{\text{dir}}).
- Reduced penalty if containment is satisfied:
[
L_{\text{dir}} \text{ scaled by } 0.1 \quad \text{if} \quad \mathbf{dir}{\text{pred}} \cdot \mathbf{dir}{\text{target}} \geq \mathbf{fov}_{\text{pred}} \cdot \mathbf{\hat{x}}.
]
-
Half-FOV Mismatch:
- Penalized by (L_{\text{fov}}) for deviations from the target half-fov.
-
Containment Failure:
- Penalized by (L_{\text{contain}}) if the target direction lies outside the predicted FoV.
Intuition
- The loss prioritizes containment (via (L_{\text{contain}})), ensuring the target direction is within the predicted FoV.
- Slight direction errors are tolerated if containment is satisfied (reduced (L_{\text{dir}}) penalty).
- Overly large/small half-fovs are penalized via (L_{\text{fov}}), while containment failures incur a harsh penalty.
Example Code Snippet (Pseudocode)
def compute_loss(dir_pred, dir_target, fov_pred, fov_target, lambda_contain=100):
# Compute direction loss
dir_loss = 2 * (1 - torch.dot(dir_pred, dir_target))
# Compute half-fov loss
fov_loss = 2 * (1 - torch.dot(fov_pred, fov_target))
# Compute containment penalty
cos_theta = torch.dot(dir_pred, dir_target)
cos_phi_pred = torch.dot(fov_pred, torch.tensor([1.0, 0.0]))
containment_violation = torch.max(torch.tensor(0.0), cos_phi_pred - cos_theta)
contain_penalty = containment_violation * lambda_contain
# Apply reduced direction penalty if containment is satisfied
if cos_theta >= cos_phi_pred:
dir_loss *= 0.1
total_loss = dir_loss + fov_loss + contain_penalty
return total_loss
This formulation balances accuracy in direction/fov prediction while enforcing strict constraints on containment. Adjust hyperparameters (e.g., (\lambda_{\text{contain}})) to prioritize specific aspects.