PyTorch Toolbox for Image Quality Assessment

0.1.15.post2 · active · verified Mon Apr 13

pyiqa is a comprehensive PyTorch-based toolbox for Image Quality Assessment (IQA), offering reimplementations of numerous mainstream full-reference (FR) and no-reference (NR) metrics. It aims for GPU acceleration, often outperforming MATLAB counterparts, and provides calibrated results against official scripts where available. The library is actively maintained with frequent releases, adding new metrics, features, and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to list available metrics, create both Full-Reference (FR) and No-Reference (NR) IQA models, perform inference with dummy PyTorch tensors, and use a metric (like LPIPS) as a loss function with gradient propagation.

import pyiqa
import torch

# List all available metrics
print("Available metrics:", pyiqa.list_models())

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Create a Full-Reference (FR) metric (e.g., LPIPS)
# For gradient propagation (e.g., as a loss function), set as_loss=True
iqa_fr_metric = pyiqa.create_metric('lpips', device=device, as_loss=True)
print(f"LPIPS metric ('lower_better'): {iqa_fr_metric.lower_better}")

# Create a No-Reference (NR) metric (e.g., NIQE)
iqa_nr_metric = pyiqa.create_metric('niqe', device=device)
print(f"NIQE metric ('lower_better'): {iqa_nr_metric.lower_better}")

# Dummy image tensors (Batch, Channels, Height, Width), RGB, 0~1 range
img1 = torch.rand(1, 3, 256, 256).to(device)
img2 = torch.rand(1, 3, 256, 256).to(device)

# Compute FR score
score_fr = iqa_fr_metric(img1, img2)
print(f"LPIPS score: {score_fr.item():.4f}")

# Compute NR score
score_nr = iqa_nr_metric(img1)
print(f"NIQE score: {score_nr.item():.4f}")

# Example of using a metric as a loss function (requires as_loss=True)
loss = score_fr # LPIPS is lower_better, directly usable as loss
loss.backward() # Gradients will be computed
print("Backward pass complete for LPIPS loss.")

view raw JSON →