An open API service indexing awesome lists of open source software.

https://github.com/wasasquatch/was_affine

Apply AFFINE transforms to latent space to improve image quality, especially with light loras
https://github.com/wasasquatch/was_affine

Last synced: 11 months ago
JSON representation

Apply AFFINE transforms to latent space to improve image quality, especially with light loras

Awesome Lists containing this project

README

          

![Affine Logo](was_affine.png)

# AFFINE
## Adaptive Field Filtering by Intermittent Noise Enhancement

**AFFINE** is a comprehensive ComfyUI node suite that performs sparse **Adaptive Field Filtering by Intermittent Noise Enhancement** in latent space to steer diffusion results during sampling.
It works by applying controlled scale and bias (`z' = scale * z + bias`) to masked regions of the latent tensor, enabling precise control over brightness, contrast, and detail enhancement without collapsing noise structure.

The suite includes:
- **Core Affine Nodes** - Basic latent space transformations
- **Integrated Samplers** - KSampler and Custom Sampling with built-in affine scheduling
- **Ultimate SD Upscaler Ports** - Tiled upscaling with affine enhancement
- **Pattern Noise Generation** - AFFINE augmented random noise for custom sampling
- **Modular Options System** - Granular control over all parameters (also a super options node for the adventurous)

### Example
Here we use some aggressive settings to give a neutral cinematic look without hard contrast.
- [Affine WAN Example](https://youtu.be/2IgdGCjVCcg)
- [Affine USDU 720p -> 2k Example](https://www.youtube.com/watch?v=-VsFb6-6xog) (Nevermind the base generation, was playing with I2T2V experiment. <.<)

**Things to take note of:**
- The background rock quality is improved.
- The trees and brush look more naturally dispersed.
- Roads are more realistic
- The foreground detail is *essentially* better preserved.
- The overall contrast is reduced.

---

## ๐Ÿง  How it works (ELI5)

- **Controlled enhancement**: Apply scale and bias transforms to specific regions defined by procedural masks.
- **Pattern-based masking**: Choose from 20+ patterns including Perlin noise, spectral noise types, geometric patterns, and content-aware masks.
- **Temporal consistency**: Static or per-frame mask generation for stable video processing.
- **External gating**: Use custom mask images to limit where effects are applied.
- **Modular configuration**: Mix and match common options with pattern-specific parameters.
- **Integrated workflows**: Built-in samplers handle scheduling automatically, or use manual latent transforms between sampling passes.

The core transformation: `z' = scale * z + bias` where the mask determines the blend between original and transformed values.

---

## โœจ Why AFFINE?

- **Improves Quality** โ€“ Enhance detail in light LoRA outputs or tame overly contrasted/burned results from speed-boosting LoRAs.
- **Works in latent space** โ€“ Avoids costly VAE decode/encode cycles and raster-space artifacts.
- **Sparse control** โ€“ Apply adjustments selectively using sophisticated mask patterns.
- **Stable video** โ€“ Temporal modes maintain consistency or controlled variation across frames.
- **Flexible integration** โ€“ Use standalone transforms, integrated samplers, or tiled upscaling workflows.
- **Advanced patterns** โ€“ 20+ mask types from simple geometric to complex spectral and content-aware patterns.
- **Ultimate SD Upscaler compatibility** โ€“ Tiled processing with world-aligned noise and affine enhancement.

---

## ๐Ÿ”ง Workflow Options

### Option 1: Manual Latent Transform (Classic)
1. Run **KSampler Advanced** or **Custom Sampling** for high-noise steps
2. Apply **Latent Affine** or **Latent Affine Simple**
3. Resume with second sampler for low-noise steps

### Option 2: Integrated Samplers
- Use **KSampler Affine** or **KSampler Affine Advanced** for automatic scheduling
- Use **Custom Sampler Affine Advanced** for custom sampling workflows
- Configure affine schedule, interval, and pattern parameters directly

### Option 3: Tiled Upscaling
- Use **Ultimate Affine KSampler - USDU** nodes for large image processing
- Combines upscaling, tiling, and affine enhancement in one step
- Supports custom samplers and sigma schedules
-
---

## ๐Ÿ“‹ Node Reference

### Core Affine Nodes

#### Latent Affine
The main affine transformation node with full configurability.

**Inputs:**
- `latent` โ€“ Input latent tensor
- `scale` โ€“ Multiplicative factor (1.0 = no change, <1 darkens, >1 brightens)
- `bias` โ€“ Additive offset (-2.0 to 2.0)
- `pattern` โ€“ Mask pattern (20+ options including procedural, spectral, and content-aware)
- `temporal_mode` โ€“ `static` or `per_frame` for video
- `seed` โ€“ Random seed for procedural patterns
- `external_mask` โ€“ Optional IMAGE input for custom masking
- `options` โ€“ Base options DICT (use WASLatentAffineCommonOptions)
- `noise_options` โ€“ Pattern-specific options DICT

**Returns:**
- `latent` โ€“ Transformed latent
- `mask` โ€“ Generated mask for visualization

#### Latent Affine Simple
Streamlined version with auto-tuned parameters for quick results.

**Inputs:**
- `latent` โ€“ Input latent
- `scale` โ€“ Multiplicative gain (0.95-0.98 for subtle darkening)
- `noise_pattern` โ€“ Auto-tuned mask pattern
- `seed` โ€“ Random seed
- `temporal_mode` โ€“ `static` or `per_frame`
- `frame_seed_stride` โ€“ Seed increment per frame

**Returns:**
- `latent` โ€“ Adjusted latent
- `mask` โ€“ Generated mask

### Integrated Samplers

#### KSampler Affine / KSampler Affine Advanced
Drop-in replacements for standard KSamplers with built-in affine scheduling.

**Key Parameters:**
- `affine_interval` โ€“ Apply affine every N steps
- `max_scale` / `max_bias` โ€“ Peak transformation values
- `affine_schedule` โ€“ WASAffineScheduleOptions DICT for timing curve
- `affine_seed` / `affine_seed_increment` โ€“ Seed management
- All standard KSampler parameters

#### Custom Sampler Affine Advanced
Advanced custom sampling with affine integration.

**Inputs:**
- `noise` / `guider` / `sampler` / `sigmas` โ€“ Standard custom sampling inputs
- `latent_image` โ€“ Input latent
- Affine parameters (same as KSampler variants)

**Returns:**
- `output` โ€“ Final latent
- `denoised_output` โ€“ Denoised latent

### Ultimate SD Upscaler Ports

#### Ultimate Affine KSampler - USDU
Full upscaling with model-based upscaling and affine enhancement.

**Key Features:**
- `upscale_model` / `upscale_factor` โ€“ Upscaling configuration
- Tiling parameters (`tile_width`, `tile_height`, `tile_padding`)
- Seam fixing options
- Full affine parameter set
- World-aligned noise for consistent tiling

#### Ultimate Affine KSampler (No Upscale) - USDU
Tiled processing without initial upscaling.

#### Ultimate Affine KSampler (Custom) - USDU
Supports custom samplers and sigma schedules.

### Pattern Noise Generation

#### Affine Pattern Noise
Generates structured noise by augmenting base ComfyUI noise with procedural patterns.

**Inputs:**
- `pattern` โ€“ Noise pattern type
- `seed` โ€“ Random seed
- `affine_scale` โ€“ Pattern amplitude multiplier
- `normalize` โ€“ Center and scale pattern
- `affine_bias` โ€“ Additive bias
- `options` / `noise_options` โ€“ Pattern parameters

**Returns:**
- `noise` โ€“ Structured noise generator

---

## ๐ŸŽจ Available Patterns

### Spectral Noise Types
- **white_noise** โ€“ Uniform frequency spectrum
- **pink_noise** โ€“ 1/f frequency falloff (natural)
- **brown_noise** / **red_noise** โ€“ 1/fยฒ falloff (warmer)
- **blue_noise** โ€“ High-frequency emphasis
- **violet_noise** / **purple_noise** โ€“ fยฒ emphasis (harsh)
- **green_noise** โ€“ Mid-frequency band-pass
- **black_noise** โ€“ Sparse narrowband spectrum

### Geometric Patterns
- **checker** โ€“ Checkerboard tiles
- **bayer** โ€“ Ordered dithering matrix
- **solid** โ€“ Constant alpha mask

### Procedural Patterns
- **perlin** โ€“ Smooth fractal noise (organic)
- **worley_edges** โ€“ Cellular noise emphasizing edges
- **poisson_blue_mask** โ€“ Blue-noise Poisson-disk distance field
- **cross_hatch** โ€“ Oriented gratings and cross-hatch
- **tile_oriented_lines** โ€“ Per-tile oriented lines
- **dot_screen_jitter** โ€“ Halftone dots with jitter
- **velvet_noise** โ€“ Sparse high-frequency impulses

### Frequency Domain
- **ring_noise** โ€“ Narrow annulus in frequency domain
- **highpass_white** โ€“ High-pass filtered white noise

### Content-Aware (from latent)
- **detail_region** โ€“ High texture/variance areas
- **smooth_region** โ€“ Low detail areas
- **edges_sobel** โ€“ Sobel edge detection
- **edges_laplacian** โ€“ Laplacian edge detection

### External
- **external_mask** โ€“ Use provided IMAGE directly

---

## ๐Ÿ”ง Options System

### Common Options (WASLatentAffineCommonOptions)
Base parameters that apply to all patterns:

- **mask_strength** โ€“ Scales mask intensity (0.0-2.0)
- **threshold** โ€“ Binarize mask if > 0 (0.0-1.0)
- **invert_mask** โ€“ Invert after threshold/blur
- **blur_ksize** / **blur_sigma** โ€“ Gaussian blur for soft edges
- **clamp** / **clamp_min** / **clamp_max** โ€“ Output value clamping
- **frame_seed_stride** โ€“ Seed increment per frame (temporal mode)
- **compute_device** โ€“ Where to generate masks (auto/cuda/cpu)
- **sharpen_enable** / **sharpen_sigma** / **sharpen_amount** โ€“ Unsharp masking

### Pattern-Specific Options
Each pattern type has dedicated option nodes:

#### WASPerlinOptions
- `perlin_scale` โ€“ Base feature size (larger = smoother)
- `perlin_octaves` โ€“ Number of octaves (1-8)
- `perlin_persistence` โ€“ Amplitude falloff per octave
- `perlin_lacunarity` โ€“ Frequency multiplier per octave

#### WASWorleyEdgesOptions
- `worley_points_per_kpx` โ€“ Cell density per 1000 pixels
- `worley_metric` โ€“ Distance metric (L2/L1)
- `worley_edge_sharpness` โ€“ Edge emphasis exponent

#### WASPoissonBlueOptions
- `poisson_radius_px` โ€“ Minimum dot spacing
- `poisson_softness` โ€“ Distance field smoothing

#### WASCrossHatchOptions
- `hatch_freq_cyc_px` โ€“ Line frequency
- `hatch_angle1_deg` / `hatch_angle2_deg` โ€“ Hatch angles
- `hatch_square` โ€“ Square wave vs sine
- `hatch_phase_jitter` โ€“ Random phase variation
- `hatch_supersample` โ€“ Anti-aliasing factor

#### WASRingNoiseOptions
- `ring_center_frac` โ€“ Ring center (fraction of Nyquist)
- `ring_bandwidth_frac` โ€“ Ring thickness

#### WASHighpassWhiteOptions
- `highpass_cutoff_frac` โ€“ Butterworth cutoff
- `highpass_order` โ€“ Filter steepness

#### WASTileLinesOptions
- `tile_line_tile_size` โ€“ Tile dimensions
- `tile_line_freq_cyc_px` โ€“ Line frequency per tile
- `tile_line_jitter` โ€“ Orientation randomness

#### WASDotScreenOptions
- `dot_cell_size` โ€“ Halftone cell size
- `dot_jitter_px` โ€“ Dot center randomness
- `dot_fill_ratio` โ€“ Coverage per cell

#### WASGreenNoiseOptions / WASBlackNoiseOptions / WASVelvetOptions
- Spectral and impulse noise parameters
- See node tooltips for detailed ranges

#### WASCheckerOptions / WASBayerOptions
- `checker_size` / `bayer_size` โ€“ Pattern scale

#### WASDetailRegionOptions / WASSmoothRegionOptions
- `content_window` โ€“ Kernel size for content analysis

### Affine Schedule Options (WASAffineScheduleOptions)
Controls timing and intensity curves for integrated samplers:

- **start** / **end** โ€“ Active range (0.0-1.0 of total steps)
- **bias** โ€“ Curve bias toward start/end
- **exponent** โ€“ Power curve shaping
- **curve** โ€“ Easing function (linear, sine, cubic, etc.)
- **start_offset** / **end_offset** โ€“ Value adjustments

**Returns:** Schedule DICT + visualization plot

---

## ๐Ÿž๏ธ External Mask Gating

Use external masks to limit where affine effects are applied:

- Connect a grayscale IMAGE to `external_mask` input
- Set `pattern` to any procedural pattern (not `external_mask`)
- The generated pattern mask is multiplied by your external mask
- Affine transforms only apply where both masks are bright

**Use Cases:**
- Apply effects only to specific objects or regions
- Combine with segmentation masks for targeted enhancement
- Create complex composite effects with multiple mask layers

---

## ๐Ÿ“‚ Installation

### Manual
1. Clone the repository to your `ComfyUI/custom_nodes` directory:
```bash
cd ComfyUI/custom_nodes
git clone https://github.com/WASasquatch/was_affine.git
```
2. Restart ComfyUI via [Manager](https://github.com/Comfy-Org/ComfyUI-Manager) or console

### Manager
1. Open [Manager](https://github.com/Comfy-Org/ComfyUI-Manager) and click "Install Custom Nodes"
2. Search "**WAS Affine**" and click install on the custom node by author WAS.
3. Restart ComfyUI via Manager's main menu.

**Dependencies:** Standard ComfyUI installation (torch, numpy). ***[Ultimate SD Upscaler](https://github.com/ssitu/ComfyUI_UltimateSDUpscale) nodes require the UltimateSDUpscale extension.***

---

## ๐Ÿงช Tips & Best Practices

### General Guidelines
- **Start subtle**: Latent space is sensitive - `scale=0.95` can be quite strong
- **Model sensitivity varies**: Some models respond more dramatically than others
- **Flux/Krea models**: May need positive scale values (inverted behavior)
- **Lightning LoRAs**: Combine with lower CFG (1-2) for best results

### Pattern Selection
- **Organic content**: perlin, worley_edges work naturally
- **Architectural/geometric**: checker, bayer, cross_hatch
- **Fine detail work**: velvet_noise, highpass_white, ring_noise
- **Content-aware**: detail_region for textures, smooth_region for skies

---

## ๐Ÿ“œ License

[MIT](LICENSE) โ€“ Free to use, modify, and share with attribution.

---

## ๐Ÿ™ Acknowledgments

- ComfyUI team for the excellent software
- Ultimate SD Upscaler and ComfyUI wrapper developers
- Community feedback and testing contributions like "Ansel" and "Lucifer".