Image-to-Text
Transformers
ONNX
Safetensors
Japanese
English
vision-encoder-decoder
feature-extraction
ocr
kuzushiji
japanese
historical-documents
custom_code
Instructions to use yuta1984/honkoku-ocr with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use yuta1984/honkoku-ocr with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-text" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("image-to-text", model="yuta1984/honkoku-ocr", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("yuta1984/honkoku-ocr", trust_remote_code=True) model = AutoModel.from_pretrained("yuta1984/honkoku-ocr", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download examples/check_data.py from yuta1984/honkoku-ocr: direct link, hf CLI and curl.
- Browser
- Download file 3.55 kB
-
https://huggingface.co/yuta1984/honkoku-ocr/resolve/main/examples/check_data.py
- Command line
-
hf download hf://yuta1984/honkoku-ocr/examples/check_data.py
-
curl -L -o check_data.py https://huggingface.co/yuta1984/honkoku-ocr/resolve/main/examples/check_data.py
3.55 kB
| #!/usr/bin/env python3 | |
| """追学習の前にデータを点検する。 | |
| 見るもの: | |
| 1. 語彙カバレッジ(OOV率)… 語彙 7,710 の外の文字は <UNK> になり、学習も推論もできない | |
| 2. ラベル長 … 256 トークンで切り捨てられる。超過が多いなら行の切り出しを疑う | |
| 3. 構造タグの有無 … タグ無しデータで学習すると構造出力能力を失う(不可逆) | |
| 4. 画像 … 開けるか、極端な縦横比が無いか | |
| 使い方: | |
| python3 check_data.py my_data/data.jsonl [--repo yuta1984/honkoku-ocr] [--images-root my_data] | |
| """ | |
| import argparse, json, os, re | |
| from collections import Counter | |
| from transformers import AutoTokenizer | |
| TAGS = re.compile(r'<(ruby|/ruby|rt|/rt|rt2|/rt2|KAERI|/KAERI|OKURI|/OKURI|TATE|WARI|/WARI|WARI_SEP|BLOCK)>') | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument('jsonl') | |
| ap.add_argument('--repo', default='yuta1984/honkoku-ocr') | |
| ap.add_argument('--images-root', default=None, help='image パスの基準ディレクトリ(既定は jsonl と同じ場所)') | |
| ap.add_argument('--max-length', type=int, default=256) | |
| a = ap.parse_args() | |
| root = a.images_root or os.path.dirname(os.path.abspath(a.jsonl)) | |
| tok = AutoTokenizer.from_pretrained(a.repo) | |
| vocab = set(tok.get_vocab()) | |
| unk = tok.unk_token_id | |
| rows = [json.loads(l) for l in open(a.jsonl, encoding='utf-8') if l.strip()] | |
| print(f'{len(rows):,} 行') | |
| n_tok = n_oov = 0 | |
| oov_chars = Counter() | |
| lens = [] | |
| n_struct = n_missing_img = 0 | |
| for r in rows: | |
| text = r['text'] | |
| ids = tok(text, add_special_tokens=True)['input_ids'] | |
| lens.append(len(ids)) | |
| for t, i in zip(tok.convert_ids_to_tokens(ids), ids): | |
| n_tok += 1 | |
| if i == unk: | |
| n_oov += 1 | |
| for ch in TAGS.sub('', text): | |
| if ch not in vocab: | |
| oov_chars[ch] += 1 | |
| if TAGS.search(text): | |
| n_struct += 1 | |
| p = r['image'] if os.path.isabs(r['image']) else os.path.join(root, r['image']) | |
| if not os.path.exists(p): | |
| n_missing_img += 1 | |
| lens.sort() | |
| pct = lambda q: lens[min(len(lens) - 1, int(len(lens) * q))] | |
| print(f'\n[1] 語彙カバレッジ') | |
| print(f' OOV トークン {n_oov:,} / {n_tok:,} = {100*n_oov/max(1,n_tok):.4f}%') | |
| if oov_chars: | |
| print(f' 語彙外の文字 {len(oov_chars)} 種: ' + ' '.join(f'{c}({n})' for c, n in oov_chars.most_common(20))) | |
| print(' → 0.5% を超えるなら語彙拡張を検討(README「語彙を広げる」参照)') | |
| else: | |
| print(' 語彙外の文字なし') | |
| print(f'\n[2] ラベル長(特殊トークン込み)') | |
| print(f' 中央値 {pct(0.5)} / p95 {pct(0.95)} / 最大 {lens[-1]}') | |
| over = sum(1 for x in lens if x > a.max_length) | |
| print(f' {a.max_length} 超過 {over:,} 行 ({100*over/max(1,len(lens)):.2f}%)' | |
| + (' ← 切り捨てられる。行の切り出しを見直すこと' if over else '')) | |
| print(f'\n[3] 構造タグ') | |
| print(f' タグを含む行 {n_struct:,} / {len(rows):,} ({100*n_struct/max(1,len(rows)):.1f}%)') | |
| if n_struct == 0: | |
| print(' ★タグが1行も無い。このまま学習すると構造出力能力を失う(不可逆)。') | |
| print(' 本文のみの用途なら問題ないが、意図した選択か確認すること。') | |
| elif n_struct < len(rows) * 0.05: | |
| print(' ★タグを含む行が少ない。構造の再現性は落ちる可能性が高い。') | |
| print(f'\n[4] 画像') | |
| print(f' 見つからない画像 {n_missing_img:,} 行' + (' ← --images-root を確認' if n_missing_img else '')) | |