This repository contains code, dataset setup, and scripts used to fine-tune Qwen2.5-VL-3B for converting handwritten mathematical expressions into LaTeX code.
Ideal for automating LaTeX conversion in:
Math learning apps Academic tools E-notes platforms Personal study or research agents
Model available on Hugging Face: https://huggingface.co/aaghaazkhan/Qwen2_5_3B_VL_HandWritten_LaTeX_OCR
This project demonstrates how to:
- Fine-tune a multimodal vision-language model using LoRA
- Train a model on low VRAM (6GB GPU)
- Convert handwritten equations into clean LaTeX
- Measure performance using Exact Match and Token Accuracy
- Run inference locally with Transformers
- Handwritten Math to LaTeX Code
- Trained on NVIDIA RTX 3050 (6GB)
- Total VRAM usage: 5.72 GB
- Qwen 2.5-VL-3B multimodal base
- Efficient fine-tuning using 4-bit quantization + LoRA
Base model:
Sample 1:
Sample 2:
Fine-tuned model:
Sample 1:
Sample 2:
Training Loss Curve
Validation Loss Curve
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(
model_id, 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)| 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 |
| Metric | Value |
|---|---|
| Token Accuracy | ~99.8% |
| Exact Match | ~90% |
| Val Loss | ~0.006 |
Install the dependencies using:
pip install -r requirements.txt
This project is licensed under the MIT License.
Aaghaaz Khan
- Hugging Face: https://huggingface.co/aaghaazkhan
- LinkedIn: https://www.linkedin.com/in/aaghaaz-khan-778b372a8