convert.py 3.2 KB
Newer Older
J
jiangjiajun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#   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.
S
SunAhong1993 已提交
14

15 16
from six import text_type as _text_type
import argparse
17

J
jiangjiajun 已提交
18

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",
                        "-m",
                        type=_text_type,
                        default=None,
                        help="model file path")
    parser.add_argument("--proto",
                        "-p",
                        type=_text_type,
                        default=None,
                        help="proto file of caffe model")
    parser.add_argument("--weight",
                        "-w",
                        type=_text_type,
                        default=None,
                        help="weight file of caffe model")
    parser.add_argument("--save_dir",
                        "-s",
                        type=_text_type,
                        default=None,
                        help="path to save translated model")
    parser.add_argument("--framework",
                        "-f",
                        type=_text_type,
                        default=None,
                        help="define which deeplearning framework")
    return parser
J
jiangjiajun 已提交
47

48 49 50

def tf2paddle(model, save_dir):
    print("Now translating model from tensorflow to paddle.")
S
SunAhong1993 已提交
51 52 53
    from x2paddle.parser.tf_parser import TFParser
    from x2paddle.optimizer.tf_optimizer import TFGraphOptimizer
    from x2paddle.emitter.tf_emitter import TFEmitter
54 55 56 57 58 59 60
    parser = TFParser(model)
    emitter = TFEmitter(parser)
    emitter.run()
    emitter.save_python_model(save_dir)


def caffe2paddle(proto, weight, save_dir):
S
SunAhong1993 已提交
61 62 63 64 65 66 67
    print("Now translating model from caffe to paddle.")
    from x2paddle.parser.caffe_parser import CaffeParser
    from x2paddle.emitter.caffe_emitter import CaffeEmitter
    parser = CaffeParser(proto, weight)
    emitter = CaffeEmitter(parser)
    emitter.run()
    emitter.save_python_model(save_dir)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90


def main():
    parser = arg_parser()
    args = parser.parse_args()

    assert args.framework is not None, "--from is not defined(tensorflow/caffe)"
    assert args.save_dir is not None, "--save_dir is not defined"

    if args.framework == "tensorflow":
        assert args.model is not None, "--model should be defined while translate tensorflow model"
        tf2paddle(args.model, args.save_dir)

    elif args.framework == "caffe":
        assert args.proto is not None, "--proto and --weight should be defined while translate caffe model"
        caffe2paddle(args.proto, args.weight, args.save_dir)

    else:
        raise Exception("--framework only support tensorflow/caffe now")


if __name__ == "__main__":
    main()