primitive-operation-painter README

The project's README from github.com/wonderfulearth/primitive-operation-painter. As one markdown file: README.md.

Primitive Operation Painter

Primitive Operation Painter explores image generation as an explicit, human-readable drawing process. Instead of emitting pixels or latent variables directly, the model builds an image one primitive operation at a time.

Six examples of autoregressive completion

Six bundled 11-step inputs are shown in a 3-column × 2-row grid for three seconds, then the model adds one primitive at a time until every sample reaches 144 steps. The animation is a rendering of the model's operation history, not a pixel-space interpolation.

Why primitive operations?

Most image generators expose a prompt and a final image, while the intermediate process is difficult to inspect, explain, or take over. This project asks a different question: can an image model draw through a representation that a person can read and modify while it is working?

Here, a canvas is a sequence of background, ellipse, and rotated-rectangle operations. Each operation has an explicit centre, angle, width, height, shape type, and RGB colour. The model predicts these values autoregressively rather than directly changing pixels. A completed image is therefore also a drawing history: a concrete list of operations that can be rendered, inspected, or edited at the operation level.

Human and model co-creation

A model completion before and after a person edits a primitive-operation prefix

The top path shows the model completing an initial set of primitives on its own. The bottom path shows the intended collaboration loop: a person changes a visible operation in the prefix, then the model continues from that modified history. This makes the intervention part of the model's actual input rather than a post-hoc pixel edit.

The representation is directly editable; this repository currently provides the sequence format, inference, rendering, and training code needed to study that workflow. It does not yet provide a polished graphical editor or a general guarantee that a particular manual edit will produce a particular semantic outcome.

How the pipeline works

  1. Decompose images. The Rust/WGPU fast_shape_render converter approximates training images with primitive drawing operations and writes the CSV format consumed by the Python data loader.
  2. Encode operations. Every drawing step is represented by nine discrete tokens: x, y, angle, width, height, shape type, and RGB colour.
  3. Predict the next operation. The GPT model reads the preceding operation history and autoregressively predicts the next primitive field by field.
  4. Render, inspect, and continue. Decoded tokens reproduce a canvas. A person can provide or alter an operation prefix, and the model can complete the remaining sequence from that state.

Current scope and limitations

This repository contains the files required to use and continue training the released 144-step model: the model definition, token layout, full-sequence data encoder, training entry point, renderer, GPU image-to-sequence converter, and a six-sequence inference example.

License and data boundary

The code and released EMA inference weights are intended to be licensed under MIT. Training data is not included or redistributed. Anyone using this project is responsible for confirming they have the necessary rights for their own input and training data.

Setup

Use Python 3.10 or later and install a PyTorch build suitable for your system, then install the remaining dependencies:

python -m pip install -r requirements.txt

Set the training-data root with ANIME_PAINTER_DATA_DIR, or generate it with the bundled fast_shape_render converter. Its default output directory is data/output_256, which is also the Python programs' default data location. The data root must contain versioned CSV directories such as v1/*.csv.

$env:ANIME_PAINTER_DATA_DIR = 'D:\datasets\output_256'
python train_gpt_pretrain.py

Convert an image dataset to training sequences

fast_shape_render is a separate Rust/WGPU GPU converter. It resizes each input image to 256×256, approximates it with primitive drawing operations, and writes the CSV sequence layout consumed by the Python dataset loader.

Install the Rust toolchain, then set the input directory before running it. The converter requires a hardware GPU: DirectX 12 on Windows, Vulkan on Linux, and Metal on macOS. Its default output is data/output_256; no personal path is embedded in the converter.

$env:SHAPE_RENDERER_INPUT_DIR = 'D:\images\faces_256'

Push-Location fast_shape_render
cargo run --release
Pop-Location

Optional environment variables:

After conversion, either use the default data/output_256 location or point training and visualization to a custom output root with ANIME_PAINTER_DATA_DIR or --data-dir.

Six-sequence inference example

The repository includes six compact example sequences under example/sequences/v1/data_part_1.csv. Each one has eleven steps: a background operation followed by ten primitives. They are a quick inference demonstration, not training data: the released trainer requires complete 144-step sequences.

Download or copy the complete model package into a model folder at the project root. Keep both package files directly inside that folder:

primitive_operation_painter/
├── example/
│   └── sequences/v1/data_part_1.csv
├── model/
│   ├── config.json
│   └── model.safetensors
└── example.py

The model/ folder is ignored by Git, so the weight is never added to the code repository. With this layout, run the demonstration without arguments:

python example.py

It renders a two-column, six-row PNG. The left side shows the eleven input operations; the right side starts from those same eleven operations and samples the remaining 133 steps to make a complete 144-step sequence. If model/, config.json, or model.safetensors is missing, example.py prints the expected location and exits before it tries to load or download a model.

To use a model package stored elsewhere, override the default explicitly:

python example.py `
  --model-dir path\to\primitive-operation-painter-weight

By default the result is written to example/example_inference.png. Use --csv-path, --output-path, --seed, or --device to override the example inputs and inference settings.

Recreate the autoregressive animation

Recreate the tracked README animation with the same local model package and seed:

python example.py `
  --gif-output example\autoregressive_prediction.gif

Use --gif-fps to change the prediction-frame rate; the input hold is always three seconds.

Both example.py and visualize.py use the same field-aware sampling schedule. For each generated token, temperature and top_k are evaluated as a * step + b from GPT_SAMPLING_CONFIG; step is the one-based drawing operation index, including the background operation. top_k is rounded down to an integer and clipped to the current field's valid token range.

Local visualization

visualize.py requires a local model package and a CSV data root. It never falls back to a personal absolute path or silently selects a checkpoint.

python visualize.py `
  --model-dir path\to\primitive-operation-painter-weight `
  --data-dir D:\datasets\output_256 `
  --num-tests 8

Continue training the released weight

Place or download the model package in a local directory containing config.json and model.safetensors, then point the trainer at your own CSV dataset and that directory:

$env:ANIME_PAINTER_DATA_DIR = 'D:\datasets\output_256'
python train_gpt_pretrain.py `
  --initial-model-dir path\to\primitive-operation-painter-weight

The trainer validates that the package is the compatible 144-step model before loading its EMA weights. New resumable training checkpoints are written to checkpoints_gpt_fullseq_144ctx_256reso/. On later runs, omit --initial-model-dir to resume from the newest local training checkpoint.

primitive-operation-painter (wonderfulearth/primitive-operation-painter)

Searching Database…

这是什么?一个自回归图像生成器,把图片构建为一系列基础绘图图元(背景、椭圆、旋转矩形)而非像素,因此整个绘画过程可以逐步检查和编辑。
它是现成的应用,还是我从命令行运行的研究代码?它是研究代码:用于推理、训练和可视化的 Python 脚本,外加一个名为“fastshaperender”的独立 Rust/WGPU 工具,用于把图像转换为训练序列。目前还没有成熟的图形编辑器。
我怎么运行自带的推理示例?把模型包(config.json 和 model.safetensors)下载到项目根目录的 model/ 文件夹,然后不带参数运行 example.py,即可渲染自带的六序列演示;可用 --csv-path、--output-path、--seed 或 --device 覆盖默认行为。
运行它之前我需要安装什么?模型代码需要 Python 3.10 或更新版本以及与你系统匹配的 PyTorch 构建;如果你还想自己运行“fastshaperender”转换器,则需要 Rust 工具链和一块硬件 GPU(DirectX 12、Vulkan 或 Metal)。
它附带训练数据或模型权重吗?不附带,训练数据、发布的权重和可续训的检查点都被有意排除在仓库之外,必须单独提供或下载。
代码和训练数据的许可情况如何?代码和发布的 EMA 推理权重拟采用 MIT 许可,但训练数据既不包含也不再分发,用户必须自行确认对所用数据的权利。
模型用来作画的“图元操作”到底是什么?每个操作是背景、椭圆或旋转矩形之一,由九个离散 token 定义:x、y、角度、宽度、高度、形状类型以及 RGB 颜色。
模型还在画的时候我能编辑画作吗?可以,预期的工作流允许人修改序列前缀中某个可见的操作,之后模型会从修改后的历史继续预测,而不是基于像素级的修补。
为什么要构建操作序列生成器,而不是普通的像素/提示词图像模型?作者想要一个人类能真正读懂并在生成过程中修改的图像流程,因为典型的文生图或潜在扩散模型会隐藏其中间步骤。
fastshaperender 是什么?它是一个独立的 Rust/WGPU GPU 工具,用图元绘制操作逼近训练图像,并把它们写成 CSV 序列供 Python 数据加载器使用。
Size
19.9 MB
Stars
227
Updated
CommitAug 24
Age
1mo
Commits · 90 Days