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.
pip install piqVerified import paths — ran on the pinned version, not inferred.
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]`).
Update all import statements to use `from piq import ...`.
Update your PyTorch and torchvision installations to a version greater than `1.5.0`.
Ensure input tensors are normalized to the expected `data_range` (e.g., `[0, 1]`) or provide the correct `data_range` argument. For specific metrics, consult documentation for `allow_negative` flags or shape requirements (e.g., `multi_scale_gmsd` requires `height, width >= 2 ** number_of_scales + 1`).
Run your Python script with the `-O` flag (e.g., `python -O your_script.py`) to disable all assertions.
Ensure input tensors are normalized to the `data_range` expected by the metric (e.g., `data_range=1.0` for `[0,1]`, `data_range=255.0` for `[0,255]`). Apply an activation function (like `torch.sigmoid`) to your model's output if it produces values outside the expected positive range and the metric doesn't support negative values via a flag. Some metrics have an `allow_negative=True` flag to permit negative inputs.
Verify the shape of your input tensors. If your images are `NHWC`, use `tensor.permute(0, 3, 1, 2)` to convert them to `NCHW` before passing to PIQ functions. Also, check specific metric documentation for any minimum height/width requirements (e.g., `multi_scale_gmsd`).
Update your import statements to use the new package name: `from piq import ssim` (or other desired metrics).
Ensure that the `prediction` and `target` tensors passed to PIQ metrics have identical shapes across all dimensions (batch size, channels, height, width). Resample or crop images if necessary before comparison.