Update inference code and model card
Browse files- README.md +39 -27
- example.py +22 -0
- inference.py +64 -0
- model.py +340 -0
- normalize.py +67 -0
- release_metadata.json +19 -0
README.md
CHANGED
|
@@ -3,45 +3,46 @@ license: apache-2.0
|
|
| 3 |
library_name: pytorch
|
| 4 |
pipeline_tag: image-classification
|
| 5 |
tags:
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
---
|
| 12 |
|
| 13 |
# Handwritten
|
| 14 |
|
| 15 |
-
MobileNetV2
|
| 16 |
-
|
| 17 |
-
grayscale input.
|
| 18 |
|
| 19 |
-
##
|
| 20 |
|
| 21 |
-
|
|
| 22 |
|---|---:|
|
| 23 |
| PyTorch top-1 | 95.47% |
|
| 24 |
| PyTorch top-10 | 99.58% |
|
| 25 |
-
|
|
| 26 |
-
|
|
| 27 |
|
| 28 |
-
##
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
`runs/mbv2_phase1_aug/best.pt`:
|
| 40 |
|
| 41 |
```python
|
| 42 |
import torch
|
| 43 |
-
|
| 44 |
-
from src.models import build_model
|
| 45 |
|
| 46 |
checkpoint = torch.load("best.pt", map_location="cpu", weights_only=False)
|
| 47 |
model = build_model(checkpoint["cfg"]["model"], num_classes=3755)
|
|
@@ -49,10 +50,21 @@ model.load_state_dict(checkpoint["model"])
|
|
| 49 |
model.eval()
|
| 50 |
```
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
##
|
| 56 |
|
| 57 |
-
CASIA HWDB1.1
|
| 58 |
-
official CASIA channel and comply with its license.
|
|
|
|
| 3 |
library_name: pytorch
|
| 4 |
pipeline_tag: image-classification
|
| 5 |
tags:
|
| 6 |
+
- handwritten-chinese-character-recognition
|
| 7 |
+
- casia-hwdb
|
| 8 |
+
- mobilenet-v2
|
| 9 |
+
- ncnn
|
| 10 |
+
- int8
|
| 11 |
---
|
| 12 |
|
| 13 |
# Handwritten
|
| 14 |
|
| 15 |
+
MobileNetV2 手写汉字识别模型,覆盖 3755 个 GB2312 一级汉字。输入为 64×64
|
| 16 |
+
灰度图,训练数据来自 CASIA HWDB1.1。
|
|
|
|
| 17 |
|
| 18 |
+
## 指标
|
| 19 |
|
| 20 |
+
| 指标 | 数值 |
|
| 21 |
|---|---:|
|
| 22 |
| PyTorch top-1 | 95.47% |
|
| 23 |
| PyTorch top-10 | 99.58% |
|
| 24 |
+
| NCNN INT8 权重 | 4.1 MB |
|
| 25 |
+
| 预估移动端延迟 | 2–5 ms |
|
| 26 |
|
| 27 |
+
## 直接使用 NCNN
|
| 28 |
|
| 29 |
+
```bash
|
| 30 |
+
pip install numpy Pillow ncnn
|
| 31 |
+
python example.py path/to/image.png
|
| 32 |
+
```
|
| 33 |
|
| 34 |
+
```python
|
| 35 |
+
from inference import HCCRRecognizer
|
| 36 |
|
| 37 |
+
model = HCCRRecognizer.from_pretrained(".")
|
| 38 |
+
print(model.predict_file("path/to/image.png", k=10))
|
| 39 |
+
```
|
| 40 |
|
| 41 |
+
## 加载 PyTorch checkpoint
|
|
|
|
| 42 |
|
| 43 |
```python
|
| 44 |
import torch
|
| 45 |
+
from model import build_model
|
|
|
|
| 46 |
|
| 47 |
checkpoint = torch.load("best.pt", map_location="cpu", weights_only=False)
|
| 48 |
model = build_model(checkpoint["cfg"]["model"], num_classes=3755)
|
|
|
|
| 50 |
model.eval()
|
| 51 |
```
|
| 52 |
|
| 53 |
+
## 文件
|
| 54 |
+
|
| 55 |
+
| 文件 | 内容 |
|
| 56 |
+
|---|---|
|
| 57 |
+
| `best.pt` | PyTorch checkpoint、训练配置、epoch 和 top-1 |
|
| 58 |
+
| `model.py` | PyTorch 模型定义 |
|
| 59 |
+
| `inference.py` | 自包含 NCNN 推理入口 |
|
| 60 |
+
| `normalize.py` | 与训练一致的图像归一化 |
|
| 61 |
+
| `example.py` | 单图命令行示例 |
|
| 62 |
+
| `ncnn/model.ncnn.*` | NCNN INT8 网络和权重 |
|
| 63 |
+
| `ncnn/charset.json` | 输出索引到汉字的映射 |
|
| 64 |
+
| `release_metadata.json` | 发布来源、指标和文件校验值 |
|
| 65 |
+
|
| 66 |
+
完整训练、导出与 Android 部署见 [https://github.com/Ismantic/Handwritten](https://github.com/Ismantic/Handwritten)。
|
| 67 |
|
| 68 |
+
## 数据许可
|
| 69 |
|
| 70 |
+
本仓库不再分发 CASIA HWDB1.1。请从官方渠道申请数据并遵守其许可。
|
|
|
example.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""用发布包识别一张白底黑字图片。"""
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
from inference import HCCRRecognizer
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def main() -> None:
|
| 10 |
+
parser = argparse.ArgumentParser()
|
| 11 |
+
parser.add_argument("image", type=Path)
|
| 12 |
+
parser.add_argument("--model-dir", type=Path, default=Path("."))
|
| 13 |
+
parser.add_argument("-k", type=int, default=10)
|
| 14 |
+
args = parser.parse_args()
|
| 15 |
+
|
| 16 |
+
model = HCCRRecognizer.from_pretrained(args.model_dir)
|
| 17 |
+
for char, probability in model.predict_file(args.image, k=args.k):
|
| 18 |
+
print(f"{char}\t{probability:.2%}")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
if __name__ == "__main__":
|
| 22 |
+
main()
|
inference.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""随 HF 模型发布的自包含 NCNN 推理入口。"""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import json
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
import ncnn
|
| 9 |
+
import numpy as np
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
from normalize import normalize
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class HCCRRecognizer:
|
| 16 |
+
def __init__(
|
| 17 |
+
self,
|
| 18 |
+
param_path: str | Path,
|
| 19 |
+
bin_path: str | Path,
|
| 20 |
+
charset_path: str | Path,
|
| 21 |
+
num_threads: int = 4,
|
| 22 |
+
) -> None:
|
| 23 |
+
self.net = ncnn.Net()
|
| 24 |
+
self.net.opt.use_vulkan_compute = False
|
| 25 |
+
self.net.opt.num_threads = num_threads
|
| 26 |
+
self.net.load_param(str(param_path))
|
| 27 |
+
self.net.load_model(str(bin_path))
|
| 28 |
+
data = json.loads(Path(charset_path).read_text(encoding="utf-8"))
|
| 29 |
+
self.idx_to_char = {int(i): ch for ch, i in data["char_to_idx"].items()}
|
| 30 |
+
|
| 31 |
+
@classmethod
|
| 32 |
+
def from_pretrained(cls, model_dir: str | Path = ".", num_threads: int = 4):
|
| 33 |
+
root = Path(model_dir)
|
| 34 |
+
return cls(
|
| 35 |
+
root / "ncnn/model.ncnn.param",
|
| 36 |
+
root / "ncnn/model.ncnn.bin",
|
| 37 |
+
root / "ncnn/charset.json",
|
| 38 |
+
num_threads=num_threads,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
@staticmethod
|
| 42 |
+
def preprocess(image: Image.Image) -> np.ndarray:
|
| 43 |
+
gray = np.asarray(image.convert("L"), dtype=np.uint8)
|
| 44 |
+
normalized = normalize(gray)
|
| 45 |
+
return ((255 - normalized).astype(np.float32) / 255.0)[None, :, :]
|
| 46 |
+
|
| 47 |
+
def predict(self, image: Image.Image, k: int = 10) -> list[tuple[str, float]]:
|
| 48 |
+
x = self.preprocess(image)
|
| 49 |
+
if not x.any():
|
| 50 |
+
return []
|
| 51 |
+
extractor = self.net.create_extractor()
|
| 52 |
+
extractor.input("in0", ncnn.Mat(x))
|
| 53 |
+
ret, out = extractor.extract("out0")
|
| 54 |
+
if ret != 0:
|
| 55 |
+
raise RuntimeError(f"ncnn extract failed: {ret}")
|
| 56 |
+
logits = np.asarray(out)
|
| 57 |
+
probs = np.exp(logits - logits.max())
|
| 58 |
+
probs /= probs.sum()
|
| 59 |
+
indices = np.argsort(-probs)[:k]
|
| 60 |
+
return [(self.idx_to_char[int(i)], float(probs[i])) for i in indices]
|
| 61 |
+
|
| 62 |
+
def predict_file(self, path: str | Path, k: int = 10) -> list[tuple[str, float]]:
|
| 63 |
+
with Image.open(path) as image:
|
| 64 |
+
return self.predict(image, k=k)
|
model.py
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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)
|
| 7 |
+
- 不用 dropout(BN 已经有正则效果);如需可在 classifier 前加
|
| 8 |
+
|
| 9 |
+
约束:
|
| 10 |
+
- 只用标准算子(Conv2d / BN / ReLU / MaxPool / Linear)便于后续导出
|
| 11 |
+
- 固定输入 shape(避免动态控制流)
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
from __future__ import annotations
|
| 15 |
+
|
| 16 |
+
import torch
|
| 17 |
+
import torch.nn as nn
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def _conv_bn_relu(in_ch: int, out_ch: int, kernel_size: int = 3, padding: int = 1) -> nn.Sequential:
|
| 21 |
+
return nn.Sequential(
|
| 22 |
+
nn.Conv2d(in_ch, out_ch, kernel_size, padding=padding, bias=False),
|
| 23 |
+
nn.BatchNorm2d(out_ch),
|
| 24 |
+
nn.ReLU(inplace=True),
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class GWAP(nn.Module):
|
| 29 |
+
"""Global Weighted Average Pooling(Melnyk et al. 2020)。
|
| 30 |
+
|
| 31 |
+
学一个空间权重图代替朴素 GAP:
|
| 32 |
+
out[c] = sum_{i,j} W[c, i, j] * x[c, i, j]
|
| 33 |
+
初始化为 1/(H*W) 即等价 GAP,有梯度后偏离学到"哪些位置更重要"。
|
| 34 |
+
|
| 35 |
+
可学参数:C × H × W(对 448 ch / 4×4 spatial 是 7168,可忽略)。
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
def __init__(self, channels: int, spatial_h: int, spatial_w: int) -> None:
|
| 39 |
+
super().__init__()
|
| 40 |
+
init_val = 1.0 / (spatial_h * spatial_w)
|
| 41 |
+
self.weight = nn.Parameter(
|
| 42 |
+
torch.full((channels, spatial_h, spatial_w), init_val, dtype=torch.float32)
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 46 |
+
# x: [B, C, H, W] → [B, C]
|
| 47 |
+
return (x * self.weight).sum(dim=(2, 3))
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class PlainCNN(nn.Module):
|
| 51 |
+
"""5 stage CNN,每 stage 后 MaxPool 下采样一次。
|
| 52 |
+
|
| 53 |
+
[1,64,64] → 32 → 32(/2)=32 → 64(/2)=16 → 96(/2)=8 → 128(/2)=4 → 192(/2)=2 → GAP → Linear
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
def __init__(self, num_classes: int, in_channels: int = 1) -> None:
|
| 57 |
+
super().__init__()
|
| 58 |
+
self.features = nn.Sequential(
|
| 59 |
+
_conv_bn_relu(in_channels, 32),
|
| 60 |
+
nn.MaxPool2d(2), # 64→32
|
| 61 |
+
_conv_bn_relu(32, 64),
|
| 62 |
+
nn.MaxPool2d(2), # 32→16
|
| 63 |
+
_conv_bn_relu(64, 96),
|
| 64 |
+
nn.MaxPool2d(2), # 16→8
|
| 65 |
+
_conv_bn_relu(96, 128),
|
| 66 |
+
nn.MaxPool2d(2), # 8→4
|
| 67 |
+
_conv_bn_relu(128, 192),
|
| 68 |
+
nn.MaxPool2d(2), # 4→2
|
| 69 |
+
)
|
| 70 |
+
self.gap = nn.AdaptiveAvgPool2d(1)
|
| 71 |
+
self.classifier = nn.Linear(192, num_classes)
|
| 72 |
+
|
| 73 |
+
self._init_weights()
|
| 74 |
+
|
| 75 |
+
def _init_weights(self) -> None:
|
| 76 |
+
for m in self.modules():
|
| 77 |
+
if isinstance(m, nn.Conv2d):
|
| 78 |
+
nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
|
| 79 |
+
elif isinstance(m, nn.BatchNorm2d):
|
| 80 |
+
nn.init.ones_(m.weight)
|
| 81 |
+
nn.init.zeros_(m.bias)
|
| 82 |
+
elif isinstance(m, nn.Linear):
|
| 83 |
+
nn.init.normal_(m.weight, 0, 0.01)
|
| 84 |
+
nn.init.zeros_(m.bias)
|
| 85 |
+
|
| 86 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 87 |
+
x = self.features(x)
|
| 88 |
+
x = self.gap(x).flatten(1)
|
| 89 |
+
return self.classifier(x)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class MelnykNet(nn.Module):
|
| 93 |
+
"""Melnyk-Net(2020 Soft Computing,Melnyk/You/Li)。
|
| 94 |
+
|
| 95 |
+
原版 96×96 输入,这里改造为 64×64(我们的部署口径)。
|
| 96 |
+
14 conv(3×3 + BN + ReLU)分 5 stage,4 次 avg pool,GWAP,FC。
|
| 97 |
+
|
| 98 |
+
spatial 流动:64 → 32 → 16 → 8 → 4
|
| 99 |
+
通道流动(注意"瓶颈"模式 high-low-high):
|
| 100 |
+
stage 1: 64, 64 @ 64×64
|
| 101 |
+
stage 2: 96, 64, 96 @ 32×32
|
| 102 |
+
stage 3: 128, 96, 128 @ 16×16
|
| 103 |
+
stage 4: 256, 192, 256 @ 8×8
|
| 104 |
+
stage 5: 448, 256, 448 @ 4×4
|
| 105 |
+
|
| 106 |
+
总参数 ~6.5M(同原版),FP32 ~25 MB,INT8 ~6.5 MB(超 plan 5MB,需要后续考虑)。
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
def __init__(self, num_classes: int, in_channels: int = 1) -> None:
|
| 110 |
+
super().__init__()
|
| 111 |
+
|
| 112 |
+
def block(in_ch: int, out_ch: int) -> nn.Sequential:
|
| 113 |
+
return _conv_bn_relu(in_ch, out_ch)
|
| 114 |
+
|
| 115 |
+
self.stage1 = nn.Sequential(block(in_channels, 64), block(64, 64))
|
| 116 |
+
self.stage2 = nn.Sequential(block(64, 96), block(96, 64), block(64, 96))
|
| 117 |
+
self.stage3 = nn.Sequential(block(96, 128), block(128, 96), block(96, 128))
|
| 118 |
+
self.stage4 = nn.Sequential(block(128, 256), block(256, 192), block(192, 256))
|
| 119 |
+
self.stage5 = nn.Sequential(block(256, 448), block(448, 256), block(256, 448))
|
| 120 |
+
self.pool = nn.AvgPool2d(2)
|
| 121 |
+
self.gwap = GWAP(448, 4, 4)
|
| 122 |
+
self.classifier = nn.Linear(448, num_classes)
|
| 123 |
+
|
| 124 |
+
self._init_weights()
|
| 125 |
+
|
| 126 |
+
def _init_weights(self) -> None:
|
| 127 |
+
for m in self.modules():
|
| 128 |
+
if isinstance(m, nn.Conv2d):
|
| 129 |
+
nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
|
| 130 |
+
elif isinstance(m, nn.BatchNorm2d):
|
| 131 |
+
nn.init.ones_(m.weight)
|
| 132 |
+
nn.init.zeros_(m.bias)
|
| 133 |
+
elif isinstance(m, nn.Linear):
|
| 134 |
+
nn.init.normal_(m.weight, 0, 0.01)
|
| 135 |
+
nn.init.zeros_(m.bias)
|
| 136 |
+
|
| 137 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 138 |
+
x = self.stage1(x); x = self.pool(x)
|
| 139 |
+
x = self.stage2(x); x = self.pool(x)
|
| 140 |
+
x = self.stage3(x); x = self.pool(x)
|
| 141 |
+
x = self.stage4(x); x = self.pool(x)
|
| 142 |
+
x = self.stage5(x) # 4×4
|
| 143 |
+
x = self.gwap(x)
|
| 144 |
+
return self.classifier(x)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def _build_mobilenet_v3_small_hccr(num_classes: int, in_channels: int = 1) -> nn.Module:
|
| 148 |
+
"""torchvision MobileNetV3-Small 改造:
|
| 149 |
+
- 第一层 conv 改成单通道输入(3 → 1)
|
| 150 |
+
- 分类头去掉 576 → 1024 隐层,直接 576 → num_classes(省 ~600K params,3755 类下分类头本身就是大头)
|
| 151 |
+
- 保留 HSwish / SE 块 / depthwise conv,PNNX/NCNN 都原生支持
|
| 152 |
+
|
| 153 |
+
输入: [B, 1, 64, 64] spatial 走 stride 2×5 → 64 → 32 → 16 → 8 → 4 → 2
|
| 154 |
+
输出: [B, num_classes]
|
| 155 |
+
"""
|
| 156 |
+
from torchvision.models import mobilenet_v3_small
|
| 157 |
+
|
| 158 |
+
model = mobilenet_v3_small(num_classes=num_classes)
|
| 159 |
+
model.features[0][0] = nn.Conv2d(
|
| 160 |
+
in_channels, 16, kernel_size=3, stride=2, padding=1, bias=False
|
| 161 |
+
)
|
| 162 |
+
model.classifier = nn.Sequential(
|
| 163 |
+
nn.Dropout(p=0.2, inplace=False),
|
| 164 |
+
nn.Linear(576, num_classes),
|
| 165 |
+
)
|
| 166 |
+
# torchvision 在 mobilenet_v3_small() 构造时跑过 init loop(Conv kaiming_normal,
|
| 167 |
+
# Linear normal(0, 0.01))。我们之后才替换层,新层是 PyTorch 默认 init,
|
| 168 |
+
# stddev 偏大(分类头 2.3x),早期梯度信号偏强 —— 重新走一遍同样的 init。
|
| 169 |
+
for layer in (model.features[0][0], model.classifier[1]):
|
| 170 |
+
if isinstance(layer, nn.Conv2d):
|
| 171 |
+
nn.init.kaiming_normal_(layer.weight, mode="fan_out")
|
| 172 |
+
if layer.bias is not None:
|
| 173 |
+
nn.init.zeros_(layer.bias)
|
| 174 |
+
elif isinstance(layer, nn.Linear):
|
| 175 |
+
nn.init.normal_(layer.weight, 0, 0.01)
|
| 176 |
+
nn.init.zeros_(layer.bias)
|
| 177 |
+
return model
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
class MobileNetV2WithGWAP(nn.Module):
|
| 181 |
+
"""V2 改造 + 借 Melnyk-Net 的 GWAP 替换 GAP。
|
| 182 |
+
|
| 183 |
+
流程:V2 features → GWAP(C × 2 × 2 学习权重)→ Dropout → Linear。
|
| 184 |
+
输入 [B,1,64,64]:V2 5 次 stride 2 下采样 → 2×2 spatial。
|
| 185 |
+
"""
|
| 186 |
+
|
| 187 |
+
def __init__(
|
| 188 |
+
self, num_classes: int, in_channels: int = 1, last_channel: int = 576
|
| 189 |
+
) -> None:
|
| 190 |
+
super().__init__()
|
| 191 |
+
from torchvision.models import mobilenet_v2
|
| 192 |
+
|
| 193 |
+
base = mobilenet_v2()
|
| 194 |
+
base.features[0][0] = nn.Conv2d(
|
| 195 |
+
in_channels, 32, kernel_size=3, stride=2, padding=1, bias=False
|
| 196 |
+
)
|
| 197 |
+
base.features[18] = nn.Sequential(
|
| 198 |
+
nn.Conv2d(320, last_channel, kernel_size=1, bias=False),
|
| 199 |
+
nn.BatchNorm2d(last_channel),
|
| 200 |
+
nn.ReLU6(inplace=True),
|
| 201 |
+
)
|
| 202 |
+
self.features = base.features
|
| 203 |
+
# 64 input → 5 次 stride 2 → 2×2 spatial 在末端
|
| 204 |
+
self.gwap = GWAP(last_channel, 2, 2)
|
| 205 |
+
self.classifier = nn.Sequential(
|
| 206 |
+
nn.Dropout(p=0.2, inplace=False),
|
| 207 |
+
nn.Linear(last_channel, num_classes),
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
for layer in (
|
| 211 |
+
self.features[0][0],
|
| 212 |
+
self.features[18][0],
|
| 213 |
+
self.features[18][1],
|
| 214 |
+
self.classifier[1],
|
| 215 |
+
):
|
| 216 |
+
if isinstance(layer, nn.Conv2d):
|
| 217 |
+
nn.init.kaiming_normal_(layer.weight, mode="fan_out")
|
| 218 |
+
if layer.bias is not None:
|
| 219 |
+
nn.init.zeros_(layer.bias)
|
| 220 |
+
elif isinstance(layer, nn.BatchNorm2d):
|
| 221 |
+
nn.init.ones_(layer.weight)
|
| 222 |
+
nn.init.zeros_(layer.bias)
|
| 223 |
+
elif isinstance(layer, nn.Linear):
|
| 224 |
+
nn.init.normal_(layer.weight, 0, 0.01)
|
| 225 |
+
nn.init.zeros_(layer.bias)
|
| 226 |
+
|
| 227 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 228 |
+
x = self.features(x) # [B, last_ch, 2, 2]
|
| 229 |
+
x = self.gwap(x) # [B, last_ch]
|
| 230 |
+
return self.classifier(x)
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def _replace_relu6_with_relu(module: nn.Module) -> None:
|
| 234 |
+
"""递归把 ReLU6 替换为 ReLU。
|
| 235 |
+
|
| 236 |
+
用途:MobileNetV2 默认 ReLU6 在 INT8 量化时(activation 被 clip 到 [0, 6]
|
| 237 |
+
+ 1×1 expand 高动态范围 + DW 各通道差异大)精度损失大。换成 ReLU 后
|
| 238 |
+
激活分布更线性,PTQ 友好,文献和实测都验证过。
|
| 239 |
+
|
| 240 |
+
FP32 精度通常基本不变(< 0.3 点),换来 INT8 PTQ 损失从 2-3 点 → < 1 点。
|
| 241 |
+
"""
|
| 242 |
+
for name, child in module.named_children():
|
| 243 |
+
if isinstance(child, nn.ReLU6):
|
| 244 |
+
setattr(module, name, nn.ReLU(inplace=child.inplace))
|
| 245 |
+
else:
|
| 246 |
+
_replace_relu6_with_relu(child)
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def _build_mobilenet_v2_relu_hccr(
|
| 250 |
+
num_classes: int, in_channels: int = 1, last_channel: int = 576
|
| 251 |
+
) -> nn.Module:
|
| 252 |
+
"""V2-HCCR + ReLU(替换全部 ReLU6),量化友好版。"""
|
| 253 |
+
model = _build_mobilenet_v2_hccr(num_classes, in_channels, last_channel)
|
| 254 |
+
_replace_relu6_with_relu(model)
|
| 255 |
+
return model
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def _build_mobilenet_v2_hccr(
|
| 259 |
+
num_classes: int, in_channels: int = 1, last_channel: int = 576
|
| 260 |
+
) -> nn.Module:
|
| 261 |
+
"""torchvision MobileNetV2 改造 for HCCR:
|
| 262 |
+
- 第一层 conv 改单通道��入(3 → 1)
|
| 263 |
+
- 最后 1×1 expand 从 320→1280 改为 320→last_channel(默认 576),
|
| 264 |
+
避免分类头 Linear(1280, 3755) 单层就 4.8M params
|
| 265 |
+
- 分类头 Linear(last_channel, num_classes)
|
| 266 |
+
|
| 267 |
+
保留 ReLU6 + 标准 inverted residual(无 SE,无 HSwish,无 NAS 不规则宽度),
|
| 268 |
+
量化最稳。skip connection(ResNet 思想)在 inverted residual 块内自动包含。
|
| 269 |
+
|
| 270 |
+
输入: [B, 1, 64, 64]
|
| 271 |
+
输出: [B, num_classes]
|
| 272 |
+
"""
|
| 273 |
+
from torchvision.models import mobilenet_v2
|
| 274 |
+
|
| 275 |
+
model = mobilenet_v2(num_classes=num_classes)
|
| 276 |
+
# 1. 第一层 conv:3 → 1
|
| 277 |
+
model.features[0][0] = nn.Conv2d(
|
| 278 |
+
in_channels, 32, kernel_size=3, stride=2, padding=1, bias=False
|
| 279 |
+
)
|
| 280 |
+
# 2. 最后 1×1 expand:320 → last_channel
|
| 281 |
+
model.features[18] = nn.Sequential(
|
| 282 |
+
nn.Conv2d(320, last_channel, kernel_size=1, bias=False),
|
| 283 |
+
nn.BatchNorm2d(last_channel),
|
| 284 |
+
nn.ReLU6(inplace=True),
|
| 285 |
+
)
|
| 286 |
+
# 3. 分类头:Dropout + Linear
|
| 287 |
+
model.classifier = nn.Sequential(
|
| 288 |
+
nn.Dropout(p=0.2, inplace=False),
|
| 289 |
+
nn.Linear(last_channel, num_classes),
|
| 290 |
+
)
|
| 291 |
+
# 4. 替换层后重新跑 torchvision 的 init 约定(同 V3 处理)
|
| 292 |
+
for layer in (
|
| 293 |
+
model.features[0][0],
|
| 294 |
+
model.features[18][0],
|
| 295 |
+
model.features[18][1],
|
| 296 |
+
model.classifier[1],
|
| 297 |
+
):
|
| 298 |
+
if isinstance(layer, nn.Conv2d):
|
| 299 |
+
nn.init.kaiming_normal_(layer.weight, mode="fan_out")
|
| 300 |
+
if layer.bias is not None:
|
| 301 |
+
nn.init.zeros_(layer.bias)
|
| 302 |
+
elif isinstance(layer, nn.BatchNorm2d):
|
| 303 |
+
nn.init.ones_(layer.weight)
|
| 304 |
+
nn.init.zeros_(layer.bias)
|
| 305 |
+
elif isinstance(layer, nn.Linear):
|
| 306 |
+
nn.init.normal_(layer.weight, 0, 0.01)
|
| 307 |
+
nn.init.zeros_(layer.bias)
|
| 308 |
+
return model
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def build_model(name: str, num_classes: int) -> nn.Module:
|
| 312 |
+
if name == "plain_cnn":
|
| 313 |
+
return PlainCNN(num_classes)
|
| 314 |
+
if name == "mobilenet_v3_small":
|
| 315 |
+
return _build_mobilenet_v3_small_hccr(num_classes)
|
| 316 |
+
if name == "mobilenet_v2":
|
| 317 |
+
return _build_mobilenet_v2_hccr(num_classes)
|
| 318 |
+
if name == "mobilenet_v2_relu":
|
| 319 |
+
return _build_mobilenet_v2_relu_hccr(num_classes)
|
| 320 |
+
if name == "mobilenet_v2_gwap":
|
| 321 |
+
return MobileNetV2WithGWAP(num_classes)
|
| 322 |
+
if name == "melnyk_net":
|
| 323 |
+
return MelnykNet(num_classes)
|
| 324 |
+
raise ValueError(f"未知模型: {name}")
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def count_params(model: nn.Module) -> int:
|
| 328 |
+
return sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
if __name__ == "__main__":
|
| 332 |
+
# smoke:确认能 forward,参数量符合预期
|
| 333 |
+
x = torch.randn(2, 1, 64, 64)
|
| 334 |
+
for name in ("plain_cnn", "mobilenet_v3_small", "mobilenet_v2", "mobilenet_v2_relu", "mobilenet_v2_gwap", "melnyk_net"):
|
| 335 |
+
model = build_model(name, num_classes=3755)
|
| 336 |
+
n = count_params(model)
|
| 337 |
+
bytes_ = sum(p.numel() * p.element_size() for p in model.parameters())
|
| 338 |
+
y = model(x)
|
| 339 |
+
print(f"{name}: {n:,} params ({n/1e6:.2f}M) fp32={bytes_/1e6:.2f}MB "
|
| 340 |
+
f"int8≈{bytes_/4/1e6:.2f}MB out={tuple(y.shape)}")
|
normalize.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""位图 → 64×64 归一化。
|
| 2 |
+
|
| 3 |
+
输入:HWDB GNT 原始灰度位图(uint8,255=白底,0=黑笔画,任意宽高)
|
| 4 |
+
输出:归一化的 64×64 uint8 灰度图,**保持原始规约**(255=白底,0=黑笔画)
|
| 5 |
+
|
| 6 |
+
为什么保持白底黑字:推理时手机端把用户笔迹渲染成"白纸黑笔"喂模型,训练数据
|
| 7 |
+
和推理输入分布对齐;若要反转让前景=高值(MNIST 风格),在 dataloader 用
|
| 8 |
+
`255 - x` 即可。
|
| 9 |
+
|
| 10 |
+
流程:
|
| 11 |
+
1. 阈值化找笔画像素的最小外接矩形(笔画 = 像素值低)
|
| 12 |
+
2. 等比缩放,长边对齐到 56 像素
|
| 13 |
+
3. 居中放到 64×64 画布(白底,周围 4 像素白边距)
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
import numpy as np
|
| 19 |
+
from PIL import Image
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
CANVAS_SIZE = 64
|
| 23 |
+
CONTENT_SIZE = 56
|
| 24 |
+
MARGIN = (CANVAS_SIZE - CONTENT_SIZE) // 2 # 4
|
| 25 |
+
FG_THRESHOLD = 220 # 像素 < 阈值算前景(笔画);GNT 背景 ≈ 255,笔画值偏低
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def normalize(bitmap: np.ndarray) -> np.ndarray:
|
| 29 |
+
"""归一化到 64×64 uint8,白底黑字(255=白,0=黑)。"""
|
| 30 |
+
if bitmap.ndim != 2:
|
| 31 |
+
raise ValueError(f"期望 2D 位图,得到 shape={bitmap.shape}")
|
| 32 |
+
|
| 33 |
+
# 1. 找前景(笔画)外接矩形
|
| 34 |
+
fg = bitmap < FG_THRESHOLD
|
| 35 |
+
if not fg.any():
|
| 36 |
+
# 全白(空样本),返回纯白画布
|
| 37 |
+
return np.full((CANVAS_SIZE, CANVAS_SIZE), 255, dtype=np.uint8)
|
| 38 |
+
ys, xs = np.where(fg)
|
| 39 |
+
y0, y1 = ys.min(), ys.max() + 1
|
| 40 |
+
x0, x1 = xs.min(), xs.max() + 1
|
| 41 |
+
cropped = bitmap[y0:y1, x0:x1]
|
| 42 |
+
|
| 43 |
+
# 2. 等比缩放,长边对齐到 CONTENT_SIZE
|
| 44 |
+
h, w = cropped.shape
|
| 45 |
+
scale = CONTENT_SIZE / max(h, w)
|
| 46 |
+
new_h = max(1, int(round(h * scale)))
|
| 47 |
+
new_w = max(1, int(round(w * scale)))
|
| 48 |
+
pil = Image.fromarray(cropped, mode="L")
|
| 49 |
+
pil = pil.resize((new_w, new_h), Image.BILINEAR)
|
| 50 |
+
resized = np.asarray(pil, dtype=np.uint8)
|
| 51 |
+
|
| 52 |
+
# 3. 居中放到 64×64 画布,白底
|
| 53 |
+
canvas = np.full((CANVAS_SIZE, CANVAS_SIZE), 255, dtype=np.uint8)
|
| 54 |
+
off_y = (CANVAS_SIZE - new_h) // 2
|
| 55 |
+
off_x = (CANVAS_SIZE - new_w) // 2
|
| 56 |
+
canvas[off_y:off_y + new_h, off_x:off_x + new_w] = resized
|
| 57 |
+
return canvas
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
# 自测:造一个 80×100 的白底图,中间一块黑笔画,跑归一化
|
| 62 |
+
src = np.full((100, 80), 255, dtype=np.uint8)
|
| 63 |
+
src[20:80, 15:65] = 50 # 一块笔画(低值)
|
| 64 |
+
out = normalize(src)
|
| 65 |
+
print(f"input shape: {src.shape}, output shape: {out.shape}")
|
| 66 |
+
print(f"output range: [{out.min()}, {out.max()}]")
|
| 67 |
+
print(f"前景像素数(< {FG_THRESHOLD}): {(out < FG_THRESHOLD).sum()}")
|
release_metadata.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "Handwritten",
|
| 3 |
+
"source_checkpoint": "runs/mbv2_phase1_aug/best.pt",
|
| 4 |
+
"source_ncnn_param": "save/output/mbv2_aug.int8.ncnn.param",
|
| 5 |
+
"source_ncnn_bin": "save/output/mbv2_aug.int8.ncnn.bin",
|
| 6 |
+
"source_charset": "data/processed/charset.json",
|
| 7 |
+
"model": "mobilenet_v2",
|
| 8 |
+
"classes": 3755,
|
| 9 |
+
"metrics": {
|
| 10 |
+
"top1": 0.9547,
|
| 11 |
+
"top10": 0.9958
|
| 12 |
+
},
|
| 13 |
+
"sha256": {
|
| 14 |
+
"best.pt": "151f32b03c49dbed77f3b8076035d28ce050d70a1a102e12581274a44cb10460",
|
| 15 |
+
"ncnn/model.ncnn.param": "8c4d0e594d8a4644fb97411d2b6335f55fed2e1b37057bc88fced724295902a3",
|
| 16 |
+
"ncnn/model.ncnn.bin": "86a4802e3c8b2ddf1180f3de361546517d708674381e58b3d10b766dc8fba49e",
|
| 17 |
+
"ncnn/charset.json": "31f5700027c8692122cec5281d12c6382506b36d56075331cd73718f624d4ed2"
|
| 18 |
+
}
|
| 19 |
+
}
|