|
| 1 | +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# |
| 16 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | +# you may not use this file except in compliance with the License. |
| 18 | +# You may obtain a copy of the License at |
| 19 | +# |
| 20 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | +# |
| 22 | +# Unless required by applicable law or agreed to in writing, software |
| 23 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | +# See the License for the specific language governing permissions and |
| 26 | +# limitations under the License. |
| 27 | + |
| 28 | +import paddle |
| 29 | +import os |
| 30 | +import paddle.nn as nn |
| 31 | +import time |
| 32 | +import logging |
| 33 | +import sys |
| 34 | +import importlib |
| 35 | + |
| 36 | +__dir__ = os.path.dirname(os.path.abspath(__file__)) |
| 37 | +#sys.path.append(__dir__) |
| 38 | +sys.path.append(os.path.abspath(os.path.join(__dir__, '..'))) |
| 39 | + |
| 40 | +from utils.utils_single import load_yaml, load_dy_model_class, get_abs_model, create_data_loader |
| 41 | +from utils.save_load import load_model, save_model, save_jit_model |
| 42 | +from paddle.io import DistributedBatchSampler, DataLoader |
| 43 | +import argparse |
| 44 | + |
| 45 | +logging.basicConfig( |
| 46 | + format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) |
| 47 | +logger = logging.getLogger(__name__) |
| 48 | + |
| 49 | + |
| 50 | +def parse_args(): |
| 51 | + parser = argparse.ArgumentParser(description='paddle-rec run') |
| 52 | + parser.add_argument("-m", "--config_yaml", type=str) |
| 53 | + args = parser.parse_args() |
| 54 | + args.abs_dir = os.path.dirname(os.path.abspath(args.config_yaml)) |
| 55 | + args.config_yaml = get_abs_model(args.config_yaml) |
| 56 | + return args |
| 57 | + |
| 58 | + |
| 59 | +def main(args): |
| 60 | + paddle.seed(12345) |
| 61 | + # load config |
| 62 | + config = load_yaml(args.config_yaml) |
| 63 | + dy_model_class = load_dy_model_class(args.abs_dir) |
| 64 | + config["config_abs_dir"] = args.abs_dir |
| 65 | + # tools.vars |
| 66 | + use_gpu = config.get("runner.use_gpu", True) |
| 67 | + train_data_dir = config.get("runner.train_data_dir", None) |
| 68 | + epochs = config.get("runner.epochs", None) |
| 69 | + print_interval = config.get("runner.print_interval", None) |
| 70 | + model_save_path = config.get("runner.model_save_path", "model_output") |
| 71 | + model_init_path = config.get("runner.model_init_path", None) |
| 72 | + |
| 73 | + logger.info("**************common.configs**********") |
| 74 | + logger.info( |
| 75 | + "use_gpu: {}, train_data_dir: {}, epochs: {}, print_interval: {}, model_save_path: {}". |
| 76 | + format(use_gpu, train_data_dir, epochs, print_interval, |
| 77 | + model_save_path)) |
| 78 | + logger.info("**************common.configs**********") |
| 79 | + |
| 80 | + place = paddle.set_device('gpu' if use_gpu else 'cpu') |
| 81 | + |
| 82 | + dy_model = dy_model_class.create_model(config) |
| 83 | + |
| 84 | + load_model(model_init_path, dy_model) |
| 85 | + # example dnn model forward |
| 86 | + dy_model = paddle.jit.to_static( |
| 87 | + dy_model, |
| 88 | + input_spec=[[ |
| 89 | + paddle.static.InputSpec( |
| 90 | + shape=[None, 1], dtype='int64') for jj in range(26) |
| 91 | + ], paddle.static.InputSpec( |
| 92 | + shape=[None, 13], dtype='float32')]) |
| 93 | + save_jit_model(dy_model, model_save_path, prefix='tostatic') |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + args = parse_args() |
| 98 | + main(args) |
0 commit comments