inference_model.py 3.2 KB
Newer Older
0
0YuanZhang0 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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
# 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.
"""save inference model for auto dialogue evaluation"""

import os
import sys
import six
import numpy as np
import time
import paddle
import paddle.fluid as fluid

import ade.reader as reader
from ade_net import create_net

from ade.utils.configure import PDConfig
from ade.utils.input_field import InputField
from ade.utils.model_check import check_cuda


def do_save_inference_model(args):

    test_prog = fluid.default_main_program()
    startup_prog = fluid.default_startup_program()

    with fluid.program_guard(test_prog, startup_prog):
        test_prog.random_seed = args.random_seed
        startup_prog.random_seed = args.random_seed

        with fluid.unique_name.guard():

0
0YuanZhang0 已提交
43
            context_wordseq = fluid.data(
P
pkpk 已提交
44 45 46 47
                name='context_wordseq',
                shape=[-1, 1],
                dtype='int64',
                lod_level=1)
0
0YuanZhang0 已提交
48
            response_wordseq = fluid.data(
P
pkpk 已提交
49 50 51 52 53
                name='response_wordseq',
                shape=[-1, 1],
                dtype='int64',
                lod_level=1)
            labels = fluid.data(name='labels', shape=[-1, 1], dtype='int64')
0
0YuanZhang0 已提交
54 55 56

            input_inst = [context_wordseq, response_wordseq, labels]
            input_field = InputField(input_inst)
57
            data_reader = fluid.io.DataLoader.from_generator(
P
pkpk 已提交
58
                feed_list=input_inst, capacity=4, iterable=False)
0
0YuanZhang0 已提交
59 60

            logits = create_net(
P
pkpk 已提交
61
                is_training=False, model_input=input_field, args=args)
0
0YuanZhang0 已提交
62 63 64 65 66 67 68 69 70 71

    if args.use_cuda:
        place = fluid.CUDAPlace(0)
    else:
        place = fluid.CPUPlace()

    exe = fluid.Executor(place)
    exe.run(startup_prog)

    assert (args.init_from_params) or (args.init_from_pretrain_model)
P
pkpk 已提交
72

0
0YuanZhang0 已提交
73
    if args.init_from_params:
74
        fluid.load(test_prog, args.init_from_params)
0
0YuanZhang0 已提交
75
    elif args.init_from_pretrain_model:
76
        fluid.load(test_prog, args.init_from_pretrain_model)
0
0YuanZhang0 已提交
77 78 79

    # saving inference model
    fluid.io.save_inference_model(
P
pkpk 已提交
80 81 82 83 84 85 86 87 88 89
        args.inference_model_dir,
        feeded_var_names=[
            input_field.context_wordseq.name,
            input_field.response_wordseq.name,
        ],
        target_vars=[logits, ],
        executor=exe,
        main_program=test_prog,
        model_filename="model.pdmodel",
        params_filename="params.pdparams")
0
0YuanZhang0 已提交
90 91 92 93 94

    print("save inference model at %s" % (args.inference_model_dir))


if __name__ == "__main__":
P
pkpk 已提交
95
    args = PDConfig(yaml_file="./data/config/ade.yaml")
0
0YuanZhang0 已提交
96 97 98 99 100
    args.build()

    check_cuda(args.use_cuda)

    do_save_inference_model(args)