sema / README.md
realangoose's picture
Fix the PyTorch example
4e19e89 verified
|
Raw History Blame Contribute Delete
12 kB
---
license: cc-by-nc-sa-4.0
pipeline_tag: image-feature-extraction
tags: [numismatics, coins, ancient-coins, image-retrieval, dinov2, onnx]
base_model:
- timm/vit_base_patch14_reg4_dinov2.lvd142m
- timm/vit_small_patch14_reg4_dinov2.lvd142m
---
# Sema 1.0
Sema identifies the type of an ancient coin from a photograph of one side. It was made for
[Solidi](https://github.com/Angoooose/solidi), a catalogue of ancient coin types built from
Nomisma.org and the museum collections linked to it.
It is two models fine-tuned from DINOv2. **Sema 1 Small** turns a photograph into a vector,
and is small enough to run in a web browser. The **search index** holds one vector for each
side of 28,137 coin types, made with the larger **Sema 1 Base** from the
photographs of every coin of that type. Identifying a coin is finding the nearest vectors.
## Files
| Path | What it is |
|---|---|
| `browser/model-int8.onnx` | Sema 1 Small in ONNX, weights stored as one byte each, with the index's turn built in: 224px pixels in, a unit vector out |
| `browser/index.bin`, `browser/index.json` | the search index, product-quantised, and how to read it |
| `browser/manifest.json` | the release: every file's size and sha256 |
| `small/model.safetensors`, `small/config.json` | Sema 1 Small: `vit_small_patch14_reg4_dinov2.lvd142m` at 224px, with a layer into Sema 1 Base's space (384 to 768 numbers) |
| `base/model.safetensors`, `base/config.json` | Sema 1 Base: `vit_base_patch14_reg4_dinov2.lvd142m` at 336px |
The `browser/` folder is the release exactly as Solidi's site serves it. Its index can
only be searched with the ONNX model next to it, which ends with the same turn. The
safetensors weights give the models' raw vectors, for further training or other uses.
## How well it works
Graded on 5,770 coins from 9 collections that were never trained on or
indexed, so no test coin shares a photographer, background or lighting with the index. Each
was searched among all 28,137 types using both of its sides, with the
browser's model (model-int8.onnx) through ONNX Runtime:
| Measure | Score |
|---|---|
| Right type first | 52.9% |
| Right type in the first 10 | 83.7% |
| Right design in the first 10 | 92.5% |
A design groups types with the same denomination, portrait and reverse description; many
types differ only in a mint mark or an officina letter, which a photograph may not show.
Sema knows only the types with photographed coins in the collections below, mostly Roman
and Greek. It expects one side of one coin, cropped close.
## Use
```python
import json
import numpy as np
import onnxruntime
from huggingface_hub import snapshot_download
from PIL import Image
folder = snapshot_download("solidiorg/sema", revision="v1.0", allow_patterns="browser/*")
model = onnxruntime.InferenceSession(f"{folder}/browser/model-int8.onnx")
layout = json.load(open(f"{folder}/browser/index.json"))
raw = open(f"{folder}/browser/index.bin", "rb").read()
# The index: patterns [parts, patterns, width] as 32-bit numbers, then one byte per part for
# every type's obverse, then every type's reverse.
parts, count = layout["parts"], layout["patterns"]
width = layout["dims"] // parts
cut = parts * count * width * 4
patterns = np.frombuffer(raw[:cut], "<f4").reshape(parts, count, width)
codes = np.frombuffer(raw[cut:], np.uint8).reshape(-1, parts)
index = np.concatenate([patterns[p][codes[:, p]] for p in range(parts)], axis=1)
mean, std = np.array([0.48500001430511475, 0.4560000002384186, 0.4059999883174896]), np.array([0.2290000021457672, 0.2240000069141388, 0.22499999403953552])
photo = Image.open("coin.jpg").convert("RGB").resize((224, 224), Image.BICUBIC)
pixels = ((np.asarray(photo, np.float32) / 255 - mean) / std).transpose(2, 0, 1)[None].astype(np.float32)
vector = model.run(None, {"pixels": pixels})[0][0]
scores = (index @ vector).reshape(2, -1).max(axis=0) # the better of obverse and reverse
for i in np.argsort(-scores)[:10]:
print(layout["types"][i], round(float(scores[i]), 3))
```
Types are named by corpus and id: `ocre/ric.1(2).aug.1` is
https://numismatics.org/ocre/id/ric.1(2).aug.1. To load the weights in PyTorch:
```python
import timm
from huggingface_hub import snapshot_download
from safetensors.torch import load_file
folder = snapshot_download("solidiorg/sema", revision="v1.0", allow_patterns="base/*")
weights = load_file(f"{folder}/base/model.safetensors")
model = timm.create_model("vit_base_patch14_reg4_dinov2.lvd142m", pretrained=False, num_classes=0, img_size=336)
model.load_state_dict({k: v for k, v in weights.items() if not k.startswith("projection.")})
```
## Training data
Trained and indexed on 150,900 photographed coins from 31 collections,
each used on the licence or legal ground shown. Some allow only non-commercial use; the
SITNAM photographs, clippings from auction catalogues gathered by the American Numismatic
Society, carry no licence and are used under US fair use and the EU text and data mining
exception. No photograph is included in this repository; each stays under its collection's
own terms. A rights holder who wants their photographs left out of future releases can email
contact@solidi.org.
| Collection | Coins | Used on |
|---|---|---|
| [American Numismatic Society](http://numismatics.org/search/) | 65,766 | public domain (ancient coins), as the coin pages state; non-commercial research use otherwise |
| [SITNAM](https://numismatics.org/sitnam/) | 42,014 | US fair use; the photos' rights holders are third parties, who have not been asked |
| [Interaktiver Katalog des Münzkabinetts](https://www.ikmk.at) | 16,020 | CC BY-NC-SA 3.0 AT |
| [Interaktiver Katalog der Münzsammlung](https://ikmk.uni-freiburg.de) | 9,965 | the institution marks its photos Public Domain Mark 1.0 |
| [National Numismatic Collection, De Nederlandsche Bank, Amsterdam](https://nnc.dnb.nl/dnb-nnc-ontsluiting-frontend/#/collectie/) | 3,858 | free use of images of coins out of copyright, as DNB states; database contents CC BY-NC 4.0 |
| [Das Digitale Münzkabinett des Instituts für Klassische Archäologie](https://www.ikmk.uni-tuebingen.de) | 1,566 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett des Instituts für Numismatik und Geldgeschichte](https://www.univie.ac.at/ikmk) | 1,419 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Heinrich-Heine-Universität Düsseldorf](https://muenzkatalog.hhu-hosting.de) | 1,199 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Münster](https://archaeologie.uni-muenster.de/ikmk) | 1,101 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett des Historischen Instituts, Abt. Alte Geschichte, und des Instituts für Altertumskunde, Abt. Byzantinistik](https://numid.uni-koeln.de) | 1,003 | the institution marks its photos Public Domain Mark 1.0 |
| [Interaktiver Katalog des Münzkabinetts der Stadt Winterthur](https://ikmk-win.ch) | 827 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Johannes Gutenberg-Universität Mainz](https://numid.uni-mainz.de) | 807 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Rostock](https://numid.uni-rostock.de) | 756 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Goethe-Universität, Frankfurt am Main](http://www.mkfrankfurt.uni-frankfurt.de) | 639 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Bochum](https://mk-bochum.ikmk.net) | 499 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Augsburg](https://muenzen.philhist.uni-augsburg.de) | 450 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Trier](https://ikmk.uni-trier.de) | 379 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Philipps-Universität Marburg](https://mk-marburg.ikmk.net) | 341 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Würzburg](http://www.nomisma.museum.uni-wuerzburg.de) | 336 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Eichstätt](https://numid.ku.de) | 324 | the institution marks its photos Public Domain Mark 1.0 |
| [Interaktiver numismatischer Katalog der Hamburger Kunsthalle](https://hamburger-kunsthalle.ikmk.net) | 276 | the institution marks its photos Public Domain Mark 1.0 |
| [Interaktiver Katalog der Münzsammlung des Römisch-Germanischen Museums Köln](https://www.ikmkrgm.uni-koeln.de) | 275 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Heidelberg](https://pecunia.zaw.uni-heidelberg.de/ikmk) | 235 | CC BY-NC-SA 4.0 |
| [Das Digitale Münzkabinett der Universität Passau](https://numid.phil.uni-passau.de) | 215 | the institution marks its photos Public Domain Mark 1.0 |
| [Das virtuelle Münzkabinett des Herzog Anton Ulrich-Museums](https://www.virtuelles-muenzkabinett.de) | 137 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Erlangen](http://www.numid.phil.fau.de) | 124 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Greifswald](https://numid.wissenschaftliche-sammlungen.uni-greifswald.de) | 118 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Mannheim](http://ikmk.uni-mannheim.de) | 100 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der Universität Konstanz](https://ikmk.uni-konstanz.de) | 78 | the institution marks its photos Public Domain Mark 1.0 |
| [Interaktiver Katalog des Münzkabinetts](https://numid.ur.de) | 61 | the institution marks its photos Public Domain Mark 1.0 |
| [Interaktiver Katalog KIKPE](https://kikpe.ikmk.net) | 12 | the institution marks its photos Public Domain Mark 1.0 |
Graded on, never trained on, indexed or released:
| Collection | Coins | Used on |
|---|---|---|
| [The Princeton University Numismatic Collection](https://catalog.princeton.edu/numismatics) | 2,929 | scholarly use, as the catalogue invites; nothing released |
| [Digitales Münzkabinett des Akademischen Kunstmuseums der Universität Bonn](https://mk-bonn.ikmk.net) | 1,827 | the institution marks its photos Public Domain Mark 1.0 |
| [Das Digitale Münzkabinett der TU Freiberg](https://numid.ub.tu-freiberg.de) | 1,804 | the institution marks its photos Public Domain Mark 1.0 |
| [Gallo-Roman Museum Tongeren](https://exploratorium.galloromeinsmuseum.be/) | 1,057 | CC0 |
| [The Fralin Museum of Art, U.Va.](https://coins.lib.virginia.edu/) | 436 | public domain |
| [Art Institute of Chicago](https://www.artic.edu/) | 242 | CC0 |
| [Coin Collection, Department of Classics at the University of Cologne](https://muenzen.uni-koeln.de) | 117 | CC0 |
| [CEAlex](https://coins.cealex.org:443/) | 55 | CC BY-NC 4.0 |
| [The Metropolitan Museum of Art](http://www.metmuseum.org/) | 25 | CC0 |
## Licence
The weights and index are licensed under
[CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/): they may not be used
commercially, and anything made from them must be shared under the same licence, crediting
the collections above. See `LICENSE`.
Both models are fine-tuned from [DINOv2](https://github.com/facebookresearch/dinov2) with
registers, by Meta Platforms, Inc., loaded through timm. DINOv2's code and weights are
licensed under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0), whose
terms still apply to the parts of these weights that come from it; we changed them by
fine-tuning on the photographs above.