PyTorch Image Quality (PIQ)

0.8.0 · active · verified Thu Apr 16

PIQ (PyTorch Image Quality) is a collection of measures and metrics for automatic image quality assessment in image-to-image tasks such as denoising, super-resolution, and image generation. Currently at version 0.8.0, the library offers both functional interfaces for calculating metrics and PyTorch modules for using them as loss functions. It has a regular release cadence, with minor versions released every 1-2 months, continually extending its set of measures and metrics.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to calculate the Structural Similarity Index (SSIM) using both the functional interface to get a metric value and the class-based interface to use it as a loss function for gradient computation. Input tensors `x` and `y` are random PyTorch tensors, typically representing predicted and target images. The `data_range` parameter is crucial and should match the actual pixel value range of the input images (e.g., `1.0` for images in `[0, 1]` or `255.0` for images in `[0, 255]`).

import torch
from piq import ssim, SSIMLoss

# Example tensors (batch_size, channels, height, width)
x = torch.rand(4, 3, 256, 256, requires_grad=True)
y = torch.rand(4, 3, 256, 256)

# 1. Functional interface: Compute SSIM as a measure
ssim_index = ssim(x, y, data_range=1.)
print(f"SSIM index: {ssim_index.item():0.4f}")

# 2. Class interface: Use SSIM as a loss function
loss_fn = SSIMLoss(data_range=1.)
output_loss = loss_fn(x, y)
output_loss.backward() # Backpropagate the loss
print(f"SSIM Loss: {output_loss.item():0.4f}")

view raw JSON →