Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
hapi
提交
636410da
H
hapi
项目概览
PaddlePaddle
/
hapi
通知
11
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
7
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hapi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
7
合并请求
7
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
636410da
编写于
9月 08, 2020
作者:
H
huangjun12
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
trans hapi interface of bmn model to paddle2.0
上级
55cf4f56
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
66 addition
and
80 deletion
+66
-80
examples/bmn/bmn_metric.py
examples/bmn/bmn_metric.py
+2
-2
examples/bmn/eval.py
examples/bmn/eval.py
+8
-26
examples/bmn/modeling.py
examples/bmn/modeling.py
+34
-13
examples/bmn/predict.py
examples/bmn/predict.py
+7
-19
examples/bmn/reader.py
examples/bmn/reader.py
+1
-2
examples/bmn/run.sh
examples/bmn/run.sh
+8
-1
examples/bmn/train.py
examples/bmn/train.py
+6
-17
未找到文件。
examples/bmn/bmn_metric.py
浏览文件 @
636410da
...
...
@@ -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
...
...
examples/bmn/eval.py
浏览文件 @
636410da
...
...
@@ -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
:
...
...
examples/bmn/modeling.py
浏览文件 @
636410da
...
...
@@ -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
,
num_sample_perbin
)
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'
),
\
...
...
examples/bmn/predict.py
浏览文件 @
636410da
...
...
@@ -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
:
...
...
examples/bmn/reader.py
浏览文件 @
636410da
...
...
@@ -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__
)
...
...
examples/bmn/run.sh
浏览文件 @
636410da
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"
examples/bmn/train.py
浏览文件 @
636410da
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录