Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassificationTask: add class_weights parameter #1592

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions torchgeo/trainers/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(
in_channels: int = 3,
num_classes: int = 1000,
loss: str = "ce",
class_weights: Optional[Tensor] = None,
lr: float = 1e-3,
patience: int = 10,
freeze_backbone: bool = False,
Expand All @@ -53,6 +54,8 @@ def __init__(
in_channels: Number of input channels to model.
num_classes: Number of prediction classes.
loss: One of 'ce', 'bce', 'jaccard', or 'focal'.
class_weights: Optional rescaling weight given to each
class and used with 'ce' loss.
lr: Learning rate for optimizer.
patience: Patience for learning rate scheduler.
freeze_backbone: Freeze the backbone network to linear probe
Expand All @@ -62,7 +65,7 @@ def __init__(
*classification_model* was renamed to *model*.

.. versionadded:: 0.5
The *freeze_backbone* parameter.
The *class_weights* and *freeze_backbone* parameters.

.. versionchanged:: 0.5
*learning_rate* and *learning_rate_schedule_patience* were renamed to
Expand All @@ -78,7 +81,9 @@ def configure_losses(self) -> None:
"""
loss: str = self.hparams["loss"]
if loss == "ce":
self.criterion: nn.Module = nn.CrossEntropyLoss()
self.criterion: nn.Module = nn.CrossEntropyLoss(
weight=self.hparams["class_weights"]
)
elif loss == "bce":
self.criterion = nn.BCEWithLogitsLoss()
elif loss == "jaccard":
Expand Down