Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor inference #267

Merged
merged 1 commit into from
May 15, 2023
Merged

refactor inference #267

merged 1 commit into from
May 15, 2023

Conversation

liangxhao
Copy link
Contributor

@liangxhao liangxhao commented May 9, 2023

Thank you for your contribution to the MindOCR repo.
Before submitting this PR, please make sure:

Motivation

  1. 重构推理模块,将预处理、后处理、模型推理、分档、并行、串行等模块独立开来
  2. 支持yaml配置文件设置预处理、后处理参数,通过接口参数设置:
    det_model_name/det_config_path、 cls_model_name/cls_config_path、 rec_model_name/rec_config_path
    其中model_name会关联一个config_path,用户设置其一即可自动获取yaml中的参数
  3. 增加单独推理分类模型的能力
  4. 增加MindSpore Lite 1.9的后端支持

Test Plan

(How should this PR be tested? Do you require special setup to run the test or repro the fixed bug?)

Related Issues and PRs

(Is this PR part of a group of changes? Link the other relevant PRs and Issues here. Use https://help.github.com/en/articles/closing-issues-using-keywords for help on GitHub syntax)

"DecodeImage": transforms.DecodeImage,
"NormalizeImage": transforms.NormalizeImage,
"ToCHWImage": transforms.ToCHWImage,
"GridResize": transforms.ResizeImage,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个ResizeImage操作是保留长宽比的吗? 有设置keep_ratio选项,这对最终的文字检测识别预测结果影响挺大。建议默认是保留H W比,即对应mindocr/data/transforms中的ScalePadImage操作。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外, pp推理大部分情况默认也是 keep_ratio

Copy link
Contributor Author

@liangxhao liangxhao May 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

  1. 新增了ScalePadImage操作,和训练端对应起来,方便以后统一和同步,默认使用ScalePadImage
  2. 原始的ResizeImage新增keep_ratio参数,True等价于ScalePadImage,为False的时候就是不保留H/W比的Resize

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因训练模型没有使用ScalePadImage,所以这里GridResize映射为ScalePadImage导致精度略微降低
故GridResize先映射为ResizeImage(keep_ratio=False),后续等训练模型支持后再修改

- ResizeImage:
- NormalizeImage:
mean: imagenet
std: imagenet
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个建议显示地把mean,std的值写出来,因为paddle有些yaml里std没用imagenet的std,而且默认imagnet的mean, std是RGB顺序,而这里的img mode是BGR (而paddle忽略了这一点,训练时将错就错了)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: 后续考虑如何及时与mindocr目录的代码同步。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后处理代码:后续在mindocr目录的后处理代码中,补齐一些(例如映射回原始shape)操作后,应该就可以共用了
预处理代码:这部分比较稍麻烦,比如Resize的image_shape,在训练端是从__init__传入,但是推理端需要从__call__传入(因为每次Resize的shape可能不一样)

from ....infer import TextClassifier, TaskType


class CLSPreNode(ModuleBase):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLSPreNode 命名方式跟 别的地方ClsPostprocess 不一致,建议改成ClsPreNode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

Copy link
Collaborator

@HaoyangLee HaoyangLee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

py_infer/src/下有infer_args.pyargs/,是否将py_infer/src/args/改为py_infer/src/configs/更为清晰

@liangxhao
Copy link
Contributor Author

liangxhao commented May 11, 2023

py_infer/src/下有infer_args.pyargs/,是否将py_infer/src/args/改为py_infer/src/configs/更为清晰

已修改

Copy link
Collaborator

@SamitHuang SamitHuang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better!
TODO: improve resize for test images with different shape

@liangxhao liangxhao merged commit 593bf1b into mindspore-lab:main May 15, 2023
@liangxhao liangxhao deleted the py_infer_refactor branch May 15, 2023 06:07
@liangxhao liangxhao linked an issue May 30, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[630][pipeline] lite推理C++/Python支持,性能达标
3 participants