Giter Club home page Giter Club logo

paddleocrmodelconvert's Introduction

🔄 PaddleOCR Model Convert

 
PyPI SemVer2.0

Introduction

  • This repository is mainly to convert Inference Model in PaddleOCR into ONNX format.
  • Input: url or local tar path of inference model
  • Output: converted ONNX model
  • If it is a recognition model, you need to provide the original txt path of the corresponding dictionary (Open the txt file in github, click the path after raw in the upper right corner, similar to this), used to write the dictionary into the ONNX model
  • ☆ It needs to be used with the relevant reasoning code in RapidOCR
  • If you encounter a model that cannot be successfully converted, you can check which steps are wrong one by one according to the ideas in the figure below.

Overall framework

flowchart TD

A([PaddleOCR inference model]) --paddle2onnx--> B([ONNX])
B --> C([Change Dynamic Input]) --> D([Rec: save the character dict to onnx])
D --> E([Save])

Installation

pip install paddleocr_convert

Usage

Warning

Only support the reasoning model in the download address in link, if it is a training model, Manual conversion to inference format is required.

The slim quantized model in PaddleOCR does not support conversion.

Using the command line

  • Usage:
    $ paddleocr_convert -h
    usage: paddleocr_convert [-h] [-p MODEL_PATH] [-o SAVE_DIR]
                            [-txt_path TXT_PATH]
    
    optional arguments:
    -h, --help show this help message and exit
    -p MODEL_PATH, --model_path MODEL_PATH
                            The inference model url or local path of paddleocr.
                            e.g. https://paddleocr.bj.bcebos.com/PP-
                            OCRv3/chinese/ch_PP-OCRv3_det_infer.tar or
                            models/ch_PP-OCRv3_det_infer.tar
    -o SAVE_DIR, --save_dir SAVE_DIR
                            The directory of saving the model.
    -txt_path TXT_PATH, --txt_path TXT_PATH
                            The raw txt url or local txt path, if the model is
                            recognition model.
  • Example:
    #online
    $ paddleocr_convert -p https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar \
                        -o models
    
    $ paddleocr_convert -p https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar\
                        -o models\
                        -txt_path https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/ppocr/utils/ppocr_keys_v1.txt
    
    # offline
    $ paddleocr_convert -p models/ch_PP-OCRv3_det_infer.tar\
                        -o models
    
    $ paddleocr_convert -p models/ch_PP-OCRv3_rec_infer.tar\
                        -o models\
                        -txt_path models/ppocr_keys_v1.txt

Script use

  • online mode
    from paddleocr_convert import PaddleOCRModelConvert
    
    converter = PaddleOCRModelConvert()
    save_dir = 'models'
    url = 'https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar'
    txt_url = 'https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/ppocr/utils/ppocr_keys_v1.txt'
    
    converter(url, save_dir, txt_path=txt_url)
  • offline mode
    from paddleocr_convert import PaddleOCRModelConvert
    
    converter = PaddleOCRModelConvert()
    save_dir = 'models'
    model_path = 'models/ch_PP-OCRv3_rec_infer.tar'
    txt_path = 'models/ppocr_keys_v1.txt'
    converter(model_path, save_dir, txt_path=txt_path)

Use the model

Assuming that the model needs to be recognized in Japanese, and it has been converted, the path is local/models/japan.onnx

  1. Install rapidocr_onnxruntime library
    pip install rapidocr_onnxruntime
  2. Script use
    from rapidocr_onnxruntime import RapidOCR
    
    model_path = 'local/models/japan.onnx'
    engine = RapidOCR(rec_model_path=model_path)
    
    img = '1.jpg'
    result, elapse = engine(img)
  3. CLI use
    rapidocr_onnxruntime -img 1.jpg --rec_model_path local/models/japan.onnx

Changelog

Click to expand
  • 2023-09-22 v0.0.17 update:
    • Improve the log when meets the error.
  • 2023-07-27 v0.0.16 update:
    • Added the online conversion version of ModelScope.
    • Change python version from python 3.6 ~ 3.11.
  • 2023-04-13 update:
    • Add online conversion program link
  • 2023-03-05 v0.0.4~7 update:
    • Support transliteration of local models and dictionaries
    • Optimize internal logic and error feedback
  • 2023-02-28 v0.0.3 update:
    • Added setting to automatically change to dynamic input for models that are not dynamic input
  • 2023-02-27 v0.0.2 update:
    • Encapsulate the conversion model code into a package, which is convenient for self-help model conversion
  • 2022-08-15 v0.0.1 update:
    • Write the dictionary of the recognition model into the meta in the onnx model for subsequent distribution.

paddleocrmodelconvert's People

Contributors

swhl avatar znsoftm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

paddleocrmodelconvert's Issues

装环境报错

pip install -r requirements.txt -i https://pypi.douban.com/simple/
Looking in indexes: https://pypi.douban.com/simple/, https://pypi.ngc.nvidia.com
Collecting scipy==1.3.1
Downloading https://pypi.doubanio.com/packages/ee/5b/5afcd1c46f97b3c2ac3489dbc95d6ca28eacf8e3634e51f495da68d97f0f/scipy-1.3.1.tar.gz (23.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 23.6/23.6 MB 38.9 MB/s eta 0:00:00
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... error
error: subprocess-exited-with-error

× Preparing metadata (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [23 lines of output]
:386: UserWarning: Unrecognized setuptools command ('dist_info --output-dir /tmp/pip-modern-metadata-un2r8ngg --keep-egg-info'), proceeding with generating Cython sources and expanding templates
Traceback (most recent call last):
File "/home/lgy/anaconda3/envs/ocr-onnx/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in
main()
File "/home/lgy/anaconda3/envs/ocr-onnx/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/home/lgy/anaconda3/envs/ocr-onnx/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 164, in prepare_metadata_for_build_wheel
return hook(metadata_directory, config_settings)
File "/tmp/pip-build-env-kmah4yai/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 377, in prepare_metadata_for_build_wheel
self.run_setup()
File "/tmp/pip-build-env-kmah4yai/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 483, in run_setup
super(_BuildMetaLegacyBackend,
File "/tmp/pip-build-env-kmah4yai/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 335, in run_setup
exec(code, locals())
File "", line 505, in
File "", line 481, in setup_package
File "/tmp/pip-build-env-kmah4yai/overlay/lib/python3.8/site-packages/numpy/distutils/init.py", line 8, in
from . import ccompiler
File "/tmp/pip-build-env-kmah4yai/overlay/lib/python3.8/site-packages/numpy/distutils/ccompiler.py", line 17, in
from numpy.distutils import log
File "/tmp/pip-build-env-kmah4yai/overlay/lib/python3.8/site-packages/numpy/distutils/log.py", line 6, in
from distutils.log import Log as old_Log
ImportError: cannot import name 'Log' from 'distutils.log' (/tmp/pip-build-env-kmah4yai/overlay/lib/python3.8/site-packages/setuptools/_distutils/log.py)
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

你好,装环境报错,不知道为什么?

How to use recent text detectors with RapidOCR?

If I want to use one of the text detectors in this link instead of DB algorithm (default). I use paddleocr_convert but it throws an error. This is an example to convert SAST algorithm to onnex:

from paddleocr_convert import PaddleOCRModelConvert

converter = PaddleOCRModelConvert()
save_dir = 'models'
url = 'https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/det_r50_vd_sast_icdar15_v2.0_train.tar'
converter(url, save_dir)

The error log:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\paddleocr_convert\main.py:49, in PaddleOCRModelConvert.__call__(self, model_path, save_dir, txt_path, is_del_raw)
     48 try:
---> 49     self.change_to_dynamic(save_onnx_path)
     50 except Exception as e:

File ~\anaconda3\lib\site-packages\paddleocr_convert\main.py:113, in PaddleOCRModelConvert.change_to_dynamic(self, onnx_path)
    112 onnx_model = onnx.load_model(onnx_path)
--> 113 dim_shapes = onnx_model.graph.input[0].type.tensor_type.shape.ListFields()[0][1]
    115 dynamic_name = 'DynamicDimension'

IndexError: list index out of range

The above exception was the direct cause of the following exception:

ConvertError                              Traceback (most recent call last)
Cell In[24], line 7
      5 # online
      6 url = 'https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/det_r50_vd_sast_icdar15_v2.0_train.tar'
----> 7 converter(url, save_dir)

File ~\anaconda3\lib\site-packages\paddleocr_convert\main.py:51, in PaddleOCRModelConvert.__call__(self, model_path, save_dir, txt_path, is_del_raw)
     49     self.change_to_dynamic(save_onnx_path)
     50 except Exception as e:
---> 51     raise ConvertError(
     52         'change the model to dynamic meets error.') from e
     54 if is_rec:
     55     txt_path = self.get_file_path(txt_path, '.')

ConvertError: change the model to dynamic meets error.

Could you please tell me how I can use different text detection algorithms instead of the specified ones in this page here.

rec_inference_to_onnx.sh转换模型成功,测试结果也成功,但无法被RapidOCR加载成功,请问怎么解决这个问题

paddle2onnx --model_dir pretrained_models\ch_PP-OCRv3_rec_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ch_PP-OCRv3_rec_infer.onnx --opset_version 12

python save_dict_meta.py --onnx_path ch_PP-OCRv3_rec_infer.onnx --key_path ppocr\utils\ppocr_keys_v1.txt

python change_dynamic.py --onnx_path ch_PP-OCRv3_rec_infer.onnx --type_model "rec"

python tools/infer/predict_rec.py --image_dir=321.jpg --rec_model_dir=pretrained_models\ch_PP-OCRv3_rec_infer --onnx_path ch_PP-OCRv3_rec_infer.onnx --rec_char_dict_path ppocr\utils\ppocr_keys_v1.txt --use_gpu False

图1:上面的模型为我转换的模型结果,下面的是RapidAI仓库下载的模型

第一个
S1
S2
S3

本地模型转换报错(no attribute 'txt_url')

升级到最新的0.0.9版本,按照命令参数转换模型直接报以下错,是我的本地环境出问题了么?
paddleocr_convert (0.0.9)
Available versions: 0.0.9, 0.0.8, 0.0.7, 0.0.6, 0.0.5, 0.0.4, 0.0.3, 0.0.2, 0.0.1
INSTALLED: 0.0.9
LATEST: 0.0.9

paddleocr_convert -p models/ch_PP-OCRv3_det_infer.tar -o models
Traceback (most recent call last):
File "D:\anaconda3\envs\paddlepaddle241_gpu\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\anaconda3\envs\paddlepaddle241_gpu\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "D:\anaconda3\envs\paddlepaddle241_gpu\Scripts\paddleocr_convert.exe_main
.py", line 7, in
File "D:\anaconda3\envs\paddlepaddle241_gpu\lib\site-packages\paddleocr_convert\main.py", line 172, in main
converter(args.model_path, args.save_dir, args.txt_url)
AttributeError: 'Namespace' object has no attribute 'txt_url'

Alphabet for English rec model is not matching OCR on image

Thank you so much for providing code to convert paddle models to onnx format and be able to use these converted models with rapid OCR.

I used paddleocr_convert to convert the latest english models as follow:

paddleocr_convert -p  https://paddleocr.bj.bcebos.com/PP-OCRv4/english/en_PP-OCRv4_rec_infer.tar -o paddle_models  -txt_path https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.7/ppocr/utils/en_dict.txt

paddleocr_convert -p  https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar  -o paddle_models

For testing these models, I use the following image
c8fa66439d63041e

I use rapid ocr to get OCR:

from rapidocr_onnxruntime import RapidOCR

engine = RapidOCR(rec_model_path="paddle_models/en_PP-OCRv4_rec_infer/en_PP-OCRv4_rec_infer.onnx", 
det_model_path="paddle_models/en_PP-OCRv3_det_infer/en_PP-OCRv3_det_infer.onnx")

img = "c8fa66439d63041e.jpg"
result, elapse = engine(img)

and I get the following result (I only included the first few predictions):

[[[[551.0, 72.0], [952.0, 83.0], [949.0, 177.0], [548.0, 166.0]],
  'DlizAly',
  0.8711231350898743],
 [[[540.0, 211.0], [978.0, 221.0], [978.0, 250.0], [540.0, 241.0]],
  'ulD ADluAplAo jluABy',
  0.9064718882242838],
 [[[65.0, 280.0], [207.0, 277.0], [207.0, 293.0], [65.0, 297.0]],
  "B!QR%%'R}N}   T",
  0.6442324388772249],
 [[[220.0, 286.0], [270.0, 283.0], [270.0, 297.0], [220.0, 300.0]],
  'B!QV&P"%R',
  0.7667727172374725],
...

Whereas the default models produce more matching results

from rapidocr_onnxruntime import RapidOCR

engine = RapidOCR()

img = "c8fa66439d63041e.jpg"
result, elapse = engine(img)

result -->

[[[[545.0, 72.0], [954.0, 84.0], [951.0, 179.0], [542.0, 167.0]],
  'WEBSTER',
  0.8526573106646538],
 [[[539.0, 210.0], [975.0, 218.0], [975.0, 250.0], [539.0, 243.0]],
  'NEW TWENTIETH CENTUR',
  0.9136510831969125],
 [[[63.0, 280.0], [209.0, 277.0], [209.0, 295.0], [63.0, 299.0]],
  'Underrte, r.aa ferg',
  0.6786298751831055],
 [[[220.0, 285.0], [271.0, 282.0], [271.0, 298.0], [220.0, 301.0]],
  'Undlacove',
  0.7574679881334305],

This suggests I am perhaps using the wrong dictionary. However, the only dict I could find in the paddle git repo is https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.7/ppocr/utils/EN_symbol_dict.txt.
Do you happen to know if there is some step I am doing wrong here?

thank you so much,

Chris

转换ch_PP-OCRv3_rec_slim量化版超轻量模型报错

你好,请问当前工具是否支持转换slim量化版超轻量模型?
我在使用最新版本0.0.14的paddleocr_convert,转换ch_PP-OCRv3_rec_slim量化版超轻量模型报错如下:

paddleocr_convert -p models/ch_PP-OCRv3_rec_slim_infer.tar -o models -txt_path models/ppocr_keys_v1.txt

[Paddle2ONNX] Start to parse PaddlePaddle model...
[Paddle2ONNX] Model file path: models\ch_PP-OCRv3_rec_slim_infer\inference.pdmodel
[Paddle2ONNX] Paramters file path: models\ch_PP-OCRv3_rec_slim_infer\inference.pdiparams
[Paddle2ONNX] Start to parsing Paddle model...
[Paddle2ONNX] [Info] The Paddle model is a quantized model.
[Paddle2ONNX] Oops, there are some operators not supported yet, including fake_channel_wise_quantize_dequantize_abs_max,fake_quantize_dequantize_moving_average_abs_max,
[ERROR] Due to the unsupported operators, the conversion is aborted.
Traceback (most recent call last):
File "D:\anaconda3\envs\paddlepaddle241_gpu\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\anaconda3\envs\paddlepaddle241_gpu\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "D:\anaconda3\envs\paddlepaddle241_gpu\Scripts\paddleocr_convert.exe_main
.py", line 7, in
File "D:\anaconda3\envs\paddlepaddle241_gpu\lib\site-packages\paddleocr_convert\main.py", line 164, in main
converter(args.model_path, args.save_dir, args.txt_path)
File "D:\anaconda3\envs\paddlepaddle241_gpu\lib\site-packages\paddleocr_convert\main.py", line 38, in call
raise ConvertError('paddle2onnx convert failed!')
paddleocr_convert.main.ConvertError: paddle2onnx convert failed!

Cannot install with Python 3.12

Note

请提供下述完整信息以便快速定位问题
Please provide the following information to quickly locate the problem

系统环境 (System Environment) :
Linux

使用的是哪门语言的程序 (Which programing language) :
Python

所使用语言相关版本信息 (Version) :
3.12

使用当前库的版本 (Use current version) :
0.0.17

可复现问题的demo和文件 (Demo of reproducible problems) :
N/A

完整报错 (Complete Error Message) :
Cannot install this package with Python 3.12 running in the system.

可能的解决方案 (Possible solutions) :
Update setup.py to remove <3.12 restriction.

转换cls模型报错!

代码:
from paddleocr_convert import PaddleOCRModelConvert

converter = PaddleOCRModelConvert()
save_dir = 'models'
url = 'https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_slim_infer.tar'
txt_url = 'https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/ppocr/utils/ppocr_keys_v1.txt'

converter(url, save_dir, txt_path=txt_url)

报错:
Successfully convert model to models/ch_ppocr_mobile_v2.0_cls_slim_infer/ch_ppocr_mobile_v2.0_cls_slim_infer.onnx
Traceback (most recent call last):
File "/root/miniconda3/envs/rapidocr/lib/python3.11/site-packages/paddleocr_convert/main.py", line 42, in call
self.change_to_dynamic(save_onnx_path)
File "/root/miniconda3/envs/rapidocr/lib/python3.11/site-packages/paddleocr_convert/main.py", line 108, in change_to_dynamic
onnx_model = onnx.load_model(onnx_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/miniconda3/envs/rapidocr/lib/python3.11/site-packages/onnx/init.py", line 169, in load_model
s = _load_bytes(f)
^^^^^^^^^^^^^^
File "/root/miniconda3/envs/rapidocr/lib/python3.11/site-packages/onnx/init.py", line 73, in _load_bytes
with open(typing.cast(str, f), "rb") as readable:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'models/ch_ppocr_mobile_v2.0_cls_slim_infer/ch_ppocr_mobile_v2.0_cls_slim_infer.onnx'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/mnt/workspace/RapidOCR/python/ppmodel2onnx.py", line 8, in
converter(url, save_dir, txt_path=txt_url)
File "/root/miniconda3/envs/rapidocr/lib/python3.11/site-packages/paddleocr_convert/main.py", line 44, in call
raise ConvertError(
paddleocr_convert.main.ConvertError: change the model to dynamic meets error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.