# How the BSRGAN Module Works for Image Degradation and Super-Resolution in Stable Diffusion

> Discover how the BSRGAN module in Stable Diffusion creates realistic degraded images for blind super-resolution training by applying random blurs, downsampling, noise, and compression.

- Repository: [CompVis - Computer Vision and Learning LMU Munich/stable-diffusion](https://github.com/CompVis/stable-diffusion)
- Tags: deep-dive
- Published: 2026-03-02

---

**The BSRGAN module in Stable Diffusion is a stochastic degradation pipeline that synthesizes realistic low-quality images from high-quality inputs by applying randomized sequences of blur, downsampling, noise, JPEG compression, and optional ISP processing, enabling the diffusion model to learn blind super-resolution by reconstructing the original high-quality images.**

The BSRGAN module provides the foundation for blind super-resolution training in the CompVis/stable-diffusion repository. Located in [`ldm/modules/image_degradation/bsrgan.py`](https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/image_degradation/bsrgan.py), this degradation engine transforms clean high-resolution images into realistic low-quality counterparts, allowing diffusion models to learn robust upscaling across diverse real-world artifacts.

## BSRGAN Module Architecture and Degradation Pipeline

### The Stochastic Degradation Chain

The core entry point `degradation_bsrgan()` implements a randomized seven-block degradation sequence. The pipeline processes high-quality images through **mod-crop alignment**, **randomized blur kernels**, **multi-stage downsampling**, **Gaussian noise injection**, **JPEG compression artifacts**, and optional **camera ISP simulation** to produce training pairs.

### Stage-by-Stage Breakdown

1. **Mod-crop and Scaling**: Ensures input dimensions are multiples of the scale factor `sf` and applies optional preliminary 2× downsampling with 25% probability when `sf=4`.

2. **Randomized Operation Order**: Generates a permutation of seven degradation blocks while forcing downsample operations to execute last.

3. **Blur Kernels**: Applies anisotropic or isotropic Gaussian blur scaled by the current `sf` using `add_blur()`.

4. **Kernel-based Downsampling**: Implements either random scaling via OpenCV or kernel-based blur followed by nearest-pixel subsampling.

5. **Final Resize**: Uses `cv2.resize` with random interpolation to reach target low-resolution dimensions.

6. **Noise Injection**: Adds color or grayscale Gaussian noise with amplitudes sampled from [2, 8] via `add_Gaussian_noise()`.

7. **Compression Artifacts**: Applies JPEG compression with 90% probability at quality levels 80-95% using `add_JPEG_noise()`.

8. **ISP Simulation**: With 25% probability, processes images through a neural ISP model to simulate realistic camera pipeline artifacts.

9. **Final Compression**: Ensures consistent JPEG artifacts regardless of previous stochastic choices.

10. **Patch Extraction**: Crops aligned low-quality and high-quality patches of sizes `lq_patchsize` and `lq_patchsize·sf` respectively.

## How BSRGAN Enables Super-Resolution Training

During training, the diffusion model receives the degraded low-quality (LQ) patch and learns to reconstruct the corresponding high-quality (HQ) patch. Because `degradation_bsrgan()` covers a wide distribution of real-world degradations—including optical blur, sensor noise, and compression artifacts—the trained model becomes capable of **blind super-resolution**, handling unknown degradation kernels without explicit prior knowledge.

At inference time, feeding a low-resolution image into the diffusion sampler with the appropriate scale factor triggers the inverse process: the model denoises, deblurs, and upsamples to produce high-resolution outputs.

## Implementation and Code Examples

### Basic Usage: Generating Degraded Pairs

```python
import torch
from ldm.modules.image_degradation.bsrgan import degradation_bsrgan

# Load high-resolution image (numpy H×W×C in [0,1])

hq = util.imread_uint('image.png', 3) / 255.0

# Generate degraded pair for 4× super-resolution

lq, hq_aligned = degradation_bsrgan(
    hq, 
    sf=4, 
    lq_patchsize=72, 
    isp_model=None
)

```

### Lightweight Variant Without ISP

```python
from ldm.modules.image_degradation.bsrgan_light import degradation_bsrgan_variant

# Create degradation function for 4× scaling

degrade_fn = lambda img: degradation_bsrgan_variant(img, sf=4)

# Returns dictionary with degraded image

result = degrade_fn(hq)
lq_patch = result['image']

```

### Training Loop Integration

```python
for batch in dataloader:
    hq = batch['image']  # numpy array [0,1]

    
    # Apply BSRGAN degradation

    lq, hq_target = degradation_bsrgan(
        hq, 
        sf=4, 
        lq_patchsize=72
    )
    
    # Convert to tensors

    lq_t = torch.from_numpy(lq).permute(2,0,1).unsqueeze(0).float()
    hq_t = torch.from_numpy(hq_target).permute(2,0,1).unsqueeze(0).float()
    
    # Diffusion training step

    loss = diffusion_model(lq_t, hq_t, scale=4)
    loss.backward()
    optimizer.step()

```

## Key Source Files and Functions

- **[`ldm/modules/image_degradation/bsrgan.py`](https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/image_degradation/bsrgan.py)**: Contains the full `degradation_bsrgan()` implementation with ISP support.
- **[`ldm/modules/image_degradation/bsrgan_light.py`](https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/image_degradation/bsrgan_light.py)**: Lightweight variant `degradation_bsrgan_variant()` omitting ISP processing.
- **[`ldm/modules/image_degradation/utils_image.py`](https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/image_degradation/utils_image.py)**: Helper utilities including `imresize_np` and `imread_uint`.
- **[`ldm/modules/image_degradation/__init__.py`](https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/image_degradation/__init__.py)**: Exposes `degradation_fn_bsr` and `degradation_fn_bsr_light` to the broader codebase.

## Summary

- The BSRGAN module applies a **randomized chain of seven degradation operations** to synthesize realistic low-quality images from high-quality sources.
- Key parameters include the **scale factor (`sf`)**, **patch size (`lq_patchsize`)**, and optional **ISP model** for camera simulation.
- The pipeline is implemented in [`ldm/modules/image_degradation/bsrgan.py`](https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/image_degradation/bsrgan.py) with a lightweight alternative available in [`bsrgan_light.py`](https://github.com/CompVis/stable-diffusion/blob/main/bsrgan_light.py).
- By training on BSRGAN-degraded pairs, Stable Diffusion learns **blind super-resolution** capable of handling diverse real-world image degradation without explicit kernel estimation.

## Frequently Asked Questions

### What is the difference between `degradation_bsrgan()` and `degradation_bsrgan_variant()`?

The full `degradation_bsrgan()` function includes optional camera ISP simulation and broader degradation parameters for comprehensive training scenarios. In contrast, `degradation_bsrgan_variant()` (located in [`bsrgan_light.py`](https://github.com/CompVis/stable-diffusion/blob/main/bsrgan_light.py)) omits the ISP block and uses simplified noise ranges, making it suitable for faster experimentation and lighter computational requirements.

### How does the scale factor (`sf`) parameter affect the degradation process?

The `sf` parameter determines the upscaling target during super-resolution training and influences blur kernel sizes and cropping dimensions. When `sf=4`, the pipeline optionally applies preliminary 2× downsampling with 25% probability, and all blur operations scale their kernel sizes proportionally to maintain realistic degradation relative to the final resolution.

### Can I use the BSRGAN module for degrading images without training a diffusion model?

Yes, the BSRGAN module functions as a standalone degradation engine. Import `degradation_bsrgan` from `ldm.modules.image_degradation.bsrgan` and apply it to any high-resolution numpy array to generate realistic low-quality versions suitable for benchmarking super-resolution algorithms or creating synthetic training data.

### What is the purpose of the ISP model in the BSRGAN pipeline?

The optional ISP (Image Signal Processor) model simulates realistic camera pipeline artifacts including demosaicing errors, tone mapping, and sensor-specific noise characteristics. When provided and enabled with `isp_prob=0.25`, it processes the degraded image through `isp_model.forward()` to add authenticity mimicking real camera sensor outputs, particularly valuable for training models on smartphone or DSLR-specific degradations.