Chinese Handwriting Recognition β HSK 1
A CNN image classifier trained on the CASIA-HWDB dataset to recognise 178 HSK-1 level Chinese characters from handwritten images.
Model Architecture
- Backbone: ResNet-18 (pretrained on ImageNet, fine-tuned)
- Final layer: Dropout(0.4) β Linear(178 classes)
- Input: RGB image resized to 64 Γ 64
Performance
- Best Validation Accuracy: 98.13%
Training Setup
| Hyperparameter | Value |
|---|---|
| Optimizer | AdamW |
| Learning rate | 1e-3 |
| Weight decay | 1e-4 |
| Scheduler | OneCycleLR |
| Epochs | 15 |
| Batch size | 128 |
| GPUs | T4 x2 |
| Augmentation | RandomRotation(10Β°), RandomAffine, ColorJitter |
Usage
import torch
from PIL import Image
from torchvision import transforms
import torch.nn as nn
import torchvision.models as models
from huggingface_hub import hf_hub_download
# ββ Download checkpoint βββββββββββββββββββββββββββββββ
ckpt_path = hf_hub_download('ChrisMoe/Chinese_handwriting_model', 'chinese_hsk1_model.pth')
checkpoint = torch.load(ckpt_path, map_location='cpu')
# ββ Rebuild model βββββββββββββββββββββββββββββββββββββ
class ChineseCharCNN(nn.Module):
def __init__(self, num_classes, dropout=0.4):
super().__init__()
backbone = models.resnet18(weights=None)
in_features = backbone.fc.in_features
backbone.fc = nn.Sequential(
nn.Dropout(dropout),
nn.Linear(in_features, num_classes)
)
self.model = backbone
def forward(self, x):
return self.model(x)
model = ChineseCharCNN(num_classes=checkpoint['num_classes'])
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()
idx2char = checkpoint['idx2char']
# ββ Inference βββββββββββββββββββββββββββββββββββββββββ
transform = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
img = Image.open('your_character.png').convert('RGB')
tensor = transform(img).unsqueeze(0)
with torch.no_grad():
logits = model(tensor)
pred_idx = logits.argmax(1).item()
print('Predicted character:', idx2char[pred_idx])