Skip to content

Commit

Permalink
suport torch inference (#320)
Browse files Browse the repository at this point in the history
* feat: add rapidocr_torch inference

* ci: fix push workflow
  • Loading branch information
Joker1212 authored Feb 8, 2025
1 parent 86ae3f5 commit 42e4d9d
Show file tree
Hide file tree
Showing 47 changed files with 12,296 additions and 0 deletions.
105 changes: 105 additions & 0 deletions .github/workflows/gen_whl_to_pypi_rapidocr_torch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: Push rapidocr_torch to pypi

on:
push:
# branches: [ main ]
# paths:
# - 'python/rapidocr_torch/**'
# - 'python/setup_torch.py'
# - '.github/workflows/gen_whl_to_pypi_rapidocr_torch.yml'
# - 'python/requirements_torch.txt'
tags:
- torch_v*

env:
RESOURCES_URL: https://github.com/Joker1212/RapidOCR/releases/download/v0.0.0/torch_test.zip

jobs:
UnitTesting:
runs-on: ubuntu-latest
steps:
- name: Pull latest code
uses: actions/checkout@v4

- name: Set up Python 3.7
uses: actions/setup-python@v4
with:
python-version: '3.7'
architecture: 'x64'

- name: Display Python version
run: python -c "import sys; print(sys.version)"

- name: Unit testings
run: |
wget $RESOURCES_URL
ZIP_NAME=${RESOURCES_URL##*/}
DIR_NAME=${ZIP_NAME%.*}
unzip $DIR_NAME
cp $DIR_NAME/resources/models/*.pth python/rapidocr_torch/models/
cd python
pip install -r requirements_torch.txt
pip install pytest wheel get_pypi_latest_version
cd tests
pytest test_torch.py
GenerateWHL_PushPyPi:
needs: UnitTesting
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python 3.7
uses: actions/setup-python@v4
with:
python-version: '3.7'
architecture: 'x64'

# - name: Set SSH Environment
# env:
# DEPLOY_KEYS: ${{ secrets.GEN_PYTHON_SDK }}
# run: |
# mkdir -p ~/.ssh/
# echo "$DEPLOY_KEYS" > ~/.ssh/id_rsa
# chmod 600 ~/.ssh/id_rsa
# chmod 700 ~/.ssh && chmod 600 ~/.ssh/*

- name: Download models
run: |
cd python
wget $RESOURCES_URL
ZIP_NAME=${RESOURCES_URL##*/}
DIR_NAME=${ZIP_NAME%.*}
unzip $ZIP_NAME
cp $DIR_NAME/resources/models/*.pth rapidocr_torch/models/
- name: Run setup_torch.py
run: |
cd python
pip install setuptools get_pypi_latest_version wheel
mkdir rapidocr_torch_t
mv rapidocr_torch rapidocr_torch_t
mv rapidocr_torch_t rapidocr_torch
cd rapidocr_torch
echo "from .rapidocr_torch.main import RapidOCR, VisRes" > __init__.py
cd ..
python -m pip install --upgrade pip
python setup_torch.py bdist_wheel ${{ github.ref_name }}
mv dist ../
# - name: Publish distribution 📦 to PyPI
# uses: pypa/[email protected]
# with:
# password: ${{ secrets.PYPI_API_TOKEN }}
# packages_dir: dist/
- name: Publish distribution 📦 to Test PyPI
uses: pypa/[email protected]
with:
username: TEST_PYPI_API_TOKEN
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
packages_dir: dist/
5 changes: 5 additions & 0 deletions python/rapidocr_torch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
from .main import RapidOCR
from .utils import LoadImageError, VisRes
98 changes: 98 additions & 0 deletions python/rapidocr_torch/arch_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
ch_ptocr_mobile_v2.0_cls_infer:
model_type: cls
algorithm: CLS
Transform:
Backbone:
name: MobileNetV3
scale: 0.35
model_name: small
Neck:
Head:
name: ClsHead
class_dim: 2

ch_PP-OCRv4_det_infer:
model_type: det
algorithm: DB
Transform: null
Backbone:
name: PPLCNetV3
scale: 0.75
det: True
Neck:
name: RSEFPN
out_channels: 96
shortcut: True
Head:
name: DBHead
k: 50


ch_PP-OCRv4_det_server_infer:
model_type: det
algorithm: DB
Transform: null
Backbone:
name: PPHGNet_small
det: True
Neck:
name: LKPAN
out_channels: 256
intracl: true
Head:
name: PFHeadLocal
k: 50
mode: "large"


ch_PP-OCRv4_rec_infer:
model_type: rec
algorithm: SVTR_LCNet
Transform:
Backbone:
name: PPLCNetV3
scale: 0.95
Head:
name: MultiHead
out_channels_list:
CTCLabelDecode: 6625 #'blank' + ...(6623) + ' '
head_list:
- CTCHead:
Neck:
name: svtr
dims: 120
depth: 2
hidden_dims: 120
kernel_size: [ 1, 3 ]
use_guide: True
Head:
fc_decay: 0.00001
- NRTRHead:
nrtr_dim: 384
max_text_length: 25


ch_PP-OCRv4_rec_server_infer:
model_type: rec
algorithm: SVTR_HGNet
Transform:
Backbone:
name: PPHGNet_small
Head:
name: MultiHead
out_channels_list:
CTCLabelDecode: 6625 #'blank' + ...(6623) + ' '
head_list:
- CTCHead:
Neck:
name: svtr
dims: 120
depth: 2
hidden_dims: 120
kernel_size: [ 1, 3 ]
use_guide: True
Head:
fc_decay: 0.00001
- NRTRHead:
nrtr_dim: 384
max_text_length: 25
4 changes: 4 additions & 0 deletions python/rapidocr_torch/cal_rec_boxes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
from .main import CalRecBoxes
Loading

0 comments on commit 42e4d9d

Please sign in to comment.