Skip to content

Commit

Permalink
5344 implement pq metric (Project-MONAI#5377)
Browse files Browse the repository at this point in the history
Fixes Project-MONAI#5344 .

### Description

This PR implements the metric of Panoptic Quality.


### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [x] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [x] In-line docstrings updated.
- [x] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: Yiheng Wang <vennw@nvidia.com>
Co-authored-by: Behrooz Hashemian <3968947+drbeh@users.noreply.github.com>
Co-authored-by: Dženan Zukić <dzenan.zukic@kitware.com>
Co-authored-by: Wenqi Li <831580+wyli@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 8, 2022
1 parent e7bfac6 commit 9f15b01
Show file tree
Hide file tree
Showing 11 changed files with 612 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/source/handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ Surface distance metrics handler
:members:


Panoptic Quality metrics handler
--------------------------------
.. autoclass:: PanopticQuality
:members:


Mean squared error metrics handler
----------------------------------
.. autoclass:: MeanSquaredError
Expand Down
7 changes: 7 additions & 0 deletions docs/source/metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ Metrics
.. autoclass:: SurfaceDiceMetric
:members:

`PanopticQualityMetric`
-----------------------
.. autofunction:: compute_panoptic_quality

.. autoclass:: PanopticQualityMetric
:members:

`Mean squared error`
--------------------
.. autoclass:: MSEMetric
Expand Down
1 change: 1 addition & 0 deletions monai/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .metrics_saver import MetricsSaver
from .mlflow_handler import MLFlowHandler
from .nvtx_handlers import MarkHandler, RangeHandler, RangePopHandler, RangePushHandler
from .panoptic_quality import PanopticQuality
from .parameter_scheduler import ParamSchedulerHandler
from .postprocessing import PostProcessing
from .probability_maps import ProbMapProducer
Expand Down
67 changes: 67 additions & 0 deletions monai/handlers/panoptic_quality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Callable, Union

from monai.handlers.ignite_metric import IgniteMetric
from monai.metrics import PanopticQualityMetric
from monai.utils import MetricReduction


class PanopticQuality(IgniteMetric):
"""
Computes Panoptic quality from full size Tensor and collects average over batch, class-channels, iterations.
"""

def __init__(
self,
num_classes: int,
metric_name: str = "pq",
reduction: Union[MetricReduction, str] = MetricReduction.MEAN_BATCH,
match_iou_threshold: float = 0.5,
smooth_numerator: float = 1e-6,
output_transform: Callable = lambda x: x,
save_details: bool = True,
) -> None:
"""
Args:
num_classes: number of classes. The number should not count the background.
metric_name: output metric. The value can be "pq", "sq" or "rq".
reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values,
available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``,
``"mean_channel"``, ``"sum_channel"``}, default to `self.reduction`. if "none", will not do reduction.
match_iou_threshold: IOU threshould to determine the pairing between `y_pred` and `y`. Usually,
it should >= 0.5, the pairing between instances of `y_pred` and `y` are identical.
If set `match_iou_threshold` < 0.5, this function uses Munkres assignment to find the
maximal amout of unique pairing.
smooth_numerator: a small constant added to the numerator to avoid zero.
output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then
construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or
lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`.
`engine.state` and `output_transform` inherit from the ignite concept:
https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial:
https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb.
save_details: whether to save metric computation details per image, for example: panoptic quality of
every image.
default to True, will save to `engine.state.metric_details` dict with the metric name as key.
See also:
:py:meth:`monai.metrics.panoptic_quality.compute_panoptic_quality`
"""
metric_fn = PanopticQualityMetric(
num_classes=num_classes,
metric_name=metric_name,
reduction=reduction,
match_iou_threshold=match_iou_threshold,
smooth_numerator=smooth_numerator,
)
super().__init__(metric_fn=metric_fn, output_transform=output_transform, save_details=save_details)
1 change: 1 addition & 0 deletions monai/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .meandice import DiceMetric, compute_dice, compute_meandice
from .meaniou import MeanIoU, compute_iou, compute_meaniou
from .metric import Cumulative, CumulativeIterationMetric, IterationMetric, Metric
from .panoptic_quality import PanopticQualityMetric, compute_panoptic_quality
from .regression import MAEMetric, MSEMetric, PSNRMetric, RMSEMetric, SSIMMetric
from .rocauc import ROCAUCMetric, compute_roc_auc
from .surface_dice import SurfaceDiceMetric, compute_surface_dice
Expand Down
Loading

0 comments on commit 9f15b01

Please sign in to comment.