deepvisiontools 0.0.4__tar.gz → 0.1.0__tar.gz

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.
Files changed (64) hide show
  1. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/PKG-INFO +17 -13
  2. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/README.md +1 -0
  3. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/pyproject.toml +13 -10
  4. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/__init__.py +3 -1
  5. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/data/dataset.py +5 -3
  6. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/formats/base_data.py +7 -1
  7. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/formats/base_formats.py +7 -2
  8. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/inference/__init__.py +2 -1
  9. deepvisiontools-0.1.0/src/deepvisiontools/inference/evaluator.py +256 -0
  10. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/inference/predictor.py +39 -8
  11. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/metrics/available_metrics.py +1 -1
  12. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/metrics/base_metric.py +229 -17
  13. deepvisiontools-0.1.0/src/deepvisiontools/metrics/func.py +36 -0
  14. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools.egg-info/PKG-INFO +17 -13
  15. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools.egg-info/SOURCES.txt +1 -0
  16. deepvisiontools-0.1.0/src/deepvisiontools.egg-info/requires.txt +10 -0
  17. deepvisiontools-0.0.4/src/deepvisiontools/metrics/func.py +0 -34
  18. deepvisiontools-0.0.4/src/deepvisiontools.egg-info/requires.txt +0 -9
  19. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/LICENSE.txt +0 -0
  20. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/setup.cfg +0 -0
  21. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/setup.py +0 -0
  22. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/config/__init__.py +0 -0
  23. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/config/config.py +0 -0
  24. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/config/errors.py +0 -0
  25. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/data/__init__.py +0 -0
  26. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/data/additional_augmentations.py +0 -0
  27. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/data/augmentation_class.py +0 -0
  28. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/data/batch_augmentations.py +0 -0
  29. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/data/data_reader/__init__.py +0 -0
  30. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/data/data_reader/readers.py +0 -0
  31. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/data/errors.py +0 -0
  32. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/formats/__init__.py +0 -0
  33. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/formats/errors.py +0 -0
  34. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/formats/formats.py +0 -0
  35. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/formats/utils.py +0 -0
  36. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/inference/patchifier.py +0 -0
  37. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/metrics/__init__.py +0 -0
  38. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/metrics/matcher.py +0 -0
  39. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/metrics/utils.py +0 -0
  40. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/__init__.py +0 -0
  41. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/basemodel.py +0 -0
  42. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/mask2former/__init__.py +0 -0
  43. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/mask2former/mask2former.py +0 -0
  44. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/smp/__init__.py +0 -0
  45. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/smp/errors.py +0 -0
  46. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/smp/smp.py +0 -0
  47. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/yolov8/__init__.py +0 -0
  48. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/yolov8/errors.py +0 -0
  49. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/yolov8/utils.py +0 -0
  50. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/yolov8/yolov8.py +0 -0
  51. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/yolov8seg/__init__.py +0 -0
  52. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/yolov8seg/errors.py +0 -0
  53. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/yolov8seg/utils.py +0 -0
  54. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/models/yolov8seg/yolov8seg.py +0 -0
  55. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/preprocessing/__init__.py +0 -0
  56. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/preprocessing/image.py +0 -0
  57. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/preprocessing/preprocessing.py +0 -0
  58. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/train/__init__.py +0 -0
  59. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/train/trainer.py +0 -0
  60. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/utils/__init__.py +0 -0
  61. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/utils/errors.py +0 -0
  62. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools/utils/visualization.py +0 -0
  63. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools.egg-info/dependency_links.txt +0 -0
  64. {deepvisiontools-0.0.4 → deepvisiontools-0.1.0}/src/deepvisiontools.egg-info/top_level.txt +0 -0
@@ -1,8 +1,8 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: deepvisiontools
3
- Version: 0.0.4
4
- Summary: Overlay of PyTorch to generalize trainning and inference processes for detection & instance segmentation tasks.
5
- Author-email: Lucas Bernigaud Samatan <lucas.bernigaud-samatan@inrae.fr>, Jordan Bernigaud Samatan <jordan.bernigaud-samatan@inrae.fr>
3
+ Version: 0.1.0
4
+ Summary: Overlay of PyTorch to generalize trainning and inference processes for detection, instance segmentation and semantic segmentation tasks.
5
+ Author-email: Lucas Bernigaud Samatan <lucas.bernigaud-samatan@inrae.fr>, Jordan Bernigaud Samatan <jordan.bernigaud-samatan@inrae.fr>, Paul Faucher <paul.faucher@inrae.fr>
6
6
  Maintainer-email: Jordan Bernigaud Samatan <jordan.bernigaud-samatan@inrae.fr>
7
7
  License: GNU AFFERO GENERAL PUBLIC LICENSE
8
8
  Version 3, 19 November 2007
@@ -670,20 +670,23 @@ Project-URL: Homepage, https://forgemia.inra.fr/ue-apc/librairies/python/deepvis
670
670
  Project-URL: Issues, https://forgemia.inra.fr/ue-apc/librairies/python/deepvisiontools_v1/-/issues
671
671
  Project-URL: Documentation, https://deepvisiontools.readthedocs.io/en/latest/
672
672
  Project-URL: Repository, https://forgemia.inra.fr/ue-apc/librairies/python/deepvisiontools_v1
673
- Classifier: Development Status :: 3 - Alpha
673
+ Classifier: Development Status :: 4 - Beta
674
674
  Classifier: Programming Language :: Python :: 3.11
675
- Requires-Python: >=3.11.9
675
+ Classifier: Programming Language :: Python :: 3.12
676
+ Requires-Python: <=3.12.9,>=3.11.9
676
677
  Description-Content-Type: text/markdown
677
678
  License-File: LICENSE.txt
678
- Requires-Dist: torch==2.4.0
679
- Requires-Dist: torchvision==0.19.0
679
+ Requires-Dist: torch<=2.6.0,>=2.4.0
680
+ Requires-Dist: torchvision<=0.21.0,>=0.19.0
680
681
  Requires-Dist: pycocotools==2.0.8
681
- Requires-Dist: torchmetrics==1.4.0
682
- Requires-Dist: tensorboard==2.18.0
683
- Requires-Dist: ultralytics==8.3.28
684
- Requires-Dist: transformers==4.44.2
682
+ Requires-Dist: torchmetrics<=1.7.0,>=1.4.0
683
+ Requires-Dist: tensorboard<=2.19.0,>=2.18.0
684
+ Requires-Dist: ultralytics<=8.3.95,>=8.3.28
685
+ Requires-Dist: transformers<=4.50.0,>=4.44.2
685
686
  Requires-Dist: colorcet==3.1.0
686
- Requires-Dist: segmentation-models-pytorch==0.3.4
687
+ Requires-Dist: segmentation-models-pytorch<=0.4.0,>=0.3.4
688
+ Requires-Dist: openpyxl==3.1.5
689
+ Dynamic: license-file
687
690
 
688
691
  <!--
689
692
  <p align="center">
@@ -713,6 +716,7 @@ Similar stands for other models.
713
716
  All documentation about deepvisiontools, including tutorials, can be found at : https://deepvisiontools.readthedocs.io/en/latest/
714
717
 
715
718
  ## Installation
719
+ We have tested deepvisiontools for python versions between 3.11.9 and 3.12.9
716
720
 
717
721
  ```shell
718
722
  pip install deepvisiontools
@@ -26,6 +26,7 @@ Similar stands for other models.
26
26
  All documentation about deepvisiontools, including tutorials, can be found at : https://deepvisiontools.readthedocs.io/en/latest/
27
27
 
28
28
  ## Installation
29
+ We have tested deepvisiontools for python versions between 3.11.9 and 3.12.9
29
30
 
30
31
  ```shell
31
32
  pip install deepvisiontools
@@ -7,28 +7,31 @@ name = "deepvisiontools"
7
7
  authors = [
8
8
  { name = "Lucas Bernigaud Samatan", email = "lucas.bernigaud-samatan@inrae.fr" },
9
9
  { name = "Jordan Bernigaud Samatan", email = "jordan.bernigaud-samatan@inrae.fr" },
10
+ { name = "Paul Faucher", email = "paul.faucher@inrae.fr" },
10
11
  ]
11
12
  maintainers = [
12
13
  { name = "Jordan Bernigaud Samatan", email = "jordan.bernigaud-samatan@inrae.fr" },
13
14
  ]
14
- description = "Overlay of PyTorch to generalize trainning and inference processes for detection & instance segmentation tasks."
15
+ description = "Overlay of PyTorch to generalize trainning and inference processes for detection, instance segmentation and semantic segmentation tasks."
15
16
  readme = "README.md"
16
17
  classifiers = [
17
- "Development Status :: 3 - Alpha",
18
+ "Development Status :: 4 - Beta",
18
19
  "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12"
19
21
  ]
20
22
  dynamic = ["version"]
21
- requires-python = ">=3.11.9"
23
+ requires-python = ">=3.11.9,<=3.12.9"
22
24
  dependencies = [
23
- "torch==2.4.0",
24
- "torchvision==0.19.0",
25
+ "torch>=2.4.0,<=2.6.0",
26
+ "torchvision>=0.19.0,<=0.21.0",
25
27
  "pycocotools==2.0.8",
26
- "torchmetrics==1.4.0",
27
- "tensorboard==2.18.0",
28
- "ultralytics==8.3.28",
29
- "transformers==4.44.2",
28
+ "torchmetrics>=1.4.0,<=1.7.0",
29
+ "tensorboard>=2.18.0,<=2.19.0",
30
+ "ultralytics>=8.3.28,<=8.3.95",
31
+ "transformers>=4.44.2,<=4.50.0",
30
32
  "colorcet==3.1.0",
31
- "segmentation-models-pytorch==0.3.4"
33
+ "segmentation-models-pytorch>=0.3.4,<=0.4.0",
34
+ "openpyxl==3.1.5"
32
35
  ]
33
36
 
34
37
  license = { file = "LICENSE.txt" }
@@ -3,8 +3,9 @@ from deepvisiontools.train.trainer import Trainer
3
3
  from deepvisiontools.data.dataset import DeepVisionDataset, DeepVisionLoader
4
4
  from deepvisiontools.utils import visualization
5
5
  from deepvisiontools.inference.predictor import Predictor
6
+ from deepvisiontools.inference.evaluator import Evaluator
6
7
 
7
- __version__ = "0.0.4"
8
+ __version__ = "0.1.0"
8
9
 
9
10
  __all__ = (
10
11
  Configuration,
@@ -13,4 +14,5 @@ __all__ = (
13
14
  DeepVisionLoader,
14
15
  visualization,
15
16
  Predictor,
17
+ Evaluator,
16
18
  )
@@ -191,19 +191,21 @@ class DeepVisionDataset(Dataset):
191
191
  dataset3._indexes = idx[stop2:]
192
192
  return dataset1, dataset2, dataset3
193
193
 
194
- def keep_indexes(self, indexes: Union[list, slice, Tensor]):
194
+ def keep_indexes(self, indexes: Union[list, slice, Tensor]) -> DeepVisionDataset:
195
195
  """Filter dataset by keeping only indices given in arg.
196
196
 
197
197
  Args:
198
198
  indexes (``Union[list, slice, Tensor]``): can be slice, Tensor or list. To use slice please use : slice(i, j) with i, j desired slice indexes in arg.
199
199
  """
200
+ dataset = copy.deepcopy(self)
200
201
  if isinstance(indexes, Tensor):
201
202
  assert (
202
203
  indexes.dim() == 1
203
204
  ), f"Must use Tensor of dim 1 for indexes, got {indexes.shape}"
204
205
  indexes = indexes.tolist()
205
- indx = torch.tensor(self._indexes)[indexes].tolist()
206
- self._indexes = indx
206
+ indx = torch.tensor(dataset._indexes)[indexes].tolist()
207
+ dataset._indexes = indx
208
+ return dataset
207
209
 
208
210
  def export_dataset(
209
211
  self,
@@ -144,7 +144,13 @@ class SemanticMaskData(BaseData):
144
144
  mask = Mask(mask[0])
145
145
  self._value = copy.deepcopy(mask) # convert to long
146
146
  self._canvas_size = (mask.shape[-2], mask.shape[-1])
147
- self._nb_object = torch.unique(mask).shape[0] - 1
147
+ # compute nb of objects (unique cls ids) in mask that are not background (i.e 0)
148
+ nb_object = (
149
+ torch.unique(mask).shape[0] - 1
150
+ if 0 in torch.unique(mask).tolist()
151
+ else torch.unique(mask).shape[0]
152
+ )
153
+ self._nb_object = nb_object
148
154
  self._device = Configuration().device
149
155
  # we use here a trick : handling scores operation is complicated at format level.
150
156
  # We dynamically will modify it in the operationhandler class and implement the modification here
@@ -190,8 +190,13 @@ class BaseSemanticFormat(BaseFormat):
190
190
  scores.shape[-2:] == data.value.shape[-2:]
191
191
  ), f"scores do not have the same shape as data.value, got {scores.shape} and {data.value.shape}"
192
192
  self._data = data
193
- # labels are derived from semantic mask data value
194
- self._labels: Tensor = data.value.unique()[1:].long()
193
+ # labels are derived from semantic mask data value (careful : need to handle the case where 0 is not present in mask ...)
194
+ labs = (
195
+ data.value.unique()[1:].long()
196
+ if 0 in data.value.unique().tolist()
197
+ else data.value.unique().long()
198
+ )
199
+ self._labels: Tensor = labs
195
200
  # Check score
196
201
  if scores != None:
197
202
  scores = (
@@ -1,4 +1,5 @@
1
1
  from deepvisiontools.inference.patchifier import BasePatchifier, SemanticPatchifier, DetectPatchifier, PostProcesser
2
2
  from deepvisiontools.inference.predictor import Predictor, PredictorDataLoader
3
+ from deepvisiontools.inference.evaluator import Evaluator
3
4
 
4
- __all__ = ("Predictor", "BasePatchifier", "DetectPatchifier", "SemanticPatchifier", "PostProcesser", "PredictorDataLoader")
5
+ __all__ = ("Predictor", "BasePatchifier", "DetectPatchifier", "SemanticPatchifier", "PostProcesser", "PredictorDataLoader", "Evaluator")
@@ -0,0 +1,256 @@
1
+ from deepvisiontools import DeepVisionDataset, Configuration
2
+ from deepvisiontools.inference import Predictor
3
+ from deepvisiontools.utils import visualization
4
+ from tqdm import tqdm
5
+ from pathlib import Path
6
+ import pandas as pd
7
+ from typing import Literal, Union
8
+ from openpyxl import load_workbook
9
+ from openpyxl.styles import Font, PatternFill
10
+ import random
11
+
12
+
13
+ class Evaluator:
14
+ """Evaluator class : evaluate a given Predictor (model + patch_size + additional Configurations) on a dataset with given metrics.
15
+ The results are saved in a generated csv file (metrics at dataset level) and in a xlsx file (metrics at dataset and sample levels).
16
+ Highlighting samples that deviate from the mean or median (giving deviation_method) by nb_sigma sigma in the xlsx file.
17
+ Create visualizations of predictions giving number_visu.
18
+ Returns the dictionnary with metrics at dataset level and the dictionnary with metrics at sample level.
19
+ Prints a dataframe with metrics at dataset level (same content as in the generated csv file).
20
+
21
+ Args:
22
+ predictor (``Predictor``): Predictor class to evaluate
23
+ metrics (``list``): List of metrics to evaluate the Predictor on
24
+ deviation_method (``Literal[&quot;mean&quot;, &quot;median&quot;]``, **optional**): method to compute outlayers. Defaults to "mean".
25
+ nb_sigma (``Union[int, float]``, **optional**): number of standard deviations for outlayers. Defaults to 2.
26
+
27
+ Example:
28
+ ----------
29
+
30
+ .. highlight:: python
31
+ .. code-block:: python
32
+
33
+ >>> from deepvisiontools import Evaluator, Predictor
34
+ >>> from deepvisiontools.metrics import DetectF1Score
35
+ >>> predictor = Predictor(model=\path\to\model.pth)
36
+ >>> evaluator = Evaluator(predictor, metrics=[DetectF1Score()])
37
+ >>> evaluator.evaluate(mydataset, "results")
38
+
39
+ Attributes
40
+ ----------
41
+
42
+ Attributes:
43
+ predictor (``Predictor``)
44
+ metrics (``List[BaseMetric]``)
45
+ nb_sigma (``int``)
46
+ data_type (``Literal[&quot;instance_mask&quot;, &quot;bbox&quot;, &quot;keypoint&quot;, &quot;semantic_mask&quot;]``)
47
+ deviation_method (```Literal[&quot;mean&quot;, &quot;median&quot;]``)
48
+
49
+
50
+ **Methods**
51
+
52
+ """
53
+
54
+ def __init__(
55
+ self,
56
+ predictor: Predictor,
57
+ metrics: list,
58
+ deviation_method: Literal["mean", "median"] = "mean",
59
+ nb_sigma: Union[int, float] = 2,
60
+ ):
61
+
62
+ self.predictor = predictor
63
+ self.metrics = metrics
64
+ self.nb_sigma = nb_sigma
65
+ self.data_type = Configuration().data_type
66
+ self.deviation_method = deviation_method
67
+ assert deviation_method in [
68
+ "mean",
69
+ "median",
70
+ ], "deviation_method must be 'mean' or 'median'"
71
+
72
+ def evaluate(
73
+ self,
74
+ dataset: DeepVisionDataset,
75
+ result_folder: Union[str, Path],
76
+ number_visu: Union[Literal["all"], int] = "all",
77
+ ):
78
+ """Run evaluation on dataset. Compute metrics for dataset and for each sample of the dataset."""
79
+ result_folder = (
80
+ result_folder if isinstance(result_folder, Path) else Path(result_folder)
81
+ )
82
+ result_folder.mkdir(parents=True, exist_ok=True)
83
+ export_csv_path = result_folder / "results.csv"
84
+ export_xlsx_path = result_folder / "results.xlsx"
85
+ visu_folder_path = result_folder / "visu"
86
+
87
+ assert (
88
+ isinstance(number_visu, int) or number_visu == "all"
89
+ ), "number_visu must be integer or equal to 'all'"
90
+
91
+ dataset.preprocessing = None # because already preprocess in Predictor
92
+
93
+ # Creation and initialisation of the dictionary containing the metrics for each image (one dictionary/metric containing one dictionary/image)
94
+ metric_sample_dict = {}
95
+ for metric in self.metrics:
96
+ metric.to(Configuration().device)
97
+ metric_sample_dict.update({metric.name: {}})
98
+
99
+ if (
100
+ number_visu != 0 and number_visu != "all"
101
+ ): # randomly select index of the number_visu images to visualize
102
+ list_index_img = [i for i in range(len(dataset))]
103
+ random.shuffle(list_index_img)
104
+ list_index_img_visu = list_index_img[:number_visu]
105
+
106
+ # For each sample in the dataset : prediction then calculation and storage of tp, fp, tn, fn (for each metric)
107
+ iterator = tqdm(dataset, total=len(dataset), desc=f"Evaluation progress ")
108
+
109
+ for index_img, (image, target, img_name) in enumerate(iterator):
110
+ prediction = self.predictor.predict(image)
111
+
112
+ for metric in self.metrics:
113
+ metric.update(
114
+ prediction, target
115
+ ) # update 'stats' attribute for each metric
116
+
117
+ metric_sample_dict[metric.name].update(
118
+ {img_name: {"img_index_in_dataset": index_img}}
119
+ ) # creation of the image dictionary (for the given metric) with its index information
120
+ # compute metric for the image (all classes combined and for each class)
121
+ metric_sample = metric.compute_last_sample()
122
+ metric_sample_dict[metric.name][img_name].update(metric_sample)
123
+
124
+ # prediction visualization
125
+ if number_visu != 0 and number_visu != "all":
126
+ if index_img in list_index_img_visu:
127
+ visualization(
128
+ image,
129
+ prediction,
130
+ save_path=visu_folder_path
131
+ / "visu_img{0}_{1}".format(index_img, img_name),
132
+ )
133
+ elif number_visu == "all":
134
+ visualization(
135
+ image,
136
+ prediction,
137
+ save_path=visu_folder_path
138
+ / "visu_img{0}_{1}".format(index_img, img_name),
139
+ )
140
+
141
+ # after all updates recompute to get averaged values of metric (global, samplewise, macro, micro), save in metric_dict
142
+ metric_dict = {}
143
+ for metric in self.metrics:
144
+ results = metric.compute()
145
+ metric_dict.update({metric.name: results})
146
+
147
+ # Converts dictionary metric values from tensor to float :
148
+ for metric_name in metric_dict:
149
+ for sous_metric_name in metric_dict[metric_name]:
150
+ metric_dict[metric_name][sous_metric_name] = round(
151
+ metric_dict[metric_name][sous_metric_name].item(), 3
152
+ )
153
+
154
+ # Converts the dict into a dataframe, which is saved in a csv file:
155
+ df_metrics = pd.DataFrame(metric_dict)
156
+ df_metrics.to_csv(export_csv_path)
157
+
158
+ # Create the Excel file with metrics values for each image
159
+ sheet_names = self._create_xls(export_xlsx_path, df_metrics, metric_sample_dict)
160
+
161
+ # Modifies the Excel file with metrics : highlights extreme values and adds mean, std, median
162
+ self._formate_xls(export_xlsx_path, sheet_names, metric_sample_dict)
163
+
164
+ print(df_metrics)
165
+ return (metric_dict, metric_sample_dict)
166
+
167
+ def _create_xls(self, export_xlsx_path, df_metrics, metric_sample_dict):
168
+ """Create an Excel file with metrics values for each image"""
169
+ sheet_names = []
170
+ with pd.ExcelWriter(export_xlsx_path) as writer:
171
+ df_metrics.to_excel(
172
+ writer, sheet_name="Dataset_Metrics", index=True
173
+ ) # creates a first sheet with the 'sub-metrics' for each metric (equivalent to the contents of the csv generated earlier)
174
+ for metric in metric_sample_dict:
175
+ df_sample = pd.DataFrame(metric_sample_dict[metric]).T
176
+ df_sample["Outlier?"] = None
177
+ sheet_name = "{0}_by_samples".format(metric)
178
+ if len(sheet_name) > 31: # the length of excel sheets name is 31
179
+ sheet_name = sheet_name[: len(sheet_name) - (len(sheet_name) - 31)]
180
+ df_sample.to_excel(
181
+ writer, sheet_name=sheet_name, index=True
182
+ ) # creates a sheet by metric with details by image
183
+ sheet_names.append(sheet_name)
184
+ return sheet_names
185
+
186
+ def _highlight_cells(self, sheet, colonne_verif, min_val, max_val):
187
+ """Applies a red background and bold text if the value is outside the range"""
188
+ # Style for formatting (red fill + bold text)
189
+ red_fill = PatternFill(
190
+ start_color="FFC7CE", end_color="FFC7CE", fill_type="solid"
191
+ )
192
+ bold_font = Font(bold=True, color="9C0006")
193
+
194
+ for row in sheet.iter_rows(
195
+ min_row=2, min_col=colonne_verif, max_col=colonne_verif
196
+ ):
197
+ for cell in row:
198
+ if cell.value is not None and (
199
+ cell.value < min_val or cell.value > max_val
200
+ ):
201
+ cell.fill = red_fill
202
+ sheet.cell(row=cell.row, column=1).font = (
203
+ bold_font # Make the index bold and red
204
+ )
205
+ sheet.cell(cell.row, sheet.max_column).value = (
206
+ "True" # True in "Outlier?" column
207
+ )
208
+
209
+ def _formate_xls(self, export_xlsx_path, sheet_names, metric_sample_dict):
210
+ """Modifies the Excel file with metrics : highlights extreme values and adds mean, std, median"""
211
+
212
+ wb = load_workbook(export_xlsx_path)
213
+
214
+ for sh_name, metric in zip(sheet_names, metric_sample_dict):
215
+
216
+ df_sample = pd.DataFrame(metric_sample_dict[metric]).T
217
+ columns_list = df_sample.columns.tolist()
218
+ sheet_maw_row = wb[sh_name].max_row
219
+
220
+ # add mean, std, median labels in last rows
221
+ wb[sh_name].cell(sheet_maw_row + 2, 1).value = "Mean : "
222
+ wb[sh_name].cell(sheet_maw_row + 3, 1).value = "Std : "
223
+ wb[sh_name].cell(sheet_maw_row + 4, 1).value = "Median : "
224
+
225
+ # add mean, std and median values in last rows for each class and "all_class"
226
+ for num_col, col_name in enumerate(columns_list[1:]):
227
+ mean = df_sample.mean()[col_name]
228
+ std = df_sample.std()[col_name]
229
+ median = df_sample.median()[col_name]
230
+
231
+ num_col_xsl = num_col + 3
232
+ # highlights extreme values
233
+ if self.deviation_method == "median":
234
+ self._highlight_cells(
235
+ wb[sh_name],
236
+ colonne_verif=num_col_xsl,
237
+ min_val=median - (self.nb_sigma * std),
238
+ max_val=median + (self.nb_sigma * std),
239
+ )
240
+ else:
241
+ self._highlight_cells(
242
+ wb[sh_name],
243
+ colonne_verif=num_col_xsl,
244
+ min_val=mean - (self.nb_sigma * std),
245
+ max_val=mean + (self.nb_sigma * std),
246
+ )
247
+
248
+ # add mean, std and median in last rows
249
+ wb[sh_name].cell(sheet_maw_row + 2, num_col_xsl).value = round(mean, 3)
250
+ wb[sh_name].cell(sheet_maw_row + 3, num_col_xsl).value = round(std, 3)
251
+ wb[sh_name].cell(sheet_maw_row + 4, num_col_xsl).value = round(
252
+ median, 3
253
+ )
254
+
255
+ # Save file after modifications
256
+ wb.save(export_xlsx_path)
@@ -16,6 +16,7 @@ from deepvisiontools.inference.patchifier import (
16
16
  )
17
17
  from torchvision.transforms.v2 import Pad, CenterCrop
18
18
  from tqdm import tqdm
19
+ from deepvisiontools.formats import SemanticMaskData, SemanticMaskFormat
19
20
 
20
21
 
21
22
  def build_patchifier(*args, **kwargs):
@@ -39,7 +40,8 @@ class Predictor:
39
40
  nms_iou_threshold (``float``, **optional**): nms threshold to be used when upatchifying. Defaults to 0.45.
40
41
  final_score_threshold (``float``, **optional**): Apply a score thresholding after penalty and after nms. Defaults to 0.4.
41
42
  categories (``Dict[int, str]``, **optional**): To rename your categories in the visualization.
42
- patchifier (``Union[BasePatchifier, None], **optional``): If None use default SemanticPatchifier or DetectPatchifier according to Configuration().data_type. Default to None.
43
+ patchifier (``Union[BasePatchifier, None], **optional**): If None use default SemanticPatchifier or DetectPatchifier according to Configuration().data_type. Default to None.
44
+ verbose (``bool``, **optional**) : if set to True will display progress state in patchs predictions. Default to True.
43
45
 
44
46
  Example:
45
47
  ----------
@@ -64,6 +66,7 @@ class Predictor:
64
66
  cropper (``Transform``)
65
67
  patchifier (``BasePatchifier``)
66
68
  categories (``Dict[int, str]``)
69
+ verbose (``bool``)
67
70
 
68
71
 
69
72
  **Methods**
@@ -79,6 +82,7 @@ class Predictor:
79
82
  batch_size: int = 1,
80
83
  categories: Dict[int, str] = None,
81
84
  patchifier: Union[BasePatchifier, None] = None,
85
+ verbose: bool = True,
82
86
  ):
83
87
  assert any(
84
88
  [
@@ -103,24 +107,30 @@ class Predictor:
103
107
  else:
104
108
  self.patchifier = patchifier
105
109
  self.categories = categories
110
+ self.verbose = verbose
106
111
 
107
112
  def forward_pass(self, batch_patchs: Tensor) -> BatchedFormat:
108
113
  """Run predictions on image / batch of patches"""
109
114
  with torch.no_grad():
110
115
  loader = PredictorDataLoader(batch_patchs, self.batch_size)
111
116
  # wrap in tqdm if more than one batch
112
- loader = (
113
- tqdm(loader, desc="Predict on batch of patches : ", total=len(loader))
114
- if len(loader) > 1
115
- else loader
116
- )
117
+ if self.verbose:
118
+ loader = (
119
+ tqdm(
120
+ loader, desc="Predict on batch of patches : ", total=len(loader)
121
+ )
122
+ if len(loader) > 1
123
+ else loader
124
+ )
117
125
  predictions = BatchedFormat([])
118
126
  for patch in loader:
119
127
  predictions += self.model.get_predictions(patch)
120
128
  return predictions
121
129
 
122
130
  def predict(
123
- self, image: Union[str, Path, Tensor], visu_path: Union[str, Path] = ""
131
+ self,
132
+ image: Union[str, Path, Tensor],
133
+ visu_path: Union[str, Path] = "",
124
134
  ) -> BaseFormat:
125
135
  """Main function of ```Predictor``` : call everything needed for prediction.
126
136
 
@@ -143,12 +153,33 @@ class Predictor:
143
153
  h_original, w_original = image.shape[-2:]
144
154
  image = image.to(Configuration().device)
145
155
  # preprocess
146
- preprocessed_image = self.preprocessing(image[None, :])[0]
156
+ if self.preprocessing != None:
157
+ preprocessed_image = self.preprocessing(image[None, :])[0]
158
+ else:
159
+ preprocessed_image = image
147
160
  # handles patch if needed
148
161
  if self.patch_size != None and self.patch_size != (
149
162
  image.shape[-2],
150
163
  image.shape[-1],
151
164
  ):
165
+ _h_pad, _w_pad = 0, 0
166
+ if image.shape[-2] < self.patch_size[0]:
167
+ _h_pad = self.patch_size[0] - image.shape[-2]
168
+ _t_pad = int(_h_pad // 2)
169
+ _t_pad = _t_pad if _t_pad > 0 else 0
170
+ _b_pad = _h_pad - _t_pad
171
+ _b_pad = _b_pad if _b_pad > 0 else 0
172
+ if image.shape[-2] < self.patch_size[0]:
173
+ _w_pad = self.patch_size[1] - image.shape[-1]
174
+ _l_pad = int(_w_pad // 2)
175
+ _l_pad = _l_pad if _l_pad > 0 else 0
176
+ _r_pad = _w_pad - _l_pad
177
+ _r_pad = _r_pad if _r_pad > 0 else 0
178
+ if _h_pad > 0 or _w_pad > 0:
179
+ preprocessed_image = Pad((_l_pad, _t_pad, _r_pad, _b_pad))(
180
+ preprocessed_image
181
+ )
182
+
152
183
  batch_patch, pad_origins, padded_image, pad_coord = (
153
184
  self.patchifier.patchify(preprocessed_image)
154
185
  )
@@ -118,7 +118,7 @@ class ClassifRecall(ClassifMetric):
118
118
  super().__init__(func=F.recall, name="ClassifRecall", *args, **kwargs)
119
119
 
120
120
 
121
- class ClassifAccuracy(ClassifMetric):
121
+ class ClassifPrecision(ClassifMetric):
122
122
  """Classification precision score"""
123
123
 
124
124
  def __init__(self, *args, **kwargs):
@@ -35,12 +35,10 @@ class DetectMetric(Metric):
35
35
  Configuration().data_type != "semantic_mask"
36
36
  ), "Can't use detection metrics with Configuration().data_type = semantic_mask. Must be instance_mask or bbox"
37
37
  super().__init__(**kwargs)
38
- self.add_state("tp", default=torch.tensor(0), dist_reduce_fx="sum")
39
- self.add_state("fp", default=torch.tensor(0), dist_reduce_fx="sum")
40
- self.add_state("fn", default=torch.tensor(0), dist_reduce_fx="sum")
41
- self.add_state("samplewise", default=[], dist_reduce_fx="cat")
42
38
  self.func = func
43
39
  self.name = name
40
+ self.add_state("stats", default=[], dist_reduce_fx="cat")
41
+ self.nc = 1 # always one class
44
42
 
45
43
  def update(
46
44
  self,
@@ -73,25 +71,81 @@ class DetectMetric(Metric):
73
71
  tp, fp, fn, _ = matcher.match_pred_target(pred, targ)
74
72
  elif pred.nb_object != 0 and targ.nb_object == 0:
75
73
  tp, fp, fn, _ = 0, pred.nb_object, 0, None
76
- elif prediction.size == 0 and target.size != 0:
74
+ #elif prediction.size == 0 and target.size != 0:
75
+ elif pred.nb_object == 0 and targ.nb_object != 0:
77
76
  tp, fp, fn, _ = 0, 0, targ.nb_object, None
78
77
  else:
79
- return
78
+ tp, fp, fn, _ = torch.nan, torch.nan, torch.nan, None
80
79
 
81
- self.tp += tp
82
- self.fp += fp
83
- self.fn += fn
84
- # None because of no tn in detection
85
- self.samplewise.append(self.func(tp, fp, None, fn))
80
+ # NaN because of no tn in detection
81
+ self.stats.append(torch.tensor([[[tp, fp, torch.nan, fn, torch.nan]]])) # fix tn and support to nan
82
+
83
+ def global_micro_compute(self) -> Tensor:
84
+ """Compute metric with global/micro averagging."""
85
+ n_samples = len(self.stats)
86
+ samples_stack = torch.cat(self.stats).view(n_samples, self.nc, 5) # (N, NC, 5)
87
+ # sum stats accross samples
88
+ samples_stack = samples_stack.sum(dim=0) # (NC, 5)
89
+ # sum stats accross classes
90
+ micro_stack = torch.sum(samples_stack, dim=0) # (5,)
91
+ # compute metric
92
+ tp, fp, tn, fn, _ = micro_stack.unbind(0)
93
+ return self.func(tp, fp, tn, fn)
94
+
95
+ def samplewise_micro(self) -> Tensor:
96
+ """Compute metric with samplewise/micro averagging."""
97
+ n_samples = len(self.stats)
98
+ samples_stack = torch.cat(self.stats).view(n_samples, self.nc, 5) # (N, NC, 5))
99
+ # sum stat accross classes
100
+ samples_stack = samples_stack.sum(dim=1) # (NC, 5)
101
+ # compute metric/sample
102
+ tp, fp, tn, fn, _ = samples_stack.unbind(dim=1)
103
+ samples_metrics = self.func(tp, fp, tn, fn)
104
+ # mean accross samples
105
+ return torch.nanmean(samples_metrics)
106
+
107
+ def compute_last_sample(self):
108
+ """Return metrics values of the last sample in self.stats.
109
+ Used in combination with self.update
110
+
111
+ Returns:
112
+ Dict[str, Float]: dictionnary with metric value for all classes combined and for each class
113
+ """
86
114
 
115
+ if not self.stats:
116
+ return {'all_cls': torch.tensor(torch.nan)}
117
+
118
+ metric_sample_dict = {}
119
+
120
+ matrix_stats_img = self.stats[-1].squeeze(0) # matrix with stats (tp, fp, tn, fn) of one sample (one row for each class)
121
+ nb_class = matrix_stats_img.size(dim=0)
122
+ tp, fp, tn, fn, _ = matrix_stats_img.nansum(dim=0).unbind(dim=0) # sum over all classes
123
+ all_class_metric_value = round(self.func(tp, fp, tn, fn).item(),3) # metric compute for all classes
124
+ metric_sample_dict['all_cls'] = all_class_metric_value
125
+
126
+ if nb_class > 1:
127
+ for index_class in range (nb_class) : # metric compute for each class
128
+ matrix_stats_img_class = matrix_stats_img[index_class,:]
129
+ tp, fp, tn, fn, _ = matrix_stats_img_class.unbind(dim=0) # stats (tp, fp, tn, fn) of the given class
130
+ class_metric_value = round(self.func(tp, fp, tn, fn).item(),3) # metric compute for the given class
131
+ metric_sample_dict["cls_{0}".format(index_class)] = class_metric_value
132
+
133
+ return metric_sample_dict
134
+
87
135
  def compute(self) -> Dict[str, Tensor]:
88
136
  """Return metric computed with internal state.
89
137
 
90
138
  Returns:
91
139
  Dict[str, Tensor]: dictionnary with aggregation_method: value
92
140
  """
93
- global_value = self.func(self.tp, self.fp, None, self.fn)
94
- samplewise_value = torch.nanmean(torch.tensor(self.samplewise))
141
+ if not self.stats:
142
+ return {self.name: torch.tensor(torch.nan)}
143
+
144
+ # global micro
145
+ global_value = self.global_micro_compute()
146
+ # samplewise micro
147
+ samplewise_value = self.samplewise_micro()
148
+
95
149
  metric_dict = {"global": global_value, "samplewise": samplewise_value}
96
150
 
97
151
  return metric_dict
@@ -122,6 +176,7 @@ class ClassWiseDetectMetric(Metric):
122
176
  Configuration().data_type != "semantic_mask"
123
177
  ), "Can't use detection metrics with Configuration().data_type = semantic_mask. Must be instance_mask or bbox"
124
178
  num_classes = Configuration().num_classes
179
+ self.nc = num_classes
125
180
  assert (
126
181
  num_classes >= 2
127
182
  ), f"{num_classes} is an invalid number of classes for ClassWiseMetric (must be >= 2). If you have one class please use DetectMetric instead of ClasswiseMetric"
@@ -135,6 +190,8 @@ class ClassWiseDetectMetric(Metric):
135
190
  DetectMetric(func, name=f"{name}/cls_{i}").to(self.device)
136
191
  for i in range(num_classes)
137
192
  ]
193
+ self.add_state("stats", default=[], dist_reduce_fx="cat")
194
+ self.func = func
138
195
 
139
196
  def update(
140
197
  self,
@@ -155,13 +212,38 @@ class ClassWiseDetectMetric(Metric):
155
212
  target = (
156
213
  target if isinstance(target, BatchedFormat) else BatchedFormat([target])
157
214
  )
215
+ list_met_class = []
158
216
  for i, met in enumerate(self.classmetrics):
159
- filtered_pred = self._filter_classes(prediction, i)
160
- filtered_targ = self._filter_classes(target, i)
217
+ met_samples_result = []
161
218
  if i == 0:
162
219
  met.update(prediction, target)
163
220
  else:
221
+ filtered_pred = self._filter_classes(prediction, i-1) # -1 because classes start at 0
222
+ filtered_targ = self._filter_classes(target, i-1)
164
223
  met.update(filtered_pred, filtered_targ)
224
+
225
+ # retrieve and save in a list the stats for each sample in the batch for the given class
226
+ batch_size = prediction.size
227
+ for num_elem in range (batch_size,0,-1):
228
+ met_samples_result.append(met.stats[-num_elem])
229
+
230
+ list_met_class.append(met_samples_result) # save the list in the list containing the list of each class
231
+
232
+ # save the batch sample stats for each class in a matrix (N, NC, 5) and append in metric 'stats' attribute
233
+ for num_sample in range(batch_size):
234
+ sample_stats = []
235
+ for num_class in range(1,len(list_met_class)):
236
+ sample_stats.append(list_met_class[num_class][num_sample])
237
+
238
+ class_stack = torch.cat(sample_stats).view(1, Configuration().num_classes, 5) # (N, NC, 5)
239
+ self.stats.append(class_stack)
240
+
241
+
242
+
243
+ # for sample_list_met_class in list_met_class:
244
+ # sample_list_met_class = sample_list_met_class[1:]
245
+ # class_stack = torch.cat(sample_list_met_class).view(1, Configuration().num_classes, 5) # (N, NC, 5)
246
+ # self.stats.append(class_stack)
165
247
 
166
248
  def _filter_classes(self, batchedformat: BatchedFormat, cls: int):
167
249
  new_formats = []
@@ -170,14 +252,116 @@ class ClassWiseDetectMetric(Metric):
170
252
  new_formats.append(new_form)
171
253
  return BatchedFormat(new_formats)
172
254
 
255
+ def global_micro_compute(self) -> Tensor:
256
+ """Compute metric with global/micro averagging."""
257
+ n_samples = len(self.stats)
258
+ samples_stack = torch.cat(self.stats).view(n_samples, self.nc, 5) # (N, NC, 5)
259
+ # sum stats accross samples
260
+ samples_stack = samples_stack.nansum(dim=0) # (NC, 5)
261
+ # sum stats accross classes
262
+ micro_stack = torch.nansum(samples_stack, dim=0) # (5,)
263
+ # compute metric
264
+ tp, fp, tn, fn, _ = micro_stack.unbind(0)
265
+ return self.func(tp, fp, tn, fn)
266
+
267
+ def global_macro_compute(self) -> Tuple[Tensor, Tensor]:
268
+ """Compute metric with global/macro averraging.
269
+ Return also metric/class tensor."""
270
+ n_samples = len(self.stats)
271
+ samples_stack = torch.cat(self.stats).view(n_samples, self.nc, 5) # (N, NC, 5)
272
+ # sum stats accross samples
273
+ samples_stack = samples_stack.nansum(dim=0) # (NC, 5)
274
+ # compute metric/class
275
+ tp, fp, tn, fn, _ = samples_stack.unbind(1)
276
+ class_metrics = self.func(tp, fp, tn, fn) # (NC,)
277
+ return torch.nanmean(class_metrics), class_metrics
278
+
279
+ def samplewise_micro(self) -> Tensor:
280
+ """Compute metric with samplewise/micro averagging."""
281
+ n_samples = len(self.stats)
282
+ samples_stack = torch.cat(self.stats).view(n_samples, self.nc, 5) # (N, NC, 5))
283
+ # sum stat accross classes
284
+ samples_stack = samples_stack.nansum(dim=1) # (N, 5)
285
+ # compute metric/sample
286
+ tp, fp, tn, fn, _ = samples_stack.unbind(dim=1)
287
+ samples_metrics = self.func(tp, fp, tn, fn)
288
+ # mean accross samples
289
+ return torch.nanmean(samples_metrics)
290
+
291
+ def samplewise_macro(self) -> Tensor:
292
+ """Compute metric with samplewise/macro averagging."""
293
+ n_samples = len(self.stats)
294
+ samples_stack = torch.cat(self.stats).view(n_samples, self.nc, 5) # (N, NC, 5)
295
+ # compute metric/class/sample
296
+ tp, fp, tn, fn, _ = samples_stack.unbind(2)
297
+ class_metrics = self.func(tp, fp, tn, fn) # (N,NC)
298
+ # mean accross classes
299
+ macro = torch.nanmean(class_metrics, dim=1) # (N,)
300
+ # mean accross samples
301
+ macro_samplewise = torch.nanmean(macro, dim=0)
302
+
303
+ return macro_samplewise
304
+
305
+ def compute_last_sample(self):
306
+ """Return metrics values of the last sample in self.stats.
307
+ Used in combination with self.update
308
+
309
+ Returns:
310
+ Dict[str, Float]: dictionnary with metric value for all classes combined and for each class
311
+ """
312
+
313
+ if not self.stats:
314
+ return {'all_cls': torch.tensor(torch.nan)}
315
+
316
+ metric_sample_dict = {}
317
+
318
+ matrix_stats_img = self.stats[-1].squeeze(0) # matrix with stats (tp, fp, tn, fn) of one sample (one row for each class)
319
+ nb_class = matrix_stats_img.size(dim=0)
320
+ tp, fp, tn, fn, _ = matrix_stats_img.nansum(dim=0).unbind(dim=0) # sum over all classes
321
+ all_class_metric_value = round(self.func(tp, fp, tn, fn).item(),3) # metric compute for all classes
322
+ metric_sample_dict['all_cls'] = all_class_metric_value
323
+
324
+ if nb_class > 1:
325
+ for index_class in range (nb_class) : # metric compute for each class
326
+ matrix_stats_img_class = matrix_stats_img[index_class,:]
327
+ tp, fp, tn, fn, _ = matrix_stats_img_class.unbind(dim=0) # stats (tp, fp, tn, fn) of the given class
328
+ class_metric_value = round(self.func(tp, fp, tn, fn).item(),3) # metric compute for the given class
329
+ metric_sample_dict["cls_{0}".format(index_class)] = class_metric_value
330
+
331
+ return metric_sample_dict
332
+
173
333
  def compute(self):
174
334
  """Return metrics values.
175
335
 
176
336
  Returns:
177
337
  Dict[str, Tensor]: dictionnary with all "global" DetectMetric in self.classmetrics
178
338
  """
179
- classwisedict = {met.name: met.compute()["global"] for met in self.classmetrics}
180
- return classwisedict
339
+ #classwisedict = {met.name: met.compute()["global"] for met in self.classmetrics}
340
+ #return classwisedict
341
+
342
+ if not self.stats:
343
+ return {self.name: torch.tensor(torch.nan)}
344
+
345
+ metric_dict = {}
346
+ # global micro
347
+ global_micro = self.global_micro_compute()
348
+ metric_dict.update({"_global_micro": global_micro})
349
+ # global macro
350
+ global_macro, class_metrics = self.global_macro_compute()
351
+ classes_dict = {
352
+ f"/cls_{i}": class_metrics[i]
353
+ for i in range(class_metrics.nelement())
354
+ }
355
+ metric_dict.update({"_global_macro": global_macro})
356
+ metric_dict.update(classes_dict)
357
+ # samplewise micro
358
+ samplewise_micro = self.samplewise_micro()
359
+ metric_dict.update({"_samplewise_micro": samplewise_micro})
360
+ # samplewise macro
361
+ samplewise_macro = self.samplewise_macro()
362
+ metric_dict.update({"_samplewise_macro": samplewise_macro})
363
+
364
+ return metric_dict
181
365
 
182
366
  # override
183
367
  def reset(self):
@@ -320,7 +504,35 @@ class ClassifMetric(Metric):
320
504
  macro_samplewise = torch.nanmean(macro, dim=0)
321
505
 
322
506
  return macro_samplewise
507
+
508
+ def compute_last_sample(self):
509
+ """Return metrics values of the last sample in self.stats.
510
+ Used in combination with self.update
511
+
512
+ Returns:
513
+ Dict[str, Float]: dictionnary with metric value for all classes combined and for each class
514
+ """
323
515
 
516
+ if not self.stats:
517
+ return {'all_cls': torch.tensor(torch.nan)}
518
+
519
+ metric_sample_dict = {}
520
+
521
+ matrix_stats_img = self.stats[-1].squeeze(0) # matrix with stats (tp, fp, tn, fn) of one sample (one row for each class)
522
+ nb_class = matrix_stats_img.size(dim=0)
523
+ tp, fp, tn, fn, _ = matrix_stats_img.nansum(dim=0).unbind(dim=0) # sum over all classes
524
+ all_class_metric_value = round(self.func(tp, fp, tn, fn).item(),3) # metric compute for all classes
525
+ metric_sample_dict['all_cls'] = all_class_metric_value
526
+
527
+ if nb_class > 1:
528
+ for index_class in range (nb_class) : # metric compute for each class
529
+ matrix_stats_img_class = matrix_stats_img[index_class,:]
530
+ tp, fp, tn, fn, _ = matrix_stats_img_class.unbind(dim=0) # stats (tp, fp, tn, fn) of the given class
531
+ class_metric_value = round(self.func(tp, fp, tn, fn).item(),3) # metric compute for the given class
532
+ metric_sample_dict["cls_{0}".format(index_class)] = class_metric_value
533
+
534
+ return metric_sample_dict
535
+
324
536
  def compute(self):
325
537
  """Comput metric with all averag strategy and return a dict with all values."""
326
538
  if not self.stats:
@@ -0,0 +1,36 @@
1
+ import torch
2
+ from torch import Tensor
3
+
4
+
5
+ # functionnals metrics
6
+ def f1score(tp: Tensor, fp: Tensor, tn: Tensor, fn: Tensor) -> Tensor:
7
+ for t in [tp,fp,tn,fn]:
8
+ assert torch.is_tensor(t), "argument must be a tensor"
9
+ return (2 * tp) / (2 * tp + fp + fn)
10
+
11
+
12
+ def precision(tp: Tensor, fp: Tensor, tn: Tensor, fn: Tensor) -> Tensor:
13
+ for t in [tp,fp,tn,fn]:
14
+ assert torch.is_tensor(t), "argument must be a tensor"
15
+ return (tp) / (tp + fp)
16
+
17
+
18
+ def recall(tp: Tensor, fp: Tensor, tn: Tensor, fn: Tensor) -> Tensor:
19
+ for t in [tp,fp,tn,fn]:
20
+ assert torch.is_tensor(t), "argument must be a tensor"
21
+ return (tp) / (tp + fn)
22
+
23
+
24
+ def iou(tp: Tensor, fp: Tensor, tn: Tensor, fn: Tensor) -> Tensor:
25
+ """Compute IoU from statistics."""
26
+ for t in [tp,fp,tn,fn]:
27
+ assert torch.is_tensor(t), "argument must be a tensor"
28
+ return tp / (tp + fp + fn)
29
+
30
+
31
+ def accuracy(tp: Tensor, fp: Tensor, tn: Tensor, fn: Tensor) -> Tensor:
32
+ """Compute accuracy from statistics. In case of detection, tn is nan : -> 0 for computation"""
33
+ for t in [tp,fp,tn,fn]:
34
+ assert torch.is_tensor(t), "argument must be a tensor"
35
+ tn[tn != tn] = 0 # set all NaN to 0 in tn
36
+ return (tp + tn) / (tn + tp + fp + fn)
@@ -1,8 +1,8 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: deepvisiontools
3
- Version: 0.0.4
4
- Summary: Overlay of PyTorch to generalize trainning and inference processes for detection & instance segmentation tasks.
5
- Author-email: Lucas Bernigaud Samatan <lucas.bernigaud-samatan@inrae.fr>, Jordan Bernigaud Samatan <jordan.bernigaud-samatan@inrae.fr>
3
+ Version: 0.1.0
4
+ Summary: Overlay of PyTorch to generalize trainning and inference processes for detection, instance segmentation and semantic segmentation tasks.
5
+ Author-email: Lucas Bernigaud Samatan <lucas.bernigaud-samatan@inrae.fr>, Jordan Bernigaud Samatan <jordan.bernigaud-samatan@inrae.fr>, Paul Faucher <paul.faucher@inrae.fr>
6
6
  Maintainer-email: Jordan Bernigaud Samatan <jordan.bernigaud-samatan@inrae.fr>
7
7
  License: GNU AFFERO GENERAL PUBLIC LICENSE
8
8
  Version 3, 19 November 2007
@@ -670,20 +670,23 @@ Project-URL: Homepage, https://forgemia.inra.fr/ue-apc/librairies/python/deepvis
670
670
  Project-URL: Issues, https://forgemia.inra.fr/ue-apc/librairies/python/deepvisiontools_v1/-/issues
671
671
  Project-URL: Documentation, https://deepvisiontools.readthedocs.io/en/latest/
672
672
  Project-URL: Repository, https://forgemia.inra.fr/ue-apc/librairies/python/deepvisiontools_v1
673
- Classifier: Development Status :: 3 - Alpha
673
+ Classifier: Development Status :: 4 - Beta
674
674
  Classifier: Programming Language :: Python :: 3.11
675
- Requires-Python: >=3.11.9
675
+ Classifier: Programming Language :: Python :: 3.12
676
+ Requires-Python: <=3.12.9,>=3.11.9
676
677
  Description-Content-Type: text/markdown
677
678
  License-File: LICENSE.txt
678
- Requires-Dist: torch==2.4.0
679
- Requires-Dist: torchvision==0.19.0
679
+ Requires-Dist: torch<=2.6.0,>=2.4.0
680
+ Requires-Dist: torchvision<=0.21.0,>=0.19.0
680
681
  Requires-Dist: pycocotools==2.0.8
681
- Requires-Dist: torchmetrics==1.4.0
682
- Requires-Dist: tensorboard==2.18.0
683
- Requires-Dist: ultralytics==8.3.28
684
- Requires-Dist: transformers==4.44.2
682
+ Requires-Dist: torchmetrics<=1.7.0,>=1.4.0
683
+ Requires-Dist: tensorboard<=2.19.0,>=2.18.0
684
+ Requires-Dist: ultralytics<=8.3.95,>=8.3.28
685
+ Requires-Dist: transformers<=4.50.0,>=4.44.2
685
686
  Requires-Dist: colorcet==3.1.0
686
- Requires-Dist: segmentation-models-pytorch==0.3.4
687
+ Requires-Dist: segmentation-models-pytorch<=0.4.0,>=0.3.4
688
+ Requires-Dist: openpyxl==3.1.5
689
+ Dynamic: license-file
687
690
 
688
691
  <!--
689
692
  <p align="center">
@@ -713,6 +716,7 @@ Similar stands for other models.
713
716
  All documentation about deepvisiontools, including tutorials, can be found at : https://deepvisiontools.readthedocs.io/en/latest/
714
717
 
715
718
  ## Installation
719
+ We have tested deepvisiontools for python versions between 3.11.9 and 3.12.9
716
720
 
717
721
  ```shell
718
722
  pip install deepvisiontools
@@ -26,6 +26,7 @@ src/deepvisiontools/formats/errors.py
26
26
  src/deepvisiontools/formats/formats.py
27
27
  src/deepvisiontools/formats/utils.py
28
28
  src/deepvisiontools/inference/__init__.py
29
+ src/deepvisiontools/inference/evaluator.py
29
30
  src/deepvisiontools/inference/patchifier.py
30
31
  src/deepvisiontools/inference/predictor.py
31
32
  src/deepvisiontools/metrics/__init__.py
@@ -0,0 +1,10 @@
1
+ torch<=2.6.0,>=2.4.0
2
+ torchvision<=0.21.0,>=0.19.0
3
+ pycocotools==2.0.8
4
+ torchmetrics<=1.7.0,>=1.4.0
5
+ tensorboard<=2.19.0,>=2.18.0
6
+ ultralytics<=8.3.95,>=8.3.28
7
+ transformers<=4.50.0,>=4.44.2
8
+ colorcet==3.1.0
9
+ segmentation-models-pytorch<=0.4.0,>=0.3.4
10
+ openpyxl==3.1.5
@@ -1,34 +0,0 @@
1
- import torch
2
- from torch import Tensor
3
-
4
-
5
- # functionnals metrics
6
- def f1score(tp, fp, tn, fn):
7
- return (2 * tp) / (2 * tp + fp + fn)
8
-
9
-
10
- def precision(tp, fp, tn, fn):
11
- # if tp + fp == 0:
12
- # return torch.tensor(torch.nan)
13
- return (tp) / (tp + fp)
14
-
15
-
16
- def recall(tp, fp, tn, fn):
17
- # if tp + fn == 0:
18
- # return torch.tensor(torch.nan)
19
- return (tp) / (tp + fn)
20
-
21
-
22
- def iou(tp: Tensor, fp: Tensor, tn: Tensor, fn: Tensor) -> Tensor:
23
- """Compute IoU from statistics."""
24
- # if tp + fp + fn == 0:
25
- # return torch.tensor(torch.nan)
26
- return tp / (tp + fp + fn)
27
-
28
-
29
- def accuracy(tp: Tensor, fp: Tensor, tn: Tensor, fn: Tensor):
30
- """Compute accuracy from statistics. In case of detection, tn is none : -> 0 for computation"""
31
- if tn == None:
32
- return tp / (tp + fp + fn)
33
- else:
34
- return (tp + tn) / (tn + tp + fp + fn)
@@ -1,9 +0,0 @@
1
- torch==2.4.0
2
- torchvision==0.19.0
3
- pycocotools==2.0.8
4
- torchmetrics==1.4.0
5
- tensorboard==2.18.0
6
- ultralytics==8.3.28
7
- transformers==4.44.2
8
- colorcet==3.1.0
9
- segmentation-models-pytorch==0.3.4