From Procedurally Generated Staves to Real Engravings: A Convolutional–Transformer Encoder–Decoder for Optical Music Recognition
Abstract
A convolutional–Transformer encoder–decoder (8.6M parameters) maps single-staff images to tokens in the PrIMuS semantic encoding. Trained only on procedurally generated staves, it reaches a symbol error rate (SER) of 2.8% on clean and 7.3% on scan-degraded synthetic test images, but 73.6% on real engravings: 37 vocal-line excerpts from held-out songs of the CC0 OpenScore Lieder corpus, rendered with MuseScore. Clefs and key signatures transfer; time signatures do not, and the autoregressive decoder then imposes the misread metre on barlines and durations. Fine-tuning for 3,000 steps on 5,327 real engravings from 1,242 other songs, mixed half and half with synthetic data, lowers held-out real-engraving SER to 1.9% (75.7% of excerpts exact) and also improves the synthetic tests (1.8% clean, 5.2% degraded). The worst held-out error involves accidentals that hold through the measure, a convention the synthetic engraver does not model. A scanned orchestral page is still not transcribed usefully. We also report fixed-shape batching as a remedy for shape-dependent memory growth when training on Apple-silicon GPUs.
1 Introduction
Optical music recognition (OMR) converts images of sheet music into a symbolic encoding that software can play, search and edit. Recent systems treat a single staff as a sequence-recognition problem, much like line-level text recognition: an image encoder reads the staff left to right and a decoder emits one symbol per step, which removes the hand-built segmentation stages of earlier pipelines.
We describe a small, reproducible system of this kind and test how far training on synthetic staves carries over to real engraving. Its contributions are:
1. A generator, engraver and degradation model that produce unlimited labelled monophonic staves in the PrIMuS semantic token format, with fixed seeds per split so validation and test sets never change between runs.
2. A CNN–Transformer encoder–decoder trained from scratch on one consumer GPU in 78 minutes, reaching 2.8% SER on clean and 7.3% SER on degraded synthetic test staves.
3. A real-engraving evaluation on held-out songs of the OpenScore Lieder corpus, split by song, which shows that the synthetic-only model fails (73.6% SER) and diagnoses why.
4. Song-level fine-tuning on MuseScore renders of the same corpus that lowers held-out SER to 1.9% while also improving synthetic accuracy.
5. A diagnosis of, and fix for, memory growth when training variable-width sequence models on Apple MPS.
The scope is narrow: one voice per staff; no chords, ties, tuplets, slurs or dynamics; beams only in the real engravings, not in the synthetic data; and clean digital engravings rather than scans for the real-data experiments.
2 Related work
End-to-end monophonic OMR. Calvo-Zaragoza and Rizo introduced PrIMuS, a dataset of more than 80,000 printed single-staff monophonic scores with paired images and symbolic encodings, and showed that a convolutional–recurrent network trained with CTC transcribes whole staves without segmentation [1]. Camera-PrIMuS added photographic distortions to the same incipits to study robustness [2]. Our token vocabulary follows their semantic encoding so that a model trained here can be fine-tuned on either corpus without changing its output layer.
Sequence models for recognition. That design combines a convolutional–recurrent network from scene-text recognition [3] with the CTC loss [4]. Attention-based encoder–decoders replaced recurrence with self-attention [5] and are now common for text-line recognition, for example with Transformer decoders over image features [6]. End-to-end transcription has since been extended to two-staff piano scores, together with the GrandStaff dataset of 53,882 single-system piano scores [7]. Calvo-Zaragoza et al. survey the field [8].
Synthetic data. PrIMuS pairs real incipits with rendered images [1]. Here we also generate the music itself, which gives unlimited, exactly labelled data and controlled degradation at the cost of realism. For real engraving we use the OpenScore Lieder corpus, about 1,350 nineteenth-century songs transcribed in MuseScore and released under the CC0 public-domain dedication [9, 10]; rendering its scores yields real engraving with exact labels.
3 Method
3.1 Representation. A staff is a sequence of tokens in the PrIMuS semantic encoding: clef-G2, keySignature-DM, timeSignature-6/8, note-F#4_quarter., rest-eighth, barline. Each note token joins a sounding pitch (letter, alteration, octave) and a duration (eight values, including dotted ones). The vocabulary has 723 tokens: 4 special, 2 clefs, 7 keys, 5 time signatures, 1 barline, 8 rests and 696 notes.
3.2 Data generator. Each staff draws a clef (G2 or F4), a key (C, G, D, A, F, B♭ or E♭ major) and a time signature (2/4, 3/4, 4/4, 3/8, 6/8), then 2–4 measures. Durations are sampled with weights favouring quarters and eighths and fill each measure exactly. Pitches follow a random walk, mostly by step with occasional leaps, clamped to the clef’s written range (A3–C6 treble, C2–E4 bass). Notes take the key’s alteration; 8% receive a different explicit accidental and 12% of events are rests.
3.3 Engraver. A purpose-written renderer draws each staff at 128 px height with a 10 px staff space: clefs, key signatures, time signatures, noteheads (filled or hollow), stems, flags, dots, ledger lines, accidentals, rests and barlines. Spacing depends on duration plus small random jitter, and line thickness varies between images. Beams, chords and multiple voices are not drawn.
3.4 Degradation model. At strength s ∈ [0, 1], each transform fires with probability proportional to s: rotation up to ±1.8°, rescaling to 0.85–1.1, ink spread or thinning, Gaussian blur, a horizontal lighting gradient, contrast compression, Gaussian noise and JPEG compression at quality 25–70. Training samples s uniformly from [0, 1] per image.
3.5 Architecture. A strided convolution and three convolutional blocks reduce the image by 16 in height and 8 in width. The remaining height is folded into the channels, so each column becomes one frame. A linear layer projects frames to d = 256, sinusoidal positions are added, and a 4-layer Transformer encoder (8 heads, feed-forward 1,024, pre-norm) contextualises them. A 4-layer Transformer decoder with causal self-attention and cross-attention over the encoder output predicts the next token.
model architecture · 8.6M parameters
The image is encoded once; decoding then runs one token per step until the end token.
3.6 Training and decoding. The loss is cross-entropy (1) with label smoothing 0.1 under teacher forcing, ignoring padding. Inference uses greedy decoding or beam search with width k, ranking finished hypotheses by length-normalised log-probability.
ℒ = −∑t=1T log pθ(yt | y<t, x) (1)
3.7 Real-engraving data. From each song of the OpenScore Lieder corpus we take the vocal line and cut 2–3-measure windows the vocabulary can express: treble or bass clef, at most three sharps or flats, one of the five supported metres, no ties, tuplets, chords, grace notes, pickups or mid-window changes, at least four notes, rests no more than a third of the notes, and no whole-bar rests (engravers draw these as a centred whole rest whatever the metre). Each window is rebuilt without lyrics, dynamics or text, engraved with MuseScore 4.6.5 at 300 dpi, scaled so the staff space is 10 px, and cropped to the same 128 px band as the synthetic images. Unlike the synthetic data, these images contain beams, the Leland music font, justified spacing, and the convention that an accidental holds for the rest of its measure.
4 Experimental setup
Splits. Sample i of split s is generated from a seed derived from the pair (s, i), so the train, validation and test streams never overlap and are identical across runs. Validation uses 512 staves, scored twice: clean (s = 0) and degraded (s = 1, the same staves). Each test set has 1,000 staves from the test stream, again clean and degraded.
Training. 4,000 steps at batch 32 (128,000 generated staves), AdamW (β = 0.9, 0.98; weight decay 0.01), peak learning rate 5 × 10⁻⁴ with 400 warmup steps and cosine decay to 5% of the peak, gradient-norm clipping at 1.0, dropout 0.1. Every batch is padded to 1,024 × 40 (Section 6). The checkpoint with the lowest degraded-validation SER is kept; it was the final one. Training took 78 minutes on an Apple M4 GPU through PyTorch 2.10’s MPS backend. One seed was run.
Real-engraving splits. Songs, not excerpts, are split. The test set is 60 songs drawn with seed 0; 37 of them yield a valid window and contribute their first one. Fifty further songs (seed 1) give 234 validation excerpts, and the remaining 1,242 songs give 5,327 training excerpts, up to eight per song at a two-measure stride. No test song contributes training data.
Fine-tuning. Starting from the synthetic-only checkpoint, we train 3,000 steps at batch 32 with peak learning rate 2 × 10⁻⁴ and 200 warmup steps. Each sample comes from the real-engraving training set with probability 0.5 and otherwise from the synthetic generator, and degradation with strength drawn from [0, 1] is applied to both. Real images wider than 1,016 px are narrowed so every batch keeps the fixed shape. The checkpoint with the lowest real-engraving validation SER is kept (it was the final one). Fine-tuning took about 65 minutes of compute.
Metrics. SER is the token-level edit distance between prediction and reference, summed over the corpus and divided by the total reference length. Pitch ER and duration ER apply the same measure after reducing each token to its pitch part or its duration part; a rest counts as the pitch “rest” plus its duration, and other tokens keep their full form in both. Sequence accuracy is the share of staves transcribed exactly.
5 Results
On 1,000 held-out synthetic staves per condition the synthetic-only model reaches 2.8% SER on clean renders and 7.3% under degradation; degradation roughly multiplies every error rate by 2.5 to 3. Real engravings, and the effect of fine-tuning on them, are reported at the end of this section.
Table 1. Greedy decoding, best checkpoint (step 4,000).
runs/base/log.csv · validation at steps 500 to 4,000
Get Jung-Hua Liu’s stories in your inbox
Join Medium for free to get updates from this writer.
Validation error was still falling at the last check, so these numbers are not converged.
Beam search. On a 200-staff subset, beam search with k = 4 lowers degraded SER from 6.9% to 6.3% and clean SER from 2.4% to 2.3% relative to greedy decoding on the same staves. The small gain suggests that most errors are not close calls between competing hypotheses.
Error analysis. Pitch errors outnumber duration errors about two to one in both conditions. Among substitutions in equal-length predictions, the ten most frequent all involve accidentals or dots:
Table 2. Most frequent substitutions on the degraded test set.
Two causes fit this pattern. Explicit accidentals are rare by construction (8% of notes), so the model sees few examples of each sign at each position. The signs are also small, about 10 × 20 px before an 8× horizontal and 16× vertical reduction, so blur and noise remove much of what distinguishes a sharp from a flat or natural. Several references are unusual spellings (E#, Cb) that the generator produces but real music seldom uses.
Qualitative example. On one degraded staff in D major and 6/8, the prediction matched the reference on 14 of 15 tokens; the single error read the final dotted half note as a plain half, a missed augmentation dot.
Real engravings. On the 37 held-out OpenScore excerpts the synthetic-only model’s SER is 73.6%, and no excerpt is transcribed exactly. It reads every clef and 34 of 37 key signatures correctly but outputs 3/8 as the time signature for all 37. The decoder then places barlines to fit that metre and drops or merges notes to fill them. Scored on notes and rests alone, ignoring barlines and time signatures, 56% of pitches and 65% of durations are still wrong, consistent with the model never having seen beams. After fine-tuning, the same excerpts give 1.9% SER:
Table 3. Greedy decoding on the synthetic sets, beam search (k = 4) on the OpenScore excerpts.
Real-engraving validation SER fell from 9.4% at step 500 to 2.3% at step 3,000. The median held-out excerpt is now transcribed perfectly, and the synthetic tests improved rather than degrading. The worst held-out excerpt (17% SER) shows one remaining error: a B-flat written once, which in standard engraving holds for the rest of the measure, was read as B natural on its unmarked repetitions. The synthetic engraver redraws every accidental, so it never teaches this convention.
6 Training on Apple MPS: shape-dependent memory growth
Padding every batch to one fixed shape kept GPU memory flat at 5.7 GB, where variable shapes had grown it to 24 GB within 150 steps. With batches padded only to their own maximum width, step time rose from about 0.8 s to over 10 s and the process pushed the host into swap. Repeating one fixed batch showed no growth (6.9 GB, constant), which isolated batch shape as the cause; our reading is that the MPS caching allocator keeps separately sized buffers for each distinct shape and did not reuse them across shapes.
Three changes removed the problem. Every batch is padded to 1,024 px × 40 tokens, which covers the widest generated staff (968 px) and the longest sequence (37 tokens), so the GPU sees one shape. The first convolution became strided, which removes full-resolution activations, the largest memory cost. The allocator is capped through PYTORCH_MPS_HIGH_WATERMARK_RATIO, so a future leak fails with an error instead of degrading the whole machine.
Fixed padding wastes compute on short staves (the median width is 443 px), but on this hardware a stable 0.7–1.0 s per step beat a nominally cheaper schedule that slowed without bound. Bucketing into a small number of fixed widths would recover some of the waste.
7 Discussion and limitations
The results show that a small encoder–decoder trained on generated staves does not transfer to real engraving on its own, and that a few thousand real engravings with exact labels, rendered from an open corpus, close most of the gap. They do not show performance on scanned or photographed scores, and several limits remain.
• Engraving, not scanning. The real-engraving tests use clean digital renders from one engraver (MuseScore with its default Leland font). Other engravers, older printed editions and camera capture are untested, and our degradation model only approximates scan damage.
• Restricted notation. One voice per staff, two clefs, seven major key signatures and five metres; no chords, ties, tuplets, grace notes, slurs, dynamics or lyrics. 23 of the 60 sampled test songs had no expressible window, and excerpts were filtered to the vocabulary, which favours the model.
• Small real test set. 37 excerpts from 37 songs, with per-excerpt SER between 0% and 17%, so the 1.9% carries substantial sampling uncertainty.
• Single runs. One seed per stage and no hyperparameter search or ablation, for example of the real-to-synthetic mixing ratio.
• No external baseline. No comparison with a CRNN–CTC model and no evaluation on PrIMuS or Camera-PrIMuS, so the numbers are not directly comparable with published figures.
Real-score check. To probe the domain gap directly, we ran the model on six staves cropped from the first page of Holst’s The Planets, “Mars” (a public-domain work; a low-resolution page image from the Dover full-score edition’s product listing, ISBN 0–486–29277–0). No staff was transcribed usefully. The model read the alto and bass clefs of the violas, cellos and basses as treble clefs, added key signatures where the page has none, could not express the 5/4 metre, which is outside its vocabulary, and turned beamed triplets and eighths into unrelated note values; only the violin and timpani clefs and the repeated-note pattern of the violins carried over. The page also falls outside the degradation model: its staff lines are about 3.5 px apart and mostly broken, so the staves had to be cropped by hand. The fine-tuned model also fails on this page: clean engraving was the easier half of the gap, while scan quality and missing notation (alto clef, 5/4, triplets) remain. Crops and raw predictions from both models are in the code repository.
Future work. Teaching the synthetic engraver the measure-scope accidental rule targets the error seen in the worst held-out excerpt. Beyond that: extending the vocabulary and engraver to the alto clef, more metres, ties and tuplets; rendering corpus excerpts in other engravers and fonts; fine-tuning on Camera-PrIMuS to cover photographed pages; and a CTC head as a faster baseline.
8 Conclusion
A CNN–Transformer encoder–decoder trained on generated staves reaches 2.8% SER on clean and 7.3% on degraded synthetic images, but 73.6% on real engravings from held-out songs. Fine-tuning on 5,327 MuseScore renders of an open CC0 corpus, split by song, lowers that to 1.9% while improving the synthetic results. Open digital score collections are thus a practical source of real-engraving training data. Scanned pages remain unsolved. For practitioners training sequence models on Apple GPUs, fixed-shape batching is a simple guard against shape-dependent memory growth.
Code and data availability. Code, training logs, evaluation results and the real-engraving test crops are at https://github.com/gwrxuk/omr-transformer. Synthetic data are generated from fixed seeds, and the real-engraving training set is rebuilt from the CC0 OpenScore Lieder corpus by a script in the repository, so no dataset needs to be distributed.
References
1. J. Calvo-Zaragoza and D. Rizo. End-to-end neural optical music recognition of monophonic scores. Applied Sciences, 8(4):606, 2018. doi:10.3390/app8040606
2. J. Calvo-Zaragoza and D. Rizo. Camera-PrIMuS: Neural end-to-end optical music recognition on realistic monophonic scores. In Proc. 19th International Society for Music Information Retrieval Conference (ISMIR), pages 248–255, 2018.
3. B. Shi, X. Bai and C. Yao. An end-to-end trainable neural network for image-based sequence recognition and its application to scene text recognition. IEEE Transactions on Pattern Analysis and Machine Intelligence, 39(11):2298–2304, 2017. doi:10.1109/TPAMI.2016.2646371
4. A. Graves, S. Fernández, F. Gomez and J. Schmidhuber. Connectionist temporal classification: labelling unsegmented sequence data with recurrent neural networks. In Proc. 23rd International Conference on Machine Learning (ICML), pages 369–376, 2006. doi:10.1145/1143844.1143891
5. A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, Ł. Kaiser and I. Polosukhin. Attention is all you need. In Advances in Neural Information Processing Systems 30, 2017.
6. M. Li, T. Lv, J. Chen, L. Cui, Y. Lu, D. Florencio, C. Zhang, Z. Li and F. Wei. TrOCR: Transformer-based optical character recognition with pre-trained models. In Proc. AAAI Conference on Artificial Intelligence, 37(11):13094–13102, 2023. doi:10.1609/aaai.v37i11.26538
7. A. Ríos-Vila, D. Rizo, J. M. Iñesta and J. Calvo-Zaragoza. End-to-end optical music recognition for pianoform sheet music. International Journal on Document Analysis and Recognition, 26(3):347–362, 2023. doi:10.1007/s10032–023–00432-z
8. J. Calvo-Zaragoza, J. Hajič Jr. and A. Pacha. Understanding optical music recognition. ACM Computing Surveys, 53(4):1–35, 2020. doi:10.1145/3397499
9. M. Gotham and P. Jonas. The OpenScore Lieder Corpus. In Music Encoding Conference Proceedings 2021, pages 131–136. Humanities Commons, 2022. doi:10.17613/1my2-dm23
10. M. Gotham, P. Jonas, B. Bower, W. Bosworth, D. Rootham and L. VanHandel. Scores of scores: an OpenScore project to encode and share sheet music. In Proc. 5th International Conference on Digital Libraries for Musicology (DLfM), pages 87–95, 2018. doi:10.1145/3273024.3273026






