0
# Image Quality Metrics
1
2
Image quality assessment metrics including structural similarity, peak signal-to-noise ratio, and perceptual quality measures for computer vision applications and image processing evaluation.
3
4
## Capabilities
5
6
### Signal-to-Noise Ratio Metrics
7
8
Measures signal quality and reconstruction fidelity in images.
9
10
```python { .api }
11
class PeakSignalNoiseRatio(Metric):
12
def __init__(
13
self,
14
data_range: Optional[float] = None,
15
base: float = 10.0,
16
reduction: str = "elementwise_mean",
17
**kwargs
18
): ...
19
20
class PeakSignalNoiseRatioWithBlockedEffect(Metric):
21
def __init__(
22
self,
23
block_size: int = 8,
24
data_range: Optional[float] = None,
25
reduction: str = "elementwise_mean",
26
**kwargs
27
): ...
28
```
29
30
### Structural Similarity Metrics
31
32
Evaluates structural information preservation between images.
33
34
```python { .api }
35
class StructuralSimilarityIndexMeasure(Metric):
36
def __init__(
37
self,
38
gaussian_kernel: bool = True,
39
sigma: Union[float, Tuple[float, float]] = 1.5,
40
kernel_size: Union[int, Tuple[int, int]] = 11,
41
reduction: str = "elementwise_mean",
42
data_range: Optional[float] = None,
43
k1: float = 0.01,
44
k2: float = 0.03,
45
**kwargs
46
): ...
47
48
class MultiScaleStructuralSimilarityIndexMeasure(Metric):
49
def __init__(
50
self,
51
gaussian_kernel: bool = True,
52
sigma: Union[float, Tuple[float, float]] = 1.5,
53
kernel_size: Union[int, Tuple[int, int]] = 11,
54
reduction: str = "elementwise_mean",
55
data_range: Optional[float] = None,
56
k1: float = 0.01,
57
k2: float = 0.03,
58
betas: Tuple[float, ...] = (0.0448, 0.2856, 0.3001, 0.2363, 0.1333),
59
normalize: Optional[str] = "relu",
60
**kwargs
61
): ...
62
```
63
64
### Universal Image Quality Metrics
65
66
General-purpose image quality assessment measures.
67
68
```python { .api }
69
class UniversalImageQualityIndex(Metric):
70
def __init__(
71
self,
72
kernel_size: Union[int, Tuple[int, int]] = 8,
73
sigma: Union[float, Tuple[float, float]] = 1.5,
74
reduction: str = "elementwise_mean",
75
**kwargs
76
): ...
77
78
class VisualInformationFidelity(Metric):
79
def __init__(
80
self,
81
sigma_n_sq: float = 2.0,
82
**kwargs
83
): ...
84
```
85
86
### Spectral Quality Metrics
87
88
Metrics that analyze spectral properties of images.
89
90
```python { .api }
91
class SpectralAngleMapper(Metric):
92
def __init__(
93
self,
94
reduction: str = "elementwise_mean",
95
**kwargs
96
): ...
97
98
class SpectralDistortionIndex(Metric):
99
def __init__(
100
self,
101
**kwargs
102
): ...
103
104
class SpatialDistortionIndex(Metric):
105
def __init__(
106
self,
107
**kwargs
108
): ...
109
110
class RelativeAverageSpectralError(Metric):
111
def __init__(
112
self,
113
**kwargs
114
): ...
115
116
class ErrorRelativeGlobalDimensionlessSynthesis(Metric):
117
def __init__(
118
self,
119
ratio: float = 1.0,
120
ws: float = 1.0,
121
**kwargs
122
): ...
123
```
124
125
### Advanced Image Quality Metrics
126
127
Sophisticated metrics for comprehensive image quality assessment.
128
129
```python { .api }
130
class RootMeanSquaredErrorUsingSlidingWindow(Metric):
131
def __init__(
132
self,
133
**kwargs
134
): ...
135
136
class TotalVariation(Metric):
137
def __init__(
138
self,
139
reduction: str = "sum",
140
**kwargs
141
): ...
142
143
class QualityWithNoReference(Metric):
144
def __init__(
145
self,
146
**kwargs
147
): ...
148
149
class SpatialCorrelationCoefficient(Metric):
150
def __init__(
151
self,
152
**kwargs
153
): ...
154
```
155
156
### Perceptual Quality Metrics
157
158
Deep learning-based perceptual quality assessment (require optional dependencies).
159
160
```python { .api }
161
class FrechetInceptionDistance(Metric):
162
def __init__(
163
self,
164
feature: int = 2048,
165
reset_real_features: bool = True,
166
normalize: bool = False,
167
**kwargs
168
): ...
169
170
class InceptionScore(Metric):
171
def __init__(
172
self,
173
feature: Union[int, str] = 2048,
174
splits: int = 10,
175
normalize: bool = False,
176
**kwargs
177
): ...
178
179
class KernelInceptionDistance(Metric):
180
def __init__(
181
self,
182
feature: int = 2048,
183
subsets: int = 100,
184
subset_size: int = 1000,
185
degree: int = 3,
186
gamma: Optional[float] = None,
187
coef0: float = 1.0,
188
reset_real_features: bool = True,
189
normalize: bool = False,
190
**kwargs
191
): ...
192
193
class LearnedPerceptualImagePatchSimilarity(Metric):
194
def __init__(
195
self,
196
net_type: str = "alex",
197
reduction: str = "mean",
198
normalize: bool = False,
199
**kwargs
200
): ...
201
```
202
203
### Advanced Perceptual Metrics
204
205
State-of-the-art perceptual quality assessment methods.
206
207
```python { .api }
208
class DeepImageStructureAndTextureSimilarity(Metric):
209
def __init__(
210
self,
211
**kwargs
212
): ...
213
214
class MemorizationInformedFrechetInceptionDistance(Metric):
215
def __init__(
216
self,
217
feature: int = 2048,
218
**kwargs
219
): ...
220
221
class PerceptualPathLength(Metric):
222
def __init__(
223
self,
224
num_samples: int = 10000,
225
batch_size: int = 64,
226
interpolation_method: str = "lerp",
227
epsilon: float = 1e-4,
228
resize: Optional[int] = 64,
229
lower_discard: Optional[float] = None,
230
upper_discard: Optional[float] = None,
231
**kwargs
232
): ...
233
```
234
235
## Usage Examples
236
237
### Basic Image Quality
238
239
```python
240
import torch
241
from torchmetrics.image import (
242
PeakSignalNoiseRatio,
243
StructuralSimilarityIndexMeasure
244
)
245
246
# Initialize metrics
247
psnr = PeakSignalNoiseRatio()
248
ssim = StructuralSimilarityIndexMeasure()
249
250
# Sample image data (batch, channels, height, width)
251
preds = torch.rand(4, 3, 256, 256)
252
target = torch.rand(4, 3, 256, 256)
253
254
# Compute image quality metrics
255
psnr_score = psnr(preds, target)
256
ssim_score = ssim(preds, target)
257
258
print(f"PSNR: {psnr_score:.4f} dB")
259
print(f"SSIM: {ssim_score:.4f}")
260
```
261
262
### Multi-Scale Quality Assessment
263
264
```python
265
from torchmetrics.image import MultiScaleStructuralSimilarityIndexMeasure
266
267
# Multi-scale SSIM
268
ms_ssim = MultiScaleStructuralSimilarityIndexMeasure()
269
270
# High-resolution images
271
preds = torch.rand(2, 3, 512, 512)
272
target = torch.rand(2, 3, 512, 512)
273
274
# Compute MS-SSIM
275
ms_ssim_score = ms_ssim(preds, target)
276
print(f"MS-SSIM: {ms_ssim_score:.4f}")
277
```
278
279
### Perceptual Quality with FID
280
281
```python
282
from torchmetrics.image import FrechetInceptionDistance
283
284
# Initialize FID (requires torch-fidelity)
285
try:
286
fid = FrechetInceptionDistance(feature=2048)
287
288
# Generate synthetic images (must be 3-channel RGB)
289
real_images = torch.randint(0, 256, (50, 3, 299, 299), dtype=torch.uint8)
290
fake_images = torch.randint(0, 256, (50, 3, 299, 299), dtype=torch.uint8)
291
292
# Update with real and fake images
293
fid.update(real_images, real=True)
294
fid.update(fake_images, real=False)
295
296
# Compute FID
297
fid_score = fid.compute()
298
print(f"FID: {fid_score:.4f}")
299
300
except ImportError:
301
print("FID requires the 'torch-fidelity' package")
302
```
303
304
### Perceptual Similarity with LPIPS
305
306
```python
307
from torchmetrics.image import LearnedPerceptualImagePatchSimilarity
308
309
# Initialize LPIPS (requires torchvision)
310
try:
311
lpips = LearnedPerceptualImagePatchSimilarity(net_type='alex')
312
313
# Sample images in [-1, 1] range
314
preds = torch.rand(4, 3, 256, 256) * 2 - 1
315
target = torch.rand(4, 3, 256, 256) * 2 - 1
316
317
# Compute perceptual distance
318
lpips_score = lpips(preds, target)
319
print(f"LPIPS: {lpips_score:.4f}")
320
321
except ImportError:
322
print("LPIPS requires 'torchvision' package")
323
```
324
325
### Spectral Analysis
326
327
```python
328
from torchmetrics.image import SpectralAngleMapper, SpectralDistortionIndex
329
330
# Spectral quality metrics (useful for hyperspectral images)
331
sam = SpectralAngleMapper()
332
sdi = SpectralDistortionIndex()
333
334
# Multi-channel spectral images
335
preds = torch.rand(2, 64, 128, 128) # 64 spectral bands
336
target = torch.rand(2, 64, 128, 128)
337
338
# Compute spectral metrics
339
sam_score = sam(preds, target)
340
sdi_score = sdi(preds, target)
341
342
print(f"SAM: {sam_score:.4f}")
343
print(f"SDI: {sdi_score:.4f}")
344
```
345
346
### Total Variation for Smoothness
347
348
```python
349
from torchmetrics.image import TotalVariation
350
351
# Total variation (measures image smoothness)
352
tv = TotalVariation()
353
354
# Sample images
355
images = torch.rand(4, 3, 256, 256)
356
357
# Compute total variation
358
tv_score = tv(images)
359
print(f"Total Variation: {tv_score:.4f}")
360
```
361
362
### Inception Score for Generative Models
363
364
```python
365
from torchmetrics.image import InceptionScore
366
367
# Inception Score (requires torch-fidelity)
368
try:
369
inception_score = InceptionScore()
370
371
# Generated images (must be uint8 RGB)
372
generated_images = torch.randint(0, 256, (100, 3, 299, 299), dtype=torch.uint8)
373
374
# Compute IS
375
is_mean, is_std = inception_score(generated_images)
376
print(f"Inception Score: {is_mean:.4f} ± {is_std:.4f}")
377
378
except ImportError:
379
print("Inception Score requires 'torch-fidelity' package")
380
```
381
382
## Types
383
384
```python { .api }
385
from typing import Union, Optional, Tuple
386
import torch
387
from torch import Tensor
388
389
ImageTensor = Tensor # Shape: (N, C, H, W) or (C, H, W)
390
ReductionType = Union["elementwise_mean", "sum", "none"]
391
InterpolationType = Union["lerp", "slerp"]
392
```