BasicSR: Image and Video Super-Resolution Toolbox

1.4.2 · active · verified Thu Apr 16

BasicSR is an open-source toolbox for image and video super-resolution and restoration, primarily built on PyTorch. It provides a flexible framework, common architectures, and training/testing scripts for various SOTA methods. The library sees active, though somewhat irregular, releases with a focus on adding new models and utility enhancements, currently at version 1.4.2.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically perform super-resolution inference using a pre-trained Real-ESRGAN model. It covers creating a dummy input image, downloading the necessary model weights, initializing the `RealESRGANer` utility, and processing the image to save the super-resolved output. The example uses a common `RealESRGAN_x4plus` model from a public URL.

import os
import cv2
import torch
import numpy as np
from basicsr.archs.rrdbnet_arch import RRDBNet
from basicsr.utils.realesrgan_utils import RealESRGANer
from basicsr.utils.download_util import load_file_from_url

# 1. Create a dummy low-resolution image (e.g., 64x64, 3 channels)
# In a real application, this would be loaded from a file (e.g., cv2.imread)
dummy_lr_img = np.random.randint(0, 256, (64, 64, 3), dtype=np.uint8)

# 2. Define model parameters and download URL for a pre-trained Real-ESRGAN model
model_scale = 4
model_path_url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth"
output_filename = "dummy_sr_image.png"

# 3. Download the model weights
try:
    # Model weights will be saved to a 'weights' directory by default
    model_path = load_file_from_url(url=model_path_url, model_dir='weights', progress=True)
except Exception as e:
    print(f"Could not download model from {model_path_url}: {e}")
    model_path = None # Set to None if download fails

if model_path:
    # 4. Initialize the RealESRGANer for inference
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    upsampler = RealESRGANer(
        scale=model_scale,
        model_path=model_path,
        dni_weight=None, # Not used for standard RealESRGAN_x4plus
        model_arch=RRDBNet,
        tile=0, # Process image without tiling for small inputs
        tile_pad=10,
        pre_pad=0,
        half=False, # Use float32, set to True for float16 inference if supported
        device=device
    )

    # 5. Perform inference (enhance expects a NumPy array (HWC, BGR, uint8))
    output_image, _ = upsampler.enhance(dummy_lr_img, outscale=model_scale)

    # 6. Save the output image
    cv2.imwrite(output_filename, output_image)
    print(f"Super-resolved image saved to {output_filename}")
else:
    print("Skipping quickstart inference due to model download failure or missing model path.")

# Clean up downloaded weights directory if desired (optional)
# import shutil
# if os.path.exists('weights'):
#     shutil.rmtree('weights')

view raw JSON →