How linxueyuanstdio/latex_ocr works
Project Type and Purpose
• This is an end‑to‑end deep‑learning OCR system for LaTeX formulas:
• It generates training data by rendering LaTeX → image.
• It trains a Seq2Seq+Attention+BeamSearch model.
• It performs inference and evaluates results.
• It provides visualization (attention maps, TensorBoard).
File Structure
• Top‐level scripts:
• build.py (data generation)
• train.py (model training)
• predict.py / evaluate_txt.py / evaluate_img.py (inference & evaluation)
• visualize_attention.py / .ipynb (attention viz)
• configs/: JSON files describing data paths, vocab, model hyperparameters, training schedules.
• data/: raw LaTeX formula files (small and full datasets).
• model/:
• components/ (CNN encoders, attention modules, decoder cells, beam‑search)
• utils/ (data generator, image/text preprocessing, LR schedules)
• base(.torch).py (training/inference harness)
• results/: where checkpoints, logs, tensorboard data, visualization outputs live.
• External dependencies: LaTeX, Ghostscript, ImageMagick, TensorFlow1.12, Python3.5.
README
• Describes workflow (small vs. full dataset), steps to “make small/full” for data‑gen → train → eval.
• Explains architecture: Seq2Seq (CNN encoder + Attention + LSTM/GRU decoder + BeamSearch).
• Outlines evaluation metrics and visualization tools.
Main Components
a. Data Generation & Storage
• Raw LaTeX formulas (data/*.formulas)
• build.py + configs/data*.json → latex → PDF → gs+magick → PNG images + .matching.txt
• Vocabulary files (configs/vocab*.json)
b. Data Loading & Preprocessing
• model/utils/data_generator.py reads (image, token sequence) pairs
• image/text preprocessing utilities
c. Model Core
• Encoder: SimpleCNN / ResNet / DenseNet
• Attention Mechanism (custom op for visualization)
• Decoder: LSTM or GRU + Beam‐Search or Greedy
• model/base_torch.py orchestrates forward/backward pass
d. Training Engine
• train.py uses configs/training*.json, configs/model.json
• Checkpoint saving, dynamic LR schedule (lr_schedule.py)
• TensorBoard logging
e. Inference & Evaluation
• predict.py / evaluate_txt.py / evaluate_img.py
• Generates predicted LaTeX, computes perplexity, EditDistance, BLEU, ExactMatch
f. Visualization
• visualize_attention.py / .ipynb for attention heatmaps
• TensorBoard dashboard for training curves
Relationships and Data Flow
1. Raw LaTeX → build.py → Image Store + .matching.txt + Vocab
2. Images + .matching.txt + Vocab → Data Generator → Batches
3. Batches → Training Engine → Model Checkpoints + TensorBoard logs
4. Checkpoints → Inference Engine → Predicted Formulas
5. Predicted Formulas → Evaluation Module → Metrics Reports
6. Attention Weights (via custom op) → Visualization Tool → Heatmaps
7. TensorBoard logs → TensorBoard UI
Architectural Patterns & Tech Stack
• Layered architecture: Data layer, Model layer, Script layer
• Modular components (encoders, attention, decoders)
• Config‑driven pipeline (all paths and hyperparams in JSON)
• External tools invoked by scripts: latex, ghostscript, magick
• Framework: Python3.5 + TensorFlow1.12