Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Kornia Kornia PSNR Metric

From Leeroopedia
Revision as of 15:22, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Kornia_Kornia_PSNR_Metric.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Vision, Metrics
Last Updated 2026-02-09 15:00 GMT

Overview

Computes the Peak Signal-to-Noise Ratio (PSNR) between two images, measuring reconstruction quality relative to a maximum possible signal value.

Description

The psnr function calculates the Peak Signal-to-Noise Ratio between two images. PSNR is defined as:

PSNR = 10 * log10(MAX_I^2 / MSE(I, T))

where MAX_I is the maximum possible pixel value (specified via the max_val parameter) and MSE is the Mean Squared Error between the input image and the target image. PSNR is measured in decibels (dB), with higher values indicating better reconstruction quality. It is one of the most widely used metrics for evaluating image reconstruction, compression, and denoising algorithms. The implementation delegates MSE computation to PyTorch's built-in torch.nn.functional.mse_loss for efficiency.

Usage

Import this metric when evaluating image reconstruction quality, such as in super-resolution, denoising, compression, or inpainting tasks. The max_val parameter should match the dynamic range of your images (e.g., 1.0 for float images normalized to [0, 1], or 255.0 for uint8 images).

Code Reference

Source Location

Signature

def psnr(
    image: torch.Tensor,
    target: torch.Tensor,
    max_val: float
) -> torch.Tensor:

Import

from kornia.metrics import psnr

I/O Contract

Inputs

Name Type Required Description
image torch.Tensor Yes The input image tensor with arbitrary shape (*). Must match the shape of target.
target torch.Tensor Yes The reference/ground truth image tensor with arbitrary shape (*). Must match the shape of image.
max_val float Yes The maximum possible value in the input tensor (e.g., 1.0 for normalized float images, 255.0 for uint8 images).

Outputs

Name Type Description
psnr_value torch.Tensor Scalar tensor containing the PSNR value in decibels (dB).

Usage Examples

import torch
from kornia.metrics import psnr

# Simple example
ones = torch.ones(1)
result = psnr(ones, 1.2 * ones, 2.0)
# result: tensor(20.0000)
# Computed as: 10 * log10(4 / ((1.2 - 1)^2)) / log(10)

# Image reconstruction evaluation
original = torch.rand(1, 3, 256, 256)  # B, C, H, W
reconstructed = original + 0.01 * torch.randn_like(original)
reconstructed = reconstructed.clamp(0, 1)
quality = psnr(reconstructed, original, max_val=1.0)
print(f"PSNR: {quality.item():.2f} dB")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment