Vapoursynth VSGAN Resize

From wiki.selur.net
Jump to navigation Jump to search

VSGAN runs a PyTorch super-resolution model you supply – ESRGAN-family networks such as ESRGAN, Real-ESRGAN or A-ESRGAN, or EGVSR video models – on an NVIDIA GPU. It is one of the resizers of Vapoursynth Resizer; several models can be chained, and an optional color fix matches the colors back to the source.

Filtering → Vapoursynth → Frame → Resize

VSGAN controls in Hybrid
VSGAN controls in Hybrid

What it does

Every frame is passed through the selected .pth model(s). Unlike the other torch resizers there is no built-in model list. Hybrid takes the upscale factor from the file name and resizes the result to the exact output size afterwards. Without a model the network step is skipped and the clip is only resized.

Requirements

  • Windows, Hybrid's torch add-on and an NVIDIA GPU with CUDA.
  • One or more model files; the color fix needs the color fix package.

Settings

Control Parameter Range / values Hybrid default Meaning
Model / Select load() .pth file(s) empty Model to run; the factor is the number before "x_" in the file name. Models without such an "Nx_" prefix are skipped with a message in the log.
Multi – on/off off Select several models, applied in order.
No adjust – on/off off With Multi: do not resize back to the input size between the models.
32bit – on/off on Full precision; off runs in half precision.
GPU ID device 0 – 8 0 CUDA device; written as "cuda:" plus the ID.
Overlap overlap none, 16 – 624 none Overlap of the tiles vsgan uses when the GPU memory runs out; none = vsgan's default (16). Not used for EGVSR.
Duplicate aware – on/off off Upscales only the first frame of a run of near-identical frames and repeats it; fixes Overlap to none and Resizer to spline64.
Threshold – 0.001 – 1 0.001 Largest difference of the first plane that still counts as a duplicate.
Requires: Luma scale / Matrix / Modulo – pc, tv, no / matrix list / 1 – 128 pc / no / 1 Overrides the range, matrix and size multiple Hybrid prepares for the model.
Fix Color: Radius, Plane, Mode, Wavelets, Fast – radius 1 – 100, average/wavelet, wavelets 1 – 15 off (radius 10, wavelets 5) Matches the colors of the output to the clip before the model; Radius and Fast with average, Wavelets with wavelet.
Resizer – point, rect, linear, cubic, lanczos, blackman, blackmanminlobe, spline36, spline64, spline144, spline, gauss, sinc spline64 Kernel that brings the result to the exact output size.

Example

Model F:/Hybrid/64bit/vsgan_models/2x_AniScale_30000.pth selected (no model is set by default), otherwise default settings, on a 10-bit YUV 4:2:0 source (640x356), resized to 1280x712:

# changing range from limited to full range for vsVSGAN
clip = core.resize.Bicubic(clip, format=vs.YUV420P10, range_in_s="limited", range_s="full")
# setting color range to PC (full) range.
prop_name = '_Range' if core.core_version.release_major >= 74 else '_ColorRange'
clip = core.std.SetFrameProps(clip=clip, **{prop_name: vs.RANGE_FULL})
# adjusting color space from YUV420P10 to RGBS for vsVSGAN
clip = core.resize.Bicubic(clip=clip, format=vs.RGBS, matrix_in_s="470bg", range_in_s="full", range_s="full")
# resizing using VSGAN, target 1280x712
from vsgan import ESRGAN
vsgan = ESRGAN(clip=clip,device="cuda:0")
model = "F:/Hybrid/64bit/vsgan_models/2x_AniScale_30000.pth"
vsgan.load(model)
vsgan.apply() # 1280x712
clip = vsgan.clip
# making sure 0-1 limits are respected
clip = core.vszip.Limiter(clip=clip, min=[0,0,0], max=[1,1,1])
# adjusting output color from RGBS to YUV420P10 for NVEncModel
clip = core.resize.Bicubic(clip=clip, format=vs.YUV420P10, matrix_s="470bg", range_in_s="full", range_s="full", dither_type="error_diffusion")
# changing range from full to limited range for matching filter target luma scale
clip = core.resize.Bicubic(clip, format=vs.YUV420P10,range_in_s="full", range_s="limited")
# setting color range to TV (limited) range.
prop_name = '_Range' if core.core_version.release_major >= 74 else '_ColorRange'
clip = core.std.SetFrameProps(clip=clip, **{prop_name: vs.RANGE_LIMITED})

How Hybrid calls it

  • Selected with Resize enabled and Resizer set to VSGAN (see Vapoursynth Resizer).
  • The clip is converted to 32-bit float RGB (16-bit float with 32bit off), full range; there is no size multiple unless Requires sets one.
  • File names containing "EGVSR" use the EGVSR network, all others ESRGAN. The device is written as "cuda:" plus the GPU ID.
  • The factor comes from the file name: "4x_name.pth" counts as 4×. A model whose name does not start with "Nx_" is not applied (message in the log, comment in the script), and the models after it in the list are not applied either.
  • Each model works on the output of the previous one. With Multi, Hybrid resizes back to the input size between the models unless No adjust is set. At the end fmtconv resizes the result with the Resizer kernel to the exact size if needed (half-float clips are converted to 32-bit float first); with separately filtered fields the target height is halved. The color fix runs last.
  • With Duplicate aware Hybrid's duplicate-aware resizing script builds the model chain once and only feeds it the frames that are not duplicates.
  • The ONNX counterpart for user-supplied models is VSMLRT.

Background

vsgan

vsgan (rlaphoenix) is a PyTorch module for super-resolution and restoration in VapourSynth. The installed version offers the ESRGAN and EGVSR networks; the ESRGAN loader detects from the model file whether it is a classic ESRGAN or a compact Real-ESRGAN v2 network. vsgan first tries the whole frame and only on a CUDA out-of-memory error splits it into four quadrants, repeatedly if needed; the overlap is the seam between those parts.

Sources