Skip to content
FuouMPublic

About

EbSynth in Python, version 2

Topics

Resources

Stars

46 stars

Watchers

2 watching

Forks

Latest commit

 

History

175 Commits

Folders and files

Repository files navigation

ReEzSynth

ReEzSynth is a rewrite of EbSynth, the example-based synthesis tool that stylizes video from a few painted keyframes. It runs as a Python library and command line, as ComfyUI nodes, and in the browser. It also bundles FastBlend (video deflicker and keyframe interpolation) and FaceBlit (face style transfer).

Synthesis runs on one of several backends:

Backend Runs on Needs Notes
wgpu Any GPU via Vulkan, DX12 or Metal Nothing extra on Windows x64, Linux x86_64 and Apple Silicon (prebuilt wheel, installed by default) Default and fastest; Rust + WebGPU; the same kernels run in the browser
cuda NVIDIA GPUs ninja (--extra cuda), a C++ compiler and the CUDA Toolkit (JIT-built extension) Native C++/CUDA extension; SSD/NCC costs and PatchMatch search only
taichi GPU or CPU taichi (Python 3.10–3.12)
torch Anything PyTorch runs on Nothing extra Pure PyTorch, slowest

Features

  • EbSynth parity: any number of weighted guides (edges, colours, positional maps, …), modulation maps, and weighted or plain voting.
  • Video pipeline: forward and reverse passes from each keyframe, blended between keyframes by error maps and histogram matching, then Poisson fusion (several solvers). Optical flow from RAFT, NeuFlow v2, OpenCV or TorchVision; edge guides from Classic, PAGE or PST.
  • Extra matching options: SSD, NCC or SAD patch costs; PatchMatch, jump flooding or multi-candidate search; bilateral weights; float16/bfloat16 patch storage (torch backend). ezsynth/kernels/synth/capabilities.py lists the options of each backend; others raise an error.
  • Temporal stability (experimental): temporal NNF propagation (each frame starts from the previous frame's matches) and sparse feature guides (tracked features pin the style to moving objects).
  • FastBlend: blends a video stylized frame by frame against the original to remove flicker, or spreads stylized keyframes over every frame.
  • FaceBlit: landmark-guided face style transfer.
  • Workflow: YAML projects for the command line, a Python API (Ezsynth, ImageSynth), cached optical flow, and a GPU memory cap that leaves room for the desktop.

Examples

Face style Stylit Retarget
Cat.Ebsynth-2.mp4

Forward, Blend, Original and Reverse (clockwise)

Cat.Blend-2.mp4

Seamless, AMG, LSMR, LSQR, CG and Disabled blend methods (clockwise)

cat.loss.compare.mp4

Sum of squared differences (top) and normalized cross-correlation (bottom) patch costs

Speed

Measured on an RTX 3060 (12 GB) with a Ryzen 5 3600 on Windows 10, at commit 5990dce (November 2026). Times are wall clock. Command-line runs were capped with EZSYNTH_GPU_MEMORY_LIMIT_MB=4000.

Single image, run_img_synth.py --full-params. The first example of each run also pays the backend's start-up.

Backend Retarget 320×462 Stylit 1200×912 Face style 810×1000
wgpu 1.71 s 0.98 s 0.62 s
cuda 2.65 s 0.90 s 0.60 s
taichi 5.43 s 5.45 s 3.59 s
torch 3.48 s 33.6 s 23.1 s

Video, reezsynth-video on 20 frames of examples/cat_full.mp4 (960×544) with keyframes on the first and last frame: NeuFlow flow, NCC cost, temporal NNF propagation, sparse feature guides and lsqr Poisson blending. Whole run, including optical flow and saving:

Backend Time
wgpu 50 s
cuda 54 s
taichi (with use_taichi_ops) 129 s
torch 954 s

Under the 4 GiB cap the torch backend gathers its patches in chunks; with more memory it materializes them.

FastBlend on the same 20 frames, whole command including start-up:

Command wgpu cupy cuda taichi
fastblend (deflicker) 31 s 33 s 45 s 47 s
fastblend-interpolate (2 keyframes) 29 s 53 s

ComfyUI, the bundled workflows on the full 100-frame clip (flow switched to NeuFlow, since the default RAFT checkpoints are a separate download):

  • Video Synth: 112 s with wgpu, 9 min 53 s with taichi (the workflow's backend).
  • FastBlend (keyframes 0 and 99, auto picks wgpu): 118 s.
  • Image Synth (taichi): Stylit 15 s, Face style 6 s, Texture by numbers 3 s.

FaceBlit, FaceBlit/examples/target2.png (768×1024) with the watercolor style and FAN landmarks, including style preparation: torch 15.4 s (numba DFS), 11.7 s (vectorized) or 32.9 s (Python DFS); taichi 13.5 s.

The browser app synthesizes the 810×1000 face style example in 1.1 s.

Installation

You need Python 3.10–3.12 and uv. The default wgpu backend runs on any GPU; the torch, taichi and wgpu backends need no CUDA Toolkit.

git clone --filter=blob:none https://github.com/FuouM/ReEzSynth.git
cd ReEzSynth
uv sync                                   # core + the wgpu backend, with PyTorch for CUDA 12.6 (CPU/MPS build on macOS)
uv sync --extra taichi --extra pyamg      # + Taichi backend and the AMG Poisson solver
uv sync --all-extras                      # everything, incl. CuPy and FaceBlit (dlib needs CMake)

--filter=blob:none skips the large media in older history; current example media comes from Git LFS. To skip it as well, clone with GIT_LFS_SKIP_SMUDGE=1 and fetch what you need later, for example git lfs pull --include="examples/**".

Optional extras: taichi, cuda (ninja, for the JIT-built cuda backend), scipy (the lsmr/cg Poisson solvers), cupy, pyamg, phycv, faceblit.

wgpu backend

uv sync and requirements.txt install the prebuilt ezsynth_wgpu wheel of the GitHub Release on Windows x64, Linux x86_64 and Apple Silicon (abi3: any Python ≥ 3.10, any PyTorch).

On other platforms, or after changing the Rust code, build it from source with a Rust toolchain:

  • Windows: setup_env.ps1 installs one into .rust/; load it with . .\env.ps1.
  • Elsewhere: install one with rustup.

Then run uv pip install ./rust/ezsynth-wgpu (or pip install ./rust/ezsynth-wgpu); the next uv sync restores the release wheel.

It is the default backend (backend: wgpu, --backend wgpu), and FastBlend auto picks it first. To choose the graphics API and GPU, set WGPU_BACKEND=vulkan|dx12|metal and WGPU_ADAPTER_NAME.

With wgpu the video pipeline's flow warping, forward splatting, histogram blending and occlusion masks also run on WebGPU, so forward warping no longer needs Taichi. Warping and occlusion masks match OpenCV 5. Histogram blending converts to Lab in float, so a few pixels differ from OpenCV's 8-bit path (mean difference below one level).

cuda / cpu native extension

The ebsynth_torch C++/CUDA extension (ezsynth/kernels/synth/cuda_src/) is JIT-compiled the first time the cuda or cpu backend is used, then cached by PyTorch. The build needs ninja (uv sync --extra cuda).

  • Compiler: C++20 (Visual Studio 2022 "Desktop development with C++" on Windows, GCC 10+ on Linux).
  • CUDA kernels: CUDA Toolkit 12.4 or newer. The .cu files include no PyTorch headers and compile as C++17.
  • Target GPUs: taken from TORCH_CUDA_ARCH_LIST, else the local GPU.
export EZSYNTH_NATIVE_ARCH=1         # opt in to -march=native / /arch:AVX2 host tuning
export FORCE_EBSYNTH_JIT_LOADER=1    # ignore an installed ebsynth_torch wheel and JIT-build

The Releases also hold an old prototype ebsynth_torch-0.0.0-cp310-cp310-win_amd64.whl. It doesn't match the current sources or PyTorch, so use the JIT build.

GPU memory budget

Outside ComfyUI, each run caps PyTorch's CUDA allocator at 90% of the memory free at startup, always leaving 1.5 GiB for the desktop and browser. Exceeding the cap raises an out-of-memory error in ReEzSynth instead of exhausting the GPU. To change it:

  • EZSYNTH_GPU_MEMORY_LIMIT_MB: absolute cap.
  • EZSYNTH_GPU_MEMORY_FRACTION: fraction of the free memory.
  • EZSYNTH_GPU_MEMORY_RESERVE_MB: memory kept free.
  • EZSYNTH_GPU_MEMORY_LIMIT=0: disables the cap.

ComfyUI

  1. Clone into ComfyUI/custom_nodes/ReEzSynth, preferably without media: GIT_LFS_SKIP_SMUDGE=1 git clone --filter=blob:none https://github.com/FuouM/ReEzSynth.git (in Windows cmd, run set GIT_LFS_SKIP_SMUDGE=1 first).
  2. Install the requirements into ComfyUI's Python; torch and torchvision come from ComfyUI. Portable build: python_embeded\python.exe -m pip install -r ComfyUI\custom_nodes\ReEzSynth\requirements.txt. They include the ezsynth_wgpu wheel of the nodes' default wgpu backend on Windows x64, Linux x86_64 and Apple Silicon; elsewhere pick torch (or install taichi).
  3. Optical flow models: for the default RAFT engine, put the RAFT checkpoints in models/raft/ (see models/model_files.md). TorchVision downloads its weights itself; OpenCV needs none.
  4. Restart ComfyUI. The nodes appear under ReEzSynth: Guide, Guides Append, Image Synth Config, Image Synth, Video Style Keyframe(s), Video Style Keyframes Append, Video Synth Config, Video Synth, FastBlend Interpolate, FaceBlit.
  5. Example graphs are in workflows/: image synthesis (reezsynth_imgsynth_*.json), reezsynth_videosynth.json, reezsynth_fastblend.json and reezsynth_faceblit.json. Drag one onto the canvas.

Some options need extra packages. A node that needs a missing one stops and shows the pip install command.

Package Needed for
taichi taichi synthesis, FastBlend and FaceBlit backends
cupy-cuda12x FastBlend cupy backend
ninja cuda backends (JIT build)
scipy lsmr, cg and amg Poisson solvers
phycv PAGE and PST edge methods
pyamg amg Poisson solver
face-alignment FaceBlit landmark detection when no landmarks are connected
numba FaceBlit numba DFS mode

The cuda backends JIT-build the native extension, which needs ninja, a C++ compiler and the CUDA Toolkit matching ComfyUI's PyTorch.

Inside ComfyUI the memory cap is off, since ComfyUI manages VRAM. The nodes:

  • ask ComfyUI to offload its models before synthesis;
  • report progress to the progress bar;
  • stop at the next frame or pyramid level when you cancel.

The torch backend still sizes its tensors to keep EZSYNTH_GPU_MEMORY_RESERVE_MB (1.5 GiB by default) free.

Browser app

web/ is ReEzSynth in the browser, built with Solid, Vite and the SoulWindow design system. It runs on the GPU through WebGPU in current Chrome or Edge, or Firefox/Safari with WebGPU enabled. Images and videos never leave the machine.

  • Image: EbSynth on one image from a style image and any number of guide pairs.
  • Keyframes → video: load a video or its frames, stylize a few frames in any tool and add them back as keyframes (a number in the file name places each one). Every other frame is then filled in by one of two methods:
    • FastBlend interpolation: fast.
    • EbSynth: the Python pipeline's guides, passes and pass blending, with NeuFlow optical flow; optionally deflickered with FastBlend afterwards.
  • Deflicker video: FastBlend blending of a video stylized frame by frame, guided by the original.

Other details:

  • Files: drop them on the input boxes or the monitor. The monitor has a before/after split (Compare, or the C key), and settings are kept between visits.
  • Video: decoded and encoded in the browser with WebCodecs (mediabunny). MP4, WebM or MOV go in; H.264 MP4 (or WebM) or a zip of PNGs come out.
  • Memory: frames are held in memory at the working size (768 px on the longer side by default). For long or large videos, lower the working size or set a frame limit.

Optical flow model

EbSynth video uses NeuFlow v2 through ONNX Runtime Web, loaded from jsDelivr on first use. Export the model once into web/public/models/ (this needs the NeuFlow checkpoint in models/neuflow/), or choose an exported .onnx file in the app:

uv run --with onnx --with onnxscript python web/export_neuflow.py --size 960x544   # --fp16 halves it; needs WebGPU shader-f16

The model has a fixed input size. Frames up to 31 px smaller are padded; other sizes are resized and the flow rescaled.

Build

This needs Node 22.18+, the wasm32-unknown-unknown Rust target and wasm-bindgen-cli 0.2.130.

rustup target add wasm32-unknown-unknown
cargo install wasm-bindgen-cli --version 0.2.130 --locked
cd web
npm install
npm run wasm      # compiles rust/ebsynth-web into web/src/pkg (uses .rust/ from setup_env.ps1 when present)
npm run dev       # http://localhost:5173
npm run build     # static site in web/dist; serve it over https or localhost
npm run check     # TypeScript check and unit tests

Usage

Command line (YAML projects)

  1. Extract the video into frames:

    uv run reezsynth-prepare --video "path/to/video.mp4" --output "projects/my_project/content"
  2. Copy one or more frames into a style directory and paint over them.

  3. Copy configs/example_project.yml and set your paths:

    project:
      name: "MyFirstProject"
      content_dir: "projects/my_project/content"
      style_path: "projects/my_project/style/00123.png"  # the painted keyframe
      style_indices: [123]                               # its frame number
      output_dir: "output/MyFirstProject"
      cache_dir: "cache/MyFirstProject"
  4. Run it; the stylized frames go to output_dir:

    uv run reezsynth-video --config "projects/my_project/config.yml"

fastblend (deflicker) and fastblend-interpolate (keyframe interpolation) are installed alongside. Each tool also runs as python -m ezsynth.cli.<name>.

Python API

Video:

from ezsynth.api import Ezsynth, RunConfig

config = RunConfig(
    pyramid_levels=5,
    uniformity=4000.0,
    use_sparse_feature_guide=True,
    use_temporal_nnf_propagation=True,
)
synth = Ezsynth(
    content_dir="projects/my_project/content",
    style_paths=["projects/my_project/style/00123.png"],
    style_indices=[123],
    output_dir="output/api_video_output",
    config=config,
)
frames = synth.run()  # also saved to output_dir

Single image:

from ezsynth.api import ImageSynth, RunConfig, load_guide
from ezsynth.utils.io_utils import write_image

synth = ImageSynth(
    style_image="path/to/style.png", config=RunConfig(patch_size=7, uniformity=4000)
)
image, error = synth.run(
    guides=[
        load_guide("source_guide1.png", "target_guide1.png", weight=2.0),
        load_guide("source_guide2.png", "target_guide2.png", weight=1.5),
    ]
)
write_image("output/stylized.png", image)

Configuration

Settings come from a YAML file (defaults in configs/default.yml) or from RunConfig in the API.

ebsynth_params:

  • uniformity: higher values spread style patches more evenly; 3500 is a good default.
  • patch_size: odd; 7 balances detail and speed.
  • search_pruning_threshold: skips random search for patches whose error is already low; 50.0 is a good start.
  • cost_function (ssd, ncc, sad) and search_mode (patchmatch, jfa, multicand): not supported by the native cuda backend.
  • edge_weight, image_weight, pos_weight, warp_weight: guide weights (defaults 1, 6, 2, 0.5).

blending.poisson_solver:

  • lsqr: least-squares solve (NumPy conjugate gradients; no SciPy needed).
  • lsmr: SciPy's iterative solver; needs scipy.
  • cg: SciPy conjugate gradient; needs scipy.
  • amg: fastest high-quality CPU option for large frames; needs pyamg.
  • seamless: OpenCV seamlessClone; fast.
  • disabled: no Poisson fusion; only histogram blending.

pipeline:

  • use_temporal_nnf_propagation: reuse the previous frame's matches to reduce flicker.
  • use_sparse_feature_guide: track features to reduce sliding textures.

How it works

  1. Precompute: optical flow and edge maps for the whole sequence, cached on disk.

  2. Passes: the keyframes split the video into sequences. Each frame is synthesized from the nearest keyframe's style, with these guides:

    • an edge map;
    • the frame itself;
    • a positional map moved along the flow;
    • the previous result warped onto the frame.

    Frames before the first keyframe run backwards from it, and frames after the last run forwards. Frames between two keyframes run both ways.

  3. Blend: the two passes between keyframes are combined. Each pixel takes the pass with the lower patch error, and masks are carried along the flow. The passes are then histogram-matched in Lab and fused with a Poisson solve.

Troubleshooting

  • ebsynth_torch not found: the JIT build of the native extension failed. Check the compiler and CUDA Toolkit, or use the wgpu, taichi or torch backend.
  • CUDA out of memory: the run exceeded its memory budget (see GPU memory budget). Close other GPU applications, lower the resolution, or raise EZSYNTH_GPU_MEMORY_LIMIT_MB / lower EZSYNTH_GPU_MEMORY_RESERVE_MB.
  • Jitter or flicker: add keyframes, enable use_temporal_nnf_propagation and use_sparse_feature_guide, or run FastBlend deflicker on the result.

Development

uv sync --extra taichi --extra pyamg   # dev tools (pytest, ruff, pre-commit) are in the default dev group
uv run pre-commit install              # ruff check/format + uv lock on commit
uv run pytest                          # CPU-safe test suite
uv run ruff check . && uv run ruff format --check .

ezsynth/tests/test_{synth,imageops,fastblend}_parity.py compare every backend of a kernel family against its reference and skip the backends that cannot run on the machine. The JIT-built cuda extensions take minutes to compile on the first run, so their tests run only with EZSYNTH_TEST_JIT=1.

CI (.github/workflows/ci.yml) runs:

  • ruff;
  • the Python tests;
  • rustfmt, clippy (native and wasm32) and WGSL validation;
  • the web app's typecheck, tests and build;
  • the ezsynth-wgpu wheels for Windows, Linux and macOS.

A pushed v* tag publishes the wheels to that tag's GitHub Release. Backend notes are in docs/ (backend-benchmarks.md, taichi-porting-guide.md).

License

MIT (see LICENSE). Bundled third-party code (RAFT, NeuFlow v2, FastBlend, FaceBlit) keeps its own license; see NOTICE.

Acknowledgements

@misc{Jamriska2018,
  author = {Jamriska, Ondrej},
  title = {Ebsynth: Fast Example-based Image Synthesis and Style Transfer},
  year = {2018},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/jamriska/ebsynth}},
}
@misc{teed2020raftrecurrentallpairsfield,
      title={RAFT: Recurrent All-Pairs Field Transforms for Optical Flow},
      author={Zachary Teed and Jia Deng},
      year={2020},
      eprint={2003.12039},
      archivePrefix={arXiv},
      primaryClass={cs.CV},
      url={https://arxiv.org/abs/2003.12039},
}
@misc{zhang2025neuflowv2pushhighefficiency,
      title={NeuFlow v2: Push High-Efficiency Optical Flow To the Limit},
      author={Zhiyong Zhang and Aniket Gupta and Huaizu Jiang and Hanumant Singh},
      year={2025},
      eprint={2408.10161},
      archivePrefix={arXiv},
      primaryClass={cs.CV},
      url={https://arxiv.org/abs/2408.10161},
}
@article{Xu2019ACMM,
  title={Multi-Scale Geometric Consistency Guided Multi-View Stereo},
  author={Xu, Qingshan and Tao, Wenbing},
  journal={Computer Vision and Pattern Recognition (CVPR)},
  year={2019}
}
@article{Xu2020ACMP,
  title={Planar Prior Assisted PatchMatch Multi-View Stereo},
  author={Xu, Qingshan and Tao, Wenbing},
  journal={AAAI Conference on Artificial Intelligence (AAAI)},
  year={2020}
}

About

EbSynth in Python, version 2

Topics

Resources

Stars

46 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages