monai-weekly 1.5.dev2507__py3-none-any.whl → 1.5.dev2509__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
monai/__init__.py CHANGED
@@ -136,4 +136,4 @@ except BaseException:
136
136
 
137
137
  if MONAIEnvVars.debug():
138
138
  raise
139
- __commit_id__ = "44add8d134f0cf0df6e4b0f5ffdd710474d211e5"
139
+ __commit_id__ = "a09c1f08461cec3d2131fde3939ef38c3c4ad5fc"
monai/_version.py CHANGED
@@ -8,11 +8,11 @@ import json
8
8
 
9
9
  version_json = '''
10
10
  {
11
- "date": "2025-02-16T02:27:59+0000",
11
+ "date": "2025-03-02T02:29:03+0000",
12
12
  "dirty": false,
13
13
  "error": null,
14
- "full-revisionid": "bbd080bb9d263c06a0884a871b645a434e3d4cdd",
15
- "version": "1.5.dev2507"
14
+ "full-revisionid": "5f85a7bfd54b91be03213999a7c177bfe2d583b2",
15
+ "version": "1.5.dev2509"
16
16
  }
17
17
  ''' # END VERSION_JSON
18
18
 
monai/bundle/scripts.py CHANGED
@@ -15,6 +15,7 @@ import ast
15
15
  import json
16
16
  import os
17
17
  import re
18
+ import urllib
18
19
  import warnings
19
20
  import zipfile
20
21
  from collections.abc import Mapping, Sequence
@@ -58,7 +59,7 @@ from monai.utils import (
58
59
  validate, _ = optional_import("jsonschema", name="validate")
59
60
  ValidationError, _ = optional_import("jsonschema.exceptions", name="ValidationError")
60
61
  Checkpoint, has_ignite = optional_import("ignite.handlers", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Checkpoint")
61
- requests_get, has_requests = optional_import("requests", name="get")
62
+ requests, has_requests = optional_import("requests")
62
63
  onnx, _ = optional_import("onnx")
63
64
  huggingface_hub, _ = optional_import("huggingface_hub")
64
65
 
@@ -206,6 +207,16 @@ def _download_from_monaihosting(download_path: Path, filename: str, version: str
206
207
  extractall(filepath=filepath, output_dir=download_path, has_base=True)
207
208
 
208
209
 
210
+ def _download_from_bundle_info(download_path: Path, filename: str, version: str, progress: bool) -> None:
211
+ bundle_info = get_bundle_info(bundle_name=filename, version=version)
212
+ if not bundle_info:
213
+ raise ValueError(f"Bundle info not found for {filename} v{version}.")
214
+ url = bundle_info["browser_download_url"]
215
+ filepath = download_path / f"{filename}_v{version}.zip"
216
+ download_url(url=url, filepath=filepath, hash_val=None, progress=progress)
217
+ extractall(filepath=filepath, output_dir=download_path, has_base=True)
218
+
219
+
209
220
  def _add_ngc_prefix(name: str, prefix: str = "monai_") -> str:
210
221
  if name.startswith(prefix):
211
222
  return name
@@ -222,7 +233,7 @@ def _get_all_download_files(request_url: str, headers: dict | None = None) -> li
222
233
  if not has_requests:
223
234
  raise ValueError("requests package is required, please install it.")
224
235
  headers = {} if headers is None else headers
225
- response = requests_get(request_url, headers=headers)
236
+ response = requests.get(request_url, headers=headers)
226
237
  response.raise_for_status()
227
238
  model_info = json.loads(response.text)
228
239
 
@@ -266,7 +277,7 @@ def _download_from_ngc_private(
266
277
  request_url = _get_ngc_private_bundle_url(model_name=filename, version=version, repo=repo)
267
278
  if has_requests:
268
279
  headers = {} if headers is None else headers
269
- response = requests_get(request_url, headers=headers)
280
+ response = requests.get(request_url, headers=headers)
270
281
  response.raise_for_status()
271
282
  else:
272
283
  raise ValueError("NGC API requires requests package. Please install it.")
@@ -289,7 +300,7 @@ def _get_ngc_token(api_key, retry=0):
289
300
  url = "https://authn.nvidia.com/token?service=ngc"
290
301
  headers = {"Accept": "application/json", "Authorization": "ApiKey " + api_key}
291
302
  if has_requests:
292
- response = requests_get(url, headers=headers)
303
+ response = requests.get(url, headers=headers)
293
304
  if not response.ok:
294
305
  # retry 3 times, if failed, raise an error.
295
306
  if retry < 3:
@@ -303,14 +314,17 @@ def _get_ngc_token(api_key, retry=0):
303
314
 
304
315
  def _get_latest_bundle_version_monaihosting(name):
305
316
  full_url = f"{MONAI_HOSTING_BASE_URL}/{name.lower()}"
306
- requests_get, has_requests = optional_import("requests", name="get")
307
317
  if has_requests:
308
- resp = requests_get(full_url)
309
- resp.raise_for_status()
310
- else:
311
- raise ValueError("NGC API requires requests package. Please install it.")
312
- model_info = json.loads(resp.text)
313
- return model_info["model"]["latestVersionIdStr"]
318
+ resp = requests.get(full_url)
319
+ try:
320
+ resp.raise_for_status()
321
+ model_info = json.loads(resp.text)
322
+ return model_info["model"]["latestVersionIdStr"]
323
+ except requests.exceptions.HTTPError:
324
+ # for monaihosting bundles, if cannot find the version, get from model zoo model_info.json
325
+ return get_bundle_versions(name)["latest_version"]
326
+
327
+ raise ValueError("NGC API requires requests package. Please install it.")
314
328
 
315
329
 
316
330
  def _examine_monai_version(monai_version: str) -> tuple[bool, str]:
@@ -388,14 +402,14 @@ def _get_latest_bundle_version_ngc(name: str, repo: str | None = None, headers:
388
402
  version_header = {"Accept-Encoding": "gzip, deflate"} # Excluding 'zstd' to fit NGC requirements
389
403
  if headers:
390
404
  version_header.update(headers)
391
- resp = requests_get(version_endpoint, headers=version_header)
405
+ resp = requests.get(version_endpoint, headers=version_header)
392
406
  resp.raise_for_status()
393
407
  model_info = json.loads(resp.text)
394
408
  latest_versions = _list_latest_versions(model_info)
395
409
 
396
410
  for version in latest_versions:
397
411
  file_endpoint = base_url + f"/{name.lower()}/versions/{version}/files/configs/metadata.json"
398
- resp = requests_get(file_endpoint, headers=headers)
412
+ resp = requests.get(file_endpoint, headers=headers)
399
413
  metadata = json.loads(resp.text)
400
414
  resp.raise_for_status()
401
415
  # if the package version is not available or the model is compatible with the package version
@@ -585,7 +599,16 @@ def download(
585
599
  name_ver = "_v".join([name_, version_]) if version_ is not None else name_
586
600
  _download_from_github(repo=repo_, download_path=bundle_dir_, filename=name_ver, progress=progress_)
587
601
  elif source_ == "monaihosting":
588
- _download_from_monaihosting(download_path=bundle_dir_, filename=name_, version=version_, progress=progress_)
602
+ try:
603
+ _download_from_monaihosting(
604
+ download_path=bundle_dir_, filename=name_, version=version_, progress=progress_
605
+ )
606
+ except urllib.error.HTTPError:
607
+ # for monaihosting bundles, if cannot download from default host, download according to bundle_info
608
+ _download_from_bundle_info(
609
+ download_path=bundle_dir_, filename=name_, version=version_, progress=progress_
610
+ )
611
+
589
612
  elif source_ == "ngc":
590
613
  _download_from_ngc(
591
614
  download_path=bundle_dir_,
@@ -792,9 +815,9 @@ def _get_all_bundles_info(
792
815
 
793
816
  if auth_token is not None:
794
817
  headers = {"Authorization": f"Bearer {auth_token}"}
795
- resp = requests_get(request_url, headers=headers)
818
+ resp = requests.get(request_url, headers=headers)
796
819
  else:
797
- resp = requests_get(request_url)
820
+ resp = requests.get(request_url)
798
821
  resp.raise_for_status()
799
822
  else:
800
823
  raise ValueError("requests package is required, please install it.")
@@ -11,6 +11,7 @@
11
11
 
12
12
  from __future__ import annotations
13
13
 
14
+ from .average_precision import AveragePrecision
14
15
  from .checkpoint_loader import CheckpointLoader
15
16
  from .checkpoint_saver import CheckpointSaver
16
17
  from .classification_saver import ClassificationSaver
@@ -0,0 +1,53 @@
1
+ # Copyright (c) MONAI Consortium
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software
7
+ # distributed under the License is distributed on an "AS IS" BASIS,
8
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ # See the License for the specific language governing permissions and
10
+ # limitations under the License.
11
+
12
+ from __future__ import annotations
13
+
14
+ from collections.abc import Callable
15
+
16
+ from monai.handlers.ignite_metric import IgniteMetricHandler
17
+ from monai.metrics import AveragePrecisionMetric
18
+ from monai.utils import Average
19
+
20
+
21
+ class AveragePrecision(IgniteMetricHandler):
22
+ """
23
+ Computes Average Precision (AP).
24
+ accumulating predictions and the ground-truth during an epoch and applying `compute_average_precision`.
25
+
26
+ Args:
27
+ average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``}
28
+ Type of averaging performed if not binary classification. Defaults to ``"macro"``.
29
+
30
+ - ``"macro"``: calculate metrics for each label, and find their unweighted mean.
31
+ This does not take label imbalance into account.
32
+ - ``"weighted"``: calculate metrics for each label, and find their average,
33
+ weighted by support (the number of true instances for each label).
34
+ - ``"micro"``: calculate metrics globally by considering each element of the label
35
+ indicator matrix as a label.
36
+ - ``"none"``: the scores for each class are returned.
37
+
38
+ output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then
39
+ construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or
40
+ lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`.
41
+ `engine.state` and `output_transform` inherit from the ignite concept:
42
+ https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial:
43
+ https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb.
44
+
45
+ Note:
46
+ Average Precision expects y to be comprised of 0's and 1's.
47
+ y_pred must either be probability estimates or confidence values.
48
+
49
+ """
50
+
51
+ def __init__(self, average: Average | str = Average.MACRO, output_transform: Callable = lambda x: x) -> None:
52
+ metric_fn = AveragePrecisionMetric(average=Average(average))
53
+ super().__init__(metric_fn=metric_fn, output_transform=output_transform, save_details=False)
monai/inferers/inferer.py CHANGED
@@ -1202,15 +1202,16 @@ class LatentDiffusionInferer(DiffusionInferer):
1202
1202
 
1203
1203
  if self.autoencoder_latent_shape is not None:
1204
1204
  latent = torch.stack([self.autoencoder_resizer(i) for i in decollate_batch(latent)], 0)
1205
- latent_intermediates = [
1206
- torch.stack([self.autoencoder_resizer(i) for i in decollate_batch(l)], 0) for l in latent_intermediates
1207
- ]
1205
+ if save_intermediates:
1206
+ latent_intermediates = [
1207
+ torch.stack([self.autoencoder_resizer(i) for i in decollate_batch(l)], 0)
1208
+ for l in latent_intermediates
1209
+ ]
1208
1210
 
1209
1211
  decode = autoencoder_model.decode_stage_2_outputs
1210
1212
  if isinstance(autoencoder_model, SPADEAutoencoderKL):
1211
1213
  decode = partial(autoencoder_model.decode_stage_2_outputs, seg=seg)
1212
1214
  image = decode(latent / self.scale_factor)
1213
-
1214
1215
  if save_intermediates:
1215
1216
  intermediates = []
1216
1217
  for latent_intermediate in latent_intermediates:
@@ -1333,13 +1334,15 @@ class ControlNetDiffusionInferer(DiffusionInferer):
1333
1334
  raise NotImplementedError(f"{mode} condition is not supported")
1334
1335
 
1335
1336
  noisy_image = self.scheduler.add_noise(original_samples=inputs, noise=noise, timesteps=timesteps)
1336
- down_block_res_samples, mid_block_res_sample = controlnet(
1337
- x=noisy_image, timesteps=timesteps, controlnet_cond=cn_cond
1338
- )
1337
+
1339
1338
  if mode == "concat" and condition is not None:
1340
1339
  noisy_image = torch.cat([noisy_image, condition], dim=1)
1341
1340
  condition = None
1342
1341
 
1342
+ down_block_res_samples, mid_block_res_sample = controlnet(
1343
+ x=noisy_image, timesteps=timesteps, controlnet_cond=cn_cond, context=condition
1344
+ )
1345
+
1343
1346
  diffuse = diffusion_model
1344
1347
  if isinstance(diffusion_model, SPADEDiffusionModelUNet):
1345
1348
  diffuse = partial(diffusion_model, seg=seg)
@@ -1395,17 +1398,21 @@ class ControlNetDiffusionInferer(DiffusionInferer):
1395
1398
  progress_bar = iter(scheduler.timesteps)
1396
1399
  intermediates = []
1397
1400
  for t in progress_bar:
1398
- # 1. ControlNet forward
1399
- down_block_res_samples, mid_block_res_sample = controlnet(
1400
- x=image, timesteps=torch.Tensor((t,)).to(input_noise.device), controlnet_cond=cn_cond
1401
- )
1402
- # 2. predict noise model_output
1403
1401
  diffuse = diffusion_model
1404
1402
  if isinstance(diffusion_model, SPADEDiffusionModelUNet):
1405
1403
  diffuse = partial(diffusion_model, seg=seg)
1406
1404
 
1407
1405
  if mode == "concat" and conditioning is not None:
1406
+ # 1. Conditioning
1408
1407
  model_input = torch.cat([image, conditioning], dim=1)
1408
+ # 2. ControlNet forward
1409
+ down_block_res_samples, mid_block_res_sample = controlnet(
1410
+ x=model_input,
1411
+ timesteps=torch.Tensor((t,)).to(input_noise.device),
1412
+ controlnet_cond=cn_cond,
1413
+ context=None,
1414
+ )
1415
+ # 3. predict noise model_output
1409
1416
  model_output = diffuse(
1410
1417
  model_input,
1411
1418
  timesteps=torch.Tensor((t,)).to(input_noise.device),
@@ -1414,6 +1421,12 @@ class ControlNetDiffusionInferer(DiffusionInferer):
1414
1421
  mid_block_additional_residual=mid_block_res_sample,
1415
1422
  )
1416
1423
  else:
1424
+ down_block_res_samples, mid_block_res_sample = controlnet(
1425
+ x=image,
1426
+ timesteps=torch.Tensor((t,)).to(input_noise.device),
1427
+ controlnet_cond=cn_cond,
1428
+ context=conditioning,
1429
+ )
1417
1430
  model_output = diffuse(
1418
1431
  image,
1419
1432
  timesteps=torch.Tensor((t,)).to(input_noise.device),
@@ -1484,9 +1497,6 @@ class ControlNetDiffusionInferer(DiffusionInferer):
1484
1497
  for t in progress_bar:
1485
1498
  timesteps = torch.full(inputs.shape[:1], t, device=inputs.device).long()
1486
1499
  noisy_image = self.scheduler.add_noise(original_samples=inputs, noise=noise, timesteps=timesteps)
1487
- down_block_res_samples, mid_block_res_sample = controlnet(
1488
- x=noisy_image, timesteps=torch.Tensor((t,)).to(inputs.device), controlnet_cond=cn_cond
1489
- )
1490
1500
 
1491
1501
  diffuse = diffusion_model
1492
1502
  if isinstance(diffusion_model, SPADEDiffusionModelUNet):
@@ -1494,6 +1504,9 @@ class ControlNetDiffusionInferer(DiffusionInferer):
1494
1504
 
1495
1505
  if mode == "concat" and conditioning is not None:
1496
1506
  noisy_image = torch.cat([noisy_image, conditioning], dim=1)
1507
+ down_block_res_samples, mid_block_res_sample = controlnet(
1508
+ x=noisy_image, timesteps=torch.Tensor((t,)).to(inputs.device), controlnet_cond=cn_cond, context=None
1509
+ )
1497
1510
  model_output = diffuse(
1498
1511
  noisy_image,
1499
1512
  timesteps=timesteps,
@@ -1502,6 +1515,12 @@ class ControlNetDiffusionInferer(DiffusionInferer):
1502
1515
  mid_block_additional_residual=mid_block_res_sample,
1503
1516
  )
1504
1517
  else:
1518
+ down_block_res_samples, mid_block_res_sample = controlnet(
1519
+ x=noisy_image,
1520
+ timesteps=torch.Tensor((t,)).to(inputs.device),
1521
+ controlnet_cond=cn_cond,
1522
+ context=conditioning,
1523
+ )
1505
1524
  model_output = diffuse(
1506
1525
  x=noisy_image,
1507
1526
  timesteps=timesteps,
@@ -1727,9 +1746,11 @@ class ControlNetLatentDiffusionInferer(ControlNetDiffusionInferer):
1727
1746
 
1728
1747
  if self.autoencoder_latent_shape is not None:
1729
1748
  latent = torch.stack([self.autoencoder_resizer(i) for i in decollate_batch(latent)], 0)
1730
- latent_intermediates = [
1731
- torch.stack([self.autoencoder_resizer(i) for i in decollate_batch(l)], 0) for l in latent_intermediates
1732
- ]
1749
+ if save_intermediates:
1750
+ latent_intermediates = [
1751
+ torch.stack([self.autoencoder_resizer(i) for i in decollate_batch(l)], 0)
1752
+ for l in latent_intermediates
1753
+ ]
1733
1754
 
1734
1755
  decode = autoencoder_model.decode_stage_2_outputs
1735
1756
  if isinstance(autoencoder_model, SPADEAutoencoderKL):
monai/metrics/__init__.py CHANGED
@@ -12,6 +12,7 @@
12
12
  from __future__ import annotations
13
13
 
14
14
  from .active_learning_metrics import LabelQualityScore, VarianceMetric, compute_variance, label_quality_score
15
+ from .average_precision import AveragePrecisionMetric, compute_average_precision
15
16
  from .confusion_matrix import ConfusionMatrixMetric, compute_confusion_matrix_metric, get_confusion_matrix
16
17
  from .cumulative_average import CumulativeAverage
17
18
  from .f_beta_score import FBetaScore
@@ -0,0 +1,187 @@
1
+ # Copyright (c) MONAI Consortium
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software
7
+ # distributed under the License is distributed on an "AS IS" BASIS,
8
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ # See the License for the specific language governing permissions and
10
+ # limitations under the License.
11
+
12
+ from __future__ import annotations
13
+
14
+ import warnings
15
+ from typing import TYPE_CHECKING, cast
16
+
17
+ import numpy as np
18
+
19
+ if TYPE_CHECKING:
20
+ import numpy.typing as npt
21
+
22
+ import torch
23
+
24
+ from monai.utils import Average, look_up_option
25
+
26
+ from .metric import CumulativeIterationMetric
27
+
28
+
29
+ class AveragePrecisionMetric(CumulativeIterationMetric):
30
+ """
31
+ Computes Average Precision (AP). AP is a useful metric to evaluate a classifier when the classes are
32
+ imbalanced. It can take values between 0.0 and 1.0, 1.0 being the best possible score.
33
+ It summarizes a Precision-Recall curve as the weighted mean of precisions achieved at each
34
+ threshold, with the increase in recall from the previous threshold used as the weight:
35
+
36
+ .. math::
37
+ \\text{AP} = \\sum_n (R_n - R_{n-1}) P_n
38
+ :label: ap
39
+
40
+ where :math:`P_n` and :math:`R_n` are the precision and recall at the :math:`n^{th}` threshold.
41
+
42
+ Referring to: `sklearn.metrics.average_precision_score
43
+ <https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score>`_.
44
+
45
+ The input `y_pred` and `y` can be a list of `channel-first` Tensor or a `batch-first` Tensor.
46
+
47
+ Example of the typical execution steps of this metric class follows :py:class:`monai.metrics.metric.Cumulative`.
48
+
49
+ Args:
50
+ average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``}
51
+ Type of averaging performed if not binary classification.
52
+ Defaults to ``"macro"``.
53
+
54
+ - ``"macro"``: calculate metrics for each label, and find their unweighted mean.
55
+ This does not take label imbalance into account.
56
+ - ``"weighted"``: calculate metrics for each label, and find their average,
57
+ weighted by support (the number of true instances for each label).
58
+ - ``"micro"``: calculate metrics globally by considering each element of the label
59
+ indicator matrix as a label.
60
+ - ``"none"``: the scores for each class are returned.
61
+
62
+ """
63
+
64
+ def __init__(self, average: Average | str = Average.MACRO) -> None:
65
+ super().__init__()
66
+ self.average = average
67
+
68
+ def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: # type: ignore[override]
69
+ return y_pred, y
70
+
71
+ def aggregate(self, average: Average | str | None = None) -> np.ndarray | float | npt.ArrayLike:
72
+ """
73
+ Typically `y_pred` and `y` are stored in the cumulative buffers at each iteration,
74
+ This function reads the buffers and computes the Average Precision.
75
+
76
+ Args:
77
+ average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``}
78
+ Type of averaging performed if not binary classification. Defaults to `self.average`.
79
+
80
+ """
81
+ y_pred, y = self.get_buffer()
82
+ # compute final value and do metric reduction
83
+ if not isinstance(y_pred, torch.Tensor) or not isinstance(y, torch.Tensor):
84
+ raise ValueError("y_pred and y must be PyTorch Tensor.")
85
+
86
+ return compute_average_precision(y_pred=y_pred, y=y, average=average or self.average)
87
+
88
+
89
+ def _calculate(y_pred: torch.Tensor, y: torch.Tensor) -> float:
90
+ if not (y.ndimension() == y_pred.ndimension() == 1 and len(y) == len(y_pred)):
91
+ raise AssertionError("y and y_pred must be 1 dimension data with same length.")
92
+ y_unique = y.unique()
93
+ if len(y_unique) == 1:
94
+ warnings.warn(f"y values can not be all {y_unique.item()}, skip AP computation and return `Nan`.")
95
+ return float("nan")
96
+ if not y_unique.equal(torch.tensor([0, 1], dtype=y.dtype, device=y.device)):
97
+ warnings.warn(f"y values must be 0 or 1, but in {y_unique.tolist()}, skip AP computation and return `Nan`.")
98
+ return float("nan")
99
+
100
+ n = len(y)
101
+ indices = y_pred.argsort(descending=True)
102
+ y = y[indices].cpu().numpy() # type: ignore[assignment]
103
+ y_pred = y_pred[indices].cpu().numpy() # type: ignore[assignment]
104
+ npos = ap = tmp_pos = 0.0
105
+
106
+ for i in range(n):
107
+ y_i = cast(float, y[i])
108
+ if i + 1 < n and y_pred[i] == y_pred[i + 1]:
109
+ tmp_pos += y_i
110
+ else:
111
+ tmp_pos += y_i
112
+ npos += tmp_pos
113
+ ap += tmp_pos * npos / (i + 1)
114
+ tmp_pos = 0
115
+
116
+ return ap / npos
117
+
118
+
119
+ def compute_average_precision(
120
+ y_pred: torch.Tensor, y: torch.Tensor, average: Average | str = Average.MACRO
121
+ ) -> np.ndarray | float | npt.ArrayLike:
122
+ """Computes Average Precision (AP). AP is a useful metric to evaluate a classifier when the classes are
123
+ imbalanced. It summarizes a Precision-Recall according to equation :eq:`ap`.
124
+ Referring to: `sklearn.metrics.average_precision_score
125
+ <https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score>`_.
126
+
127
+ Args:
128
+ y_pred: input data to compute, typical classification model output.
129
+ the first dim must be batch, if multi-classes, it must be in One-Hot format.
130
+ for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data.
131
+ y: ground truth to compute AP metric, the first dim must be batch.
132
+ if multi-classes, it must be in One-Hot format.
133
+ for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data.
134
+ average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``}
135
+ Type of averaging performed if not binary classification.
136
+ Defaults to ``"macro"``.
137
+
138
+ - ``"macro"``: calculate metrics for each label, and find their unweighted mean.
139
+ This does not take label imbalance into account.
140
+ - ``"weighted"``: calculate metrics for each label, and find their average,
141
+ weighted by support (the number of true instances for each label).
142
+ - ``"micro"``: calculate metrics globally by considering each element of the label
143
+ indicator matrix as a label.
144
+ - ``"none"``: the scores for each class are returned.
145
+
146
+ Raises:
147
+ ValueError: When ``y_pred`` dimension is not one of [1, 2].
148
+ ValueError: When ``y`` dimension is not one of [1, 2].
149
+ ValueError: When ``average`` is not one of ["macro", "weighted", "micro", "none"].
150
+
151
+ Note:
152
+ Average Precision expects y to be comprised of 0's and 1's. `y_pred` must be either prob. estimates or confidence values.
153
+
154
+ """
155
+ y_pred_ndim = y_pred.ndimension()
156
+ y_ndim = y.ndimension()
157
+ if y_pred_ndim not in (1, 2):
158
+ raise ValueError(
159
+ f"Predictions should be of shape (batch_size, num_classes) or (batch_size, ), got {y_pred.shape}."
160
+ )
161
+ if y_ndim not in (1, 2):
162
+ raise ValueError(f"Targets should be of shape (batch_size, num_classes) or (batch_size, ), got {y.shape}.")
163
+ if y_pred_ndim == 2 and y_pred.shape[1] == 1:
164
+ y_pred = y_pred.squeeze(dim=-1)
165
+ y_pred_ndim = 1
166
+ if y_ndim == 2 and y.shape[1] == 1:
167
+ y = y.squeeze(dim=-1)
168
+
169
+ if y_pred_ndim == 1:
170
+ return _calculate(y_pred, y)
171
+
172
+ if y.shape != y_pred.shape:
173
+ raise ValueError(f"data shapes of y_pred and y do not match, got {y_pred.shape} and {y.shape}.")
174
+
175
+ average = look_up_option(average, Average)
176
+ if average == Average.MICRO:
177
+ return _calculate(y_pred.flatten(), y.flatten())
178
+ y, y_pred = y.transpose(0, 1), y_pred.transpose(0, 1)
179
+ ap_values = [_calculate(y_pred_, y_) for y_pred_, y_ in zip(y_pred, y)]
180
+ if average == Average.NONE:
181
+ return ap_values
182
+ if average == Average.MACRO:
183
+ return np.mean(ap_values)
184
+ if average == Average.WEIGHTED:
185
+ weights = [sum(y_) for y_ in y]
186
+ return np.average(ap_values, weights=weights) # type: ignore[no-any-return]
187
+ raise ValueError(f'Unsupported average: {average}, available options are ["macro", "weighted", "micro", "none"].')
@@ -66,7 +66,6 @@ from monai.utils import (
66
66
  optional_import,
67
67
  )
68
68
  from monai.utils.enums import TransformBackends
69
- from monai.utils.misc import is_module_ver_at_least
70
69
  from monai.utils.type_conversion import convert_to_dst_type, get_dtype_string, get_equivalent_dtype
71
70
 
72
71
  PILImageImage, has_pil = optional_import("PIL.Image", name="Image")
@@ -939,19 +938,10 @@ class LabelToMask(Transform):
939
938
  data = img[[*select_labels]]
940
939
  else:
941
940
  where: Callable = np.where if isinstance(img, np.ndarray) else torch.where # type: ignore
942
- if isinstance(img, np.ndarray) or is_module_ver_at_least(torch, (1, 8, 0)):
943
- data = where(in1d(img, select_labels), True, False).reshape(img.shape)
944
- # pre pytorch 1.8.0, need to use 1/0 instead of True/False
945
- else:
946
- data = where(
947
- in1d(img, select_labels), torch.tensor(1, device=img.device), torch.tensor(0, device=img.device)
948
- ).reshape(img.shape)
941
+ data = where(in1d(img, select_labels), True, False).reshape(img.shape)
949
942
 
950
943
  if merge_channels or self.merge_channels:
951
- if isinstance(img, np.ndarray) or is_module_ver_at_least(torch, (1, 8, 0)):
952
- return data.any(0)[None]
953
- # pre pytorch 1.8.0 compatibility
954
- return data.to(torch.uint8).any(0)[None].to(bool) # type: ignore
944
+ return data.any(0)[None]
955
945
 
956
946
  return data
957
947
 
@@ -18,7 +18,6 @@ import numpy as np
18
18
  import torch
19
19
 
20
20
  from monai.config.type_definitions import NdarrayOrTensor, NdarrayTensor
21
- from monai.utils.misc import is_module_ver_at_least
22
21
  from monai.utils.type_conversion import convert_data_type, convert_to_dst_type
23
22
 
24
23
  __all__ = [
@@ -215,10 +214,9 @@ def floor_divide(a: NdarrayOrTensor, b) -> NdarrayOrTensor:
215
214
  Element-wise floor division between two arrays/tensors.
216
215
  """
217
216
  if isinstance(a, torch.Tensor):
218
- if is_module_ver_at_least(torch, (1, 8, 0)):
219
- return torch.div(a, b, rounding_mode="floor")
220
217
  return torch.floor_divide(a, b)
221
- return np.floor_divide(a, b)
218
+ else:
219
+ return np.floor_divide(a, b)
222
220
 
223
221
 
224
222
  def unravel_index(idx, shape) -> NdarrayOrTensor:
monai/utils/enums.py CHANGED
@@ -213,7 +213,8 @@ class GridSamplePadMode(StrEnum):
213
213
 
214
214
  class Average(StrEnum):
215
215
  """
216
- See also: :py:class:`monai.metrics.rocauc.compute_roc_auc`
216
+ See also: :py:class:`monai.metrics.rocauc.compute_roc_auc` or
217
+ :py:class:`monai.metrics.average_precision.compute_average_precision`
217
218
  """
218
219
 
219
220
  MACRO = "macro"
@@ -335,7 +336,7 @@ class CommonKeys(StrEnum):
335
336
  `LABEL` is the training or evaluation label of segmentation or classification task.
336
337
  `PRED` is the prediction data of model output.
337
338
  `LOSS` is the loss value of current iteration.
338
- `INFO` is some useful information during training or evaluation, like loss value, etc.
339
+ `METADATA` is some useful information during training or evaluation, like loss value, etc.
339
340
 
340
341
  """
341
342
 
monai/utils/module.py CHANGED
@@ -540,11 +540,11 @@ def version_leq(lhs: str, rhs: str) -> bool:
540
540
  """
541
541
 
542
542
  lhs, rhs = str(lhs), str(rhs)
543
- pkging, has_ver = optional_import("packaging.Version")
543
+ pkging, has_ver = optional_import("packaging.version")
544
544
  if has_ver:
545
545
  try:
546
- return cast(bool, pkging.version.Version(lhs) <= pkging.version.Version(rhs))
547
- except pkging.version.InvalidVersion:
546
+ return cast(bool, pkging.Version(lhs) <= pkging.Version(rhs))
547
+ except pkging.InvalidVersion:
548
548
  return True
549
549
 
550
550
  lhs_, rhs_ = parse_version_strs(lhs, rhs)
@@ -567,12 +567,12 @@ def version_geq(lhs: str, rhs: str) -> bool:
567
567
 
568
568
  """
569
569
  lhs, rhs = str(lhs), str(rhs)
570
- pkging, has_ver = optional_import("packaging.Version")
570
+ pkging, has_ver = optional_import("packaging.version")
571
571
 
572
572
  if has_ver:
573
573
  try:
574
- return cast(bool, pkging.version.Version(lhs) >= pkging.version.Version(rhs))
575
- except pkging.version.InvalidVersion:
574
+ return cast(bool, pkging.Version(lhs) >= pkging.Version(rhs))
575
+ except pkging.InvalidVersion:
576
576
  return True
577
577
 
578
578
  lhs_, rhs_ = parse_version_strs(lhs, rhs)