File size: 6,034 Bytes
8a478a7 598e2e1 8a478a7 598e2e1 8a478a7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | ---
language:
- ckb
license: cc-by-nc-4.0
tags:
- handwritten-text-recognition
- kurdish
- sorani
- crnn
- frequency-adaptive-attention
- ctc
- pytorch
- safetensors
datasets:
- DASTNUS
metrics:
- cer
- wer
pipeline_tag: image-to-text
---
# KHWR: Kurdish Handwritten Word Recognition
This repository hosts the trained models and inference code accompanying the
study on Kurdish handwritten word recognition with the proposed
**Frequency-Adaptive Attention (FAA)** mechanism, evaluated on the **DASTNUS
Unique Words** subset against three competitive attention baselines under a
controlled multi-seed protocol.
The repository includes four base architectures (Baseline, Luong, MHSA, and
the proposed FAA).
## Repository Structure
```
KHWR/
βββ FAA-Word-Model/ # Proposed FAA (seed 42)
β βββ model.safetensors
β βββ config.json
β βββ vocab.json
β βββ idx_to_char.json
β βββ README.md
βββ Baseline-Word-Model/ # CRNN without attention (seed 42)
βββ Luong-Word-Model/ # Luong multiplicative attention (seed 42)
βββ MHSA-Word-Model/ # Multi-Head Self-Attention (seed 42)
βββ Scripts/
β βββ train.py
β βββ inference.py
βββ Sample/
β βββ sample_word.tif
β βββ sample_word.txt
βββ requirements.txt
βββ README.md
```
## Architecture
All four model families share an identical CNN and BiLSTM backbone and differ
only in the attention block placed between BiLSTM layers 2 and 3, which
isolates the contribution of each attention design.
| Model | Attention Block | Parameters |
|-------|------------------|:----------:|
| Baseline | none | 3,838,065 |
| Luong | multiplicative attention | 3,915,345 |
| MHSA | Multi-Head Self-Attention (4 heads, ff = 320) | 4,044,625 |
| **FAA (proposed)** | **Frequency-Adaptive Attention** | **3,997,859** |
Shared backbone:
- CNN: 6 convolutional blocks, maximum 256 channels
- RNN: 3 BiLSTM layers, hidden size 160 per direction
- Decoder: Connectionist Temporal Classification (CTC)
- Input resolution: 64 Γ 164 grayscale
- Vocabulary size: 113 (CTC blank + 112 Kurdish characters and symbols)
## Performance
### Multi-Seed Evaluation on DASTNUS Unique Words
Test set of 8,036 word images, five random seeds (42, 7, 123, 456, 789):
| Model | Mean CER Β± Std | Mean WER Β± Std |
|-------|:-:|:-:|
| Baseline | 0.0374 Β± 0.0009 | 0.1525 Β± 0.0037 |
| Luong | 0.0381 Β± 0.0009 | 0.1612 Β± 0.0040 |
| MHSA | 0.0403 Β± 0.0023 | 0.1709 Β± 0.0079 |
| **FAA (proposed)** | **0.0358 Β± 0.0013** | **0.1426 Β± 0.0044** |
### Few-Shot Cross-Domain Adaptation
FAA seed-42 checkpoint adapted to three additional DASTNUS subsets:
| Budget | Person Names CER | Place Names CER | Month Names CER |
|:-:|:-:|:-:|:-:|
| 0% (zero-shot) | 0.1448 | 0.2691 | 0.2043 |
| 5% | 0.1264 | 0.1856 | 0.0157 |
| 100% | **0.0380** | **0.0268** | **0.0087** |
## Installation
```bash
git clone https://huggingface.co/Karez/KHWR
cd KHWR
pip install -r requirements.txt
```
## Quick Start
### Inference
The inference script automatically detects the model family from the
`config.json` next to the chosen `model.safetensors`, so a single command
works for any of the seven model folders in this repository.
```bash
# Single image
python Scripts/inference.py \
--image Sample/sample_word.tif \
--model_path FAA-Word-Model/model.safetensors \
--vocab_path FAA-Word-Model/vocab.json
# Directory of images, save predictions to TSV
python Scripts/inference.py \
--image_dir ./test_words \
--model_path FAA-Word-Model/model.safetensors \
--vocab_path FAA-Word-Model/vocab.json \
--output_file predictions.tsv
```
### Training
The training script handles all four model families via the `--model_type`
flag, sharing the identical backbone and hyperparameters used in the paper.
```bash
# Train the proposed FAA model
python Scripts/train.py \
--model_type faa \
--data_dir ./data/DASTNUS/Unique-Words \
--vocab_path FAA-Word-Model/vocab.json \
--output_dir ./output/faa_seed42 \
--seed 42
# Train one of the baselines (replace faa with baseline / luong / mhsa)
python Scripts/train.py \
--model_type mhsa \
--data_dir ./data/DASTNUS/Unique-Words \
--vocab_path FAA-Word-Model/vocab.json \
--output_dir ./output/mhsa_seed42 \
--seed 42
```
### Few-Shot Fine-Tuning
Initialize from the seed-42 FAA checkpoint, then fine-tune on a target subset
with a smaller learning rate and a shorter schedule:
```bash
python Scripts/train.py \
--model_type faa \
--data_dir ./data/DASTNUS/Person-Names \
--vocab_path FAA-Word-Model/vocab.json \
--init_checkpoint FAA-Word-Model/best_model.pth \
--learning_rate 1e-4 \
--num_epochs 30 \
--patience 5 \
--output_dir ./output/faa_person_finetune
```
## Models
| Folder | Architecture | Test CER | Test WER |
|--------|--------------|:-:|:-:|
| `FAA-Word-Model/` | CRNN + Frequency-Adaptive Attention | 0.0373 | 0.1480 |
| `Baseline-Word-Model/` | CRNN (no attention) | 0.0380 | 0.1544 |
| `Luong-Word-Model/` | CRNN + Luong attention | 0.0391 | 0.1663 |
| `MHSA-Word-Model/` | CRNN + Multi-Head Self-Attention | 0.0383 | 0.1655 |
All values reported on the held-out test split of the corresponding subset
under greedy CTC decoding, seed 42.
## Dataset
The models in this repository are trained on the DASTNUS Kurdish handwritten
text dataset. Relevant statistics:
| Subset | Samples | Unique Words | Vocabulary |
|--------|:-:|:-:|:-:|
| Unique Words | 54,191 | 2,750 | 21,796 (full DASTNUS) |
## Citation
```bibtex
```
## License
Released under the CC BY-NC 4.0 license. The models and dataset are intended
for non-commercial scientific research only. |