Instructions to use moonbox-dev/starvector-1b-im2svg-TensorRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- TensorRT
How to use moonbox-dev/starvector-1b-im2svg-TensorRT with TensorRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
StarVector 1B TensorRT
TensorRT engines of StarVector 1B, made by Moonbox for NVIDIA RTX GPUs. Each precision is one folder holding the
engine, trt_runner.py (a small numpy runner: TensorRT plus Polygraphy, no PyTorch) and a script that does the rest
of the pipeline. What the engine takes and returns is under How to run.
Compatibility. The engines are built hardware compatible (Ampere and newer: RTX 30, 40 and 50 series) and
version compatible (any TensorRT 10.x from 10.13 on, through its lean runtime). That costs a little speed against an
engine built for one GPU, but no rebuild is needed. Install the runtime with
pip install "tensorrt-cu12>=10.13,<11" "tensorrt-lean-cu12>=10.13,<11" polygraphy numpy.
Files
The last column is the benchmark (details under How to run).
| Precision | File | Size | sha256 | Benchmark |
|---|---|---|---|---|
| fp16 | fp16/decoder_0.engine |
669.1 MB | 58b21bb5bae7 |
rendering PSNR vs the icon 14.4 dB (ONNX fp16 13.9 dB, original 14.4); 1/8 SVGs identical to the ONNX fp16 build's, text similarity 0.82; 13.8 s per icon (ONNX fp16 31.8 s) |
| fp16 | fp16/decoder_1.engine |
722.5 MB | 25e39cdbc6d7 |
(the same build: numbers in its first row) |
| fp16 | fp16/decoder_2.engine |
722.5 MB | d40bcfdc80d9 |
(the same build: numbers in its first row) |
| fp16 | fp16/decoder_3.engine |
662.7 MB | 58bbf0351356 |
(the same build: numbers in its first row) |
| fp16 | fp16/decoder_split.json |
1.8 kB | 168cdfee078f |
(the same build: numbers in its first row) |
| fp16 | fp16/device_chain.py |
7.4 kB | f53dd63d5f53 |
(the same build: numbers in its first row) |
| fp16 | fp16/embed.engine |
245.7 MB | 85574a7ac1a5 |
(the same build: numbers in its first row) |
| fp16 | fp16/image_0.engine |
391.9 MB | cfe1e87d5194 |
(the same build: numbers in its first row) |
| fp16 | fp16/image_1.engine |
306.8 MB | 7e496c92679b |
(the same build: numbers in its first row) |
| fp16 | fp16/image_split.json |
0.3 kB | edf2215c97f8 |
(the same build: numbers in its first row) |
| fp16 | fp16/pipeline_trt.py |
5.0 kB | 3a834ed2568a |
(the same build: numbers in its first row) |
| fp16 | fp16/tokenizer.json |
3.5 MB | 677ed0fa17c7 |
(the same build: numbers in its first row) |
| fp16 | fp16/trt_runner.py |
4.4 kB | cbc9abb1f59c |
(the same build: numbers in its first row) |
How to run
StarVector 1B (im2svg) (ServiceNow / StarVector: image-to-SVG generation; a CLIP image encoder and an adapter feed a
StarCoderBase-1B code model fine-tuned to write SVG) as TensorRT engines built from this model's fp16 ONNX graphs,
plus pipeline_trt.py. The pipeline is the ONNX build's pipeline.py (the repository's processor and greedy
generate_im2svg) with every graph run as engines. The decoder is a fine-tune of bigcode/starcoderbase-1b, whose own
license is the BigCode OpenRAIL-M agreement (use restrictions in its Attachment A); StarVector's card states
Apache-2.0.
import sys
from PIL import Image
sys.path.insert(0, "fp16")
from pipeline_trt import StarVector
sv = StarVector("fp16")
open("icon.svg", "w").write(sv(Image.open("icon.png")))
Or python fp16/pipeline_trt.py icon.png icon.svg. It needs numpy, tokenizers, Pillow, TensorRT 10.13
(tensorrt-cu12 or tensorrt-lean-cu12) and Polygraphy. No PyTorch and no ONNX Runtime.
The engines. Together they compute the ONNX fp16/ graphs, except for the softmax (see Precisions).
image_0..1.engine:image[1, 3, 224, 224] (normalised) ->prefix[1, 257, 2048] (the CLIP ViT, ln_vision and the adapter).embed.engine:input_ids[1, S] ->inputs_embeds[1, S, 2048].decoder_0..3.engine: the StarCoderBase-1B decoder (24 layers, multi-query attention):inputs_embeds,position_ids[1, S],past_key_values.{0..23}[1, P, 256] ->logitsof the last position [1, 49156] andpresent.*. S is 1-259 new positions, P 0-2304 cached: the 259-position prefill (the image prefix and "<svg") plus up to 2,046 new tokens.image_split.json/decoder_split.json: the order of the pieces and the tensors passed between them. The image encoder is cut into pieces of at most 0.35 GB of weights and the decoder into pieces of at most 0.7 GB, so that each engine builds within the build machine's memory.
On the GPU. The pipeline (device_chain.py) keeps the decoder's cache on the GPU: each step reads one set of
buffers and writes the other, so a step allocates nothing. Each new token's embedding goes from the embedding engine
to the decoder without leaving the GPU, and so do the activations between the pieces. Only the token ids go in and the
logits come out.
Precisions. fp16/ is strongly typed: every tensor keeps the precision the ONNX graph gives it, with one change.
The attention softmax of the image encoder and the decoder is computed in fp32. In the fp16 graphs it is fp16;
ONNX Runtime's kernel runs it with fp32 arithmetic inside, while TensorRT would run it in fp16, and greedy decoding of
long outputs is sensitive to such differences. No int8: TensorRT's int8 needs calibrated activations, and the ONNX
int8 / int4 builds are weight-only MatMulNBits, which TensorRT doesn't take.
Benchmark.
- Setup: the ONNX build's benchmark. Eight synthetic icons (flat shapes on white, 256 px), greedy decoding, the same preprocessed image for both builds, on an RTX 3080 Laptop GPU.
- Score: how well each output's rendering (resvg, 256 px) matches the input icon: PSNR, averaged over the eight. An SVG that doesn't render (cut off at the length limit) counts as 0 dB. Three of the eight don't render for either build, as for the original.
- Pass mark (set before the first run): mean PSNR at most 1 dB below the ONNX fp16 build's.
| mean PSNR of the rendering vs the icon | SVGs identical to the ONNX fp16 build's | text similarity to its SVGs | seconds per icon | |
|---|---|---|---|---|
ONNX fp16/ (ONNX Runtime CUDA) |
13.9 dB | 8 / 8 | 1.00 | 31.8 |
TensorRT fp16/ |
14.4 dB | 1 / 8 | 0.82 | 13.8 |
The original (PyTorch fp16) scores 14.4 dB on the same test. Greedy SVG text diverges after small numeric differences in such long outputs, so text identity says little; the ONNX fp16 build matches the original's text for 1 of 8 too. Per icon, the TensorRT build's rendering scores the same as the ONNX build's on six of the eight and higher on two (24.1 vs 20.5 dB, 13.7 vs 13.4 dB). The seconds include the image encoder and every decoding step, up to 2,000 new tokens. The engines load on first use, so the first icon's time includes the loading. The longest outputs (about 2,000 tokens) take 26 s instead of 68-71 s.
How these were made
From StarVector 1B at revision 380ab95d25: each engine was built with TensorRT's builder from Moonbox's published
ONNX graph of the same model and precision. The network is strongly typed: every tensor keeps the precision the
ONNX graph gives it, so an fp16 engine computes what the fp16 ONNX build computes. Built on an RTX 3080 Laptop GPU,
it was benchmarked against the original before upload, and the sha256 column above is what the files must hash to.
License
Apache-2.0. These files are a derivative of the original model and carry its license: it is copied in this repository, and it governs your use of these files. The original model card follows unchanged.
Original model card
Model Card for StarVector
StarVector is a foundation model for generating Scalable Vector Graphics (SVG) code from images and text. It utilizes a Vision-Language Modeling architecture to understand both visual and textual inputs, enabling high-quality vectorization and text-guided SVG creation.
Model Details
Model Description
This is the model card for the StarVector model, a π€ transformers model. StarVector is a foundation model for generating Scalable Vector Graphics (SVG) code from images and text. It utilizes a Vision-Language Modeling architecture to understand both visual and textual inputs, enabling high-quality vectorization and text-guided SVG creation.
- Developed by: ServiceNow Research, Mila - Quebec AI Institute, ETS, Montreal.
- Shared by : Juan A Rodriguez, Abhay Puri, Shubham Agarwal, Issam H. Laradji, Sai Rajeswar, Pau Rodriguez, David Vazquez, Christopher Pal, Marco Pedersoli.
- Model type: Vision-Language Model for SVG Generation.
- Language(s) (NLP): English.
- License: Apache 2.0
Model Architecture
The StarVector architecture integrates an image encoder and a Large Language Model (LLM) Adapter to generate SVG code from both image and text inputs. Images are first converted into embeddings using a Vision Transformer (ViT), after which the LLM Adapter maps these embeddings into the LLM's embedding space to create visual tokens. Text prompts are handled through the LLMβs tokenizer and embedder. This unified multimodal approach ensures precise and contextually rich SVG output.
Model Sources
- Repository: https://github.com/joanrod/star-vector
- Paper: https://arxiv.org/abs/2312.11556
Uses
Direct Use
Image-to-SVG generation, Text-to-SVG generation.
Downstream Use
Creation of icons, logotypes, technical diagrams, and other vector graphics.
Out-of-Scope Use
Generating realistic photographic images or complex 3D graphics.
Bias, Risks, and Limitations
Potential biases may exist in the model due to the composition of the training data (SVG-Stack). The model's ability to perfectly vectorize all types of images and interpret all textual instructions may have limitations. Users should be aware of these potential issues, especially in critical applications.
Recommendations
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. Further investigation into the model's behavior across different types of inputs is recommended.
How to Get Started with the Model
Use the code below to get started with the model.
from PIL import Image
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoProcessor
from starvector.data.util import process_and_rasterize_svg
import torch
model_name = "starvector/starvector-1b-im2svg"
starvector = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, trust_remote_code=True)
processor = starvector.model.processor
tokenizer = starvector.model.svg_transformer.tokenizer
starvector.cuda()
starvector.eval()
image_pil = Image.open('assets/examples/sample-18.png')
image = processor(image_pil, return_tensors="pt")['pixel_values'].cuda()
if not image.shape[0] == 1:
image = image.squeeze(0)
batch = {"image": image}
raw_svg = starvector.generate_im2svg(batch, max_length=4000)[0]
svg, raster_image = process_and_rasterize_svg(raw_svg)
Training Details
Training Data
SVG-Stack: A dataset of over 2 million SVG samples.
Training Procedure
The model utilizes a Vision-Language Modeling architecture. Images are projected into embeddings via an image encoder, then mapped to the LLM hidden space using an LLM Adapter, generating Visual Tokens. Text conditioning is achieved with the LLM's tokenizer and embedder. The model learns to map token sequences (visual or textual) to SVG code.
Evaluation
Testing Data & Factors
Testing Data
SVG-Bench
Factors
SVG-Stack, SVG-Fonts, SVG-Icons, SVG-Emoji, SVG-Diagrams.
Models
StarVector models achieve state-of-the-art performance on SVG generation tasks
We provide Hugging Face π€ model checkpoints for image2SVG vectorization, for π« StarVector-8B and π« StarVector-1B. These are the results on SVG-Bench, using the DinoScore metric.
| Method | SVG-Stack | SVG-Fonts | SVG-Icons | SVG-Emoji | SVG-Diagrams |
|---|---|---|---|---|---|
| AutoTrace | 0.942 | 0.954 | 0.946 | 0.975 | 0.874 |
| Potrace | 0.898 | 0.967 | 0.972 | 0.882 | 0.875 |
| VTracer | 0.954 | 0.964 | 0.940 | 0.981 | 0.882 |
| Im2Vec | 0.692 | 0.733 | 0.754 | 0.732 | - |
| LIVE | 0.934 | 0.956 | 0.959 | 0.969 | 0.870 |
| DiffVG | 0.810 | 0.821 | 0.952 | 0.814 | 0.822 |
| GPT-4-V | 0.852 | 0.842 | 0.848 | 0.850 | - |
| π« StarVector-1B | 0.926 | 0.978 | 0.975 | 0.929 | 0.943 |
| π« StarVector-8B | 0.966 | 0.982 | 0.984 | 0.981 | 0.959 |
Note: StarVector models will not work for natural images or illustrations, as they have not been trained on those images. They excel in vectorizing icons, logotypes, technical diagrams, graphs, and charts.
As shown in the table above, StarVector-8B achieves the highest performance across all benchmark datasets, demonstrating its effectiveness in generating high-quality SVG code from images. The model's ability to understand and reproduce complex vector graphics makes it particularly valuable for applications requiring precise vectorization of icons, logos, and technical diagrams.
Summary
StarVector represents a significant advancement in the field of vector graphics generation. By combining the power of vision-language models with a comprehensive training dataset, we've created a system that can accurately translate images into high-quality SVG code. The model's performance on SVG-Bench demonstrates its effectiveness across a wide range of vector graphics tasks.
We believe that StarVector will enable new applications in design, illustration, and technical documentation, making vector graphics more accessible and easier to create. We invite the research community to build upon our work and explore new directions in this exciting field.
For more details, please refer to our paper and explore our code repository.
BibTeX entry and citation info
@misc{rodriguez2024starvector,
title={StarVector: Generating Scalable Vector Graphics Code from Images and Text},
author={Juan A. Rodriguez and Abhay Puri and Shubham Agarwal and Issam H. Laradji and Pau Rodriguez and Sai Rajeswar and David Vazquez and Christopher Pal and Marco Pedersoli},
year={2024},
eprint={2312.11556},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2312.11556},
}
- Downloads last month
- -
Model tree for moonbox-dev/starvector-1b-im2svg-TensorRT
Base model
starvector/starvector-1b-im2svg