基于Vision Transformer的Latex公式識別系統(tǒng)的設(shè)計與實現(xiàn)
近年來深度學(xué)習(xí),在圖像與自然語言處理領(lǐng)域取得顯著成效.而這其中像ResNet、Transformer等網(wǎng)絡(luò)發(fā)揮著巨大作用。本系列以https://github.com/lukas-blecher/LaTeX-OCR為例,闡述下如何基于人工智能技術(shù)實現(xiàn)latex公式識別服務(wù)。本系列主要分為3篇,分別從系統(tǒng)構(gòu)建(環(huán)境+訓(xùn)練)、系統(tǒng)原理(代碼層面)、系統(tǒng)的增強三個部分展開論述。
環(huán)境構(gòu)建
查看cuda版本
下面看到,cuda版本最高支持到12.1,我們下面選用的cu116。
gpu版本查看
創(chuàng)建conda環(huán)境
conda env create -f 下述文件。
name: latex3.9
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- conda-forge
dependencies:
- python=3.9
- pip
- pip:
- tqdm>=4.47.0
- munch>=2.5.0
- torch==1.13.1+cu116
- opencv_python_headless>=4.1.1.26
- requests>=2.22.0
- einops>=0.3.0
- x_transformers==0.15.0
- transformers>=4.18.0
- tokenizers>=0.13.0
- numpy>=1.19.5
- Pillow>=9.1.0
- PyYAML>=5.4.1
- pandas>=1.0.0
- timm==0.5.4
- albumentations>=0.5.2
- pyreadline3>=3.4.1
- python-Levenshtein>=0.12.2
- torchtext>=0.6.0
- imagesize>=1.2.0
- wandb>=0.15.5
- --extra-index-url https://download.pytorch.org/whl/cu116
檢查pytorch與gpu是否兼容
import torch
if torch.cuda.is_available():
cuda_version = torch.version.cuda
print(f"PyTorch CUDA版本為: {cuda_version}")
# 檢查CUDA版本是否與PyTorch兼容
if torch.backends.cudnn.version() is None:
print("PyTorch不支持當(dāng)前CUDA版本")
else:
print("PyTorch支持當(dāng)前CUDA版本")
else:
print("PyTorch不支持GPU加速")
數(shù)據(jù)準(zhǔn)備
準(zhǔn)備數(shù)據(jù)(包括訓(xùn)練集+驗證集+測試集)
現(xiàn)在訓(xùn)練測試數(shù)據(jù)集,然后放的位置很講究!
數(shù)據(jù)存放位置
- activate latex3.9 (這個在windows上執(zhí)行,linux上 conda activate latex3.9)
- python -m pix2tex.dataset.dataset --equations data/math.txt --images data/train --out data/train.pkl
- python -m pix2tex.dataset.dataset --equations data/math.txt --images data/val--out data/val.pkl
- python -m pix2tex.dataset.dataset --equations data/math.txt --images data/test--out data/test.pkl
訓(xùn)練調(diào)試
Pycharm上debug訓(xùn)練代碼
注:參數(shù)--config D:\LanJing\LaTeX-OCR-main\pix2tex\model\settings\debug.yaml --debug。
pycharm配置
debug.yaml配置修改
一輪epoch結(jié)束
加速訓(xùn)練
為了加速訓(xùn)練過程,可以使用提供的權(quán)重,并更新配置文件使用權(quán)重。該步驟也叫做fine tune。
下載 https://github.com/lukas-blecher/LaTeX-OCR/releases/download/v0.0.1/weights.pth
訪問權(quán)重路徑
訓(xùn)練測試數(shù)據(jù)
https://drive.google.com/drive/folders/13CA4vAmOmD_I_dSbvLp-Lf0s6KiaNfuO。
數(shù)據(jù)集解釋
接下來在第二篇中我會重點介紹下,代碼的實現(xiàn),如何構(gòu)建這樣一個訓(xùn)練網(wǎng)絡(luò),以及它的關(guān)鍵代碼,尤其是數(shù)據(jù)shape是如何變化的。