Boos4721 commited on
Commit
c132e85
·
verified ·
1 Parent(s): f753a5b

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - en
5
+ - zh
6
+ tags:
7
+ - ocr
8
+ - captcha
9
+ - crnn
10
+ - ctc
11
+ - onnx
12
+ library_name: onnx
13
+ pipeline_tag: image-to-text
14
+ ---
15
+
16
+ # cpdaily-ocr
17
+
18
+ A lightweight CRNN+CTC OCR model for recognizing 5-character alphanumeric captchas
19
+ (随机彩色斜体、旋转, 白底无干扰线 / random colored italic & rotated glyphs on a clean
20
+ white background). Trained from scratch on real captcha samples, exported to pure ONNX
21
+ for dependency-free inference (works with `tract`, `onnxruntime`, etc.).
22
+
23
+ 一个轻量级 CRNN+CTC 验证码识别模型, 识别 5 位字母数字验证码。从真实样本自训, 导出为
24
+ 纯 ONNX, 推理无任何 Python 依赖。
25
+
26
+ ## Files / 文件
27
+
28
+ | File | Size | Description |
29
+ |---|---|---|
30
+ | `cpdaily_captcha_ocr.onnx` | 2.24 MB | fp32 full-precision master / fp32 全精度母本 |
31
+ | `cpdaily_captcha_ocr_fp16.onnx` | 1.07 MB | fp16-stored, fp32-compute (lossless, recommended) / fp16 存储 fp32 计算, 无损, 推荐部署 |
32
+ | `charset.json` | — | Character table, index 0 = CTC blank / 字符表, index 0 为 CTC blank |
33
+ | `config.json` | — | Input size, preprocessing, decode info / 输入尺寸、预处理、解码信息 |
34
+
35
+ > The fp16 file stores weights as fp16 with `Cast(fp16→fp32)` nodes; inference engines
36
+ > constant-fold them at optimization time, so **computation stays fp32 (no accuracy loss)**
37
+ > while the file is half the size. This avoids engines that don't support fp16 compute ops
38
+ > (GRU/Conv). Standard fp16 conversion and int8 quantization were tested and fail to load
39
+ > in `tract` — this fp16-cast format is the compatible compression path.
40
+ >
41
+ > fp16 版以 fp16 存权重 + `Cast` 节点, 推理时常量折叠回 fp32 计算(精度无损), 体积砍半,
42
+ > 且规避了部分引擎不支持 fp16/量化算子的限制。
43
+
44
+ ## Specs / 规格
45
+
46
+ | | |
47
+ |---|---|
48
+ | Architecture | Depthwise-separable CNN + 2-layer BiGRU + FC, CTC decode |
49
+ | Charset | 62 classes: `A-Z` + `a-z` + `0-9` (+1 CTC blank = 63) |
50
+ | Input | grayscale, resized to `32 × 160`, normalized to `[0,1]` |
51
+ | Output | `[T, 63]` log-softmax, CTC greedy decode |
52
+ | Accuracy | 99.37% full-string (99.7% char-level) on a hand-verified validation set |
53
+ | Size | fp32 2.24 MB / fp16 1.07 MB (lossless compression) |
54
+
55
+ ## Usage (onnxruntime) / 用法
56
+
57
+ ```python
58
+ import json, numpy as np, onnxruntime as ort
59
+ from PIL import Image
60
+
61
+ chars = json.load(open("charset.json")) # ["<blank>", "A", "B", ...]
62
+ sess = ort.InferenceSession("cpdaily_captcha_ocr_fp16.onnx",
63
+ providers=["CPUExecutionProvider"])
64
+ inp = sess.get_inputs()[0].name
65
+
66
+ def recognize(path):
67
+ img = Image.open(path).convert("L").resize((160, 32), Image.BILINEAR)
68
+ x = (np.asarray(img, dtype=np.float32) / 255.0)[None, None, :, :]
69
+ logits = sess.run(None, {inp: x})[0][0] # [T, 63]
70
+ idx = logits.argmax(-1)
71
+ out, prev = [], -1
72
+ for p in idx: # CTC greedy: dedup + drop blank
73
+ if p != prev and p != 0:
74
+ out.append(chars[p])
75
+ prev = p
76
+ return "".join(out)
77
+
78
+ print(recognize("captcha.png"))
79
+ ```
80
+
81
+ ## Usage (Rust / tract) / 用法
82
+
83
+ ```rust
84
+ use tract_onnx::prelude::*;
85
+
86
+ let model = tract_onnx::onnx()
87
+ .model_for_path("cpdaily_captcha_ocr_fp16.onnx")?
88
+ .with_input_fact(0, InferenceFact::dt_shape(f32::datum_type(), tvec!(1, 1, 32, 160)))?
89
+ .into_optimized()?
90
+ .into_runnable()?;
91
+ // preprocess to [1,1,32,160] f32 in [0,1], run, then CTC-greedy decode the [T,63] output.
92
+ ```
93
+
94
+ ## License
95
+
96
+ MIT. Trained from scratch on self-collected data.
charset.json ADDED
@@ -0,0 +1 @@
 
 
1
+ ["<blank>", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
config.json ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "img_h": 32,
3
+ "img_w": 160,
4
+ "num_classes": 63,
5
+ "charset": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
6
+ "preprocess": "grayscale, resize to 32x160, /255.0",
7
+ "blank_idx": 0,
8
+ "decode": "CTC greedy",
9
+ "hidden": 128,
10
+ "acc": 0.9937106918238994
11
+ }
cpdaily_captcha_ocr.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5eccf8c5699b4b5bfa3878d4d643c7b1f816d34c92c4b639816c05010339ace6
3
+ size 2239876
cpdaily_captcha_ocr_fp16.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea39cdb109613cc4c784610e9bdf72490c94ba4f45b0b4b371dd4b25e2470729
3
+ size 1126323