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 Normalize

From Leeroopedia


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

Overview

This module provides functions and nn.Module classes for normalizing and denormalizing image tensors using mean/standard deviation, as well as min-max normalization.

Description

normalize.py is part of the kornia.enhance module in the Kornia computer vision library. It implements intensity normalization operations commonly used in deep learning image preprocessing pipelines. The module contains:

  • normalize / Normalize -- per-channel normalization by subtracting mean and dividing by standard deviation, following the formula: input[channel] = (input[channel] - mean[channel]) / std[channel]
  • denormalize / Denormalize -- the inverse operation: input[channel] = input[channel] * std[channel] + mean[channel]
  • normalize_min_max -- rescales tensor values to a specified range [min_val, max_val] using min-max normalization

All functions support broadcasting on the channel dimension and are compatible with ONNX export. The Normalize and Denormalize classes accept mean/std as floats, tuples, lists, or tensors.

Usage

Users should import from this module when they need to normalize images for model input (e.g., ImageNet normalization with mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225]), denormalize model outputs for visualization, or rescale tensor values to a specific range.

Code Reference

Source Location

Signature

class Normalize(nn.Module):
    def __init__(
        self,
        mean: Union[torch.Tensor, Tuple[float], List[float], float],
        std: Union[torch.Tensor, Tuple[float], List[float], float],
    ) -> None
    def forward(self, input: torch.Tensor) -> torch.Tensor

def normalize(data: torch.Tensor, mean: torch.Tensor, std: torch.Tensor) -> torch.Tensor

class Denormalize(nn.Module):
    def __init__(self, mean: Union[torch.Tensor, float], std: Union[torch.Tensor, float]) -> None
    def forward(self, input: torch.Tensor) -> torch.Tensor

def denormalize(data: torch.Tensor, mean: Union[torch.Tensor, float], std: Union[torch.Tensor, float]) -> torch.Tensor

def normalize_min_max(x: torch.Tensor, min_val: float = 0.0, max_val: float = 1.0, eps: float = 1e-6) -> torch.Tensor

Import

from kornia.enhance import normalize, denormalize, normalize_min_max
from kornia.enhance import Normalize, Denormalize

I/O Contract

Inputs (normalize)

Name Type Required Description
data torch.Tensor Yes Image tensor of size (B, C, *)
mean Union[torch.Tensor, Tuple[float], List[float], float] Yes Mean for each channel
std Union[torch.Tensor, Tuple[float], List[float], float] Yes Standard deviation for each channel

Inputs (normalize_min_max)

Name Type Required Description
x torch.Tensor Yes Image tensor with shape (*, C, H, W)
min_val float No Minimum value for the new range. Default: 0.0
max_val float No Maximum value for the new range. Default: 1.0
eps float No Small number for numerical stability. Default: 1e-6

Outputs

Name Type Description
output torch.Tensor Normalized (or denormalized) tensor with the same shape as input

Usage Examples

import torch
from kornia.enhance import normalize, denormalize, normalize_min_max, Normalize

# Normalize with scalar mean and std
x = torch.rand(1, 4, 3, 3)
out = normalize(x, torch.tensor([0.0]), torch.tensor([255.0]))

# Normalize with per-channel mean and std (e.g., ImageNet)
x = torch.rand(1, 3, 224, 224)
mean = torch.tensor([0.485, 0.456, 0.406])
std = torch.tensor([0.229, 0.224, 0.225])
out = normalize(x, mean, std)

# Using the nn.Module wrapper
norm = Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
out = norm(x)

# Denormalize back to original range
from kornia.enhance import denormalize
out_denorm = denormalize(out, mean, std)

# Min-max normalization to [-1, 1] range
x = torch.rand(1, 5, 3, 3)
x_norm = normalize_min_max(x, min_val=-1.0, max_val=1.0)
# x_norm.min() == -1.0, x_norm.max() == 1.0

Related Pages

Page Connections

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