InkFormer: From Characters to Signatures via Style-Transformation-Aware Online Handwriting Generation
This repository provides the training, inference, and evaluation code for InkFormer-Char, the character-level handwriting generation model. Additionally, the training code for both the content and style evaluators is available.
- [2026/10/23] Code and pretrained weights are released.
- [2026/10/15] InkFormer is accepted by TMLR 2026 🎉🎉🎉.
InkFormer/
├── main.py # Training entry
├── inference.py # Inference entry
├── dataset_char.py / utils.py # Data loading and shared utilities
├── classifiers/ # Training code for the content/style classifiers (evaluators)
├── evaluation/ # Evaluation scripts (nDTW / content / style)
├── modules/ # Model architecture (Transformer_AR and encoders)
├── scripts/ # train.sh / train_cont.sh / train_sty.sh / infer.sh / evaluate.sh
└── data_ckpt/
├── ckpt/ # Model checkpoints
└── datas/ # Datasets
A Linux machine with NVIDIA GPUs is recommended. Install the dependencies:
conda create -n inkformer python=3.9 -y
conda activate inkformer
pip install -r requirements.txtMain dependencies: PyTorch, torchvision, accelerate, numpy, Pillow, matplotlib, tqdm, fastdtw, scipy, einops.
Download: Google Drive
After downloading, place the files under data_ckpt/ as follows:
data_ckpt/
├── ckpt/
│ ├── ar_generator_5000.pth # Generator weights (for inference)
│ ├── cont_evaluator_2000.pth # Content evaluator weights
│ ├── sty_evaluator_10000.pth # Style evaluator weights
│ ├── lookup_char2int.npy # Lookup table for content evaluation
│ └── lookup_idx2ori_on.npy # Lookup table for style evaluation
└── datas/
├── train/ # Training set (6 pools)
│ ├── Pot1.0Train_pp1.npy
│ ├── Pot1.1Train_pp1.npy
│ ├── Pot1.2Train_pp1.npy
│ ├── Pot1.0Test_pp1.npy
│ ├── Pot1.1Test_pp1.npy
│ └── Pot1.2Test_pp1.npy
├── test/ # Test set
│ └── competition_set_pp1.npy
└── standard/ # Standard characters
└── stand_zi.npy
bash scripts/train.sh- Uses 4 GPUs by default (
CUDA_VISIBLE_DEVICES=0,1,2,3inscripts/train.sh; adjust to your setup) - Multi-GPU training is launched with accelerate; run
accelerate configfirst if you have not configured it yet - Key hyperparameters (batch-user, sample-per-user, n-reference, lr, epoch-end, etc.) can be edited directly in
scripts/train.sh - Checkpoints are saved every 500 epochs (save-epoch) to
data_ckpt/ckpt/ar_generator_{epoch}.pth - To resume training, set
--epoch-beginto an existing epoch;data_ckpt/ckpt/ar_generator_{epoch_begin}.pthwill be loaded automatically
bash scripts/train_cont.sh # Content classifier → data_ckpt/ckpt/cont_evaluator_{epoch}.pth
bash scripts/train_sty.sh # Style classifier → data_ckpt/ckpt/sty_evaluator_{epoch}.pthIf logs/infer/syn_saver_ref_15.npy (the generator's inference output) exists, classifier training additionally monitors the accuracy on synthesized characters (Acc_syn); otherwise this monitoring is skipped automatically, which does not affect training or checkpoint saving.
bash scripts/infer.sh- Runs on a single GPU (GPU 0)
- Loads
data_ckpt/ckpt/ar_generator_5000.pthby default; edit the--epochargument inscripts/infer.shto use another checkpoint - Synthesized results are saved to
logs/infer/syn_saver_ref_15.npy - Side-by-side images of standard / synthesized / ground-truth characters are written to
logs/infer/img_infer/
bash scripts/evaluate.shRuns three evaluations on the inference output logs/infer/syn_saver_ref_15.npy:
- nDTW (
evaluation/evaluate_ndtw_1.py): trajectory similarity - Content score (
evaluation/evaluate_content_score.py): character recognizability, computed with the bundled content evaluator - Style score (
evaluation/evaluate_style_score.py): style similarity, computed with the bundled style evaluator
scripts/train.sh → data_ckpt/ckpt/ar_generator_*.pth (checkpoints)
scripts/infer.sh → logs/infer/syn_saver_ref_15.npy (synthesized results)
scripts/evaluate.sh → nDTW / content score / style score
- CASIA-OLHWDB, ICDAR-2013 Competition, MSDS and Make Me a Hanzi are valuable resources for this work.
If you find this work useful or use this code in your research, please consider citing the following paper.
@article{
pan2026inkformer,
title={InkFormer: From Characters to Signatures via Style-Transformation-Aware Online Handwriting Generation},
author={Wei Pan and Yilin Shi and Lianwen Jin},
journal={Transactions on Machine Learning Research},
issn={2835-8856},
year={2026},
url={https://openreview.net/forum?id=Nbhw2Y5Nmh},
note={}
}Our code is released under the MIT License. The pretrained weights, which are trained using part of the CASIA-OLHWDB dataset, follow the original license of the dataset.