predict.py 3.9 KB
Newer Older
H
huangjun12 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#  Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
#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.

import argparse
import sys
import os
import logging
19
import paddle
H
huangjun12 已提交
20 21
import paddle.fluid as fluid

D
dengkaipeng 已提交
22
from modeling import bmn, BmnLoss
H
huangjun12 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
from bmn_metric import BmnMetric
from reader import BmnDataset
from config_utils import *

DATATYPE = 'float32'

logging.root.handlers = []
FORMAT = '[%(levelname)s: %(filename)s: %(lineno)4d]: %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT, stream=sys.stdout)
logger = logging.getLogger(__name__)


def parse_args():
    parser = argparse.ArgumentParser("BMN inference.")
    parser.add_argument(
        "-d",
        "--dynamic",
        action='store_true',
        help="enable dygraph mode, only support dynamic mode at present time")
    parser.add_argument(
        '--config_file',
        type=str,
        default='bmn.yaml',
        help='path to config file of model')
    parser.add_argument(
48 49 50 51
        '--device',
        type=str,
        default='gpu',
        help='gpu or cpu, default use gpu.')
H
huangjun12 已提交
52 53 54
    parser.add_argument(
        '--weights',
        type=str,
D
dengkaipeng 已提交
55
        default=None,
H
huangjun12 已提交
56 57 58
        help='weight path, None to automatically download weights provided by Paddle.'
    )
    parser.add_argument(
H
huangjun12 已提交
59 60
        '--filelist',
        type=str,
61 62
        default=None,
        help='infer file list, None to use config file setting.')
H
huangjun12 已提交
63 64 65
    parser.add_argument(
        '--output_path',
        type=str,
66 67
        default=None,
        help='output dir path, None to use config file setting.')
H
huangjun12 已提交
68 69
    parser.add_argument(
        '--result_path',
H
huangjun12 已提交
70
        type=str,
71 72
        default=None,
        help='output dir path after post processing,  None to use config file setting.'
H
huangjun12 已提交
73
    )
H
huangjun12 已提交
74 75 76 77 78 79 80 81 82 83 84
    parser.add_argument(
        '--log_interval',
        type=int,
        default=1,
        help='mini-batch interval to log.')
    args = parser.parse_args()
    return args


# Prediction
def infer_bmn(args):
85 86
    device = paddle.set_device(args.device)
    paddle.disable_static(device) if args.dynamic else None
H
huangjun12 已提交
87

H
huangjun12 已提交
88
    #config setting
H
huangjun12 已提交
89 90 91
    config = parse_config(args.config_file)
    infer_cfg = merge_configs(config, 'infer', vars(args))

H
huangjun12 已提交
92 93 94 95 96 97
    feat_dim = config.MODEL.feat_dim
    tscale = config.MODEL.tscale
    dscale = config.MODEL.dscale
    prop_boundary_ratio = config.MODEL.prop_boundary_ratio
    num_sample = config.MODEL.num_sample
    num_sample_perbin = config.MODEL.num_sample_perbin
H
huangjun12 已提交
98 99 100 101

    #data
    infer_dataset = BmnDataset(infer_cfg, 'infer')

H
huangjun12 已提交
102 103 104
    #model
    model = bmn(tscale,
                dscale,
105
                feat_dim,
H
huangjun12 已提交
106 107 108
                prop_boundary_ratio,
                num_sample,
                num_sample_perbin,
109
                mode='infer',
H
huangjun12 已提交
110
                pretrained=args.weights is None)
111 112

    model.prepare(metrics=BmnMetric(config, mode='infer'))
H
huangjun12 已提交
113 114

    # load checkpoint
D
dengkaipeng 已提交
115
    if args.weights is not None:
H
huangjun12 已提交
116 117 118
        assert os.path.exists(
            args.weights +
            ".pdparams"), "Given weight dir {} not exist.".format(args.weights)
D
dengkaipeng 已提交
119 120
        logger.info('load test weights from {}'.format(args.weights))
        model.load(args.weights)
H
huangjun12 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134

    # here use model.eval instead of model.test, as post process is required in our case
    model.evaluate(
        eval_data=infer_dataset,
        batch_size=infer_cfg.TEST.batch_size,
        num_workers=infer_cfg.TEST.num_workers,
        log_freq=args.log_interval)

    logger.info("[INFER] infer finished")


if __name__ == '__main__':
    args = parse_args()
    infer_bmn(args)