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

Bump torchmetrics from 0.11.4 to 1.0.0 in /requirements #1465

Merged
merged 8 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dependencies = [
# torch 1.12+ required by torchvision
"torch>=1.12,<3",
# torchmetrics 0.10+ required for binary/multiclass/multilabel classification metrics
"torchmetrics>=0.10,<0.12",
"torchmetrics>=0.10,<2",
# torchvision 0.13+ required for torchvision.models._api.WeightsEnum
"torchvision>=0.13,<0.16",
]
Expand Down
2 changes: 1 addition & 1 deletion requirements/required.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ segmentation-models-pytorch==0.3.3
shapely==2.0.1
timm==0.9.2
torch==2.0.1
torchmetrics==0.11.4
torchmetrics==1.0.0
torchvision==0.15.2
20 changes: 14 additions & 6 deletions torchgeo/trainers/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from lightning.pytorch import LightningModule
from torch import Tensor
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torchmetrics import MetricCollection
from torchmetrics.detection.mean_ap import MeanAveragePrecision
from torchvision.models import resnet as R
from torchvision.models.detection.backbone_utils import resnet_fpn_backbone
Expand Down Expand Up @@ -187,8 +188,9 @@ def __init__(self, **kwargs: Any) -> None:

self.config_task()

self.val_metrics = MeanAveragePrecision()
self.test_metrics = MeanAveragePrecision()
metrics = MetricCollection([MeanAveragePrecision()])
self.val_metrics = metrics.clone(prefix="val_")
self.test_metrics = metrics.clone(prefix="test_")

def forward(self, *args: Any, **kwargs: Any) -> Any:
"""Forward pass of the model.
Expand Down Expand Up @@ -273,8 +275,11 @@ def validation_step(self, *args: Any, **kwargs: Any) -> None:
def on_validation_epoch_end(self) -> None:
"""Logs epoch level validation metrics."""
metrics = self.val_metrics.compute()
renamed_metrics = {f"val_{i}": metrics[i] for i in metrics.keys()}
self.log_dict(renamed_metrics)

# https://github.com/Lightning-AI/torchmetrics/pull/1832#issuecomment-1623890714
metrics.pop("val_classes", None)

self.log_dict(metrics)
self.val_metrics.reset()

def test_step(self, *args: Any, **kwargs: Any) -> None:
Expand All @@ -297,8 +302,11 @@ def test_step(self, *args: Any, **kwargs: Any) -> None:
def on_test_epoch_end(self) -> None:
"""Logs epoch level test metrics."""
metrics = self.test_metrics.compute()
renamed_metrics = {f"test_{i}": metrics[i] for i in metrics.keys()}
self.log_dict(renamed_metrics)

# https://github.com/Lightning-AI/torchmetrics/pull/1832#issuecomment-1623890714
metrics.pop("test_classes", None)

self.log_dict(metrics)
self.test_metrics.reset()

def predict_step(self, *args: Any, **kwargs: Any) -> list[dict[str, Tensor]]:
Expand Down
2 changes: 1 addition & 1 deletion torchgeo/trainers/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class and used with 'ce' loss
MulticlassAccuracy(
num_classes=self.hyperparams["num_classes"],
ignore_index=self.ignore_index,
mdmc_average="global",
multidim_average="global",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

average="micro",
),
MulticlassJaccardIndex(
Expand Down