Overview
Real-ESRGAN provides two training model classes:- RealESRGANModel: Full GAN training with discriminator
- RealESRNetModel: Training without GAN losses (generator only)
RealESRGANModel
Class Definition
Description
RealESRGANModel extends SRGANModel from BasicSR and is designed for training Real-ESRGAN with adversarial losses. It performs:
- Random synthesis of low-quality (LQ) images on GPU using realistic degradations
- Network optimization with GAN training (generator and discriminator)
Constructor Parameters
Configuration dictionary containing training options. Key parameters include:
queue_size(int, default: 180): Size of training pair pool for increasing degradation diversityhigh_order_degradation(bool): Enable two-order degradation synthesisscale(int): Upsampling scale factorgt_size(int): Ground truth patch size for trainingl1_gt_usm(bool): Whether to use USM-sharpened GT for L1 losspercep_gt_usm(bool): Whether to use USM-sharpened GT for perceptual lossgan_gt_usm(bool): Whether to use USM-sharpened GT for GAN loss
Key Methods
feed_data()
Accepts data from dataloader and applies two-order degradations to synthesize LQ images.Dictionary containing:
gt: Ground truth high-resolution imageskernel1: First blur kernelkernel2: Second blur kernelsinc_kernel: Final sinc filter kernellq: Low-quality images (for validation/paired training)
During training with
high_order_degradation=True, this method synthesizes realistic degradations including blur, resize, noise, and JPEG compression applied twice in sequence.optimize_parameters()
Performs one optimization step for both generator and discriminator.Current training iteration number.
- Optimizes the generator with pixel loss, perceptual loss, and adversarial loss
- Optimizes the discriminator to distinguish real vs. fake images
- Updates EMA (Exponential Moving Average) model if enabled
nondist_validation()
Runs validation without synthetic degradation process.Degradation Pipeline
The model applies a two-order degradation pipeline during training: First Degradation:- Blur with
kernel1 - Random resize (up/down/keep)
- Add Gaussian or Poisson noise
- JPEG compression
- Optional blur with
kernel2 - Random resize
- Add Gaussian or Poisson noise
- JPEG compression + sinc filter
Training Pair Pool
The model uses_dequeue_and_enqueue() to maintain a training pair pool that increases degradation diversity across batches.
RealESRNetModel
Class Definition
Description
RealESRNetModel extends SRModel from BasicSR and is designed for training Real-ESRGAN without GAN losses. It’s useful for:
- Pre-training the generator before GAN training
- Training models without adversarial losses
- Faster convergence for initial training stages
RealESRNetModel is trained without GAN losses but uses the same degradation synthesis pipeline as RealESRGANModel.
Constructor Parameters
Configuration dictionary. Key parameters:
queue_size(int, default: 180): Training pair pool sizehigh_order_degradation(bool): Enable two-order degradation synthesisscale(int): Upsampling scale factorgt_size(int): Ground truth patch sizegt_usm(bool): Apply USM sharpening to ground truth images
Key Methods
feed_data()
Same degradation synthesis as RealESRGANModel, but with optional USM sharpening on GT.Unlike RealESRGANModel, this version applies USM sharpening directly to GT if
gt_usm=True, rather than maintaining separate GT and GT_USM versions.nondist_validation()
Validation without synthetic degradations.Differences from RealESRGANModel
| Feature | RealESRGANModel | RealESRNetModel |
|---|---|---|
| Base Class | SRGANModel | SRModel |
| GAN Training | Yes (G + D) | No (G only) |
| USM GT Variants | Multiple (GT, GT_USM) | Single GT with optional USM |
| Training Speed | Slower | Faster |
| Image Quality | Higher (with GAN) | Good (no adversarial artifacts) |
Usage Example
Training Configuration
Pre-training with RealESRNetModel
Source References
- RealESRGANModel:
realesrgan/models/realesrgan_model.py:14 - RealESRNetModel:
realesrgan/models/realesrnet_model.py:13