ocl.metrics
Package for metrics.
The implemetation of metrics are grouped into submodules according to their datatype and use
- ocl.metrics.bbox: Metrics for bounding boxes
- ocl.metrics.masks: Metrics for masks
- ocl.metrics.tracking: Metrics for multiple object tracking.
- ocl.metrics.diagnosis: Metrics for diagnosing model training.
- ocl.metrics.dataset: Metrics that are computed on the whole dataset.
TensorStatistic
Bases: torchmetrics.Metric
Metric that computes summary statistic of tensors for logging purposes.
First dimension of tensor is assumed to be batch dimension. Other dimensions are reduced to a scalar by the chosen reduction approach (sum or mean).
Source code in ocl/metrics/diagnosis.py
UnsupervisedBboxIoUMetric
Bases: torchmetrics.Metric
Computes IoU metric for bounding boxes when correspondences to ground truth are not known.
Currently, assumes segmentation masks as input for both prediction and targets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_is_mask |
bool
|
If |
False
|
use_threshold |
bool
|
If |
False
|
threshold |
float
|
Value to use for thresholding masks. |
0.5
|
matching |
str
|
How to match predicted boxes to ground truth boxes. For "hungarian", computes assignment that maximizes total IoU between all boxes. For "best_overlap", uses the predicted box with maximum overlap for each ground truth box (each predicted box can be assigned to multiple ground truth boxes). |
'hungarian'
|
compute_discovery_fraction |
bool
|
Instead of the IoU, compute the fraction of ground truth classes that were "discovered", meaning that they have an IoU greater than some threshold. This is recall, or sometimes called the detection rate metric. |
False
|
correct_localization |
bool
|
Instead of the IoU, compute the fraction of images on which at least one ground truth bounding box was correctly localised, meaning that they have an IoU greater than some threshold. |
False
|
discovery_threshold |
float
|
Minimum IoU to count a class as discovered/correctly localized. |
0.5
|
Source code in ocl/metrics/bbox.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
update
Update this metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prediction |
torch.Tensor
|
Predicted mask of shape (B, C, H, W) or (B, F, C, H, W), where C is the number of instances. Assumes class probabilities as inputs. |
required |
target |
torch.Tensor
|
Ground truth mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the number of instance, if using masks as input, or bounding boxes of shape (B, K, 4) or (B, F, K, 4). |
required |
Source code in ocl/metrics/bbox.py
DatasetSemanticMaskIoUMetric
Bases: torchmetrics.Metric
Unsupervised IoU metric for semantic segmentation using dataset-wide matching of classes.
The input to this metric is an instance-level mask with objects, and a class id for each object. This is required to convert the mask to semantic classes. The number of classes for the predictions does not have to match the true number of classes.
Note that contrary to the other metrics in this module, this metric is not supposed to be added
in the online metric computation loop, which is why it does not inherit from RoutableMixin
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_predicted_classes |
int
|
Number of predictable classes, i.e. highest prediction class id that can occur. |
required |
n_classes |
int
|
Total number of classes, i.e. highest class id that can occur. |
required |
threshold |
float
|
Value to use for thresholding masks. |
0.5
|
use_threshold |
bool
|
If |
False
|
matching |
str
|
Method to produce matching between clusters and ground truth classes. If "hungarian", assigns each class one cluster such that the total IoU is maximized. If "majority", assigns each cluster to the class with the highest IoU (each class can be assigned multiple clusters). |
'hungarian'
|
ignore_background |
bool
|
If true, pixels labeled as background (class zero) in the ground truth are not taken into account when computing IoU. |
False
|
use_unmatched_as_background |
bool
|
If true, count predicted classes not selected after Hungarian matching as the background predictions. |
False
|
Source code in ocl/metrics/dataset.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
update
Update metric by computing confusion matrix between predicted and target classes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
torch.Tensor
|
Probability mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the number of object instances in the image. |
required |
targets |
torch.Tensor
|
Mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the number of object instances in the image. Class ID of objects is encoded as the value, i.e. densely represented. |
required |
prediction_class_ids |
torch.Tensor
|
Tensor of shape (B, K), containing the class id of each predicted object instance in the image. Id must be 0 <= id <= n_predicted_classes. |
required |
ignore |
Optional[torch.Tensor]
|
Ignore mask of shape (B, 1, H, W) or (B, 1, K, H, W) |
None
|
Source code in ocl/metrics/dataset.py
preprocess_predicted_mask
Preprocess predicted masks for metric computation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask |
torch.Tensor
|
Probability mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the number of object instances in the prediction. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
Binary tensor of shape (B, P, K), where P is the number of points. If |
torch.Tensor
|
True, overlapping objects for the same point are possible. |
Source code in ocl/metrics/dataset.py
preprocess_ground_truth_mask
Preprocess ground truth mask for metric computation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask |
torch.Tensor
|
Mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the number of object instances in the image. Class ID of objects is encoded as the value, i.e. densely represented. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
One-hot tensor of shape (B, P, J), where J is the number of the classes and P the number |
torch.Tensor
|
of points, with object instances with the same class ID merged together. In the case of |
torch.Tensor
|
an overlap of classes for a point, the class with the highest ID is assigned to that |
torch.Tensor
|
point. |
Source code in ocl/metrics/dataset.py
compute
Compute per-class IoU using matching.
Source code in ocl/metrics/dataset.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
ARIMetric
Bases: torchmetrics.Metric
Computes ARI metric.
Source code in ocl/metrics/masks.py
update
Update this metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prediction |
torch.Tensor
|
Predicted mask of shape (B, C, H, W) or (B, F, C, H, W), where C is the number of classes. |
required |
target |
torch.Tensor
|
Ground truth mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the number of classes. |
required |
ignore |
Optional[torch.Tensor]
|
Ignore mask of shape (B, 1, H, W) or (B, 1, K, H, W) |
None
|
Source code in ocl/metrics/masks.py
MOTMetric
Bases: torchmetrics.Metric
Multiple object tracking metric.
Source code in ocl/metrics/tracking.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
__init__
Initialize MOTMetric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_is_mask |
bool
|
Is the metrics evaluated on masks |
True
|
use_threshold |
bool
|
Use threshold to binarize predicted mask |
True
|
threshold |
float
|
Threshold value |
0.5
|
Source code in ocl/metrics/tracking.py
PatchARIMetric
Bases: ARIMetric
Computes ARI metric assuming patch masks as input.
Source code in ocl/metrics/masks.py
update
Update this metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prediction |
torch.Tensor
|
Predicted mask of shape (B, C, P) or (B, F, C, P), where C is the number of classes and P the number of patches. |
required |
target |
torch.Tensor
|
Ground truth mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the number of classes. |
required |
Source code in ocl/metrics/masks.py
UnsupervisedMaskIoUMetric
Bases: torchmetrics.Metric
Computes IoU metric for segmentation masks when correspondences to ground truth are not known.
Uses Hungarian matching to compute the assignment between predicted classes and ground truth classes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
use_threshold |
bool
|
If |
False
|
threshold |
float
|
Value to use for thresholding masks. |
0.5
|
matching |
str
|
Approach to match predicted to ground truth classes. For "hungarian", computes assignment that maximizes total IoU between all classes. For "best_overlap", uses the predicted class with maximum overlap for each ground truth class. Using "best_overlap" leads to the "average best overlap" metric. |
'hungarian'
|
compute_discovery_fraction |
bool
|
Instead of the IoU, compute the fraction of ground truth classes that were "discovered", meaning that they have an IoU greater than some threshold. |
False
|
correct_localization |
bool
|
Instead of the IoU, compute the fraction of images on which at least one ground truth class was correctly localised, meaning that they have an IoU greater than some threshold. |
False
|
discovery_threshold |
float
|
Minimum IoU to count a class as discovered/correctly localized. |
0.5
|
ignore_background |
bool
|
If true, assume class at index 0 of ground truth masks is background class that is removed before computing IoU. |
False
|
ignore_overlaps |
bool
|
If true, remove points where ground truth masks has overlappign classes from predictions and ground truth masks. |
False
|
Source code in ocl/metrics/masks.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
update
Update this metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prediction |
torch.Tensor
|
Predicted mask of shape (B, C, H, W) or (B, F, C, H, W), where C is the number of classes. Assumes class probabilities as inputs. |
required |
target |
torch.Tensor
|
Ground truth mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the number of classes. |
required |
ignore |
Optional[torch.Tensor]
|
Ignore mask of shape (B, 1, H, W) or (B, 1, K, H, W) |
None
|
Source code in ocl/metrics/masks.py
SklearnClustering
Wrapper around scikit-learn clustering algorithms.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_clusters |
int
|
Number of clusters. |
required |
method |
str
|
Clustering method to use. |
'kmeans'
|
clustering_kwargs |
Optional[Dict[str, Any]]
|
Dictionary of additional keyword arguments to pass to clustering object. |
None
|
use_l2_normalization |
bool
|
Whether to L2 normalize the representations before clustering (but after PCA). |
False
|
use_pca |
bool
|
Whether to apply PCA before fitting the clusters. |
False
|
pca_dimensions |
Optional[int]
|
Number of dimensions for PCA dimensionality reduction. If |
None
|
pca_kwargs |
Optional[Dict[str, Any]]
|
Dictionary of additional keyword arguments to pass to PCA object. |
None
|
Source code in ocl/metrics/dataset.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|