command.py 7.6 KB
Newer Older
M
mamingjie-China 已提交
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
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
M
mamingjie-China 已提交
18
import os.path as osp
19
import paddlex.utils.logging as logging
J
jiangjiajun 已提交
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 47


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")
48 49 50 51 52 53
    parser.add_argument(
        "--export_onnx",
        "-eo",
        action="store_true",
        default=False,
        help="export onnx model for deployment")
C
Channingss 已提交
54 55 56 57 58 59
    parser.add_argument(
        "--onnx_opset",
        "-oo",
        type=int,
        default=10,
        help="when use paddle2onnx, set onnx opset version to export")
S
SunAhong1993 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    parser.add_argument(
        "--data_conversion",
        "-dc",
        action="store_true",
        default=False,
        help="convert the dataset to the standard format")
    parser.add_argument(
        "--source",
        "-se",
        type=_text_type,
        default=None,
        help="define dataset format before the conversion")
    parser.add_argument(
        "--to",
        "-to",
        type=_text_type,
        default=None,
        help="define dataset format after the conversion")
    parser.add_argument(
        "--pics",
        "-p",
        type=_text_type,
        default=None,
        help="define pictures directory path")
    parser.add_argument(
        "--annotations",
        "-a",
        type=_text_type,
        default=None,
        help="define annotations directory path")
C
Channingss 已提交
90 91 92 93
    parser.add_argument(
        "--fixed_input_shape",
        "-fs",
        default=None,
94
        help="export inference model with fixed input shape:[w,h]")
M
mamingjie-China 已提交
95 96 97 98 99 100 101
    parser.add_argument(
        "--split_dataset",
        "-sd",
        action="store_true",
        default=False,
        help="split dataset with the split value")
    parser.add_argument(
M
update  
mamingjie-China 已提交
102
        "--format",
M
mamingjie-China 已提交
103 104 105
        "-f",
        default=None,
        help="define dataset format(ImageNet/COCO/VOC/Seg)")
M
mamingjie-China 已提交
106 107 108 109 110 111 112 113 114 115
    parser.add_argument(
        "--dataset_dir",
        "-dd",
        type=_text_type,
        default=None,
        help="define the path of dataset to be splited")
    parser.add_argument(
        "--val_value",
        "-vv",
        default=None,
M
mamingjie-China 已提交
116
        help="define the value of validation dataset(E.g 0.2)")
M
mamingjie-China 已提交
117 118 119 120
    parser.add_argument(
        "--test_value",
        "-tv",
        default=None,
M
mamingjie-China 已提交
121
        help="define the value of test dataset(E.g 0.1)")
J
jiangjiajun 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    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 已提交
142

J
jiangjiajun 已提交
143 144 145
    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 已提交
146 147 148 149 150

        fixed_input_shape = None
        if args.fixed_input_shape is not None:
            fixed_input_shape = eval(args.fixed_input_shape)
            assert len(
C
Channingss 已提交
151 152
                fixed_input_shape
            ) == 2, "len of fixed input shape must == 2, such as [224,224]"
S
sunyanfang01 已提交
153 154
        else:
            fixed_input_shape = None
C
Channingss 已提交
155 156

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

C
Channingss 已提交
159 160
    if args.export_onnx:
        assert args.model_dir is not None, "--model_dir should be defined while exporting onnx model"
C
Channingss 已提交
161
        assert args.save_dir is not None, "--save_dir should be defined to create onnx model"
C
Channingss 已提交
162

163 164 165 166 167 168 169 170
        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"
            )
171
        pdx.converter.export_onnx_model(model, args.save_dir, args.onnx_opset)
C
Channingss 已提交
172

S
SunAhong1993 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186
    if args.data_conversion:
        assert args.source is not None, "--source should be defined while converting dataset"
        assert args.to is not None, "--to should be defined to confirm the taregt dataset format"
        assert args.pics is not None, "--pics should be defined to confirm the pictures path"
        assert args.annotations is not None, "--annotations should be defined to confirm the annotations path"
        assert args.save_dir is not None, "--save_dir should be defined to store taregt dataset"
        if args.source == 'labelme' and args.to == 'ImageNet':
            logging.error(
                "The labelme dataset can not convert to the ImageNet dataset.",
                exit=False)
        if args.source == 'jingling' and args.to == 'PascalVOC':
            logging.error(
                "The jingling dataset can not convert to the PascalVOC dataset.",
                exit=False)
C
Channingss 已提交
187 188
        pdx.tools.convert.dataset_conversion(args.source, args.to, args.pics,
                                             args.annotations, args.save_dir)
C
Channingss 已提交
189

M
mamingjie-China 已提交
190 191
    if args.split_dataset:
        assert args.dataset_dir is not None, "--dataset_dir should be defined while spliting dataset"
J
jiangjiajun 已提交
192
        assert args.format is not None, "--format should be defined while spliting dataset"
M
mamingjie-China 已提交
193 194 195
        assert args.val_value is not None, "--val_value should be defined while spliting dataset"

        dataset_dir = args.dataset_dir
M
update  
mamingjie-China 已提交
196
        dataset_format = args.format.lower()
M
mamingjie-China 已提交
197 198 199
        val_value = float(args.val_value)
        test_value = float(args.test_value
                           if args.test_value is not None else 0)
M
mamingjie-China 已提交
200
        save_dir = dataset_dir
M
mamingjie-China 已提交
201

M
update  
mamingjie-China 已提交
202
        if not dataset_format in ["coco", "imagenet", "voc", "seg"]:
M
mamingjie-China 已提交
203
            logging.error(
M
update  
mamingjie-China 已提交
204
                "The dataset format is not correct defined.(support COCO/ImageNet/VOC/Seg)"
M
mamingjie-China 已提交
205 206 207 208 209
            )
        if not osp.exists(dataset_dir):
            logging.error("The path of dataset to be splited doesn't exist.")
        if val_value <= 0 or val_value >= 1 or test_value < 0 or test_value >= 1 or val_value + test_value >= 1:
            logging.error("The value of split is not correct.")
M
mamingjie-China 已提交
210

M
update  
mamingjie-China 已提交
211
        pdx.tools.split.dataset_split(dataset_dir, dataset_format, val_value,
M
mamingjie-China 已提交
212
                                      test_value, save_dir)
C
Channingss 已提交
213

J
jiangjiajun 已提交
214 215 216

if __name__ == "__main__":
    main()