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 Diamond Square

From Leeroopedia


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

Overview

Generates plasma fractal images using the diamond-square algorithm, producing procedural noise textures on GPU via differentiable PyTorch operations.

Description

The diamond_square module in the Kornia contrib package implements the diamond-square algorithm for generating plasma fractal images. The algorithm works by: (1) creating a small random seed image, (2) recursively doubling the resolution via alternating diamond and square interpolation steps with controlled randomness, and (3) optionally normalizing the output to a specified range. The implementation is fully differentiable and runs on GPU. Internal helper functions _diamond_square_seed and _one_diamond_one_square handle seeding and the recursive resolution-doubling step respectively.

Usage

Import this module when you need to generate procedural noise textures, terrain heightmaps, or random augmentation masks. The output can be used for data augmentation, procedural texture generation, or as input noise for generative models.

Code Reference

Source Location

Signature

def diamond_square(
    output_size: Tuple[int, int, int, int],
    roughness: Union[float, torch.Tensor] = 0.5,
    random_scale: Union[float, torch.Tensor] = 1.0,
    random_fn: Callable[..., torch.Tensor] = torch.rand,
    normalize_range: Optional[Tuple[float, float]] = None,
    device: Optional[torch.device] = None,
    dtype: Optional[torch.dtype] = None,
) -> torch.Tensor: ...

Import

from kornia.contrib import diamond_square

I/O Contract

Inputs

Name Type Required Description
output_size Tuple[int, int, int, int] Yes Desired output shape as (B, C, H, W)
roughness float or torch.Tensor No Scale factor applied at each recursion step controlling fractal roughness (default: 0.5)
random_scale float or torch.Tensor No Initial randomness scale for the recursion (default: 1.0)
random_fn Callable No Function to sample random tensors (default: torch.rand)
normalize_range Tuple[float, float] or None No If provided, min-max normalizes output to this range
device torch.device No Device to place the output tensor
dtype torch.dtype No Data type of the output tensor

Outputs

Name Type Description
fractal_image torch.Tensor Plasma fractal image with shape (B, C, H, W)

Usage Examples

import torch
from kornia.contrib import diamond_square

# Generate a batch of plasma fractal images
fractal = diamond_square(
    output_size=(4, 1, 256, 256),
    roughness=0.5,
    random_scale=1.0,
    normalize_range=(0.0, 1.0),
)
print(fractal.shape)  # torch.Size([4, 1, 256, 256])
print(fractal.min(), fractal.max())  # approximately 0.0 and 1.0

# Generate on GPU with specific dtype
fractal_gpu = diamond_square(
    output_size=(1, 3, 512, 512),
    roughness=0.7,
    device=torch.device("cuda"),
    dtype=torch.float32,
    normalize_range=(0.0, 1.0),
)

Related Pages

Page Connections

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