Instructions to use Ali0044/Qalam-Net with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use Ali0044/Qalam-Net with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://Ali0044/Qalam-Net") - Notebooks
- Google Colab
- Kaggle
Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -12,11 +12,11 @@ tags:
|
|
| 12 |
|
| 13 |
# Qalam-Net (قلم-نت): Advanced Arabic OCR
|
| 14 |
|
| 15 |
-
Qalam-Net is a high-performance, cross-backend Optical Character Recognition (OCR) model for Arabic. Built on **Keras 3**, it supports **JAX**, **PyTorch**, and **TensorFlow** backends
|
| 16 |
|
| 17 |
## 🚀 Quick Start (Advanced Usage)
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
### 1. Installation
|
| 22 |
```bash
|
|
@@ -29,16 +29,32 @@ import os
|
|
| 29 |
os.environ["KERAS_BACKEND"] = "jax" # Switch to "tensorflow" or "torch" if preferred
|
| 30 |
|
| 31 |
import keras
|
|
|
|
| 32 |
import numpy as np
|
| 33 |
import cv2
|
| 34 |
from huggingface_hub import hf_hub_download
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
class QalamNet:
|
| 37 |
def __init__(self, repo_id="Ali0044/Qalam-Net"):
|
| 38 |
-
# Download
|
| 39 |
-
print(f"Fetching model from {repo_id}...")
|
| 40 |
model_path = hf_hub_download(repo_id=repo_id, filename="model.keras")
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Standard Arabic Vocabulary
|
| 44 |
self.vocab = [' ', '!', '"', '#', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '=', '?', '[', ']', 'ء', 'آ', 'أ', 'ؤ', 'إ', 'ئ', 'ا', 'ب', 'ة', 'ت', 'ث', 'ج', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ـ', 'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ى', 'ي', 'ً', 'ٌ', 'ٍ', 'َ', 'ُ', 'ِ', 'ّ', 'ْ', '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']
|
|
@@ -53,19 +69,19 @@ class QalamNet:
|
|
| 53 |
|
| 54 |
def predict(self, image_path):
|
| 55 |
batch_img = self.preprocess(image_path)
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
# CTC Decode
|
| 59 |
-
input_len = np.ones(
|
| 60 |
-
results = keras.backend.ctc_decode(
|
| 61 |
|
| 62 |
-
|
| 63 |
-
text = "".join([self.vocab[int(res)] for res in results[0] if res != -1])
|
| 64 |
-
return text
|
| 65 |
|
| 66 |
# Usage
|
| 67 |
# ocr = QalamNet()
|
| 68 |
-
# print(
|
| 69 |
```
|
| 70 |
|
| 71 |
## 🧠 Model Architecture
|
|
@@ -73,7 +89,6 @@ Qalam-Net employs a specialized **CNN-BiLSTM-Attention** pipeline:
|
|
| 73 |
- **Spatial Features**: 3-block CNN for robust feature extraction.
|
| 74 |
- **Sequence Context**: Dual Bidirectional LSTMs to capture Arabic script flow.
|
| 75 |
- **Focus Mechanism**: Self-attention layer for character-level precision.
|
| 76 |
-
- **Decoding**: Connectionist Temporal Classification (CTC).
|
| 77 |
|
| 78 |
---
|
| 79 |
**Developed by [Ali Khalid](https://github.com/Ali0044)**
|
|
|
|
| 12 |
|
| 13 |
# Qalam-Net (قلم-نت): Advanced Arabic OCR
|
| 14 |
|
| 15 |
+
Qalam-Net is a high-performance, cross-backend Optical Character Recognition (OCR) model for Arabic. Built on **Keras 3**, it supports **JAX**, **PyTorch**, and **TensorFlow** backends.
|
| 16 |
|
| 17 |
## 🚀 Quick Start (Advanced Usage)
|
| 18 |
|
| 19 |
+
To use Qalam-Net, you must define the `CTCLayer` (used during training) and pass it as a custom object.
|
| 20 |
|
| 21 |
### 1. Installation
|
| 22 |
```bash
|
|
|
|
| 29 |
os.environ["KERAS_BACKEND"] = "jax" # Switch to "tensorflow" or "torch" if preferred
|
| 30 |
|
| 31 |
import keras
|
| 32 |
+
from keras import layers
|
| 33 |
import numpy as np
|
| 34 |
import cv2
|
| 35 |
from huggingface_hub import hf_hub_download
|
| 36 |
|
| 37 |
+
# [MANDATORY] Define the CTCLayer for deserialization
|
| 38 |
+
@keras.saving.register_keras_serializable()
|
| 39 |
+
class CTCLayer(layers.Layer):
|
| 40 |
+
def __init__(self, name=None, **kwargs):
|
| 41 |
+
super().__init__(name=name, **kwargs)
|
| 42 |
+
self.loss_fn = keras.backend.ctc_batch_cost
|
| 43 |
+
|
| 44 |
+
def call(self, y_true, y_pred):
|
| 45 |
+
return y_pred
|
| 46 |
+
|
| 47 |
class QalamNet:
|
| 48 |
def __init__(self, repo_id="Ali0044/Qalam-Net"):
|
| 49 |
+
# Download the model
|
|
|
|
| 50 |
model_path = hf_hub_download(repo_id=repo_id, filename="model.keras")
|
| 51 |
+
|
| 52 |
+
# Load with custom_objects
|
| 53 |
+
self.model = keras.saving.load_model(
|
| 54 |
+
model_path,
|
| 55 |
+
custom_objects={"CTCLayer": CTCLayer},
|
| 56 |
+
compile=False
|
| 57 |
+
)
|
| 58 |
|
| 59 |
# Standard Arabic Vocabulary
|
| 60 |
self.vocab = [' ', '!', '"', '#', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '=', '?', '[', ']', 'ء', 'آ', 'أ', 'ؤ', 'إ', 'ئ', 'ا', 'ب', 'ة', 'ت', 'ث', 'ج', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ـ', 'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ى', 'ي', 'ً', 'ٌ', 'ٍ', 'َ', 'ُ', 'ِ', 'ّ', 'ْ', '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']
|
|
|
|
| 69 |
|
| 70 |
def predict(self, image_path):
|
| 71 |
batch_img = self.preprocess(image_path)
|
| 72 |
+
# The model has 2 inputs [image, label] but we only need image for prediction
|
| 73 |
+
# We can pass dummy labels or use the internal prediction layers
|
| 74 |
+
preds = self.model.predict([batch_img, np.zeros((1, 1))])
|
| 75 |
|
| 76 |
+
# CTC Decode
|
| 77 |
+
input_len = np.ones(preds.shape[0]) * preds.shape[1]
|
| 78 |
+
results = keras.backend.ctc_decode(preds, input_length=input_len, greedy=True)[0][0]
|
| 79 |
|
| 80 |
+
return "".join([self.vocab[int(res)] for res in results[0] if res != -1])
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Usage
|
| 83 |
# ocr = QalamNet()
|
| 84 |
+
# print(ocr.predict("text.png"))
|
| 85 |
```
|
| 86 |
|
| 87 |
## 🧠 Model Architecture
|
|
|
|
| 89 |
- **Spatial Features**: 3-block CNN for robust feature extraction.
|
| 90 |
- **Sequence Context**: Dual Bidirectional LSTMs to capture Arabic script flow.
|
| 91 |
- **Focus Mechanism**: Self-attention layer for character-level precision.
|
|
|
|
| 92 |
|
| 93 |
---
|
| 94 |
**Developed by [Ali Khalid](https://github.com/Ali0044)**
|