command.py 3.5 KB
Newer Older
Z
ZeyuChen 已提交
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
2
#
Z
ZeyuChen 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
6
#
Z
ZeyuChen 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
Z
ZeyuChen 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

J
jiangjiajun 已提交
15 16 17
from six import text_type as _text_type
import argparse
import sys
18
import paddlex.utils.logging as logging
J
jiangjiajun 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46


def arg_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--model_dir",
        "-m",
        type=_text_type,
        default=None,
        help="define model directory path")
    parser.add_argument(
        "--save_dir",
        "-s",
        type=_text_type,
        default=None,
        help="path to save inference model")
    parser.add_argument(
        "--version",
        "-v",
        action="store_true",
        default=False,
        help="get version of PaddleX")
    parser.add_argument(
        "--export_inference",
        "-e",
        action="store_true",
        default=False,
        help="export inference model for C++/Python deployment")
47 48 49 50 51 52
    parser.add_argument(
        "--export_onnx",
        "-eo",
        action="store_true",
        default=False,
        help="export onnx model for deployment")
C
Channingss 已提交
53 54 55 56
    parser.add_argument(
        "--fixed_input_shape",
        "-fs",
        default=None,
57
        help="export inference model with fixed input shape:[w,h]")
J
jiangjiajun 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    return parser


def main():
    import os
    os.environ['CUDA_VISIBLE_DEVICES'] = ""

    import paddlex as pdx

    if len(sys.argv) < 2:
        print("Use command 'paddlex -h` to print the help information\n")
        return
    parser = arg_parser()
    args = parser.parse_args()

    if args.version:
        print("PaddleX-{}".format(pdx.__version__))
        print("Repo: https://github.com/PaddlePaddle/PaddleX.git")
        print("Email: paddlex@baidu.com")
        return
C
Channingss 已提交
78

J
jiangjiajun 已提交
79 80 81
    if args.export_inference:
        assert args.model_dir is not None, "--model_dir should be defined while exporting inference model"
        assert args.save_dir is not None, "--save_dir should be defined to save inference model"
C
Channingss 已提交
82 83 84 85 86

        fixed_input_shape = None
        if args.fixed_input_shape is not None:
            fixed_input_shape = eval(args.fixed_input_shape)
            assert len(
C
Channingss 已提交
87 88
                fixed_input_shape
            ) == 2, "len of fixed input shape must == 2, such as [224,224]"
S
sunyanfang01 已提交
89 90
        else:
            fixed_input_shape = None
C
Channingss 已提交
91 92

        model = pdx.load_model(args.model_dir, fixed_input_shape)
93
        model.export_inference_model(args.save_dir)
J
jiangjiajun 已提交
94

C
Channingss 已提交
95 96
    if args.export_onnx:
        assert args.model_dir is not None, "--model_dir should be defined while exporting onnx model"
C
Channingss 已提交
97
        assert args.save_dir is not None, "--save_dir should be defined to create onnx model"
C
Channingss 已提交
98

99 100 101 102 103 104 105 106
        model = pdx.load_model(args.model_dir)
        if model.status == "Normal" or model.status == "Prune":
            logging.error(
                "Only support inference model, try to export model first as below,",
                exit=False)
            logging.error(
                "paddlex --export_inference --model_dir model_path --save_dir infer_model"
            )
C
Channingss 已提交
107
        pdx.convertor.export_onnx_model(model, args.save_dir)
C
Channingss 已提交
108

J
jiangjiajun 已提交
109 110 111

if __name__ == "__main__":
    main()