Views
No views yet
1graph TD
2 A[Input Arabic Image] --> B[ViT Encoder]
3 B -->|Visual Embeddings| C[Cross-Attention]
4 D[Previous Tokens] --> E[RoBERTa Decoder]
5 E --> C
6 C --> F[Next Token Prediction]
7 F -->|Generated Text| G[Final Arabic Transcription]
8
9 subgraph "Encoder (Vision Transformer)"
10 B
11 end
12
13 subgraph "Decoder (Language Model)"
14 E
15 endmssqpi/Arabic-OCR-Dataset for robust handling of various Arabic fonts and styles.microsoft/trocr-base-handwritten.[!IMPORTANT] Qalam-Net V2 differs from traditional OCR by eliminating the need for an external language model or a separate CTC (Connectionist Temporal Classification) layer.
| Metric | Value |
|---|---|
| Training Samples | 5,000 |
| Optimizer | AdamW |
| Learning Rate | 3e-5 |
| Convergence (Loss) | 9.5 → 0.03 |
[!TIP] Even with a single epoch, the model reached a training loss of 0.03, indicating highly efficient transfer learning from the base TrOCR weights.
pip install transformers datasets Pillow torch1import torch
2from PIL import Image, ImageDraw, ImageFont
3from transformers import TrOCRProcessor, VisionEncoderDecoderModel
4
5MODEL_NAME = "Ali0044/Qalam_Net_V2"
6processor = TrOCRProcessor.from_pretrained(MODEL_NAME)
7model = VisionEncoderDecoderModel.from_pretrained(MODEL_NAME)
8
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10model.to(device)
11model.eval()
12
13def run_ocr(image):
14 pixel_values = processor(image, return_tensors="pt").pixel_values.to(device)
15 with torch.no_grad():
16 generated_ids = model.generate(pixel_values)
17 return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
18
19image = Image.new('RGB', (200, 50), color = 'white')
20d = ImageDraw.Draw(image)
21try:
22 font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
23except IOError:
24 font = ImageFont.load_default()
25d.text((10,10), "المتميزة", fill=(0,0,0), font=font)
26
27print(f"Predicted Transcription: {run_ocr(image)}")
28image.show()Ali0044.