提交 636410da 编写于 作者: H huangjun12

trans hapi interface of bmn model to paddle2.0

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