Mozilla Public License 2.0

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度分析:SVG Differential Rendering 项目 PyTorch-SVGRender

项目背景与痛点

在图像处理领域,矢量图形以其无损缩放、清晰度不受分辨率影响等特性备受青睐。然而,传统的矢量图形生成方式依赖于手动设计或软件绘制,这无法适应规模化生产的需要。尤其是在大规模数据生成与处理场景下,如何通过自动化手段生成高质量的矢量图形成为了一个痛点。此时,“ximinng/PyTorch-SVGRender”项目应运而生,它专注于利用神经网络进行矢量图形的生成与编辑。其核心任务是在给定文本、图像或矢量图形的输入条件下,自动生成对应的矢量图形(SVG)。

核心技术揭秘

PyTorch-SVGRender 项目采用神经网络作为核心技术,实现了从文本、图像到矢量图形的转换及矢量图形的编辑功能。其技术架构可以概括如下:

graph TD A[用户输入] --> B[预处理] B --> C{"类型检测"} C -->|"图像"| D[图像到SVG] C -->|"文本"| E[文本到SVG] C -->|"SVG文件" | F[SVG编辑] D --> G[SVG渲染] E --> G F --> G G --> H[输出SVG]
  1. 预处理:对输入数据进行格式化和标准化,确保后续处理的一致性。
  2. 类型检测:识别输入数据的类型,包括文本、图像或已有SVG文件。
  3. 图像到SVG:对于图像输入,项目利用神经网络学习图像特征,并将其映射到SVG指令集,生成相应的矢量图形。
  4. 文本到SVG:对于文本输入,模型会根据文本内容生成一系列矢量路径和形状,形成SVG格式的图形输出。
  5. SVG编辑:提供对SVG文件的编辑功能,允许用户进行修改和扩展。

整体来看,PyTorch-SVGRender 项目的核心在于构建一个能够理解并转化矢量图形的神经网络模型,并且这是一个复杂的多模态处理任务,需要涵盖特征提取、模式识别与生成多个阶段。

功能亮点与差异

相较于同类竞品,PyTorch-SVGRender 有几个明显的优势:

  • 多模态处理:项目支持从文本、图像到SVG的全链条转换,不仅限于单一输入形式,这大大扩展了其适用范围。
  • 神经网络生成:相较于通过传统算法解析和生成,该项目采用深度学习技术,能够生成更加符合预期、结构更复杂的SVG图形。
  • 可编辑性:此外,项目还提供SVG的编辑功能,这为用户提供了一个灵活多变的工具,可进一步定制和优化生成图形。

相比之下,市面上的许多同类工具通常只支持单一转换场景,并且生成质量或灵活性不足。

应用场景与落地建议

在生产环境中,PyTorch-SVGRender 适用于需要高质量矢量图形处理的各种业务场景,包括但不限于:

  • 数据科学与可视化:利用其生成的SVG图形进行复杂数据的展示和分析。
  • UI/UX 设计:设计人员可以生成自定义图标和图例,实现更高效的原型制作和视觉测试。
  • 动态文档生成:在文稿编辑器或出版系统中,可以自动生成高质量矢量图。

使用时,需要注意的是,由于其采用了深度学习方法,因此在部署阶段需要考虑计算资源的配置,确保模型加载和推理的速度满足业务需求。

综合评价

综上所述,PyTorch-SVGRender 项目结合深度学习与矢量图形处理,提供了多模态输入输出的矢量图形生成与编辑能力。虽然优势显著,但也存在模型训练与调优复杂度高的挑战以及对计算资源的需求。对于寻求高质量矢量图形解决策略的团队来说,PyTorch-SVGRender 是一个值得考虑的技术选项。

关于

GitHub 原始描述(英文)

SVG Differentiable Rendering: Generating vector graphics using neural networks. Support: text-to-SVG, Image-to-SVG, SVG Editing.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

Python
Shell

README

Pytorch-SVGRender

pyhton license website docs group

Pytorch-SVGRender

Pytorch-SVGRender: The go-to library for differentiable rendering methods for SVG generation.

Updates • Table of Contents • Installation • Quickstart • FAQ • TODO • Contribution • Acknowledgment • Citation • Licence

🔥 Recent Updates

  • [12/2023] 🔥 We open-sourced Pytorch-SVGRender V1.0.

💡 Table of Contents

▴ Back to top

1. Image Vectorization

  • DiffVG: Differentiable Vector Graphics Rasterization for Editing and Learning (SIGGRAPH 2020)

    [Project] [Paper] [Code]

    DiffVG is a differentiable rasterizer for 2D vector graphics. This repository is heavily based on DiffVG.

  • LIVE: Towards Layer-wise Image Vectorization (CVPR 2022)

    [Project] [Paper] [Code]

  • CLIPasso: Semantically-Aware Object Sketching (SIGGRAPH 2022)

    [Project] [Paper] [Code]

  • CLIPascene: Scene Sketching with Different Types and Levels of Abstraction (ICCV 2023)

    [Project] [Paper] [Code]

2. Text-to-SVG Synthesis

⚙️ Installation

🛠️ Step 1: Set Up the Environment

To quickly get started with PyTorch-SVGRender, follow the steps below.
These instructions will help you run quick inference locally.

🚀 Option 1: Standard Installation

Run the following command in the top-level directory:

chmod +x script/install.sh
bash script/install.sh
🐳 Option 2: Using Docker
chmod +x script/run_docker.sh
sudo bash script/run_docker.sh

👩‍🎨🎨 Quickstart

▴ Back to top

For more information, read the docs.

1. Basic Usage

DiffVG vectorizes any raster images:

python svg_render.py x=diffvg target='./data/fallingwater.png'
# change 'num_paths' and 'num_iter' for better results
python svg_render.py x=diffvg target='./data/fallingwater.png' x.num_paths=512 x.num_iter=2000

LIVE vectorizes the raster emojis images (in original PNG format):

python svg_render.py x=live target='./data/simile.png'
# change 'num_paths' and 'schedule_each' for better results
python svg_render.py x=live target='./data/simile.png' x.num_paths=5 x.schedule_each=1

CLIPasso synthesizes vectorized sketches from images:

note: first download the U2Net model sh script/download_u2net.sh.

python svg_render.py x=clipasso target='./data/horse.png'

CLIPascene synthesizes vectorized sketches from images:

note: first download the U2Net model bash script/download_u2net.sh.

python svg_render.py x=clipascene target='./data/ballerina.png'

CLIPDraw synthesizes SVGs based on text prompts:

python svg_render.py x=clipdraw "prompt='a photo of a cat'"

StyleCLIPDraw synthesizes SVG based on a text prompt and a reference image:

python svg_render.py x=styleclipdraw "prompt='a photo of a cat'" target='./data/starry.png'

CLIPFont styles vector fonts according to text prompts:

python svg_render.py x=clipfont "prompt='Starry Night by Vincent van gogh'" target='./data/alphabet1.svg'

Note: Since the following methods rely on Stable Diffusion, add diffuser.download=True to the command the first time you run the script.

SVGDreamer generates diverse SVG styles based on text prompts. It supports six vector primitives: Iconography, Sketch, Pixel Art, Low-Poly, Painting, and Ink & Wash.

# 🖼️ Primitive: Iconography
## 1. German shepherd
python svg_render.py x=svgdreamer state.mprec='fp16' "prompt='A colorful German shepherd in vector art. tending on artstation.'" save_step=50 x.guidance.n_particle=6 x.guidance.vsd_n_particle=4 x.guidance.phi_n_particle=2 result_path='svgdreamer/GermanShepherd'
## 2. sydney opera house
python svg_render.py x=svgdreamer state.mprec='fp16' "prompt='Sydney opera house. oil painting. by Van Gogh'" save_step=50 x.guidance.n_particle=6 x.guidance.vsd_n_particle=4 x.guidance.phi_n_particle=2 x.num_paths=512 result_path='svgdreamer/SydneyOperaHouse'

# 🔺 Primitive: Low-Poly
python svg_render.py x=svgdreamer state.mprec='fp16' "prompt='A picture of a bald eagle. low-ploy. polygon'" x.style='low-poly' save_step=50 x.guidance.n_particle=6 x.guidance.vsd_n_particle=4 x.guidance.phi_n_particle=2 x.grid=30 x.guidance.num_iter=1000 result_path='svgdreamer/BaldEagle'

# 🎨 Primitive: Pixel Art
python svg_render.py x=svgdreamer state.mprec='fp16' "prompt='Darth vader with lightsaber. ultrarealistic. pixelart. trending on artstation.'" x.style='pixelart' save_step=50 x.guidance.n_particle=6 x.guidance.vsd_n_particle=4 x.guidance.phi_n_particle=2 x.guidance.num_iter=1000 result_path='svgdreamer/DarthVader'

# 🖌️ Primitive: Painting
python svg_render.py x=svgdreamer state.mprec='fp16' "prompt='self portrait of Van Gogh. oil painting. cmyk portrait. multi colored. defiant and beautiful. cmyk. expressive eyes.'" x.style='painting' save_step=50 x.guidance.n_particle=6 x.guidance.vsd_n_particle=4 x.guidance.phi_n_particle=2 x.guidance.t_schedule='randint' x.num_paths=1500 result_path='svgdreamer/VanGogh_portrait'

# ✏️ Primitive: Sketch
python svg_render.py x=svgdreamer state.mprec='fp16' "prompt='A free-hand drawing of A speeding Lamborghini. black and white drawing.'" x.style='sketch' save_step=50 x.guidance.n_particle=6 x.guidance.vsd_n_particle=4 x.guidance.phi_n_particle=2 x.guidance.t_schedule='randint' x.num_paths=128 result_path='svgdreamer/Lamborghini'

# 🏯 Primitive: Ink & Wash
python svg_render.py x=svgdreamer state.mprec='fp16' "prompt='Big Wild Goose Pagoda. ink style. Minimalist abstract art grayscale watercolor.'" x.style='ink' save_step=50 x.guidance.n_particle=6 x.guidance.vsd_n_particle=4 x.guidance.phi_n_particle=2 x.guidance.t_schedule='randint' x.num_paths=128 x.width=6 result_path='svgdreamer/BigWildGoosePagoda'

VectorFusion synthesizes SVGs in various styles based on text prompts:

# 🖼️ Primitive: Iconography
python svg_render.py x=vectorfusion x.style='iconography' "prompt='a panda rowing a boat in a pond. minimal flat 2d vector icon. lineal color. trending on artstation.'"

# 🎨 Primitive: Pixel Art
python svg_render.py x=vectorfusion x.style='pixelart' "prompt='a panda rowing a boat in a pond. pixel art. trending on artstation.'"

# ✏️ Primitive: Sketch
python svg_render.py x=vectorfusion x.style='sketch' "prompt='a panda rowing a boat in a pond. minimal 2d line drawing. trending on artstation.'"

Following SVGDreamer, we’ve added three additional styles (Paining, Ink and Wash and low-ploy) to VectorFusion.

DiffSketcher synthesizes vector sketches based on text prompts:

# DiffSketcher
python svg_render.py x=diffsketcher "prompt='a photo of Sydney opera house'" x.token_ind=5 seed=8019

# DiffSketcher, variable stroke width
python svg_render.py x=diffsketcher "prompt='a photo of Sydney opera house'" x.token_ind=5 x.optim_width=True seed=8019

# DiffSketcher RGBA version
python svg_render.py x=diffsketcher "prompt='a photo of Sydney opera house'" x.token_ind=5 x.optim_width=True x.optim_rgba=True x.optim_opacity=False seed=8019

# DiffSketcher + style transfer
python svg_render.py x=stylediffsketcher "prompt='The French Revolution. highly detailed. 8k. ornate. intricate. cinematic. dehazed. atmospheric. oil painting. by Van Gogh'" x.token_ind=4 x.num_paths=2000 target='./data/starry.png' seed=876809

Word-As-Image follow a text prompt to style a letter in a word:

# Inject the meaning of the word bunny into the 'Y' in the word 'BUNNY'
python svg_render.py x=wordasimage x.word='BUNNY' prompt='BUNNY' x.optim_letter='Y'
# Change font: 'LuckiestGuy-Regular', default: 'KaushanScript-Regular'
python svg_render.py x=wordasimage x.word='DRAGONFLY' prompt='Dragonfly' x.optim_letter='Y' x.font='LuckiestGuy-Regular'

2. SDS Loss-based Approach

This approach leverages a pretrained text-to-image diffusion model as a powerful image prior to supervise the training of PyDiffVG, enabling SVG rendering that aligns with textual descriptions. This capability is fundamentally driven by Score Distillation Sampling (SDS), which serves as the key mechanism for bridging raster images from diffusion models to the SVG domain. This allows for the optimization of SVG parameters without requiring explicit image supervision.

Notable methods based on this approach include VectorFusion, DiffSketcher, and SVGDreamer.

We focus solely on evaluating the performance of SDS, meaning no additional loss functions are applied:

# Standard SDS loss
python svg_render.py x=vectorfusion "prompt='a panda rowing a boat in a pond. minimal flat 2d vector icon. lineal color. trending on artstation.'"

# Input Augmentation SDS loss (LSDS loss)
python svg_render.py x=vectorfusion x.style='sketch' "prompt='an elephant. minimal 2d line drawing. trending on artstation.'" x.skip_live=True

# Input Augmentation SDS loss (ASDS loss)
python svg_render.py x=diffsketcher "prompt='an elephant. minimal 2d line drawing. trending on artstation.'" x.token_ind=2 x.sds.grad_scale=1 x.sds.num_aug=4 x.clip.vis_loss=0 x.perceptual.coeff=0 x.opacity_delta=0.3 

# Vectorized Particle-based Score Distillation (VPSD loss)
python svg_render.py x=svgdreamer "prompt='a panda rowing a boat in a pond. minimal flat 2d vector icon. lineal color. trending on artstation.'" save_step=60 x.guidance.n_particle=6 x.guidance.vsd_n_particle=6 x.guidance.phi_n_particle=6 state.mprec='fp16'  

❓ FAQ

▴ Back to top

  • Q: Where can I get more scripts and visualizations?

  • A: Check the pytorch-svgrender.readthedocs.io.

  • Q: An error says HuggingFace cannot find the model in the disk cache.

  • A: Add diffuser.download=True to the command for downloading model checkpoints the first time you run the script.

  • Q: It says xFormers is not built with CUDA support or xFormers cannot load C++/CUDA extensions.

  • A: You need to install xFormers again using the command pip install --pre -U xformers instead of the conda one.

🗒 TODO

▴ Back to top

  • SVG Layout Pipeline: Given words and layout attrs, returning layout in SVG format.
  • integrated SVGDreamer & SVGDreamer supports fp16 optimization.

🤝 Contribution

  • We welcome all contributions! 🚀 If you’ve found a bug or developed a feature that could benefit others, feel free to submit a pull request. Your contributions are invaluable in improving PyTorch-SVGRender community and making it even better!

    For more details, please check our Contribution Guidelines.

    Tip: Before committing your changes, make sure to run test/test_svgrender.py to verify your code. ✅

💘 Acknowledgement

▴ Back to top

The project is built based on the following repository:

BachiLi/diffvg, huggingface/diffusers, threestudio-project/threestudio, yael-vinker/CLIPasso, ximinng/DiffSketcher, THUDM/ImageReward, advimman/lama

We gratefully thank the authors for their wonderful works.

📚 Citation

▴ Back to top

If you use this code for your research, please cite the following work:

@InProceedings{xing2024svgdreamer,
    author    = {Xing, Ximing and Zhou, Haitao and Wang, Chuang and Zhang, Jing and Xu, Dong and Yu, Qian},
    title     = {SVGDreamer: Text Guided SVG Generation with Diffusion Model},
    booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
    month     = {June},
    year      = {2024},
    pages     = {4546-4555}
}
@inproceedings{xing2023diffsketcher,
    title={DiffSketcher: Text Guided Vector Sketch Synthesis through Latent Diffusion Models},
    author={XiMing Xing and Chuang Wang and Haitao Zhou and Jing Zhang and Qian Yu and Dong Xu},
    booktitle={Thirty-seventh Conference on Neural Information Processing Systems (NeurIPS)},
    year={2023},
    url={https://openreview.net/forum?id=CY1xatvEQj}
}

Licence

▴ Back to top

This work is licensed under a Mozilla Public License Version 2.0.

评论

暂无评论