Ismail Ben Alla

Image-to-CadQuery

Custom Vision-to-Code Model

Multimodal · Oct 2026 · In progress

A neural network that turns a picture of a 3D part into code

Built withPyTorchResNet18TransformerHugging Face DatasetspytesttorchvisionHugging Face TransformerstorchrunRuff

Image-to-CadQuery — overview

Overview

CadQuery is a Python library for describing 3D parts as code. I wanted to know whether a model trained purely from data could look at a rendered CAD part and write the CadQuery script that rebuilds it, with no LLM API and no prompt engineering involved.

I built an encoder-decoder model in plain PyTorch: a ResNet18 turns the image into a 7x7 grid of visual tokens, and a 6-layer Transformer decoder writes the script one token at a time while attending to those image tokens. Around it I wrote a typed configuration, a data pipeline for the GenCAD-Code dataset (147k image/script training pairs), a training loop that scales from a laptop CPU to multiple GPUs and machines, an inference CLI, and a CPU-only pytest suite.

The pipeline is verified end to end: in an overfit sanity check on real data, the model memorized 8 samples and regenerated their scripts almost exactly. A full GPU training run and evaluation of the generated code (valid-syntax rate and IoU against the true solid) are the next milestone.

The question

Can a purpose-built ResNet encoder and autoregressive Transformer decoder learn to translate CAD images directly into editable CadQuery programs?

Key features

How it works

  1. Prepare data

    Each GenCAD-Code render is resized to 224x224 and normalized; each script is tokenized with GPT-2 BPE and wrapped as BOS, code, EOS, with a dedicated pad token so the model still learns when to stop.

  2. Encode the image

    A ResNet18 without pooling produces a 7x7 feature grid, flattened into 49 image tokens with learned 2D positions.

  3. Decode the code

    A 6-layer pre-norm Transformer decoder uses masked self-attention over the code written so far and cross-attention to the image tokens, with the output layer tied to the token embedding.

  4. Train

    Teacher-forced next-token cross-entropy, AdamW with separate learning rates for encoder and decoder, linear warmup then cosine decay, and gradient clipping.

  5. Generate

    The image is encoded once, then the decoder appends one token at a time until every sequence emits EOS or reaches the token limit.

Examples

Input

Overfit sanity check: 8 real GenCAD-Code samples, 26.4M-parameter model, 150 steps on a laptop CPU (about 15 minutes)

Output

Validation loss fell from 10.64 to 0.06; reloading the checkpoint and generating from the 8 training images reproduced the scripts at 98-100% token accuracy (4 of 8 exactly).

The gate before full training: proof that data, masking, and loss are wired correctly, not a measure of generalization.

Tech stack

What I learned

Limitations