Sheet Music Scanner

Sheet music scanning model I'm building for my musician wife. The model is designed to run on device in an iOS app for her iPad. I'm not a musician so any pointers would be appreciated.

A photo of a bent page, the staves the finder traced, two straightened lines and what the reader wrote

It reads a phone photo of a page of music for one instrument and writes out every note. Two small networks do the work: a U-Net finds each staff in the photo, and a CTC transformer reads each staff once it's been straightened. They have 7.8 million weights between them, and both run in Core ML on the phone, with no server.

It never saw a real photo in training. Every page it learned from was rendered in Blender from music whose true score was known, and it reads my iPhone photos of real music better than it reads the synthetic test set.

Test set Pictures Notes Notes wrong After the measure check
My photos of real music: flat, bent, shadowed, angled, on a stand 81 13,127 1.10% 0.21%
My photos of made-up sheets 33 6,732 0.53% 0.53%
The survey: clean pictures of real string-quartet parts 434 70,278 3.32% 2.17%
t1: synthetic photos it never trained on 2,000 398,148 2.86% 2.91%

Version 1.0 (reader r51k-clean, finder f30k), scored 2026-11-17 on an RTX 4090 with the test set's evaluate.py, which rebuilds this table from the files here. On a CPU about one photo in ten reads a note or a few differently. A mistake is a note or rest read at the wrong pitch or length, missed or added, counted over every note in the set. The measure check is a rule that runs after the reader (below).

The real-music photos are ten pages, eight or nine photos of each, so they're a small test. The goal I'm working to is 99.9% of notes right on a flat, well-lit photo and 99.5% on a bent or badly lit one. With the check, my real-music photos are at 99.8% across every condition together, and the 20 flat ones at 99.94%: 2 notes wrong in 3,234, which is on target but too few notes to prove it. The survey is further off, and it's where I find out what's still misread.

Use it

pip install "sheet-model @ git+https://github.com/gregology/sheet-music-scanner-model"
from sheet_model.hub import load

scanner = load()                      # this repo, at revision v1.0
page = scanner.read("page.jpg")
for line in page.lines:
    print(" ".join(line.pitches))

The first line of my photo of Haydn's Op. 1 No. 1, violin part, on a music stand:

clef-G2 key-2b time-6/8 note-Bb3_8 barline note-Bb3_4 note-D4_8 note-D4_4 note-F4_8 barline note-F4_4 ...

Each Line also has the staff's position in the photo, the straightened picture the reader saw, the tokens as written and how sure the reader was of each.

The Core ML programs are in coreml/, as the app ships them:

File Input Output Size
Finder.mlpackage picture: 1 ร— 1 ร— 1024 ร— 768, grey, paper near +1 and ink near -1 marks: 3 ร— 1024 ร— 768: on a staff and near its middle line (before the sigmoid), and the log of the staff space 3 MB, 16-bit
Reader.mlpackage picture: 1 ร— 1 ร— 160 ร— 1600, one straightened line scores: 200 steps ร— 1,275 tokens, before the softmax 24 MB, 32-bit

The tracing and straightening between them is geometry, in finder.py and lines.py, and the app has a Swift copy of it. The reader is 32-bit because at 16 bits it read all zeros in the iOS simulator on the Intel Mac the app was built on. 16-bit hasn't been tried on a phone.

How it works

Most of the design follows from running on the phone. Core ML wants fixed input shapes and handles a decoder that writes one token at a time badly (homr, the best open model I found, keeps its decoder on the CPU for that reason). So nothing here loops.

Finding the staves

The finder is a U-Net with 1.56 million weights. It sees the whole photo shrunk to 768 by 1024 and marks three things at every pixel: whether it's on a staff, how near it is to the staff's middle line, and how far apart that staff's lines are. Plain geometry then follows each middle line's peaks across the picture, joins them into staves and lays the five lines around each at the marked spacing.

At that size a staff space is about five pixels, too coarse to place a line to a tenth of a space. So each staff is cut from the full photo, followed in slices and nudged to where the ink is. In the tests that takes the lines from 0.42 staff spaces off to 0.02.

Straightening

Each staff is cut out along its own five lines, bends and all, into a picture 1600 by 160 pixels with the staff in the middle and 11.4 pixels to a staff space. A tilted, bent or distant staff comes out looking like a flat one, so the reader only ever has to read one kind of picture.

Reading a line

Convolutions turn the line into 200 columns of features. Four layers of self-attention (256 wide, 4 heads) let each column see the whole line, since a note's meaning depends on the clef and key at the start of the line. A linear layer scores 1,275 tokens at each column, and CTC turns the columns into tokens: drop repeats, drop blanks. One pass, 6.22 million weights.

The reader writes where a note sits on the staff, not its pitch: note-S3_4 is a quarter note in the third space, acc-# a sharp before the next note. Code then applies the clef, the key and the measure's accidentals to get pitches. It's the "agnostic" encoding from the PrIMuS dataset (Calvo-Zaragoza and Rizo, 2018). A pitch reader has to carry the clef and key along the line in its head, and mine slipped: whole stretches an octave low, sharps from the key dropped on bent pages. My first position reader beat the pitch reader under every photo condition. The catch is that a misread clef or key token now moves every note on its line, so those few tokens matter most.

The measure check

A triplet printed with no 3 looks exactly like three plain eighths. Nothing in the line tells them apart, only whether the measure adds up. So after reading, each measure is summed against the time signature, and where it's off the check looks for the change of triplet marks that makes it right, starting with the notes the reader was least sure of. It changes nothing when no change fits or two fit equally. On my real-music photos it takes the mistakes from 1.10% to 0.21%. On synthetic photos it does slight harm, because there a measure is more often short for another reason, like a note the photo cut off.

Training data

All synthetic, from my generator:

  1. The music. At first a random composer that writes in a style and at a difficulty. From v2 on, three in four pages are cut from real scores: PDMX (102,235 scores people typed into MuseScore) and the OpenScore Lieder songs.
  2. The page. Verovio engraves it in one of five music fonts, with a part's page head. Over half the pages get a player's pencil marks: fingerings, bowings, circles, the odd word.
  3. The photo. Blender lays the page in a room: flat, curled, in a book or on a music stand, under daylight, lamps or a phone's shadow, shot by a model of the iPhone 16's cameras with its noise, blur and processing.
  4. The truth. Every staff line's path through the photo and every symbol's tokens come from the generator's own data, never read back from pixels. Every render is checked against its truth; 5 of 54,000 failed and were thrown out.

Music the reader is tested on is held out of training by fingerprint: thirteen notes in a row, as their twelve steps of pitch and length, so the same tune in another key or arrangement still matches. 359 PDMX scores were left out that way, among them Beethoven's Op. 18 No. 1 arranged for four marimbas.

Dataset Photos What it added
v0 29,997 Composed music, every kind of photo
v1 10,000 Chords, three ways of marking triplets, grace notes, long rests drawn as MuseScore draws them, music stands
v2 9,776 Three in four pages cut from real scores
v3-tremolo 1,425 Pages that each hold a tremolo between two notes

That's 51,198 photos and 267,244 lines trained on, after holding back one example in fifty and leaving out lines too long for the reader's 200 steps.

PDMX's makers flagged some scores whose licence on musescore.com disagrees with the one in the file. My generator leaves those out by default, but v2 and v3-tremolo were made with them in, so version 1.0 trains on those two datasets without their 2,797 flagged pages. The table above counts what was left. A reader trained with them, the one the app shipped first, scores the same within the noise between runs (TRAINING.md).

Training

One RTX 4090, 30 epochs of 7.3 minutes, about three and a half hours. Batch 48, AdamW at 3e-4 with weight decay 0.01, 1,000 steps of warm-up then cosine decay, bf16 autocast, gradients clipped at 1. CTC loss. One example in fifty is held back, and the checkpoint kept is the one with the fewest tokens wrong on those synthetic lines, so no test set chooses it.

Each line is varied as it's loaded: moved, scaled and tilted a little, as a real line finder leaves it; three in four bent by a smooth curve of up to 0.4 staff spaces; its staff space stretched by up to 8% from one end to the other; blurred, lightened or darkened. The finder trained for 20 epochs on v0's photos.

TRAINING.md has every run, what each data change fixed and broke, and the run I threw away.

Evaluation

Three test sets, because each one fools you differently:

  • My photos. 114 iPhone photos of 18 printed pages: 10 of real string-quartet parts and 8 made-up sheets that each test one thing (keys, rhythms, clefs, dense runs, markings, repeats, a lead sheet). Taken flat, shadowed, angled, bent, on a music stand. This is the test that counts and it's small.
  • The survey. Page one of every part of OpenScore's string quartets that MuseScore would draw: 434 pages, 70,278 notes, clean pictures with no camera. It's the test of real notation at scale, and it's where the misreads are.
  • t1. 2,000 synthetic photos from a seed no training set uses, made by an earlier generator. The true staff lines are known, so it splits the finder's mistakes from the reader's, and it's deliberately harsher than my photos: steep angles, dim light, pages cut off.

None of them was trained on, and the survey's music is fingerprinted out of the training scores.

Limits

  • One line of music. Parts for one instrument, not piano or full scores. Two voices on one staff, lyrics and chord names aren't read.
  • Unmarked triplets. The reader reads them as plain notes: 1,018 of 1,078 on the survey. The measure check mends most on my photos. I tried teaching the reader instead and it learned to guess (TRAINING.md).
  • Steep photos. Past 30 degrees of tilt the finder loses far staves, which shrink under 3 pixels between lines in its picture. On t1, with an earlier reader, those photos were 8% of the set and 45% of the mistakes. The app asks for a better photo when it sees small staves.
  • Not measured yet. Time on an iPhone. How much two training runs differ, so the tables have no error bars. Whether the reader's confidence means anything: it's sure of most of what it gets wrong.

Versions

Releases are tags on this repo. load(revision="v1.0") pins one.

Version Reader Finder Notes
v1.0 r51k-clean f30k In the app from version 0.11. Version 0.10 ran r54k-tremolo, the same data with the flagged pages in

A change to the tokens is a new major version, new weights a minor one, and docs a patch. config.json records each version's runs, epochs and datasets.

Licence and credits

MIT, for the weights and the code.

The real scores in training come from PDMX (Long et al., 2025, under CC BY 4.0) and the OpenScore Lieder Corpus (CC0). The test set's real music is from OpenScore's string quartets (CC0). Pages are engraved with Verovio (LGPL) in fonts under the SIL Open Font License, and rendered with Blender (GPL); none of those licences reaches the pictures they made.

homr is AGPL. I ran it to compare against and to check the true scores of my test pages. None of its code or weights is in here.

Downloads last month
53
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Dataset used to train gregology/sheet-music-scanner