train.py 5.3 KB
Newer Older
H
huangjun12 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#  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 paddle.fluid as fluid
import argparse
import logging
import sys
import os

L
LielinJiang 已提交
21
from paddle.incubate.hapi.model import set_device, Input
D
dengkaipeng 已提交
22

H
huangjun12 已提交
23 24
from reader import BmnDataset
from config_utils import *
D
dengkaipeng 已提交
25
from modeling import bmn, BmnLoss
H
huangjun12 已提交
26 27 28 29 30 31 32 33 34 35 36 37

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("Paddle high level api of BMN.")
    parser.add_argument(
H
huangjun12 已提交
38
        "-d", "--dynamic", action='store_true', help="enable dygraph mode")
H
huangjun12 已提交
39 40 41 42 43 44 45 46 47
    parser.add_argument(
        '--config_file',
        type=str,
        default='bmn.yaml',
        help='path to config file of model')
    parser.add_argument(
        '--batch_size',
        type=int,
        default=None,
H
huangjun12 已提交
48
        help='training batch size. None for read from config file.')
H
huangjun12 已提交
49 50 51
    parser.add_argument(
        '--learning_rate',
        type=float,
52
        default=None,
H
huangjun12 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        help='learning rate use for training. None to use config file setting.')
    parser.add_argument(
        '--resume',
        type=str,
        default=None,
        help='filename to resume training based on previous checkpoints. '
        'None for not resuming any checkpoints.')
    parser.add_argument(
        '--device',
        type=str,
        default='gpu',
        help='gpu or cpu, default use gpu.')
    parser.add_argument(
        '--epoch',
        type=int,
H
huangjun12 已提交
68 69
        default=None,
        help='epoch number, None for read from config file')
H
huangjun12 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    parser.add_argument(
        '--valid_interval',
        type=int,
        default=1,
        help='validation epoch interval, 0 for no validation.')
    parser.add_argument(
        '--save_dir',
        type=str,
        default="checkpoint",
        help='path to save train snapshoot')
    parser.add_argument(
        '--log_interval',
        type=int,
        default=10,
        help='mini-batch interval to log.')
    args = parser.parse_args()
    return args


# Optimizer
def optimizer(cfg, parameter_list):
    bd = [cfg.TRAIN.lr_decay_iter]
    base_lr = cfg.TRAIN.learning_rate
    lr_decay = cfg.TRAIN.learning_rate_decay
    l2_weight_decay = cfg.TRAIN.l2_weight_decay
    lr = [base_lr, base_lr * lr_decay]
    optimizer = fluid.optimizer.Adam(
        fluid.layers.piecewise_decay(
            boundaries=bd, values=lr),
        parameter_list=parameter_list,
        regularization=fluid.regularizer.L2DecayRegularizer(
            regularization_coeff=l2_weight_decay))
    return optimizer


# TRAIN
def train_bmn(args):
    device = set_device(args.device)
    fluid.enable_dygraph(device) if args.dynamic else None

    if not os.path.isdir(args.save_dir):
        os.makedirs(args.save_dir)

H
huangjun12 已提交
113
    #config setting
H
huangjun12 已提交
114 115 116 117
    config = parse_config(args.config_file)
    train_cfg = merge_configs(config, 'train', vars(args))
    val_cfg = merge_configs(config, 'valid', vars(args))

H
huangjun12 已提交
118 119 120 121 122 123 124 125 126 127 128 129
    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

    # input and label list
    inputs = [Input([None, feat_dim, tscale], 'float32', name='feat_input')]
    gt_iou_map = Input([None, dscale, tscale], 'float32', name='gt_iou_map')
    gt_start = Input([None, tscale], 'float32', name='gt_start')
    gt_end = Input([None, tscale], 'float32', name='gt_end')
H
huangjun12 已提交
130 131 132 133 134 135 136
    labels = [gt_iou_map, gt_start, gt_end]

    # data
    train_dataset = BmnDataset(train_cfg, 'train')
    val_dataset = BmnDataset(val_cfg, 'valid')

    # model
H
huangjun12 已提交
137 138 139 140 141 142
    model = bmn(tscale,
                dscale,
                prop_boundary_ratio,
                num_sample,
                num_sample_perbin,
                pretrained=False)
H
huangjun12 已提交
143 144 145
    optim = optimizer(config, parameter_list=model.parameters())
    model.prepare(
        optimizer=optim,
H
huangjun12 已提交
146
        loss_function=BmnLoss(tscale, dscale),
H
huangjun12 已提交
147 148 149 150 151 152 153 154 155 156
        inputs=inputs,
        labels=labels,
        device=device)

    # if resume weights is given, load resume weights directly
    if args.resume is not None:
        model.load(args.resume)
    model.fit(train_data=train_dataset,
              eval_data=val_dataset,
              batch_size=train_cfg.TRAIN.batch_size,
H
huangjun12 已提交
157
              epochs=train_cfg.TRAIN.epoch,
H
huangjun12 已提交
158 159 160 161 162 163 164 165 166 167 168
              eval_freq=args.valid_interval,
              log_freq=args.log_interval,
              save_dir=args.save_dir,
              shuffle=train_cfg.TRAIN.use_shuffle,
              num_workers=train_cfg.TRAIN.num_workers,
              drop_last=True)


if __name__ == "__main__":
    args = parse_args()
    train_bmn(args)