You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.8 KiB
91 lines
2.8 KiB
from .base import *
|
|
from typing import Literal
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
|
|
class CrossEntropyLossParams(BaseModel):
|
|
"""
|
|
Class for handling parameters for `nn.CrossEntropyLoss`.
|
|
"""
|
|
model_config = ConfigDict(frozen=True, arbitrary_types_allowed=True)
|
|
|
|
weight: Optional[torch.Tensor] = None
|
|
ignore_index: int = -100
|
|
reduction: Literal["none", "mean", "sum"] = "mean"
|
|
label_smoothing: float = 0.0
|
|
|
|
def asdict(self):
|
|
"""
|
|
Returns a dictionary of valid parameters for `nn.CrossEntropyLoss`.
|
|
|
|
Returns:
|
|
Dict[str, Any]: Dictionary of parameters for nn.CrossEntropyLoss.
|
|
"""
|
|
loss_kwargs = self.model_dump()
|
|
return {k: v for k, v in loss_kwargs.items() if v is not None} # Remove None values
|
|
|
|
|
|
|
|
class CrossEntropyLoss(BaseLoss):
|
|
"""
|
|
Custom loss function wrapper for `nn.CrossEntropyLoss` with tracking of loss metrics.
|
|
"""
|
|
|
|
def __init__(self, ce_params: Optional[CrossEntropyLossParams] = None):
|
|
"""
|
|
Initializes the loss function with optional CrossEntropyLoss parameters.
|
|
|
|
Args:
|
|
ce_params (Optional[Dict[str, Any]]): Parameters for nn.CrossEntropyLoss (default: None).
|
|
"""
|
|
super().__init__()
|
|
_ce_params = ce_params.asdict() if ce_params is not None else {}
|
|
|
|
# Initialize loss functions with user-provided parameters or PyTorch defaults
|
|
self.ce_loss = nn.CrossEntropyLoss(**_ce_params)
|
|
|
|
# Using CumulativeAverage from MONAI to track loss metrics
|
|
self.loss_ce_metric = CumulativeAverage()
|
|
|
|
|
|
def forward(self, outputs: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
|
"""
|
|
Computes the loss between true labels and prediction outputs.
|
|
|
|
Args:
|
|
outputs (torch.Tensor): Model predictions of shape (batch_size, channels, H, W).
|
|
target (torch.Tensor): Ground truth labels of shape (batch_size, H, W).
|
|
|
|
Returns:
|
|
torch.Tensor: The total loss value.
|
|
"""
|
|
# Ensure target is on the same device as outputs
|
|
assert (
|
|
target.device == outputs.device
|
|
), (
|
|
"Target tensor must be moved to the same device as outputs "
|
|
"before calling forward()."
|
|
)
|
|
|
|
loss = self.ce_loss(outputs, target)
|
|
self.loss_ce_metric.append(loss.item())
|
|
|
|
return loss
|
|
|
|
|
|
def get_loss_metrics(self) -> Dict[str, float]:
|
|
"""
|
|
Retrieves the tracked loss metrics.
|
|
|
|
Returns:
|
|
Dict[str, float]: A dictionary containing the average CrossEntropy loss.
|
|
"""
|
|
return {
|
|
"loss": round(self.loss_ce_metric.aggregate().item(), 4),
|
|
}
|
|
|
|
|
|
def reset_metrics(self):
|
|
"""Resets the stored loss metrics."""
|
|
self.loss_ce_metric.reset()
|