How ddalko/svgagent works
1. What this repo is
SVGAgent is a pipeline for turning a raster image into an SVG by combining:
- a local SVG generation model (OmniSVG),
- a simple image decomposition step,
- and a judge that decides whether a candidate SVG is good enough or should be refined.
2. Top-level orchestration
The architectural center is pipeline.py, with CLI entrypoints in run_pipeline.py and run_benchmark.py. The pipeline is not a monolith: it is a controller that routes an input image through generation, evaluation, optional decomposition, recursive refinement, and final composition.
3. Primary data flow
The main flow is:
input image → full-image SVG generation → judge/accept-or-refine → optional decomposition into subregions → per-region generation and selection → recursive verification → SVG composition → final SVG and rendered artifacts.
The pipeline is optimized for early exit when the first full-image SVG is judged acceptable.
4. Generation subsystem
agents/generate_agent.py wraps the SVG-producing model. The README indicates it can call either:
- OmniSvgClient for local in-process inference, or
- a Gemini-based generation path if configured that way.
The generated output is an SVG string, not just tokens or tensors, so generation is the boundary between ML inference and the rest of the pipeline.
5. Local OmniSVG inference stack
The omnisvg_inference/ directory is a vendored model runtime. It contains tokenization, decoding, SVG libraries, transformer/model code, and supporting utilities from OmniSVG/DeepSVG. This is the low-level ML subsystem that converts model outputs into SVG syntax and supports local GPU inference via PyTorch/CUDA.
6. Decomposition subsystem
agents/decompose_agent.py splits an input image into smaller regions using Otsu thresholding, connected components, and transparency-aware filtering. This creates leaf regions that can be handled independently. Architecturally, this is the “branching” point that turns one difficult image into multiple smaller generation tasks.
7. Judging subsystem
There are two judge implementations:
- agents/judge_agent.py uses Gemini VLM calls for semantic verification/picking the best candidate.
- agents/mse_judge.py uses a local pixel-MSE comparison against the source image, avoiding API calls.
The judge is the quality gate: it determines whether a candidate SVG is accepted, which candidate among several is best, and whether recursive refinement should continue.
8. Composition subsystem
compositor.py implements the final merge of leaf SVGs using a painter’s-algorithm-style z-order concatenation under a single SVG root. This is important because the pipeline is not stitching pixels; it is assembling vector subtrees into one coherent SVG document.
9. Supporting image/SVG utilities
The utils/ package contains helper code for rendering SVGs, image handling, visualization, prompt templates, and client wrappers. These utilities sit at the boundary between model output and human-readable artifacts: they render SVGs to PNGs, create comparison grids, and support debugging/inspection.
10. Configuration boundary
Runtime behavior is controlled by YAML config files:
- config/default.yaml provides defaults,
- config.yaml.example is the user-facing template.
The key architectural toggle is the judge backend (mse vs gemini), which changes whether the pipeline stays local or depends on an external VLM API.
11. Benchmarking and dataset integration
scripts/extract_mmsvgbench.py converts the MMSVGBench parquet into images plus a manifest. run_benchmark.py then runs the pipeline across the benchmark and emits per-sample outputs, summary JSON, and an HTML gallery. This is a separate evaluation path from single-image inference, but it uses the same core pipeline.
12. Visualization and observability
Files like visualize_decompose.py, utils/decompose_viz.py, utils/visualizer.py, and the runs/ artifacts described in the README indicate a strong inspection layer. The repository is designed to save intermediate images, SVGs, rendered PNGs, and comparison grids so the decomposition and generation decisions can be audited.
13. Main architectural boundary
The key boundary in this repo is between:
- model-facing code: OmniSVG inference, Gemini calls, tokenization/decoding,
- and pipeline/control code: decomposition, judging, composition, benchmark orchestration.
That separation makes it possible to swap judge backends or generation backends without changing the overall control flow.
14. Expected deployment/runtime profile
The repo is primarily a Python inference/evaluation system, with one path requiring local GPU-backed PyTorch and another optionally depending on external Gemini API access. It is not a web service; its natural runtime is CLI-based batch evaluation or single-image processing with saved artifacts.
Main entry points: pipeline.py Pipeline · generate_agent.py Generate · decoder.py Decoder · omnisvg_client.py OmniSVG Client
Components 20
Orchestration
CLI entrypoints and the central pipeline controller.
- Pipeline · Main control flow for generation, judge/accept-or-refine, decomposition, recursion, and composition.
pipeline.py - Run Pipe · Single-image CLI entrypoint.
run_pipeline.py - Run Bench · Benchmark CLI entrypoint that drives batch evaluation.
run_benchmark.py - Compositor · Merges leaf SVGs into one document using z-order composition.
compositor.py
Agents
Generation, decomposition, and judging subsystems.
- Generate · Wraps SVG generation backends and returns SVG strings.
agents/generate_agent.py - Decompose · Splits images into regions via thresholding and connected components.
agents/decompose_agent.py - Judge · External VLM-based semantic judging and candidate selection.
agents/judge_agent.py - MSE Judge · Local pixel-distance judge for candidate comparison.
agents/mse_judge.py
OmniSVG Runtime
Vendored local SVG model stack and token-to-SVG machinery.
- Tokenizer · Converts SVG/model symbols for the OmniSVG runtime.
omnisvg_inference/tokenizer.py - Decoder · Decodes model outputs into SVG structure.
omnisvg_inference/decoder.py - Transformer · Core OmniSVG/DeepSVG model implementation.
omnisvg_inference/deepsvg/model/model.py - SVG Lib · Low-level SVG primitives and path utilities.
omnisvg_inference/deepsvg/svglib/svg.py
Utilities
Rendering, image handling, clients, and inspection helpers.
- OmniSVG Client · Local inference wrapper for the vendored OmniSVG stack.
utils/omnisvg_client.py - Gemini Client · External VLM client used for generation or judging when configured.
utils/gemini_client.py - SVG Renderer · Renders SVG artifacts for inspection and comparison.
utils/svg_renderer.py - Image Utils · Image loading, conversion, and comparison helpers.
utils/image_utils.py - Visualizer · Builds comparison grids and debug visuals.
utils/visualizer.py - Decompose Viz · Visualization helpers for decomposition output.
utils/decompose_viz.py
Benchmarking
Benchmark ingestion and evaluation workflows.
- Bench Extract · Converts MMSVGBench parquet into images and a manifest.
scripts/extract_mmsvgbench.py - Viz Decompose · Standalone decomposition visualization entrypoint.
visualize_decompose.py
Connections 26
- Run Pipe → Pipeline: invokes · CLI enters the main controller.
- Run Bench → Pipeline: drives · Batch evaluation uses the same core pipeline.
- Run Bench → Bench Extract: prepares · Benchmark flow relies on dataset extraction and manifests.
- Pipeline → Generate: generate · Creates an initial full-image SVG candidate.
- Pipeline → Judge: judge · Uses semantic judging to accept or refine candidates.
- Pipeline → MSE Judge: judge · Optional local quality gate for candidate comparison.
- Pipeline → Decompose: decompose · Branches into region-level subtasks when needed.
- Pipeline → Compositor: compose · Assembles accepted leaf SVGs into final output.
- Pipeline → SVG Renderer: render · Produces rendered artifacts for inspection.
- Pipeline → Visualizer: inspect · Generates comparison grids and debug outputs.
- Pipeline → Decompose Viz: inspect · Supports decomposition auditing.
- Generate → OmniSVG Client: local path · Primary local SVG generation backend.
- Generate → Gemini Client: alt path · Optional external generation backend.
- OmniSVG Client → Tokenizer: uses · Tokenization is part of local model inference.
- OmniSVG Client → Decoder: decodes · Converts model outputs into SVG syntax.
- Decoder → Transformer: runs on · Decoding depends on the underlying OmniSVG transformer model.
- Decoder → SVG Lib: builds · Uses SVG primitives and path utilities to form output.
- Judge → Gemini Client: calls · Judge semantics are handled through Gemini VLM calls.
- MSE Judge → Image Utils: compares · Uses image conversion/comparison helpers for pixel scoring.
- Decompose → Image Utils: segments · Uses image preprocessing and region extraction utilities.
- Compositor → SVG Lib: merges · Composes leaf SVG structures under a single root.
- Visualizer → SVG Renderer: renders · Builds visual comparisons from rendered SVGs.
- Visualizer → Image Utils: formats · Prepares image grids and side-by-side views.
- Decompose Viz → SVG Renderer: renders · Shows decomposition results as viewable artifacts.
- Viz Decompose → Decompose: uses · Standalone decomposition visualization workflow.
- Viz Decompose → Decompose Viz: shows · Generates inspection outputs for decomposition.