-
Notifications
You must be signed in to change notification settings - Fork 411
/
plotting.py
456 lines (335 loc) · 12.7 KB
/
plotting.py
1
2
3
4
5
6
7
8
9
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
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
268
269
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# Copyright The Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import matplotlib.pyplot as plt
import torch
def pesq_example() -> tuple:
"""Plot PESQ audio example."""
from torchmetrics.audio.pesq import PerceptualEvaluationSpeechQuality
p = lambda: torch.randn(8000)
t = lambda: torch.randn(8000)
# plot single value
metric = PerceptualEvaluationSpeechQuality(8000, "nb")
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = PerceptualEvaluationSpeechQuality(16000, "wb")
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def pit_example() -> tuple:
"""Plot PIT audio example."""
from torchmetrics.audio.pit import PermutationInvariantTraining
from torchmetrics.functional import scale_invariant_signal_noise_ratio
p = lambda: torch.randn(3, 2, 5)
t = lambda: torch.randn(3, 2, 5)
# plot single value
metric = PermutationInvariantTraining(scale_invariant_signal_noise_ratio, "max")
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = PermutationInvariantTraining(scale_invariant_signal_noise_ratio, "max")
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def sdr_example() -> tuple:
"""Plot SDR audio example."""
from torchmetrics.audio.sdr import SignalDistortionRatio
p = lambda: torch.randn(8000)
t = lambda: torch.randn(8000)
# plot single value
metric = SignalDistortionRatio()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = SignalDistortionRatio()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def si_sdr_example() -> tuple:
"""Plot SI-SDR audio example."""
from torchmetrics.audio.sdr import ScaleInvariantSignalDistortionRatio
p = lambda: torch.randn(5)
t = lambda: torch.randn(5)
# plot single value
metric = ScaleInvariantSignalDistortionRatio()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = ScaleInvariantSignalDistortionRatio()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def snr_example() -> tuple:
"""Plot SNR audio example."""
from torchmetrics.audio.snr import SignalNoiseRatio
p = lambda: torch.randn(4)
t = lambda: torch.randn(4)
# plot single value
metric = SignalNoiseRatio()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = SignalNoiseRatio()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def si_snr_example() -> tuple:
"""Plot SI-SNR example."""
from torchmetrics.audio.snr import ScaleInvariantSignalNoiseRatio
p = lambda: torch.randn(4)
t = lambda: torch.randn(4)
# plot single value
metric = ScaleInvariantSignalNoiseRatio()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = ScaleInvariantSignalNoiseRatio()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def stoi_example() -> tuple:
"""Plot STOI example."""
from torchmetrics.audio.stoi import ShortTimeObjectiveIntelligibility
p = lambda: torch.randn(8000)
t = lambda: torch.randn(8000)
# plot single value
metric = ShortTimeObjectiveIntelligibility(8000, False)
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = ShortTimeObjectiveIntelligibility(8000, False)
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def accuracy_example() -> tuple:
"""Plot Accuracy example."""
from torchmetrics.classification import MulticlassAccuracy
p = lambda: torch.randn(20, 5)
t = lambda: torch.randint(5, (20,))
# plot single value
metric = MulticlassAccuracy(num_classes=5)
metric.update(p(), t())
fig, ax = metric.plot()
# plot a value per class
metric = MulticlassAccuracy(num_classes=5, average=None)
metric.update(p(), t())
fig, ax = metric.plot()
# plot two values as a series
metric = MulticlassAccuracy(num_classes=5)
val1 = metric(p(), t())
val2 = metric(p(), t())
fig, ax = metric.plot([val1, val2])
# plot a series of values per class
metric = MulticlassAccuracy(num_classes=5, average=None)
val1 = metric(p(), t())
val2 = metric(p(), t())
fig, ax = metric.plot([val1, val2])
return fig, ax
def mean_squared_error_example() -> tuple:
"""Plot mean squared error example."""
from torchmetrics.regression import MeanSquaredError
p = lambda: torch.randn(20)
t = lambda: torch.randn(20)
# single val
metric = MeanSquaredError()
metric.update(p(), t())
fig, ax = metric.plot()
# multiple values
metric = MeanSquaredError()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def confusion_matrix_example() -> tuple:
"""Plot confusion matrix example."""
from torchmetrics.classification import MulticlassConfusionMatrix
p = lambda: torch.randn(20, 5)
t = lambda: torch.randint(5, (20,))
# plot single value
metric = MulticlassConfusionMatrix(num_classes=5)
metric.update(p(), t())
fig, ax = metric.plot()
return fig, ax
def spectral_distortion_index_example() -> tuple:
"""Plot spectral distortion index example example."""
from torchmetrics.image.d_lambda import SpectralDistortionIndex
p = lambda: torch.rand([16, 3, 16, 16])
t = lambda: torch.rand([16, 3, 16, 16])
# plot single value
metric = SpectralDistortionIndex()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = SpectralDistortionIndex()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def error_relative_global_dimensionless_synthesis() -> tuple:
"""Plot error relative global dimensionless synthesis example."""
from torchmetrics.image.ergas import ErrorRelativeGlobalDimensionlessSynthesis
gen = torch.manual_seed(42)
p = lambda: torch.rand([16, 1, 16, 16], generator=gen)
t = lambda: torch.rand([16, 1, 16, 16], generator=gen)
# plot single value
metric = ErrorRelativeGlobalDimensionlessSynthesis()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = ErrorRelativeGlobalDimensionlessSynthesis()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def peak_signal_noise_ratio() -> tuple:
"""Plot peak signal noise ratio example."""
from torchmetrics.image.psnr import PeakSignalNoiseRatio
p = lambda: torch.tensor([[0.0, 1.0], [2.0, 3.0]])
t = lambda: torch.tensor([[3.0, 2.0], [1.0, 0.0]])
# plot single value
metric = PeakSignalNoiseRatio()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = PeakSignalNoiseRatio()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def spectral_angle_mapper() -> tuple:
"""Plot spectral angle mapper example."""
from torchmetrics.image.sam import SpectralAngleMapper
gen = torch.manual_seed(42)
p = lambda: torch.rand([16, 3, 16, 16], generator=gen)
t = lambda: torch.rand([16, 3, 16, 16], generator=gen)
# plot single value
metric = SpectralAngleMapper()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = SpectralAngleMapper()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def structural_similarity_index_measure() -> tuple:
"""Plot structural similarity index measure example."""
from torchmetrics.image.ssim import StructuralSimilarityIndexMeasure
gen = torch.manual_seed(42)
p = lambda: torch.rand([3, 3, 256, 256], generator=gen)
t = lambda: p() * 0.75
# plot single value
metric = StructuralSimilarityIndexMeasure()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = StructuralSimilarityIndexMeasure()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def multiscale_structural_similarity_index_measure() -> tuple:
"""Plot multiscale structural similarity index measure example."""
from torchmetrics.image.ssim import MultiScaleStructuralSimilarityIndexMeasure
gen = torch.manual_seed(42)
p = lambda: torch.rand([3, 3, 256, 256], generator=gen)
t = lambda: p() * 0.75
# plot single value
metric = MultiScaleStructuralSimilarityIndexMeasure()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = MultiScaleStructuralSimilarityIndexMeasure()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def universal_image_quality_index() -> tuple:
"""Plot universal image quality index example."""
from torchmetrics.image.uqi import UniversalImageQualityIndex
p = lambda: torch.rand([16, 1, 16, 16])
t = lambda: p() * 0.75
# plot single value
metric = UniversalImageQualityIndex()
metric.update(p(), t())
fig, ax = metric.plot()
# plot multiple values
metric = UniversalImageQualityIndex()
vals = [metric(p(), t()) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def mean_average_precision() -> tuple:
"""Plot MAP metric."""
from torchmetrics.detection.mean_ap import MeanAveragePrecision
preds = lambda: [
{
"boxes": torch.tensor([[258.0, 41.0, 606.0, 285.0]]) + torch.randint(10, (1, 4)),
"scores": torch.tensor([0.536]) + 0.1 * torch.rand(1),
"labels": torch.tensor([0]),
}
]
target = [
{
"boxes": torch.tensor([[214.0, 41.0, 562.0, 285.0]]),
"labels": torch.tensor([0]),
}
]
# plot single value
metric = MeanAveragePrecision()
metric.update(preds(), target)
fig, ax = metric.plot()
# plot multiple values
metric = MeanAveragePrecision()
vals = [metric(preds(), target) for _ in range(10)]
fig, ax = metric.plot(vals)
return fig, ax
def roc_example() -> tuple:
"""Plot roc metric."""
from torchmetrics.classification import BinaryROC, MulticlassROC, MultilabelROC
p = lambda: torch.rand(20)
t = lambda: torch.randint(2, (20,))
metric = BinaryROC()
metric.update(p(), t())
fig, ax = metric.plot()
p = lambda: torch.randn(200, 5)
t = lambda: torch.randint(5, (200,))
metric = MulticlassROC(5)
metric.update(p(), t())
fig, ax = metric.plot()
p = lambda: torch.rand(20, 2)
t = lambda: torch.randint(2, (20, 2))
metric = MultilabelROC(2)
metric.update(p(), t())
return fig, ax
if __name__ == "__main__":
metrics_func = {
"accuracy": accuracy_example,
"roc": roc_example,
"pesq": pesq_example,
"pit": pit_example,
"sdr": sdr_example,
"si-sdr": si_sdr_example,
"snr": snr_example,
"si-snr": si_snr_example,
"stoi": stoi_example,
"mean_squared_error": mean_squared_error_example,
"mean_average_precision": mean_average_precision,
"confusion_matrix": confusion_matrix_example,
"spectral_distortion_index": spectral_distortion_index_example,
"error_relative_global_dimensionless_synthesis": error_relative_global_dimensionless_synthesis,
"peak_signal_noise_ratio": peak_signal_noise_ratio,
"spectral_angle_mapper": spectral_angle_mapper,
"structural_similarity_index_measure": structural_similarity_index_measure,
"multiscale_structural_similarity_index_measure": multiscale_structural_similarity_index_measure,
"universal_image_quality_index": universal_image_quality_index,
}
parser = argparse.ArgumentParser(description="Example script for plotting metrics.")
parser.add_argument("metric", type=str, nargs="?", choices=list(metrics_func.keys()), default="accuracy")
args = parser.parse_args()
fig, ax = metrics_func[args.metric]()
plt.show()