Implementation:Kornia Kornia Image Module
| Knowledge Sources | |
|---|---|
| Domains | Vision, Core, Module_Framework |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
This module provides ImageModule and ImageSequential, base classes that extend PyTorch's nn.Module and nn.Sequential with automatic input/output type conversion, visualization, and ONNX export capabilities for image-based operations.
Description
The module file in the Kornia core package defines two classes: ImageModule and ImageSequential. Both inherit from PyTorch base classes (nn.Module and nn.Sequential respectively) and mix in ImageModuleMixIn for input/output type conversion (supporting pt, numpy, and pil output types) and ONNXExportMixin for ONNX export. The __call__ method is overridden to wrap the forward pass with a convert_input_output decorator that handles automatic conversion between data types. A disable_features property allows users to bypass the conversion overhead and restore standard PyTorch behavior. When features are enabled and output_type is "pt", the output image is detached and moved to CPU for storage.
Usage
Inherit from ImageModule when building custom Kornia operations that should accept numpy arrays or PIL images as input and produce outputs in the same format. Use ImageSequential to compose such operations in a pipeline.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/core/module.py
- Lines: 1-144
Signature
class ImageModule(nn.Module, ImageModuleMixIn, ONNXExportMixin):
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
@property
def disable_features(self) -> bool: ...
@disable_features.setter
def disable_features(self, value: bool = True) -> None: ...
def __call__(
self,
*inputs: Any,
input_names_to_handle: Optional[list[Any]] = None,
output_type: Literal["pt", "numpy", "pil"] = "pt",
**kwargs: Any,
) -> Any: ...
class ImageSequential(nn.Sequential, ImageModuleMixIn, ONNXExportMixin):
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
@property
def disable_features(self) -> bool: ...
@disable_features.setter
def disable_features(self, value: bool = True) -> None: ...
def __call__(
self,
*inputs: Any,
input_names_to_handle: Optional[list[Any]] = None,
output_type: Literal["pt", "numpy", "pil"] = "pt",
**kwargs: Any,
) -> Any: ...
Import
from kornia.core.module import ImageModule, ImageSequential
I/O Contract
__call__ Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| inputs | Any | Yes | Input data (tensors, numpy arrays, or PIL images). |
| input_names_to_handle | list[Any] or None | No | List of input names to convert; if None, handle all inputs. |
| output_type | Literal["pt", "numpy", "pil"] | No | Desired output type (default "pt"). |
| kwargs | Any | No | Additional keyword arguments passed to forward. |
Outputs
| Name | Type | Description |
|---|---|---|
| output | Any | The processed output in the requested output_type format. |
Usage Examples
import torch
from kornia.core.module import ImageModule, ImageSequential
# Using ImageModule as a base class
class MyTransform(ImageModule):
def forward(self, x):
return x * 0.5
transform = MyTransform()
# With a PyTorch tensor
img_tensor = torch.rand(1, 3, 224, 224)
result = transform(img_tensor) # returns torch.Tensor
# With numpy output
import numpy as np
result_np = transform(img_tensor, output_type="numpy")
# Disable conversion features for performance
transform.disable_features = True
result_fast = transform(img_tensor)
# Composing operations with ImageSequential
pipeline = ImageSequential(MyTransform(), MyTransform())
result = pipeline(img_tensor)