-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add crossentropy and dicece loss wrappers with support for class weights
- Loading branch information
Showing
8 changed files
with
117 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Loss functions API.""" | ||
|
||
from eva.core.losses.cross_entropy import CrossEntropyLoss | ||
|
||
__all__ = ["CrossEntropyLoss"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Cross-entropy based loss function.""" | ||
|
||
from typing import Sequence | ||
|
||
import torch | ||
from torch import nn | ||
|
||
|
||
class CrossEntropyLoss(nn.CrossEntropyLoss): | ||
"""A wrapper around torch.nn.CrossEntropyLoss that accepts weights in list format. | ||
Needed for .yaml file loading & class instantiation with jsonarparse. | ||
""" | ||
|
||
def __init__( | ||
self, *args, weight: Sequence[float] | torch.Tensor | None = None, **kwargs | ||
) -> None: | ||
"""Initialize the loss function. | ||
Args: | ||
args: Positional arguments from the base class. | ||
weight: A list of weights to assign to each class. | ||
kwargs: Key-word arguments from the base class. | ||
""" | ||
if weight is not None and not isinstance(weight, torch.Tensor): | ||
weight = torch.tensor(weight) | ||
super().__init__(*args, **kwargs, weight=weight) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
"""Loss functions API.""" | ||
|
||
from eva.vision.losses.dice import DiceLoss | ||
from eva.vision.losses.dice import DiceCELoss, DiceLoss | ||
|
||
__all__ = ["DiceLoss"] | ||
__all__ = ["DiceLoss", "DiceCELoss"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters