Real-ESRGAN general v3 ×4 (ONNX for the browser)
Converted by Codeep. The official
realesr-general-x4v3.pthweights from xinntao/Real-ESRGAN (release v0.2.5.0, sha2568dc7edb9ac80ccdc30c3a5dca6616509367f05fbc184ad95b731f05bece96292) exported to ONNX without changes to the weights. Codeep Image Upscaler loads these files in the browser. All credit goes to the Real-ESRGAN authors; the model is BSD-3-Clause, see LICENSE.
Real-ESRGAN general v3 is the small SRVGGNetCompact network (1.2 M parameters) that Real-ESRGAN suggests for general scenes. It upscales 4× and removes blur, noise and JPEG blocks. Where detail is lost, it draws plausible texture.
Files
| File | Weights | Input and output | Size | Use on |
|---|---|---|---|---|
onnx/model_fp16.onnx | fp16 | float32 | 2.4 MB | WebGPU |
onnx/model.onnx | fp32 | float32 | 4.9 MB | WASM (CPU) |
Both files take and return float32, so the same code works with either.
Input and output
- Input
input: float32[1, 3, H, W], RGB in0..1, NCHW. H and W can be any size. - Output
output: float32[1, 3, 4H, 4W], RGB. Clamp to0..1before converting to bytes: a few values fall slightly outside.
Usage (onnxruntime-web)
import * as ort from 'onnxruntime-web/webgpu';
const base = 'https://huggingface.co/vlad-grig/realesr-general-x4v3-onnx/resolve/main/onnx/';
const session = await ort.InferenceSession.create( base + 'model_fp16.onnx', {
executionProviders: [ 'webgpu' ],
} );
// pixels: Float32Array of 3 * height * width, planar RGB in 0..1
const input = new ort.Tensor( 'float32', pixels, [ 1, 3, height, width ] );
const { output } = await session.run( { input } );
// output.dims is [1, 3, height * 4, width * 4]
Pin a commit hash instead of main in production.
Tips:
- Large photos: process in tiles to keep GPU memory small. 256 px tiles with a 16 px overlap leave no visible seams.
- ×2: run ×4 and downscale the result by half, as Real-ESRGAN's own
--outscaledoes. - Transparency: the model is RGB only. Real-ESRGAN upscales the alpha channel by running it through the same model as a grey image. Before upscaling, fill transparent pixels with the colours of the nearest edge, otherwise a halo of the fill colour appears around the object.
Speed
512×512 → 2048×2048 in Chrome 155 on an Apple M4 (16 GB), 256 px tiles:
| Backend | File | Time |
|---|---|---|
| WebGPU | model_fp16.onnx | 0.7–0.8 s |
| WASM, 1 thread | model.onnx | 16–17 s |
| WASM, 4 threads (cross-origin isolated page) | model.onnx | 5.5 s |
How it was built
scripts/convert_realesrgan.py holds a copy of SRVGGNetCompact from Real-ESRGAN, loads the weights with strict=True and exports:
torch.onnx.export, opset 17, dynamic height and width;- fp16 with
onnxconverter_common.float16.convert_float_to_float16(model, keep_io_types=True).
mkdir weights && curl -L -o weights/realesr-general-x4v3.pth \
https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth
uv run --python 3.12 --with torch --with onnx --with onnxconverter-common --with onnxruntime \
python scripts/convert_realesrgan.py realesr-general-x4v3
# writes models/realesr-general-x4v3/model.onnx and model_fp16.onnx
The script also compares both files with PyTorch on random input in onnxruntime. Largest difference per pixel: fp32 0.000005, fp16 0.0023 (on a 0..1 scale).
Citation
@InProceedings{wang2021realesrgan,
author = {Xintao Wang and Liangbin Xie and Chao Dong and Ying Shan},
title = {Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data},
booktitle = {International Conference on Computer Vision Workshops (ICCVW)},
date = {2021}
}