Sharing Key Semantics in Transformer Makes Efficient Image Restoration

Bin Ren, Yawei Li, Jingyun Liang, Rakesh Ranjan, Mengyuan Liu, Rita Cucchiara, Luc Van Gool, Ming-Hsuan Yang, Nicu Sebe

Vision Transformers (ViTs) have taken the computer vision world by storm, but they come with a "computational tax." In the realm of Image Restoration (IR)—tasks like deblurring or super-resolution—this tax is particularly heavy. A new paper introduces SemanIR, a method that slashes this cost by sharing "key semantics" across the network.

TL;DR

SemanIR improves image restoration efficiency by creating a sparse "key-semantic dictionary" that identifies only the most relevant patches for attention. By sharing this dictionary across multiple transformer blocks within a stage, the model achieves state-of-the-art performance across six restoration tasks with linear computational complexity.

What Problem Does This Paper Address?

Image restoration is the art of recovering a clean image from a degraded one (e.g., removing noise, blur, or compression artifacts). While Vision Transformers are excellent at capturing global context, their core mechanism—Self-Attention—is notoriously "inclusive." It treats every patch of an image as potentially relevant to every other patch.

Comparison of patch selection strategies

This inclusivity leads to two major headaches:

  1. Inefficiency: Calculating relationships between every single pixel or patch results in quadratic complexity. As image resolution goes up, the math explodes.
  2. Semantic Noise: For restoration, a patch of "grass" in the top left is rarely helpful for restoring a "dog" in the bottom right. Processing these unrelated regions adds unnecessary noise to the reconstruction process.

Methods

To address these challenges, the authors propose a "SemanIR Stage" that fundamentally changes how attention is calculated. Instead of the standard dense attention, they use a two-step approach:

The architecture of SemanIR
  1. Key-Semantic Dictionary Construction: At the beginning of each transformer stage, the model uses a K-Nearest Neighbors (KNN) algorithm to find the top-$k$ most semantically similar patches for every degraded patch. This creates a sparse map of "who should talk to whom."
  2. Semantic Sharing: Rather than re-calculating these expensive KNN relationships at every single layer, SemanIR calculates them once at the start of a stage and shares that dictionary across all subsequent blocks in that stage.
  3. Efficient Implementation: To make this work in practice, the team developed custom GPU kernels using Triton. They also explored torch-mask and torch-gather implementations to balance memory footprint and speed.

By focusing only on the top-$k$ most relevant connections, the attention mechanism moves from quadratic to linear computational complexity within each window.

Datasets

The researchers evaluated SemanIR across a massive battery of benchmarks:

  • Training: DIV2K, Flickr2K, WED, and BSD400.
  • Testing (6 Tasks):
    • Deblurring: GoPro, HIDE.
  • JPEG Artifact Removal: LIVE1, BSD500, Urban100.
  • Denoising: CBSD68, Kodak24, McMaster, Urban100.
  • Super-Resolution (SR): Set5, Set14, BSD100, Urban100, Manga109.
  • Adverse Weather (Rain/Fog/Snow): Test1, SnowTest100K, RainDrop.
  • Demosaicking: Kodak, McMaster.

Key Insights and Ideas

  • The "Sharing" Secret: The most surprising finding is that you don't need to update semantic connections at every layer. Because transformer layers within a stage operate at the same semantic level, the "dictionary" remains valid, significantly reducing the overhead of KNN.
  • Sparse is Better: The results suggest that "less is more." By excluding semantically unrelated patches, the model avoids the "side effects" of irrelevant information, leading to cleaner reconstructions.
  • Random Top-k Training: During training, the authors found that randomly selecting $k$ (the number of neighbors) from a range (e.g., 64 to 512) makes the model more robust and flexible during inference than using a fixed $k$.
  • Linear Complexity: Achieving linear complexity in attention is a "holy grail" for high-resolution image processing, and SemanIR demonstrates a practical path to get there without sacrificing quality.

Key Results

SemanIR achieved State-of-the-Art (SOTA) or runner-up performance across all six tasks:

  • Deblurring: On the GoPro dataset, SemanIR outperformed the previous leader (Restormer) by 0.52 dB in PSNR.
  • Efficiency: In 4x Super-Resolution, the "SemanIR-S" (small) model used 41.7% fewer parameters than the popular HAT model while maintaining higher speeds and competitive accuracy.
  • Generalization: A single SemanIR model trained on multiple degradation levels (e.g., different noise levels) outperformed specialized models, proving its high generalization ability.
  • Qualitative: Visual results show that SemanIR is particularly good at recovering sharp edges and fine textures (like hair or fabric) that other models often over-smooth.

Limitations and Future Work

Despite the impressive results, the authors acknowledge a few hurdles:

  • Task Specificity: Currently, each restoration task (e.g., denoising vs. deblurring) requires training a separate network. A "universal" restoration model is still a future goal.
  • Parameter Budget: While efficient in FLOPs (computation), the model still requires tens of millions of parameters, which might be a squeeze for low-end mobile devices.
  • Future Directions: The authors suggest exploring "learnable $k$ values," where the model itself decides how many neighbors it needs for a specific patch, potentially pushing efficiency even further.

Check out the code and models: https://github.com/Amazingren/SemanIR

Summary updated

BibTeX
@inproceedings{ren2024sharing,
  author = {Bin Ren and Yawei Li and Jingyun Liang and Rakesh Ranjan and Mengyuan Liu and Rita Cucchiara and Luc Van Gool and Ming-Hsuan Yang and Nicu Sebe},
  title = {{Sharing Key Semantics in Transformer Makes Efficient Image Restoration}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=pebP89l4v6},
}
Figure