> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/xinntao/Real-ESRGAN/llms.txt
> Use this file to discover all available pages before exploring further.

# RRDBNet

> Residual in Residual Dense Block Network for super-resolution

## Overview

RRDBNet (Residual in Residual Dense Block Network) is the primary architecture used in Real-ESRGAN models. It's based on ESRGAN and features a deep network with residual connections and dense blocks for high-quality image super-resolution.

This architecture is imported from BasicSR and used in the flagship Real-ESRGAN models.

<Note>
  RRDBNet is defined in `basicsr.archs.rrdbnet_arch` and is part of the BasicSR library, which Real-ESRGAN depends on.
</Note>

## Class Definition

```python theme={null}
from basicsr.archs.rrdbnet_arch import RRDBNet

model = RRDBNet(
    num_in_ch=3,
    num_out_ch=3,
    num_feat=64,
    num_block=23,
    num_grow_ch=32,
    scale=4
)
```

## Parameters

<ParamField path="num_in_ch" type="int" default="3">
  Number of input channels. Typically 3 for RGB images.
</ParamField>

<ParamField path="num_out_ch" type="int" default="3">
  Number of output channels. Typically 3 for RGB images.
</ParamField>

<ParamField path="num_feat" type="int" default="64">
  Number of base feature channels. This determines the width of the network.
</ParamField>

<ParamField path="num_block" type="int" default="23">
  Number of RRDB (Residual in Residual Dense Block) blocks. More blocks increase model capacity and quality but also computational cost.

  Common configurations:

  * 23 blocks: Standard models (RealESRGAN\_x4plus, RealESRGAN\_x2plus)
  * 6 blocks: Lightweight anime model (RealESRGAN\_x4plus\_anime\_6B)
</ParamField>

<ParamField path="num_grow_ch" type="int" default="32">
  Number of growth channels in dense blocks. Controls the feature growth rate within each dense block.
</ParamField>

<ParamField path="scale" type="int" default="4">
  Upsampling scale factor. Real-ESRGAN models commonly use 2 or 4.
</ParamField>

## Architecture Details

RRDBNet consists of:

1. **Shallow feature extraction**: Initial convolution layer
2. **Deep feature extraction**: Stack of RRDB blocks with residual scaling
3. **Upsampling module**: Convolution + PixelShuffle layers
4. **Reconstruction**: Final convolution to output RGB image

Each RRDB block contains:

* Multiple dense blocks with residual connections
* Beta residual scaling for training stability
* Feature reuse through dense connections

<Info>
  The "Residual in Residual" design allows for very deep networks (23+ blocks) while maintaining stable training through multiple levels of skip connections.
</Info>

## Model Configurations

### RealESRGAN\_x4plus (Standard)

```python theme={null}
model = RRDBNet(
    num_in_ch=3,
    num_out_ch=3,
    num_feat=64,
    num_block=23,
    num_grow_ch=32,
    scale=4
)
```

The flagship Real-ESRGAN model for general image super-resolution at 4× scale.

### RealESRNet\_x4plus (No GAN)

```python theme={null}
model = RRDBNet(
    num_in_ch=3,
    num_out_ch=3,
    num_feat=64,
    num_block=23,
    num_grow_ch=32,
    scale=4
)
```

Same architecture as RealESRGAN\_x4plus but trained without adversarial loss, producing smoother results.

### RealESRGAN\_x4plus\_anime\_6B (Anime)

```python theme={null}
model = RRDBNet(
    num_in_ch=3,
    num_out_ch=3,
    num_feat=64,
    num_block=6,  # Reduced blocks for anime
    num_grow_ch=32,
    scale=4
)
```

Lightweight model optimized for anime images with only 6 RRDB blocks.

### RealESRGAN\_x2plus (2× Scale)

```python theme={null}
model = RRDBNet(
    num_in_ch=3,
    num_out_ch=3,
    num_feat=64,
    num_block=23,
    num_grow_ch=32,
    scale=2  # 2× upsampling
)
```

For 2× super-resolution with the full 23-block architecture.

## Usage Example

```python theme={null}
import torch
from basicsr.archs.rrdbnet_arch import RRDBNet

# Initialize RealESRGAN_x4plus model
model = RRDBNet(
    num_in_ch=3,
    num_out_ch=3,
    num_feat=64,
    num_block=23,
    num_grow_ch=32,
    scale=4
)

# Load pretrained weights
checkpoint = torch.load('RealESRGAN_x4plus.pth')
model.load_state_dict(checkpoint['params_ema'])
model.eval()
model = model.cuda()

# Inference
with torch.no_grad():
    lr_image = torch.randn(1, 3, 128, 128).cuda()
    sr_image = model(lr_image)  # Output: (1, 3, 512, 512)
```

## Integration with RealESRGANer

```python theme={null}
from basicsr.archs.rrdbnet_arch import RRDBNet
from realesrgan import RealESRGANer

# Create model
model = RRDBNet(
    num_in_ch=3,
    num_out_ch=3,
    num_feat=64,
    num_block=23,
    num_grow_ch=32,
    scale=4
)

# Initialize upsampler with the model
upsampler = RealESRGANer(
    scale=4,
    model_path='RealESRGAN_x4plus.pth',
    model=model,
    tile=0,
    tile_pad=10,
    pre_pad=0,
    half=True
)

# Enhance image
import cv2
img = cv2.imread('input.jpg')
output, _ = upsampler.enhance(img, outscale=4)
cv2.imwrite('output.jpg', output)
```

## Comparison with SRVGGNetCompact

| Feature          | RRDBNet                      | SRVGGNetCompact   |
| ---------------- | ---------------------------- | ----------------- |
| **Architecture** | Deep residual + dense blocks | Compact VGG-style |
| **Parameters**   | \~17M (23 blocks)            | \~1-2M            |
| **Quality**      | Highest quality              | Good quality      |
| **Speed**        | Slower                       | Faster            |
| **Use case**     | High-quality images          | Real-time, video  |

<Note>
  Choose RRDBNet for maximum quality on photos and general images. Use SRVGGNetCompact for faster processing, video upscaling, or when computational resources are limited.
</Note>

## Source

Defined in `basicsr.archs.rrdbnet_arch` (BasicSR library)

Used in `inference_realesrgan.py` for model initialization
