init
Browse files
README.md
DELETED
|
@@ -1,121 +0,0 @@
|
|
| 1 |
-
# Handwriting Recognition
|
| 2 |
-
|
| 3 |
-
Complete handwriting recognition system using CNN-BiLSTM-CTC on the IAM dataset.
|
| 4 |
-
|
| 5 |
-
## π Files
|
| 6 |
-
|
| 7 |
-
### 1. **analysis.ipynb** - Dataset Analysis
|
| 8 |
-
- Exploratory Data Analysis (EDA)
|
| 9 |
-
- 5 detailed charts saved to `charts/` folder
|
| 10 |
-
- Run locally or on Colab (no GPU needed)
|
| 11 |
-
|
| 12 |
-
### 2. **train_colab.ipynb** - Model Training (GPU)
|
| 13 |
-
- **β‘ Google Colab GPU compatible**
|
| 14 |
-
- Full training pipeline
|
| 15 |
-
- CNN-BiLSTM-CTC model (~9.1M parameters)
|
| 16 |
-
- Automatic model saving
|
| 17 |
-
- Download trained model for deployment
|
| 18 |
-
|
| 19 |
-
## π Quick Start
|
| 20 |
-
|
| 21 |
-
### Option 1: Analyze Dataset (Local/Colab)
|
| 22 |
-
```bash
|
| 23 |
-
jupyter notebook analysis.ipynb
|
| 24 |
-
```
|
| 25 |
-
- No GPU needed
|
| 26 |
-
- Generates 5 EDA charts
|
| 27 |
-
- Fast (~2 minutes)
|
| 28 |
-
|
| 29 |
-
### Option 2: Train Model (Google Colab GPU)
|
| 30 |
-
|
| 31 |
-
1. **Upload `train_colab.ipynb` to Google Colab**
|
| 32 |
-
2. **Change runtime to GPU:**
|
| 33 |
-
- Runtime β Change runtime type β GPU (T4 recommended)
|
| 34 |
-
3. **Run all cells**
|
| 35 |
-
4. **Download trained model** (last cell)
|
| 36 |
-
|
| 37 |
-
**Training Time:** ~1-2 hours for 20 epochs on T4 GPU
|
| 38 |
-
|
| 39 |
-
## π Charts Generated
|
| 40 |
-
|
| 41 |
-
From `analysis.ipynb`:
|
| 42 |
-
1. `charts/01_sample_images.png` - 10 sample handwritten texts
|
| 43 |
-
2. `charts/02_text_length_distribution.png` - Text statistics
|
| 44 |
-
3. `charts/03_image_dimensions.png` - Image analysis
|
| 45 |
-
4. `charts/04_character_frequency.png` - Character distribution
|
| 46 |
-
5. `charts/05_summary_statistics.png` - Summary table
|
| 47 |
-
|
| 48 |
-
## π― Model Details
|
| 49 |
-
|
| 50 |
-
**Architecture:**
|
| 51 |
-
- **CNN**: 7 convolutional blocks (feature extraction)
|
| 52 |
-
- **BiLSTM**: 2 layers, 256 hidden units (sequence modeling)
|
| 53 |
-
- **CTC Loss**: Alignment-free training
|
| 54 |
-
|
| 55 |
-
**Dataset:** Teklia/IAM-line (Hugging Face)
|
| 56 |
-
- Train: 6,482 samples
|
| 57 |
-
- Validation: 976 samples
|
| 58 |
-
- Test: 2,915 samples
|
| 59 |
-
|
| 60 |
-
**Metrics:**
|
| 61 |
-
- **CER** (Character Error Rate)
|
| 62 |
-
- **WER** (Word Error Rate)
|
| 63 |
-
|
| 64 |
-
## πΎ Model Files
|
| 65 |
-
|
| 66 |
-
After training in Colab:
|
| 67 |
-
- `best_model.pth` - Trained model weights
|
| 68 |
-
- `training_history.png` - Loss/CER/WER plots
|
| 69 |
-
- `predictions.png` - Sample predictions
|
| 70 |
-
|
| 71 |
-
## π¦ Requirements
|
| 72 |
-
|
| 73 |
-
```
|
| 74 |
-
torch>=2.0.0
|
| 75 |
-
datasets>=2.14.0
|
| 76 |
-
pillow>=9.5.0
|
| 77 |
-
numpy>=1.24.0
|
| 78 |
-
matplotlib>=3.7.0
|
| 79 |
-
seaborn>=0.13.0
|
| 80 |
-
jupyter>=1.0.0
|
| 81 |
-
jiwer>=3.0.0
|
| 82 |
-
```
|
| 83 |
-
|
| 84 |
-
## π§ Usage
|
| 85 |
-
|
| 86 |
-
### Load Trained Model
|
| 87 |
-
```python
|
| 88 |
-
import torch
|
| 89 |
-
|
| 90 |
-
# Load checkpoint
|
| 91 |
-
checkpoint = torch.load('best_model.pth')
|
| 92 |
-
char_mapper = checkpoint['char_mapper']
|
| 93 |
-
|
| 94 |
-
# Create model
|
| 95 |
-
from train_colab import CRNN # Copy model class
|
| 96 |
-
model = CRNN(num_chars=len(char_mapper.chars))
|
| 97 |
-
model.load_state_dict(checkpoint['model_state_dict'])
|
| 98 |
-
model.eval()
|
| 99 |
-
|
| 100 |
-
# Predict
|
| 101 |
-
# ... (preprocessing + inference)
|
| 102 |
-
```
|
| 103 |
-
|
| 104 |
-
## π Notes
|
| 105 |
-
|
| 106 |
-
- **GPU strongly recommended** for training (use Colab T4)
|
| 107 |
-
- Training on CPU will be extremely slow (~20x slower)
|
| 108 |
-
- Colab free tier: 12-hour limit, sufficient for 20 epochs
|
| 109 |
-
- Model checkpoint includes character mapper for deployment
|
| 110 |
-
|
| 111 |
-
## π Training Tips
|
| 112 |
-
|
| 113 |
-
1. **Start with fewer epochs** (5-10) to test
|
| 114 |
-
2. **Monitor CER/WER** - stop if not improving
|
| 115 |
-
3. **Increase epochs** if still improving (up to 50)
|
| 116 |
-
4. **Save checkpoint** before Colab disconnects
|
| 117 |
-
5. **Download model immediately** after training
|
| 118 |
-
|
| 119 |
-
## π License
|
| 120 |
-
|
| 121 |
-
Dataset: IAM Database (research use)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scripts/upload_instructions.md
DELETED
|
@@ -1,97 +0,0 @@
|
|
| 1 |
-
# Upload Model to Hugging Face Hub
|
| 2 |
-
|
| 3 |
-
## Quick Start (3 Steps)
|
| 4 |
-
|
| 5 |
-
### 1. Install Hugging Face Hub
|
| 6 |
-
```bash
|
| 7 |
-
pip install huggingface_hub
|
| 8 |
-
```
|
| 9 |
-
|
| 10 |
-
### 2. Login to Hugging Face
|
| 11 |
-
```bash
|
| 12 |
-
huggingface-cli login
|
| 13 |
-
```
|
| 14 |
-
Enter your Hugging Face token when prompted. Get your token from: https://huggingface.co/settings/tokens
|
| 15 |
-
|
| 16 |
-
### 3. Run Upload Script
|
| 17 |
-
```bash
|
| 18 |
-
python upload_to_huggingface.py
|
| 19 |
-
```
|
| 20 |
-
|
| 21 |
-
---
|
| 22 |
-
|
| 23 |
-
## Alternative: Manual Upload via Web Interface
|
| 24 |
-
|
| 25 |
-
1. Go to https://huggingface.co/new
|
| 26 |
-
2. Create a new model repository (e.g., `handwriting-recognition-iam`)
|
| 27 |
-
3. Click "Files" β "Add file" β "Upload files"
|
| 28 |
-
4. Upload:
|
| 29 |
-
- `best_model.pth`
|
| 30 |
-
- `README.md`
|
| 31 |
-
- `requirements.txt`
|
| 32 |
-
- `train_colab.ipynb`
|
| 33 |
-
- `training_history.png`
|
| 34 |
-
|
| 35 |
-
---
|
| 36 |
-
|
| 37 |
-
## Alternative: Upload from Python (Colab/Script)
|
| 38 |
-
|
| 39 |
-
```python
|
| 40 |
-
from huggingface_hub import HfApi, create_repo, upload_file
|
| 41 |
-
|
| 42 |
-
# Login first (in Colab)
|
| 43 |
-
from huggingface_hub import notebook_login
|
| 44 |
-
notebook_login()
|
| 45 |
-
|
| 46 |
-
# Create repository
|
| 47 |
-
api = HfApi()
|
| 48 |
-
repo_id = "your-username/handwriting-recognition-iam"
|
| 49 |
-
create_repo(repo_id, repo_type="model", exist_ok=True)
|
| 50 |
-
|
| 51 |
-
# Upload model
|
| 52 |
-
upload_file(
|
| 53 |
-
path_or_fileobj="best_model.pth",
|
| 54 |
-
path_in_repo="best_model.pth",
|
| 55 |
-
repo_id=repo_id,
|
| 56 |
-
repo_type="model"
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
print(f"β Uploaded! View at: https://huggingface.co/{repo_id}")
|
| 60 |
-
```
|
| 61 |
-
|
| 62 |
-
---
|
| 63 |
-
|
| 64 |
-
## What Gets Uploaded
|
| 65 |
-
|
| 66 |
-
- β
`best_model.pth` - Trained model checkpoint (105MB)
|
| 67 |
-
- β
`README.md` - Project documentation
|
| 68 |
-
- β
`requirements.txt` - Dependencies
|
| 69 |
-
- β
`train_colab.ipynb` - Training notebook
|
| 70 |
-
- β
`training_history.png` - Training metrics visualization
|
| 71 |
-
|
| 72 |
-
---
|
| 73 |
-
|
| 74 |
-
## Customization
|
| 75 |
-
|
| 76 |
-
Edit `upload_to_huggingface.py` to change:
|
| 77 |
-
- `REPO_NAME` - Your preferred repository name
|
| 78 |
-
- `private=False` - Set to `True` for private repository
|
| 79 |
-
- `FILES_TO_UPLOAD` - Add/remove files to upload
|
| 80 |
-
|
| 81 |
-
---
|
| 82 |
-
|
| 83 |
-
## Troubleshooting
|
| 84 |
-
|
| 85 |
-
### "Authentication required"
|
| 86 |
-
```bash
|
| 87 |
-
huggingface-cli login
|
| 88 |
-
```
|
| 89 |
-
|
| 90 |
-
### "Repository already exists"
|
| 91 |
-
- The script uses `exist_ok=True`, so it will update existing repo
|
| 92 |
-
- Or change `REPO_NAME` to create a new one
|
| 93 |
-
|
| 94 |
-
### Large file upload fails
|
| 95 |
-
- Hugging Face supports files up to 50GB
|
| 96 |
-
- Your model (105MB) should upload fine
|
| 97 |
-
- If it fails, try uploading via web interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scripts/upload_to_huggingface.py β upload_to_huggingface.py
RENAMED
|
@@ -8,7 +8,7 @@ from huggingface_hub import HfApi, create_repo, upload_file
|
|
| 8 |
# Configuration
|
| 9 |
MODEL_PATH = "best_model.pth"
|
| 10 |
REPO_NAME = "handwriting-recognition-iam" # Change this to your preferred name
|
| 11 |
-
USERNAME =
|
| 12 |
|
| 13 |
# Files to upload
|
| 14 |
FILES_TO_UPLOAD = [
|
|
|
|
| 8 |
# Configuration
|
| 9 |
MODEL_PATH = "best_model.pth"
|
| 10 |
REPO_NAME = "handwriting-recognition-iam" # Change this to your preferred name
|
| 11 |
+
USERNAME = "IsmatS" # Will use your HF username automatically
|
| 12 |
|
| 13 |
# Files to upload
|
| 14 |
FILES_TO_UPLOAD = [
|