convert.py 4.8 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
J
jiangjiajun 已提交
17
import sys
J
jiangjiajun 已提交
18
import x2paddle
19

J
jiangjiajun 已提交
20

21 22 23 24 25 26 27
def arg_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model",
                        "-m",
                        type=_text_type,
                        default=None,
                        help="model file path")
S
SunAhong1993 已提交
28
    parser.add_argument("--prototxt",
29 30 31
                        "-p",
                        type=_text_type,
                        default=None,
S
SunAhong1993 已提交
32
                        help="prototxt file of caffe model")
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    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")
S
SunAhong1993 已提交
48 49 50 51
    parser.add_argument(
        "--caffe_proto",
        "-c",
        type=_text_type,
S
SunAhong1993 已提交
52
        default=None,
S
SunAhong1993 已提交
53
        help="the .py file compiled by caffe proto file of caffe model")
J
jiangjiajun 已提交
54 55 56 57 58 59
    parser.add_argument("--version",
                        "-v",
                        action="store_true",
                        default=False,
                        help="get version of x2paddle")

60
    return parser
J
jiangjiajun 已提交
61

62

J
jiangjiajun 已提交
63
def tf2paddle(model_path, save_dir):
J
jiangjiajun 已提交
64 65 66 67 68 69 70 71 72 73
    # check tensorflow installation and version
    try:
        import tensorflow as tf
        version = tf.__version__
        if version >= '2.0.0' or version < '1.0.0':
            print(
                "1.0.0<=tensorflow<2.0.0 is required, and v1.14.0 is recommended"
            )
            return
    except:
S
SunAhong1993 已提交
74
        print("Tensorflow is not installed, use \"pip install tensorflow\".")
J
jiangjiajun 已提交
75 76
        return

J
jiangjiajun 已提交
77
    from x2paddle.decoder.tf_decoder import TFDecoder
J
jiangjiajun 已提交
78
    from x2paddle.op_mapper.tf_op_mapper import TFOpMapper
J
jiangjiajun 已提交
79
    from x2paddle.optimizer.tf_optimizer import TFOptimizer
J
jiangjiajun 已提交
80 81

    print("Now translating model from tensorflow to paddle.")
J
jiangjiajun 已提交
82 83
    model = TFDecoder(model_path)
    mapper = TFOpMapper(model)
J
jiangjiajun 已提交
84 85 86 87 88 89
    optimizer = TFOptimizer(mapper)
    # neccesary optimization
    optimizer.delete_redundance_code()
    # optimizer below is experimental
    optimizer.merge_activation()
    optimizer.merge_bias()
J
jiangjiajun 已提交
90
    mapper.save_inference_model(save_dir)
91 92


S
SunAhong1993 已提交
93
def caffe2paddle(proto, weight, save_dir, caffe_proto):
J
jiangjiajun 已提交
94 95
    from x2paddle.decoder.caffe_decoder import CaffeDecoder
    from x2paddle.op_mapper.caffe_op_mapper import CaffeOpMapper
J
jiangjiajun 已提交
96 97

    print("Now translating model from caffe to paddle.")
S
SunAhong1993 已提交
98
    model = CaffeDecoder(proto, weight, caffe_proto)
J
jiangjiajun 已提交
99
    mapper = CaffeOpMapper(model)
J
jiangjiajun 已提交
100
    mapper.save_inference_model(save_dir)
101 102 103


def main():
J
jiangjiajun 已提交
104
    if len(sys.argv) < 2:
J
jiangjiajun 已提交
105
        print("Use \"x2paddle -h\" to print the help information\n")
J
jiangjiajun 已提交
106 107
        return

108 109 110
    parser = arg_parser()
    args = parser.parse_args()

J
jiangjiajun 已提交
111 112 113 114
    if args.version:
        print("x2paddle-{} with python>=3.5\n".format(x2paddle.__version__))
        return

J
jiangjiajun 已提交
115 116 117 118 119 120 121 122 123
    try:
        import paddle
        v0, v1, v2 = paddle.__version__.split('.')
        if int(v0) != 1 or int(v1) < 5:
            print("paddlepaddle>=1.5.0 is required")
            return
    except:
        print("paddlepaddle not installed, use \"pip install paddlepaddle\"")

124 125 126 127
    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":
J
jiangjiajun 已提交
128
        assert args.model is not None, "--model should be defined while translating tensorflow model"
129 130 131
        tf2paddle(args.model, args.save_dir)

    elif args.framework == "caffe":
S
SunAhong1993 已提交
132 133 134
        assert args.prototxt is not None and args.weight is not None, "--prototxt and --weight should be defined while translating caffe model"
        caffe2paddle(args.prototxt, args.weight, args.save_dir,
                     args.caffe_proto)
135 136 137 138 139 140 141

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


if __name__ == "__main__":
    main()