未验证 提交 df33864a 编写于 作者: Q qingqing01 提交者: GitHub

Merge pull request #88 from huangjun12/bmn-2.0b

transfer bmn model hapi interface to paddle2.0
......@@ -20,7 +20,7 @@ import json
sys.path.append('../')
from paddle.incubate.hapi.metrics import Metric
from paddle.metric import Metric
from bmn_utils import boundary_choose, bmn_post_processing
......@@ -47,7 +47,7 @@ class BmnMetric(Metric):
if not os.path.isdir(self.cfg.INFER.result_path):
os.makedirs(self.cfg.INFER.result_path)
def add_metric_op(self, *args):
def compute(self, *args):
if self.mode == 'test':
# only extract pred_bm, pred_start, pred_en from outputs
# and video_index from label here
......
......@@ -12,14 +12,13 @@
#See the License for the specific language governing permissions and
#limitations under the License.
import paddle
import argparse
import os
import sys
import logging
import paddle.fluid as fluid
from paddle.incubate.hapi.model import set_device, Input
from modeling import bmn, BmnLoss
from bmn_metric import BmnMetric
from reader import BmnDataset
......@@ -78,8 +77,8 @@ def parse_args():
# Performance Evaluation
def test_bmn(args):
device = set_device(args.device)
fluid.enable_dygraph(device) if args.dynamic else None
device = paddle.set_device(args.device)
paddle.disable_static(device) if args.dynamic else None
#config setting
config = parse_config(args.config_file)
......@@ -92,39 +91,22 @@ def test_bmn(args):
num_sample = config.MODEL.num_sample
num_sample_perbin = config.MODEL.num_sample_perbin
#input and video index
inputs = [
Input(
[None, config.MODEL.feat_dim, config.MODEL.tscale],
'float32',
name='feat_input')
]
gt_iou_map = Input(
[None, config.MODEL.dscale, config.MODEL.tscale],
'float32',
name='gt_iou_map')
gt_start = Input([None, config.MODEL.tscale], 'float32', name='gt_start')
gt_end = Input([None, config.MODEL.tscale], 'float32', name='gt_end')
video_idx = Input([None, 1], 'int64', name='video_idx')
labels = [gt_iou_map, gt_start, gt_end, video_idx]
#data
eval_dataset = BmnDataset(eval_cfg, 'test')
#model
model = bmn(tscale,
dscale,
feat_dim,
prop_boundary_ratio,
num_sample,
num_sample_perbin,
mode='test',
pretrained=args.weights is None)
model.prepare(
loss_function=BmnLoss(tscale, dscale),
metrics=BmnMetric(
config, mode='test'),
inputs=inputs,
labels=labels,
device=device)
loss=BmnLoss(tscale, dscale), metrics=BmnMetric(
config, mode='test'))
#load checkpoint
if args.weights is not None:
......
......@@ -12,14 +12,14 @@
#See the License for the specific language governing permissions and
#limitations under the License.
import paddle
import paddle.fluid as fluid
from paddle.fluid import ParamAttr
import numpy as np
import math
from paddle.incubate.hapi.model import Model
from paddle.incubate.hapi.loss import Loss
from paddle.incubate.hapi.download import get_weights_path_from_url
from paddle.static import InputSpec
from paddle.utils.download import get_weights_path_from_url
__all__ = ["BMN", "BmnLoss", "bmn"]
......@@ -126,7 +126,7 @@ class Conv1D(fluid.dygraph.Layer):
return x
class BMN(Model):
class BMN(fluid.dygraph.Layer):
"""BMN model from
`"BMN: Boundary-Matching Network for Temporal Action Proposal Generation" <https://arxiv.org/abs/1907.09702>`_
......@@ -288,7 +288,7 @@ class BMN(Model):
return xp, xs, xe
class BmnLoss(Loss):
class BmnLoss(fluid.dygraph.Layer):
"""Loss for BMN model
Args:
......@@ -415,12 +415,14 @@ class BmnLoss(Loss):
loss = -1 * (loss_pos + loss_neg) / num_entries
return loss
def forward(self, outputs, labels):
pred_bm, pred_start, pred_end = outputs
if len(labels) == 3:
gt_iou_map, gt_start, gt_end = labels
elif len(labels) == 4: # video_index used in eval mode
gt_iou_map, gt_start, gt_end, video_index = labels
def forward(self,
pred_bm,
pred_start,
pred_end,
gt_iou_map,
gt_start,
gt_end,
video_index=None):
pred_bm_reg = fluid.layers.squeeze(
fluid.layers.slice(
pred_bm, axes=[1], starts=[0], ends=[1]),
......@@ -443,9 +445,11 @@ class BmnLoss(Loss):
def bmn(tscale,
dscale,
feat_dim,
prop_boundary_ratio,
num_sample,
num_sample_perbin,
mode,
pretrained=True):
"""BMN model
......@@ -457,8 +461,25 @@ def bmn(tscale,
num_sample_perbin (int): number of selected points in each sample, default 3.
pretrained (bool): If True, returns a model with pre-trained model, default True.
"""
model = BMN(tscale, dscale, prop_boundary_ratio, num_sample,
inputs = [
InputSpec(
[None, feat_dim, tscale], 'float32', name='feat_input')
]
gt_iou_map = InputSpec(
[None, dscale, tscale], 'float32', name='gt_iou_map')
gt_start = InputSpec([None, tscale], 'float32', name='gt_start')
gt_end = InputSpec([None, tscale], 'float32', name='gt_end')
video_idx = InputSpec([None, 1], 'int64', name='video_idx')
label_dict = {
'train': [gt_iou_map, gt_start, gt_end],
'test': [gt_iou_map, gt_start, gt_end, video_idx],
'infer': [video_idx]
}
labels = label_dict[mode]
net = BMN(tscale, dscale, prop_boundary_ratio, num_sample,
num_sample_perbin)
model = paddle.Model(net, inputs, labels)
if pretrained:
weight_path = get_weights_path_from_url(*(pretrain_infos['bmn']))
assert weight_path.endswith('.pdparams'), \
......
......@@ -16,10 +16,9 @@ import argparse
import sys
import os
import logging
import paddle
import paddle.fluid as fluid
from paddle.incubate.hapi.model import set_device, Input
from modeling import bmn, BmnLoss
from bmn_metric import BmnMetric
from reader import BmnDataset
......@@ -83,8 +82,8 @@ def parse_args():
# Prediction
def infer_bmn(args):
device = set_device(args.device)
fluid.enable_dygraph(device) if args.dynamic else None
device = paddle.set_device(args.device)
paddle.disable_static(device) if args.dynamic else None
#config setting
config = parse_config(args.config_file)
......@@ -97,31 +96,20 @@ def infer_bmn(args):
num_sample = config.MODEL.num_sample
num_sample_perbin = config.MODEL.num_sample_perbin
#input and video index
inputs = [
Input(
[None, config.MODEL.feat_dim, config.MODEL.tscale],
'float32',
name='feat_input')
]
labels = [Input([None, 1], 'int64', name='video_idx')]
#data
infer_dataset = BmnDataset(infer_cfg, 'infer')
#model
model = bmn(tscale,
dscale,
feat_dim,
prop_boundary_ratio,
num_sample,
num_sample_perbin,
mode='infer',
pretrained=args.weights is None)
model.prepare(
metrics=BmnMetric(
config, mode='infer'),
inputs=inputs,
labels=labels,
device=device)
model.prepare(metrics=BmnMetric(config, mode='infer'))
# load checkpoint
if args.weights is not None:
......
......@@ -21,8 +21,7 @@ import sys
sys.path.append('../')
from paddle.incubate.hapi.distributed import DistributedBatchSampler
from paddle.io import Dataset, DataLoader
from paddle.io import Dataset, DataLoader, DistributedBatchSampler
logger = logging.getLogger(__name__)
......
export CUDA_VISIBLE_DEVICES=0,1,2,3
python -m paddle.distributed.launch train.py
start_time=$(date +%s)
python -m paddle.distributed.launch train.py -d
end_time=$(date +%s)
cost_time=$[ $end_time-$start_time ]
echo "4 card static training time is $(($cost_time/60))min $(($cost_time%60))s"
......@@ -12,14 +12,13 @@
#See the License for the specific language governing permissions and
#limitations under the License.
import paddle
import paddle.fluid as fluid
import argparse
import logging
import sys
import os
from paddle.incubate.hapi.model import set_device, Input
from reader import BmnDataset
from config_utils import *
from modeling import bmn, BmnLoss
......@@ -104,8 +103,8 @@ def optimizer(cfg, parameter_list):
# TRAIN
def train_bmn(args):
device = set_device(args.device)
fluid.enable_dygraph(device) if args.dynamic else None
device = paddle.set_device(args.device)
paddle.disable_static(device) if args.dynamic else None
if not os.path.isdir(args.save_dir):
os.makedirs(args.save_dir)
......@@ -122,13 +121,6 @@ def train_bmn(args):
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')
labels = [gt_iou_map, gt_start, gt_end]
# data
train_dataset = BmnDataset(train_cfg, 'train')
val_dataset = BmnDataset(val_cfg, 'valid')
......@@ -136,17 +128,14 @@ def train_bmn(args):
# model
model = bmn(tscale,
dscale,
feat_dim,
prop_boundary_ratio,
num_sample,
num_sample_perbin,
mode='train',
pretrained=False)
optim = optimizer(config, parameter_list=model.parameters())
model.prepare(
optimizer=optim,
loss_function=BmnLoss(tscale, dscale),
inputs=inputs,
labels=labels,
device=device)
model.prepare(optimizer=optim, loss=BmnLoss(tscale, dscale))
# if resume weights is given, load resume weights directly
if args.resume is not None:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册