0
# Metrics Collection
1
2
Comprehensive metric collection system covering classification, regression, NLP, computer vision, clustering, and GAN evaluation. PyTorch Ignite provides 80+ built-in metrics with a consistent API and support for custom metrics.
3
4
## Capabilities
5
6
### Base Metric Classes
7
8
Foundation classes for all metrics with consistent interface and behavior.
9
10
```python { .api }
11
class Metric:
12
"""
13
Base class for all metrics.
14
15
All metrics inherit from this class and implement reset(), update(), and compute() methods.
16
"""
17
def reset(self):
18
"""Reset metric state to initial values."""
19
20
def update(self, output):
21
"""
22
Update metric state with new data.
23
24
Parameters:
25
- output: engine output (typically (y_pred, y) tuple)
26
"""
27
28
def compute(self):
29
"""
30
Compute and return the metric value.
31
32
Returns:
33
Computed metric value
34
"""
35
36
def attach(self, engine, name, usage=EpochWise()):
37
"""
38
Attach metric to an engine.
39
40
Parameters:
41
- engine: Engine to attach to
42
- name: metric name
43
- usage: how metric should be used (EpochWise, etc.)
44
"""
45
46
class EpochMetric(Metric):
47
"""
48
Base class for metrics computed at the end of each epoch.
49
50
Parameters:
51
- compute_fn: function to compute metric from accumulated values
52
- output_transform: function to transform engine output
53
- check_compute_fn: whether to validate compute_fn signature
54
- device: device for tensor operations
55
"""
56
def __init__(self, compute_fn, output_transform=None, check_compute_fn=True, device=None): ...
57
58
class RunningAverage(Metric):
59
"""
60
Running average wrapper for any metric.
61
62
Parameters:
63
- src: source metric to average
64
- alpha: smoothing factor (default: 0.98)
65
- output_transform: function to transform engine output
66
"""
67
def __init__(self, src, alpha=0.98, output_transform=None): ...
68
```
69
70
### Classification Metrics
71
72
Metrics for classification tasks including binary, multi-class, and multi-label scenarios.
73
74
```python { .api }
75
class Accuracy(Metric):
76
"""
77
Accuracy metric for classification tasks.
78
79
Parameters:
80
- output_transform: function to transform engine output
81
- is_multilabel: whether to treat as multi-label classification
82
- device: device for tensor operations
83
"""
84
def __init__(self, output_transform=None, is_multilabel=False, device=torch.device("cpu")): ...
85
86
class TopKCategoricalAccuracy(Metric):
87
"""
88
Top-K categorical accuracy.
89
90
Parameters:
91
- k: number of top predictions to consider
92
- output_transform: function to transform engine output
93
- device: device for tensor operations
94
"""
95
def __init__(self, k=5, output_transform=None, device=None): ...
96
97
class Precision(Metric):
98
"""
99
Precision score for classification.
100
101
Parameters:
102
- average: averaging strategy ('micro', 'macro', 'weighted', None)
103
- is_multilabel: whether to treat as multi-label
104
- output_transform: function to transform engine output
105
- device: device for tensor operations
106
"""
107
def __init__(self, average=False, is_multilabel=False, output_transform=None, device=None): ...
108
109
class Recall(Metric):
110
"""
111
Recall score for classification.
112
113
Parameters:
114
- average: averaging strategy ('micro', 'macro', 'weighted', None)
115
- is_multilabel: whether to treat as multi-label
116
- output_transform: function to transform engine output
117
- device: device for tensor operations
118
"""
119
def __init__(self, average=False, is_multilabel=False, output_transform=None, device=None): ...
120
121
class Fbeta(Metric):
122
"""
123
F-beta score for classification.
124
125
Parameters:
126
- beta: weight of recall in harmonic mean
127
- average: averaging strategy ('micro', 'macro', 'weighted', None)
128
- precision: precision metric (optional)
129
- recall: recall metric (optional)
130
- output_transform: function to transform engine output
131
- device: device for tensor operations
132
"""
133
def __init__(self, beta, average=True, precision=None, recall=None, output_transform=None, device=None): ...
134
135
class ConfusionMatrix(Metric):
136
"""
137
Confusion matrix for classification.
138
139
Parameters:
140
- num_classes: number of classes
141
- average: averaging strategy (None, 'samples')
142
- output_transform: function to transform engine output
143
- device: device for tensor operations
144
"""
145
def __init__(self, num_classes, average=None, output_transform=None, device=None): ...
146
147
class MultilabelConfusionMatrix(Metric):
148
"""Multi-label confusion matrix."""
149
def __init__(self, num_classes, output_transform=None, device=None): ...
150
151
class CohenKappa(Metric):
152
"""Cohen's kappa coefficient."""
153
def __init__(self, num_classes, output_transform=None, device=None): ...
154
155
class IoU(Metric):
156
"""Intersection over Union for segmentation."""
157
def __init__(self, num_classes, ignore_index=None, output_transform=None, device=None): ...
158
159
class mIoU(Metric):
160
"""Mean Intersection over Union."""
161
def __init__(self, num_classes, ignore_index=None, output_transform=None, device=None): ...
162
163
class DiceCoefficient(Metric):
164
"""Dice coefficient for segmentation."""
165
def __init__(self, output_transform=None, device=None): ...
166
```
167
168
### Regression Metrics
169
170
Metrics for regression tasks and continuous value prediction.
171
172
```python { .api }
173
class MeanAbsoluteError(Metric):
174
"""Mean absolute error."""
175
def __init__(self, output_transform=None, device=None): ...
176
177
class MeanSquaredError(Metric):
178
"""Mean squared error."""
179
def __init__(self, output_transform=None, device=None): ...
180
181
class RootMeanSquaredError(Metric):
182
"""Root mean squared error."""
183
def __init__(self, output_transform=None, device=None): ...
184
185
class MeanPairwiseDistance(Metric):
186
"""Mean pairwise distance."""
187
def __init__(self, p=2, eps=1e-6, output_transform=None, device=None): ...
188
189
class CosineSimilarity(Metric):
190
"""Cosine similarity."""
191
def __init__(self, output_transform=None, device=None): ...
192
193
class PearsonCorrelation(Metric):
194
"""Pearson correlation coefficient."""
195
def __init__(self, output_transform=None, device=None): ...
196
197
class SpearmanRankCorrelation(Metric):
198
"""Spearman rank correlation."""
199
def __init__(self, output_transform=None, device=None): ...
200
201
class R2Score(Metric):
202
"""R-squared coefficient of determination."""
203
def __init__(self, output_transform=None, device=None): ...
204
205
class MeanAbsolutePercentageError(Metric):
206
"""Mean absolute percentage error."""
207
def __init__(self, output_transform=None, device=None): ...
208
209
class MedianAbsoluteError(Metric):
210
"""Median absolute error."""
211
def __init__(self, output_transform=None, device=None): ...
212
213
class MedianAbsolutePercentageError(Metric):
214
"""Median absolute percentage error."""
215
def __init__(self, output_transform=None, device=None): ...
216
217
class FractionalAbsoluteError(Metric):
218
"""Fractional absolute error."""
219
def __init__(self, output_transform=None, device=None): ...
220
221
class FractionalBias(Metric):
222
"""Fractional bias."""
223
def __init__(self, output_transform=None, device=None): ...
224
```
225
226
### Ranking and Probability Metrics
227
228
Metrics for ranking tasks and probability-based evaluation.
229
230
```python { .api }
231
class RocAuc(Metric):
232
"""
233
ROC AUC score.
234
235
Parameters:
236
- output_transform: function to transform engine output
237
- device: device for tensor operations
238
"""
239
def __init__(self, output_transform=None, device=None): ...
240
241
class PrecisionRecallCurve(Metric):
242
"""Precision-recall curve."""
243
def __init__(self, output_transform=None, device=None): ...
244
245
class AveragePrecision(Metric):
246
"""Average precision score."""
247
def __init__(self, output_transform=None, device=None): ...
248
249
class MeanAveragePrecision(Metric):
250
"""Mean average precision."""
251
def __init__(self, output_transform=None, device=None): ...
252
```
253
254
### Loss Metrics
255
256
Wrapper for computing loss values as metrics.
257
258
```python { .api }
259
class Loss(Metric):
260
"""
261
Loss metric wrapper.
262
263
Parameters:
264
- loss_fn: PyTorch loss function
265
- output_transform: function to transform engine output
266
- batch_size: batch size for averaging
267
- device: device for tensor operations
268
"""
269
def __init__(self, loss_fn, output_transform=None, batch_size=None, device=None): ...
270
271
class GeometricAverage(Metric):
272
"""Geometric average of metrics."""
273
def __init__(self, output_transform=None, device=None): ...
274
275
class Average(Metric):
276
"""Average of values."""
277
def __init__(self, output_transform=None, device=None): ...
278
279
class VariableAccumulation(Metric):
280
"""Variable accumulation over batches."""
281
def __init__(self, op, output_transform=None, device=None): ...
282
```
283
284
### Image Quality Metrics
285
286
Metrics for evaluating image quality and similarity.
287
288
```python { .api }
289
class PSNR(Metric):
290
"""
291
Peak signal-to-noise ratio.
292
293
Parameters:
294
- data_range: dynamic range of input images
295
- output_transform: function to transform engine output
296
- device: device for tensor operations
297
"""
298
def __init__(self, data_range=1.0, output_transform=None, device=None): ...
299
300
class SSIM(Metric):
301
"""
302
Structural similarity index.
303
304
Parameters:
305
- data_range: dynamic range of input images
306
- kernel_size: size of sliding window
307
- sigma: standard deviation for Gaussian kernel
308
- k1, k2: algorithm parameters
309
- output_transform: function to transform engine output
310
- device: device for tensor operations
311
"""
312
def __init__(self, data_range=1.0, kernel_size=11, sigma=1.5, k1=0.01, k2=0.03, output_transform=None, device=None): ...
313
```
314
315
### NLP Metrics
316
317
Metrics for natural language processing tasks.
318
319
```python { .api }
320
class Bleu(Metric):
321
"""
322
BLEU score for text generation.
323
324
Parameters:
325
- ngram: n-gram order (default: 4)
326
- smooth: smoothing method
327
- output_transform: function to transform engine output
328
- device: device for tensor operations
329
"""
330
def __init__(self, ngram=4, smooth="no_smooth", output_transform=None, device=None): ...
331
332
class Rouge(Metric):
333
"""
334
ROUGE score for text summarization.
335
336
Parameters:
337
- multiref: multiref ROUGE (ROUGE-L)
338
- variants: ROUGE variants to compute
339
- output_transform: function to transform engine output
340
- device: device for tensor operations
341
"""
342
def __init__(self, multiref="best", variants=None, output_transform=None, device=None): ...
343
```
344
345
### Clustering Metrics
346
347
Metrics for evaluating clustering algorithms.
348
349
```python { .api }
350
class MutualInformation(Metric):
351
"""Mutual information score."""
352
def __init__(self, output_transform=None, device=None): ...
353
354
class NormalizedMutualInformation(Metric):
355
"""Normalized mutual information."""
356
def __init__(self, output_transform=None, device=None): ...
357
358
class AdjustedMutualInformation(Metric):
359
"""Adjusted mutual information."""
360
def __init__(self, output_transform=None, device=None): ...
361
362
class AdjustedRandIndex(Metric):
363
"""Adjusted Rand index."""
364
def __init__(self, output_transform=None, device=None): ...
365
366
class RandIndex(Metric):
367
"""Rand index."""
368
def __init__(self, output_transform=None, device=None): ...
369
370
class FowlkesMallowsIndex(Metric):
371
"""Fowlkes-Mallows index."""
372
def __init__(self, output_transform=None, device=None): ...
373
374
class CalinskeHarabaszIndex(Metric):
375
"""Calinski-Harabasz index."""
376
def __init__(self, output_transform=None, device=None): ...
377
378
class DaviesBouldinIndex(Metric):
379
"""Davies-Bouldin index."""
380
def __init__(self, output_transform=None, device=None): ...
381
382
class SilhouetteScore(Metric):
383
"""Silhouette score."""
384
def __init__(self, output_transform=None, device=None): ...
385
```
386
387
### GAN Metrics
388
389
Metrics for evaluating generative adversarial networks.
390
391
```python { .api }
392
class FID(Metric):
393
"""
394
Fréchet Inception Distance.
395
396
Parameters:
397
- num_features: number of features in feature extractor
398
- feature_extractor: feature extraction model
399
- output_transform: function to transform engine output
400
- device: device for tensor operations
401
"""
402
def __init__(self, num_features=2048, feature_extractor=None, output_transform=None, device=None): ...
403
404
class InceptionScore(Metric):
405
"""
406
Inception Score.
407
408
Parameters:
409
- output_transform: function to transform engine output
410
- device: device for tensor operations
411
"""
412
def __init__(self, output_transform=None, device=None): ...
413
```
414
415
### Frequency and Timing Metrics
416
417
Metrics for measuring frequency and timing information.
418
419
```python { .api }
420
class Frequency(Metric):
421
"""
422
Frequency measurement.
423
424
Parameters:
425
- output_transform: function to transform engine output
426
- device: device for tensor operations
427
"""
428
def __init__(self, output_transform=None, device=None): ...
429
```
430
431
## Usage Examples
432
433
### Basic Metric Usage
434
435
```python
436
from ignite.metrics import Accuracy, Loss
437
from ignite.engine import create_supervised_evaluator
438
439
# Create metrics
440
metrics = {
441
'accuracy': Accuracy(),
442
'loss': Loss(criterion)
443
}
444
445
# Create evaluator with metrics
446
evaluator = create_supervised_evaluator(model, metrics=metrics)
447
448
# Run evaluation
449
evaluator.run(val_loader)
450
print(f"Accuracy: {evaluator.state.metrics['accuracy']}")
451
print(f"Loss: {evaluator.state.metrics['loss']}")
452
```
453
454
### Custom Output Transform
455
456
```python
457
from ignite.metrics import Accuracy
458
459
def custom_output_transform(output):
460
# Extract predictions and targets from engine output
461
y_pred, y = output
462
return y_pred, y
463
464
accuracy = Accuracy(output_transform=custom_output_transform)
465
```
466
467
### Running Average
468
469
```python
470
from ignite.metrics import RunningAverage, Loss
471
472
# Create running average of loss
473
running_avg_loss = RunningAverage(Loss(criterion), alpha=0.98)
474
475
# Attach to trainer
476
running_avg_loss.attach(trainer, 'loss')
477
478
@trainer.on(Events.ITERATION_COMPLETED(every=100))
479
def log_loss(engine):
480
print(f"Running avg loss: {engine.state.metrics['loss']}")
481
```