sema / README.md
realangoose's picture
Fix the PyTorch example
4e19e89 verified
|
Raw History Blame Contribute Delete
12 kB
metadata
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, 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

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:

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 65,766 public domain (ancient coins), as the coin pages state; non-commercial research use otherwise
SITNAM 42,014 US fair use; the photos' rights holders are third parties, who have not been asked
Interaktiver Katalog des Münzkabinetts 16,020 CC BY-NC-SA 3.0 AT
Interaktiver Katalog der Münzsammlung 9,965 the institution marks its photos Public Domain Mark 1.0
National Numismatic Collection, De Nederlandsche Bank, Amsterdam 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 1,566 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett des Instituts für Numismatik und Geldgeschichte 1,419 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Heinrich-Heine-Universität Düsseldorf 1,199 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Münster 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 1,003 the institution marks its photos Public Domain Mark 1.0
Interaktiver Katalog des Münzkabinetts der Stadt Winterthur 827 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Johannes Gutenberg-Universität Mainz 807 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Rostock 756 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Goethe-Universität, Frankfurt am Main 639 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Bochum 499 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Augsburg 450 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Trier 379 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Philipps-Universität Marburg 341 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Würzburg 336 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Eichstätt 324 the institution marks its photos Public Domain Mark 1.0
Interaktiver numismatischer Katalog der Hamburger Kunsthalle 276 the institution marks its photos Public Domain Mark 1.0
Interaktiver Katalog der Münzsammlung des Römisch-Germanischen Museums Köln 275 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Heidelberg 235 CC BY-NC-SA 4.0
Das Digitale Münzkabinett der Universität Passau 215 the institution marks its photos Public Domain Mark 1.0
Das virtuelle Münzkabinett des Herzog Anton Ulrich-Museums 137 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Erlangen 124 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Greifswald 118 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Mannheim 100 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der Universität Konstanz 78 the institution marks its photos Public Domain Mark 1.0
Interaktiver Katalog des Münzkabinetts 61 the institution marks its photos Public Domain Mark 1.0
Interaktiver Katalog KIKPE 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 2,929 scholarly use, as the catalogue invites; nothing released
Digitales Münzkabinett des Akademischen Kunstmuseums der Universität Bonn 1,827 the institution marks its photos Public Domain Mark 1.0
Das Digitale Münzkabinett der TU Freiberg 1,804 the institution marks its photos Public Domain Mark 1.0
Gallo-Roman Museum Tongeren 1,057 CC0
The Fralin Museum of Art, U.Va. 436 public domain
Art Institute of Chicago 242 CC0
Coin Collection, Department of Classics at the University of Cologne 117 CC0
CEAlex 55 CC BY-NC 4.0
The Metropolitan Museum of Art 25 CC0

Licence

The weights and index are licensed under CC 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 with registers, by Meta Platforms, Inc., loaded through timm. DINOv2's code and weights are licensed under the Apache 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.