Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: zh
|
| 3 |
+
tags:
|
| 4 |
+
- image-classification
|
| 5 |
+
- chinese
|
| 6 |
+
- handwriting
|
| 7 |
+
- hsk1
|
| 8 |
+
- pytorch
|
| 9 |
+
license: mit
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Chinese Handwriting Recognition β HSK 1
|
| 13 |
+
|
| 14 |
+
A CNN image classifier trained on the CASIA-HWDB dataset
|
| 15 |
+
to recognise **178 HSK-1 level Chinese characters** from handwritten images.
|
| 16 |
+
|
| 17 |
+
## Model Architecture
|
| 18 |
+
- Backbone: **ResNet-18** (pretrained on ImageNet, fine-tuned)
|
| 19 |
+
- Final layer: Dropout(0.4) β Linear(178 classes)
|
| 20 |
+
- Input: RGB image resized to 64 Γ 64
|
| 21 |
+
|
| 22 |
+
## Performance
|
| 23 |
+
- Best Validation Accuracy: **98.13%**
|
| 24 |
+
|
| 25 |
+
## Training Setup
|
| 26 |
+
| Hyperparameter | Value |
|
| 27 |
+
|---|---|
|
| 28 |
+
| Optimizer | AdamW |
|
| 29 |
+
| Learning rate | 1e-3 |
|
| 30 |
+
| Weight decay | 1e-4 |
|
| 31 |
+
| Scheduler | OneCycleLR |
|
| 32 |
+
| Epochs | 15 |
|
| 33 |
+
| Batch size | 128 |
|
| 34 |
+
| GPUs | T4 x2 |
|
| 35 |
+
| Augmentation | RandomRotation(10Β°), RandomAffine, ColorJitter |
|
| 36 |
+
|
| 37 |
+
## Usage
|
| 38 |
+
|
| 39 |
+
```python
|
| 40 |
+
import torch
|
| 41 |
+
from PIL import Image
|
| 42 |
+
from torchvision import transforms
|
| 43 |
+
import torch.nn as nn
|
| 44 |
+
import torchvision.models as models
|
| 45 |
+
from huggingface_hub import hf_hub_download
|
| 46 |
+
|
| 47 |
+
# ββ Download checkpoint βββββββββββββββββββββββββββββββ
|
| 48 |
+
ckpt_path = hf_hub_download('ChrisMoe/Chinese_handwriting_model', 'chinese_hsk1_model.pth')
|
| 49 |
+
checkpoint = torch.load(ckpt_path, map_location='cpu')
|
| 50 |
+
|
| 51 |
+
# ββ Rebuild model βββββββββββββββββββββββββββββββββββββ
|
| 52 |
+
class ChineseCharCNN(nn.Module):
|
| 53 |
+
def __init__(self, num_classes, dropout=0.4):
|
| 54 |
+
super().__init__()
|
| 55 |
+
backbone = models.resnet18(weights=None)
|
| 56 |
+
in_features = backbone.fc.in_features
|
| 57 |
+
backbone.fc = nn.Sequential(
|
| 58 |
+
nn.Dropout(dropout),
|
| 59 |
+
nn.Linear(in_features, num_classes)
|
| 60 |
+
)
|
| 61 |
+
self.model = backbone
|
| 62 |
+
def forward(self, x):
|
| 63 |
+
return self.model(x)
|
| 64 |
+
|
| 65 |
+
model = ChineseCharCNN(num_classes=checkpoint['num_classes'])
|
| 66 |
+
model.load_state_dict(checkpoint['model_state_dict'])
|
| 67 |
+
model.eval()
|
| 68 |
+
|
| 69 |
+
idx2char = checkpoint['idx2char']
|
| 70 |
+
|
| 71 |
+
# ββ Inference βββββββββββββββββββββββββββββββββββββββββ
|
| 72 |
+
transform = transforms.Compose([
|
| 73 |
+
transforms.Resize((64, 64)),
|
| 74 |
+
transforms.ToTensor(),
|
| 75 |
+
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
|
| 76 |
+
])
|
| 77 |
+
img = Image.open('your_character.png').convert('RGB')
|
| 78 |
+
tensor = transform(img).unsqueeze(0)
|
| 79 |
+
with torch.no_grad():
|
| 80 |
+
logits = model(tensor)
|
| 81 |
+
pred_idx = logits.argmax(1).item()
|
| 82 |
+
print('Predicted character:', idx2char[pred_idx])
|
| 83 |
+
```
|