torchgeo icon indicating copy to clipboard operation
torchgeo copied to clipboard

Adds support for ClasswiseWrapper

Open robmarkcole opened this issue 6 months ago • 4 comments

Replaces https://github.com/microsoft/torchgeo/pull/2130

Adds support for labels to be used in ClasswiseWrapper

robmarkcole avatar May 26 '25 16:05 robmarkcole

@adamjstewart not sure about this image

Also not sure why seeing this - have run ruff format image

robmarkcole avatar Nov 22 '25 14:11 robmarkcole

If you expand the RtD CI log, you'll see:

/home/docs/checkouts/readthedocs.org/user_builds/torchgeo/checkouts/2800/torchgeo/trainers/__init__.py:docstring of torchgeo.trainers:1: WARNING: py:class reference target not found: torchmetrics.MulticlassAccuracy
/home/docs/checkouts/readthedocs.org/user_builds/torchgeo/checkouts/2800/torchgeo/trainers/byol.py:docstring of torchgeo.trainers.byol.BYOLTask:1: WARNING: py:class reference target not found: torchmetrics.MulticlassJaccardIndex

These are documented at torchmetrics.classification, not torchmetrics.

For ruff, make sure you're using a new-ish version fo ruff and run both ruff format and ruff check.

adamjstewart avatar Nov 22 '25 16:11 adamjstewart

Updated ruff, no issues locally

Successfully installed ruff-0.14.6
⚡ Update-seg-and-class-metrics ~/torchgeo ruff format torchgeo/trainers/segmentation.py
1 file left unchanged
⚡ Update-seg-and-class-metrics ~/torchgeo ruff check torchgeo/trainers/segmentation.py
All checks passed!

confusing that TorchMetrics supports both MulticlassJaccardIndex and JaccardIndex for multi-class problems, but they behave differently

Note this was causing a sphinx warning: Definition list ends without a blank line; unexpected unindent.

BYOL:

robmarkcole avatar Nov 23 '25 08:11 robmarkcole

In use an error is raised since ClasswiseWrapper returns dicts:

ValueError: The `.compute()` return of the metric logged as 'val_Accuracy' must be a tensor. Found {'multiclassaccuracy_background': tensor(0.9996, device='cuda:0'), 'multiclassaccuracy_solar': tensor(3.0169e-05, device='cuda:0')}

Resolved - example output

[{'test_loss': 61.49171447753906,
  'test_multiclassaccuracy_background': 0.6125907301902771,
  'test_multiclassaccuracy_solar': 0.42507660388946533,
  'test_AverageAccuracy': 0.5188336372375488,
  'test_AverageF1Score': 0.4424017667770386,
  'test_AverageJaccardIndex': 0.33135226368904114,
  'test_AveragePrecision': 0.5060101747512817,
  'test_AverageRecall': 0.5188336372375488,
  'test_multiclassfbetascore_background': 0.7360653281211853,
  'test_multiclassfbetascore_solar': 0.14873822033405304,
  'test_multiclassjaccardindex_background': 0.5823602676391602,
  'test_multiclassjaccardindex_solar': 0.08034424483776093,
  'test_OverallAccuracy': 0.5970620512962341,
  'test_OverallF1Score': 0.5970620512962341,
  'test_OverallJaccardIndex': 0.4255797863006592,
  'test_OverallPrecision': 0.5970620512962341,
  'test_OverallRecall': 0.5970620512962341,
  'test_multiclassprecision_background': 0.9218809008598328,
  'test_multiclassprecision_solar': 0.09013941884040833,
  'test_multiclassrecall_background': 0.6125907301902771,
  'test_multiclassrecall_solar': 0.42507660388946533}]

robmarkcole avatar Nov 27 '25 10:11 robmarkcole