Patch-Based Stochastic Attention for Image Editing

Nicolas Cherel, Andrés Almansa, Yann Gousseau, Alasdair Newson · DOI

Abstract

Traditional attention mechanisms in neural networks suffer from quadratic computational and memory complexity, $O(n^2)$, which effectively prohibits their application to high-resolution image editing tasks. This paper discusses the Patch-based Stochastic Attention Layer (PSAL), a novel approach that leverages the PatchMatch algorithm to reduce complexity to linear $O(n)$. By treating attention as an approximate nearest neighbor (ANN) search, PSAL enables high-fidelity image processing—such as 4K-style inpainting—on consumer-grade hardware, achieving significant reductions in memory usage and processing time without sacrificing visual quality.

Introduction

Attention mechanisms have become a cornerstone of modern computer vision, providing models with the ability to capture long-range dependencies across an image. In tasks like image inpainting or colorization, this global context is essential for maintaining structural and textural consistency. For instance, when filling a missing region in a repetitive pattern, the model must "attend" to distant pixels to replicate the correct texture.

However, the standard "Full Attention" mechanism requires comparing every pixel to every other pixel. As image resolution increases, the memory requirements for the resulting attention matrix grow quadratically. A $512 \times 512$ image requires approximately 250 GB of VRAM for the attention matrix alone, far exceeding the capacity of modern GPUs. Consequently, researchers have historically been forced to use local windows or downsampled feature maps, which often introduce artifacts or blurriness. PSAL addresses this bottleneck by introducing a stochastic, patch-based search strategy.

Methodology: Patch-based Stochastic Attention (PSAL)

The core innovation of PSAL is the realization that the softmax-based attention mechanism is essentially a "soft" nearest neighbor search. Rather than exhaustively computing all possible pixel pairings, PSAL utilizes the PatchMatch algorithm to find approximate matches efficiently.

1. PatchMatch Integration

PatchMatch operates on the principle of spatial regularity: if a patch at one location is a good match for a target, its spatial neighbors are likely to have good matches nearby as well. PSAL initializes with random guesses and then uses a propagation step to spread high-quality matches across the image. This allows the layer to identify relevant context across the entire image field without computing the full matrix.

2. Achieving Differentiability

Standard PatchMatch is non-differentiable because it typically uses an argmax operation to select the single best match. To integrate this into a deep learning framework that requires backpropagation, the authors implemented two key strategies:

  • k-Nearest Neighbors (k-NN): Instead of selecting a single match, the layer identifies the top $k$ matches and applies a softmax function to create a "soft" blend of features.
  • Patch Aggregation: The candidate list for each pixel is enriched by including matches found by its spatial neighbors, ensuring a robust and differentiable gradient flow.

3. Linear Complexity

By replacing the exhaustive search with this stochastic approximation, PSAL achieves $O(n)$ complexity. This linear scaling ensures that memory usage grows proportionally with the number of pixels, making high-resolution processing feasible.

Experimental Evaluation

The researchers evaluated PSAL across several benchmarks to test its versatility in image editing and reconstruction:

  • DAVIS: Used for guided image colorization, transferring color information between video frames.
  • Places2: Utilized for large-scale image inpainting and hole-filling validation.
  • Urban 100: A dataset of architectural images used to evaluate single-image super-resolution.
  • Video Frames: Custom datasets were used to assess the layer's general reconstruction capabilities.

Key Results and Insights

Efficiency and Scalability

The most striking result of PSAL is its memory efficiency. For a $512 \times 512$ image, PSAL requires only 0.18 GB of memory, compared to the 250.04 GB required by Full Attention. This efficiency allowed the authors to perform inpainting on a $3300 \times 2700$ image using a single NVIDIA GTX 1080 Ti (11 GB VRAM)—a task that would theoretically require over 1,000 GB of VRAM using standard methods.

Plug-and-Play Performance

PSAL is highly compatible with existing architectures. The authors demonstrated that a model pre-trained with Full Attention could have its attention layers swapped for PSAL at inference time. This swap resulted in a 40x speedup and a 225x reduction in memory usage with negligible impact on output quality.

Architectural Flexibility

Because PSAL is computationally lightweight, it can be integrated into the "shallower" layers of a network where feature maps remain at high resolution. Traditional attention is usually restricted to the deepest, most downsampled layers, which often results in the loss of fine-grained details. PSAL preserves these details, leading to crisper textures in the final edits.

Limitations and Future Work

While PSAL represents a significant leap in efficiency, it relies on the spatial regularity inherent in images and video. Consequently, it is less suited for non-spatial data, such as natural language or financial time series, where "neighboring" elements do not necessarily share contextual relevance. Additionally, as a stochastic approximation, there is a theoretical possibility of missing an optimal match, though empirical results suggest this does not meaningfully degrade visual quality. Future research will focus on designing end-to-end architectures that are natively optimized for the linear scaling provided by PSAL.

Conclusion

PSAL effectively bridges the gap between high-resolution image processing and the computational constraints of modern hardware. By reframing attention as a stochastic nearest neighbor problem, it enables the use of global context in high-res editing tasks that were previously considered computationally impossible.

Summary updated

BibTeX
@article{cherel2022patch,
  author = {Nicolas Cherel and Andrés Almansa and Yann Gousseau and Alasdair Newson},
  title = {{Patch-Based Stochastic Attention for Image Editing}},
  journal = {Comput. Vis. Image Underst.},
  year = {2022},
  doi = {10.1016/j.cviu.2023.103866},
}
Figure