-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add mindir export for trained models and related docs, tests; Rename dbnet_r50 -> dbnet_resnet50, crnn_r34 -> crnn_resnet34 for consistency #184
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e9f5ea7
add export
SamitHuang 7ea99fa
add export
SamitHuang fe3f514
add test
SamitHuang dee87e1
update export tool and add docs for it
SamitHuang f196609
update configs readme
SamitHuang 648e4e4
Merge branch 'mindspore-lab:main' into export
SamitHuang d2b847e
add test for mindir export
SamitHuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
## About configs | ||
|
||
This folder contains the configurations including | ||
- model definition | ||
- training recipes | ||
- pretrained weights | ||
- reported performance | ||
for all models trained with MindOCR. | ||
|
||
## Model Export | ||
|
||
To convert a pretrained model from mindspore checkpoint format to [MindIR](https://www.mindspore.cn/docs/zh-CN/r2.0.0-alpha/design/mindir.html) format for deployment, please use the `tools/export.py` script. | ||
|
||
``` shell | ||
# convert dbnet_resnet50 with pretrained weights to MindIR format | ||
python tools/export.py --model_name dbnet_resnet50 --pretrained | ||
|
||
# convert dbnet_resnet50 loaded with weights to MindIR format | ||
python tools/export.py --model_name dbnet_resnet50 --ckpt_load_path /path/to/checkpoint | ||
``` | ||
|
||
For more usage, run `python tools/export.py -h`. | ||
|
||
I |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import mindspore as ms | ||
import numpy as np | ||
from mindocr import list_models, build_model | ||
|
||
|
||
def test_mindir_infer(name, task='rec'): | ||
fn = f"{name}.mindir" | ||
|
||
ms.set_context(mode=ms.GRAPH_MODE) | ||
graph = ms.load(fn) | ||
model = ms.nn.GraphCell(graph) | ||
|
||
task = 'rec' | ||
if 'db' in fn: | ||
task = 'det' | ||
|
||
if task=='rec': | ||
c, h, w = 3, 32, 100 | ||
else: | ||
c, h, w = 3, 640, 640 | ||
|
||
bs = 1 | ||
x = ms.Tensor(np.ones([bs, c, h, w]), dtype=ms.float32) | ||
|
||
outputs_mindir = model(x) | ||
|
||
# get original ckpt outputs | ||
net = build_model(name, pretrained=True) | ||
outputs_ckpt = net(x) | ||
|
||
for i, o in enumerate(outputs_mindir): | ||
print('mindir net out: ', outputs_mindir[i].sum(), outputs_mindir[i].shape) | ||
print('ckpt net out: ', outputs_ckpt[i].sum(), outputs_mindir[i].shape) | ||
assert outputs_mindir[i].sum()==outputs_ckpt[i].sum() | ||
|
||
|
||
if __name__ == '__main__': | ||
names = list_models() | ||
for n in names: | ||
task = 'rec' | ||
if 'db' in n: | ||
task = 'det' | ||
print(n) | ||
test_mindir_infer(n, task) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import sys | ||
sys.path.append('.') | ||
import mindspore as ms | ||
import pytest | ||
import numpy as np | ||
from mindocr import list_models, build_model | ||
from tools.export import export | ||
|
||
@pytest.mark.parametrize('name', ['dbnet_resnet50', 'crnn_resnet34']) | ||
def test_mindir_infer(name): | ||
task = 'rec' | ||
if 'db' in name: | ||
task = 'det' | ||
|
||
export(name, task, pretrained=True) | ||
|
||
fn = f"{name}.mindir" | ||
|
||
ms.set_context(mode=ms.GRAPH_MODE) | ||
graph = ms.load(fn) | ||
model = ms.nn.GraphCell(graph) | ||
|
||
if task=='rec': | ||
c, h, w = 3, 32, 100 | ||
else: | ||
c, h, w = 3, 640, 640 | ||
|
||
bs = 1 | ||
x = ms.Tensor(np.ones([bs, c, h, w]), dtype=ms.float32) | ||
|
||
outputs_mindir = model(x) | ||
|
||
# get original ckpt outputs | ||
net = build_model(name, pretrained=True) | ||
outputs_ckpt = net(x) | ||
|
||
for i, o in enumerate(outputs_mindir): | ||
print('mindir net out: ', outputs_mindir[i].sum(), outputs_mindir[i].shape) | ||
print('ckpt net out: ', outputs_ckpt[i].sum(), outputs_mindir[i].shape) | ||
assert outputs_mindir[i].sum()==outputs_ckpt[i].sum() | ||
|
||
|
||
if __name__ == '__main__': | ||
names = list_models() | ||
test_mindir_infer(names[0]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
''' | ||
Usage: | ||
To export all trained models from ckpt to mindir as listed in configs/, run | ||
$ python tools/export.py | ||
|
||
To export a sepecific model, taking dbnet for example, run | ||
$ python tools/export.py --model_name dbnet_resnet50 --save_dir | ||
''' | ||
import sys | ||
import os | ||
__dir__ = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, ".."))) | ||
|
||
import argparse | ||
import mindspore as ms | ||
from mindocr import list_models, build_model | ||
import numpy as np | ||
|
||
|
||
def export(name, task='rec', pretrained=True, ckpt_load_path="", save_dir=""): | ||
ms.set_context(mode=ms.GRAPH_MODE) #, device_target='Ascend') | ||
|
||
net = build_model(name, pretrained=True) | ||
net.set_train(False) | ||
|
||
# TODO: extend input shapes for more models | ||
if task=='rec': | ||
c, h, w = 3, 32, 100 | ||
else: | ||
c, h, w = 3, 640, 640 | ||
|
||
bs = 1 | ||
x = ms.Tensor(np.ones([bs, c, h, w]), dtype=ms.float32) | ||
|
||
output_path = os.path.join(save_dir, name) + '.mindir' | ||
ms.export(net, x, file_name=output_path, file_format='MINDIR') | ||
|
||
print(f'=> Finish exporting {name} to {output_path}') | ||
|
||
|
||
def str2bool(v): | ||
if isinstance(v, bool): | ||
return v | ||
if v.lower() in ("yes", "true", "1"): | ||
return True | ||
elif v.lower() in ("no", "false", "0"): | ||
return False | ||
else: | ||
raise argparse.ArgumentTypeError("Boolean value expected.") | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser("Convert model checkpoint to mindir format.") | ||
parser.add_argument( | ||
'--model_name', | ||
type=str, | ||
default="", | ||
help='Name of the model to convert, choices: [crnn_resnet34, crnn_vgg7, dbnet_resnet50, ""]. You can lookup the name by calling mindocr.list_models(). If "", all models in list_models() will be converted.') | ||
parser.add_argument( | ||
'--pretrained', | ||
type=str2bool, nargs='?', const=True, | ||
default=True, | ||
help='Whether download and load the pretrained checkpoint into network.') | ||
parser.add_argument( | ||
'--ckpt_load_path', | ||
type=str, | ||
default="", | ||
help='Path to a local checkpoint. No need to set it if pretrained is True. If set, network weights will be loaded using this checkpoint file') | ||
parser.add_argument( | ||
'--save_dir', | ||
type=str, | ||
default="", | ||
help='Dir to save the exported model') | ||
|
||
args = parser.parse_args() | ||
mn = args.model_name | ||
|
||
if mn =="": | ||
names = list_models() | ||
else: | ||
names = [mn] | ||
|
||
for n in names: | ||
task = 'rec' | ||
if 'db' in n: | ||
task = 'det' | ||
export(n, task, args.pretrained, args.ckpt_load_path, args.save_dir) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo?
outputs_mindir -> outputs_ckpt