Implementation:Kornia Kornia PSNR Loss
| Knowledge Sources | |
|---|---|
| Domains | Vision, Loss_Functions |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
PSNR Loss computes the negative Peak Signal-to-Noise Ratio as a loss function for image quality assessment and optimization.
Description
Peak Signal-to-Noise Ratio (PSNR) is a widely used metric for measuring image quality, defined as the ratio between the maximum possible power of a signal and the power of corrupting noise. The PSNR loss simply negates the PSNR value so it can be minimized during training:
Where PSNR is computed as:
Failed to parse (syntax error): {\displaystyle \text{PSNR}(x, y) = 10 \cdot \log_{10}\left(\frac{\text{max\_val}^2}{\text{MSE}(x, y)}\right)}
The loss delegates to the `kornia.metrics.psnr` function for the actual PSNR computation and negates the result. Higher PSNR values indicate better image quality, so minimizing the negated PSNR maximizes quality.
Usage
Import this loss for image reconstruction and restoration tasks where PSNR is the target evaluation metric. It is commonly used in super-resolution, denoising, and compression tasks.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/losses/psnr.py
- Lines: 1-86
Signature
def psnr_loss(
image: torch.Tensor,
target: torch.Tensor,
max_val: float,
) -> torch.Tensor: ...
class PSNRLoss(nn.Module):
def __init__(self, max_val: float) -> None: ...
def forward(self, image: torch.Tensor, target: torch.Tensor) -> torch.Tensor: ...
Import
from kornia.losses import PSNRLoss
from kornia.losses import psnr_loss
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| max_val | float | Yes | The maximum value in the image tensor (e.g., 1.0 for normalized images, 255.0 for uint8) |
| image | torch.Tensor | Yes | Input image tensor with arbitrary shape (*) |
| target | torch.Tensor | Yes | Target image tensor with the same shape as image |
Outputs
| Name | Type | Description |
|---|---|---|
| loss | torch.Tensor | Scalar negative PSNR value (more negative = better quality) |
Usage Examples
import torch
from kornia.losses import PSNRLoss
# Create sample tensors (normalized to [0, 1])
ones = torch.ones(1)
target = 1.2 * ones
# Using the module API
criterion = PSNRLoss(max_val=2.0)
loss = criterion(ones, target)
# Returns tensor(-20.0000)
# Typical usage with image batches
image = torch.rand(4, 3, 64, 64, requires_grad=True)
target_img = torch.rand(4, 3, 64, 64)
criterion = PSNRLoss(max_val=1.0)
loss = criterion(image, target_img)
loss.backward()