WebSR models

Weights for WebSR (@websr/websr), a JavaScript library for real-time AI upscaling of images and video in the browser with WebGPU. These are the same models that run in production on free.upscaler.video.

Try them in your browser: WebSR demo Space.

This repo is a mirror. The canonical copies live in the WebSR GitHub repo under weights/ and are served by jsDelivr at https://cdn.jsdelivr.net/gh/sb2702/websr@v0.1.0/<path>.

Models

All are 2Γ— upscalers unless noted. network_name selects the architecture; the weights are passed separately.

network_name File Size What it is
websr/cnn-xs weights/websr/cnn-xs.bin 5.8 KB Tiny CNN (~1.4k parameters) for low-end devices
websr/cnn-s weights/websr/cnn-s.bin 35 KB Anime4K CNN-L architecture, WebSR-trained weights
websr/cnn-m weights/websr/cnn-m.bin 125 KB Wider 16-channel CNN
websr/cnn-l weights/websr/cnn-l.bin 364 KB Wider 28-channel CNN
websr/gan-s weights/websr/gan-s.bin 17.2 MB UNet distilled from Real-ESRGAN x4v3, with per-video calibration
websr/gan-m weights/websr/gan-m.bin 16.4 MB Larger UNet distilled from Real-ESRGAN x4v3
realesrgan/x4v3 weights/realesrgan/x4v3.bin 4.9 MB Real-ESRGAN realesr-general-x4v3, 4Γ—, repacked for WebSR

The websr/gan-* models are UNets distilled from Real-ESRGAN x4v3 for bandwidth-bound browser inference: they keep much of x4v3's detail at a fraction of the cost.

Rough speed on an Apple M4 (Chrome, WebGPU, 640Γ—360 frame, f16 where supported): cnn-xs ~3 ms, cnn-s ~8 ms, cnn-m ~29 ms, cnn-l ~85 ms, gan-s ~59 ms, gan-m ~95 ms, x4v3 ~0.8 s.

Usage

import WebSR from '@websr/websr';

const url = 'https://huggingface.co/katana-video/websr/resolve/main/weights/websr/gan-s.bin';
const weights = WebSR.parseBinaryWeights(await (await fetch(url)).arrayBuffer());

const gpu = await WebSR.initWebGPU();
const websr = new WebSR({ network_name: 'websr/gan-s', weights, gpu, canvas });
await websr.calibrate([videoFrame]);   // gan-* models: optional per-video calibration
await websr.render(video);

Formats, checksums and conversion scripts: WEIGHTS.md.

ONNX (websr/gan-s, websr/gan-m)

The two UNets are also available as ONNX (float32, opset 18) for use outside the browser, e.g. with onnxruntime in Python, C++ or native apps. Same weights as the WebSR files above, converted and checked against both the original PyTorch models and WebSR's WebGPU output.

File Inputs Outputs
onnx/gan-s_calibrate.onnx, onnx/gan-m_calibrate.onnx thumb [N, 3, 32, 32] alpha [banks]
onnx/gan-s.onnx, onnx/gan-m.onnx rgb [N, 3, H, W], alpha [banks] output [N, 3, 2H, 2W]
onnx/gan-s_auto.onnx, onnx/gan-m_auto.onnx rgb [1, 3, H, W] output [1, 3, 2H, 2W], alpha [banks]
onnx/gan-s_image.onnx, onnx/gan-m_image.onnx rgb [1, 3, H, W] output [1, 3, 2H, 2W]
onnx/manifest.json shapes, sha256 checksums and parity numbers for all of the above

banks is 4 for gan-s and 2 for gan-m. All images are RGB, NCHW, float32 in [0, 1]. output is not clamped, so clip it to [0, 1] before converting to 8-bit. H and W are dynamic: any size works, including odd sizes (tested from 2Γ—3 up to 720Γ—1280).

How it works. These models adapt to the content. A small gate network looks at a 32Γ—32 thumbnail and produces alpha, a set of blend weights. The upscaler blends its bottleneck weights with alpha (inside the ONNX graph) and then runs as a plain CNN. As in WebSR, you work out alpha once per video and reuse it for every frame:

  1. Calibrate: for a few frames (WebSR uses up to 64), compute the thumbnail: a 32Γ—32 area average of the whole frame, with bin i covering rows [floor(i*H/32), ceil((i+1)*H/32)) and the same for columns (identical to PyTorch adaptive_avg_pool2d(x, 32)), taken from the 8-bit frame divided by 255. Stack the thumbnails as thumb [N, 3, 32, 32]. *_calibrate.onnx returns one alpha: the mean of the per-thumbnail softmaxes, exactly what WebSR's calibrate(frames) computes.
  2. Upscale: run gan-*.onnx on each frame with that alpha.

*_auto.onnx does both steps for a single image (thumbnail, gate and upscale in one graph). It also returns the alpha it used. Use it for still images. *_image.onnx is the same graph with only the image output (one image in, one image out), for upscaler apps that expect that. For video, use calibrate and then upscale, so the look stays the same from frame to frame.

import numpy as np
import onnxruntime as ort
from huggingface_hub import hf_hub_download
from PIL import Image

def thumbnail(rgb):
    """rgb: float32 [3, H, W] in [0, 1] -> [3, 32, 32] area average (WebSR's calibration thumbnail)."""
    _, H, W = rgb.shape
    rows = [(i * H // 32, -(-(i + 1) * H // 32)) for i in range(32)]
    cols = [(j * W // 32, -(-(j + 1) * W // 32)) for j in range(32)]
    return np.array([[[rgb[c, y0:y1, x0:x1].mean() for x0, x1 in cols] for y0, y1 in rows]
                     for c in range(3)], dtype=np.float32)

get = lambda f: hf_hub_download("katana-video/websr", f"onnx/{f}")
calibrate = ort.InferenceSession(get("gan-s_calibrate.onnx"))
upscale = ort.InferenceSession(get("gan-s.onnx"))

img = np.asarray(Image.open("frame.png").convert("RGB"), dtype=np.float32) / 255.0
x = img.transpose(2, 0, 1)[None]                                    # [1, 3, H, W]

alpha = calibrate.run(None, {"thumb": thumbnail(x[0])[None]})[0]    # once per video
out = upscale.run(None, {"rgb": x, "alpha": alpha})[0]              # [1, 3, 2H, 2W]

Image.fromarray((out[0].transpose(1, 2, 0).clip(0, 1) * 255).round().astype(np.uint8)).save("frame_2x.png")

Accuracy. Compared with the PyTorch models in onnxruntime (CPU), the largest difference is under 1e-6 for both alpha and the image. Compared with WebSR 0.1.1 (WebGPU, Apple M4, same frame and alpha, 8-bit output), f32 output matches on more than 99.99% of pixels, never differing by more than 1/255. WebSR's f16 mode differs by at most 2/255.

The ONNX files are MIT, like the rest of websr/*. gan-s and gan-m were distilled from Real-ESRGAN realesr-general-x4v3 (BSD-3-Clause, xinntao/Real-ESRGAN).

License

MIT for the WebSR-trained weights (websr/*). realesrgan/x4v3 is Real-ESRGAN's realesr-general-x4v3 (BSD-3-Clause, xinntao/Real-ESRGAN). The websr/cnn-* architectures build on Anime4K (MIT).

Built by Sam Bhattacharyya (Katana Video).

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Space using katana-video/websr 1