Losses API Reference¶
Factory Function¶
create_loss¶
Create a loss function by name.
Available Losses:
| Name | Description |
|---|---|
combined |
L1 + MSE + Gradient (default) |
advanced |
+ SSIM + SAM |
spectral_focus |
Higher SAM weight |
spatial_focus |
Higher Gradient/SSIM weight |
Example:
from models import create_loss
criterion = create_loss('spectral_focus')
loss, loss_dict = criterion(pred, target)
Individual Loss Functions¶
GradientLoss¶
Computes L1 loss on image gradients for edge preservation.
SpectralAngleLoss¶
Spectral Angle Mapper (SAM) loss for spectral fidelity.
SSIMLoss¶
Structural Similarity Index loss.
PerceptualLoss¶
VGG-based perceptual loss using feature maps.
Combined Loss Functions¶
CombinedLoss¶
class CombinedLoss(nn.Module):
def __init__(
self,
l1_weight: float = 1.0,
mse_weight: float = 1.0,
gradient_weight: float = 0.1
)
Basic combined loss with L1, MSE, and gradient terms.
Returns: (total_loss, loss_dict)
loss_fn = CombinedLoss()
total_loss, loss_dict = loss_fn(pred, target)
# loss_dict = {'l1': ..., 'mse': ..., 'gradient': ..., 'total': ...}
AdvancedCombinedLoss¶
class AdvancedCombinedLoss(nn.Module):
def __init__(
self,
l1_weight: float = 1.0,
mse_weight: float = 1.0,
gradient_weight: float = 0.1,
ssim_weight: float = 0.1,
sam_weight: float = 0.1,
perceptual_weight: float = 0.0
)
Advanced loss with all components configurable.