convert.py 4.2 KB
Newer Older
J
jiangjiajun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2019  PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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 paddle_emitter import PaddleEmitter
from tensorflow_parser import TensorflowCkptParser
from tensorflow_parser import TensorflowPbParser
J
jiangjiajun 已提交
18
from six import text_type as _text_type
J
jiangjiajun 已提交
19
from utils import *
J
jiangjiajun 已提交
20
import argparse
J
jiangjiajun 已提交
21
import logging
J
jiangjiajun 已提交
22
import os
J
jiangjiajun 已提交
23
logging.basicConfig(level=logging.DEBUG)
J
jiangjiajun 已提交
24

J
jiangjiajun 已提交
25

J
jiangjiajun 已提交
26 27
def _get_parser():
    parser = argparse.ArgumentParser()
J
jiangjiajun 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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
    parser.add_argument(
        "--meta_file",
        "-m",
        type=_text_type,
        default=None,
        help="meta file path for checkpoint format")
    parser.add_argument(
        "--ckpt_dir",
        "-c",
        type=_text_type,
        default=None,
        help="checkpoint directory")
    parser.add_argument(
        "--pb_file",
        "-p",
        type=_text_type,
        default=None,
        help="pb model file path")
    parser.add_argument(
        "--in_nodes",
        "-i",
        type=_text_type,
        nargs="+",
        default=None,
        help="input nodes name")
    parser.add_argument(
        "--input_shape",
        "-is",
        type=_text_type,
        nargs="+",
        default=None,
        help="input tensor shape")
    parser.add_argument(
        "--output_nodes",
        "-o",
        type=_text_type,
        nargs="+",
        default=None,
        help="output nodes name")
    parser.add_argument(
        "--save_dir",
        "-s",
        type=_text_type,
        default=None,
        help="path to save transformed paddle model")
    parser.add_argument(
        "--input_format",
        "-sf",
        type=_text_type,
        default=None,
        help="input data format(NHWC/NCHW or OTHER)")
    parser.add_argument(
        "--use_cuda",
        "-u",
        type=_text_type,
        default="True",
        help="True for use gpu")
J
jiangjiajun 已提交
85 86
    return parser

J
jiangjiajun 已提交
87 88

def run(args):
J
jiangjiajun 已提交
89 90
    if args.meta_file is None and args.pb_file is None:
        raise Exception("Need to define --meta_file or --pb_file")
J
jiangjiajun 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    if args.input_format is None:
        raise Exception("Input format need to be defined(NHWC, NCHW or OTHER)")
    assert args.use_cuda == "True" or args.use_cuda == "False"
    if args.use_cuda == "False":
        os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

    if args.input_format == "NHWC":
        input_format = NHWC
    elif args.input_format == "NCHW":
        input_format = NCHW
    elif args.input_format == "OTHER":
        input_format = OTHER
    else:
        raise Exception("Can not identify input format(NHWC/NCHW/OTHER)")

J
jiangjiajun 已提交
106 107 108 109 110
    assert args.in_nodes is not None
    assert args.output_nodes is not None
    assert args.input_shape is not None
    assert args.save_dir is not None

J
jiangjiajun 已提交
111 112
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
J
jiangjiajun 已提交
113 114 115 116 117 118 119

    input_shape = list()
    for shape_str in args.input_shape:
        items = shape_str.split(',')
        for i in range(len(items)):
            if items[i] != "None":
                items[i] = int(items[i])
J
Jason 已提交
120 121
            else:
                items[i] = None
J
jiangjiajun 已提交
122

J
jiangjiajun 已提交
123 124
        input_shape.append(items)

J
jiangjiajun 已提交
125
    logging.info("Loading tensorflow model...")
J
jiangjiajun 已提交
126
    if args.meta_file is not None:
J
jiangjiajun 已提交
127 128 129
        parser = TensorflowCkptParser(args.meta_file, args.ckpt_dir,
                                      args.output_nodes, input_shape,
                                      args.in_nodes, input_format)
J
jiangjiajun 已提交
130
    else:
J
jiangjiajun 已提交
131 132
        parser = TensorflowPbParser(args.pb_file, args.output_nodes,
                                    input_shape, args.in_nodes, input_format)
J
jiangjiajun 已提交
133 134
    logging.info("Tensorflow model loaded!")

J
jiangjiajun 已提交
135 136 137
    emitter = PaddleEmitter(parser, args.save_dir)
    emitter.run()

J
jiangjiajun 已提交
138 139
    open(args.save_dir + "/__init__.py", "w").close()

J
jiangjiajun 已提交
140 141

def _main():
J
jiangjiajun 已提交
142 143
    parser = _get_parser()
    args = parser.parse_args()
J
jiangjiajun 已提交
144 145
    run(args)

J
jiangjiajun 已提交
146 147 148

if __name__ == "__main__":
    _main()