All training and inference code is available in the GitHub repository:
https://github.com/aaghaazkhan/Qwen2_5_3B_VL_HandWritten_LaTeX_OCR
Model Description
This model converts images of handwritten mathematical expressions into LaTeX code using a fine-tuned version of Qwen 2.5-VL. It was trained using LoRA on a 4-bit quantized model setup, making it GPU-friendly and accessible for low-resource environments.
Ideal for automating LaTeX conversion in:
- Math learning apps
- Academic tools
- E-notes platforms
- Personal study or research agents
Hardware Used
Trained locally on NVIDIA RTX 3050 (6GB VRAM)
Total VRAM usage: 5.72 GB
Using π€ Transformers to Chat
from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info
model_id = "aaghaazkhan/Qwen2_5_3B_VL_HandWritten_LaTeX_OCR"
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
"aaghaazkhan/Qwen2_5_3B_VL_HandWritten_LaTeX_OCR", torch_dtype="auto", device_map="auto"
)
processor = AutoProcessor.from_pretrained(model_id)
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "latex_demo.png",
},
{"type": "text", "text": "Convert the mathematical content in the image into LaTeX."},
],
}
]
# Preparation for inference
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to("cuda")
# Inference: Generation of the output
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
Training Details
| Setting | Value |
|---|---|
| LoRA Rank | 16 |
| LoRA Alpha | 32 |
| Learning Rate | 2e-4 |
| Batch Size | 2 (with grad accumulation 8) |
| Precision | 4-bit NF4 |
| Epochs | 1 |
| VRAM Used | ~5.7GB |
| Training Duration | 49 mins (local) |
| Gradient Checkpointing | Enabled |
Evaluation Metrics
| Metric | Value |
|---|---|
| Token Accuracy | ~99.8% |
| Exact Match | ~90% |
| Val Loss | ~0.006 |
Author
Aaghaaz Khan
Inference Providers
NEW
This model isn't deployed by any Inference Provider.
π
Ask for provider support
Model tree for aaghaazkhan/Qwen2_5_3B_VL_HandWritten_LaTeX_OCR
Base model
Qwen/Qwen2.5-VL-3B-Instruct