Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
679fa655
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
679fa655
编写于
4月 26, 2019
作者:
G
Guanghua Yu
提交者:
qingqing01
4月 26, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add global shuffle for data reader in object_detection.
上级
9afe4f67
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
129 addition
and
103 deletion
+129
-103
PaddleCV/object_detection/mobilenet_ssd.py
PaddleCV/object_detection/mobilenet_ssd.py
+109
-96
PaddleCV/object_detection/reader.py
PaddleCV/object_detection/reader.py
+3
-2
PaddleCV/object_detection/train.py
PaddleCV/object_detection/train.py
+17
-5
未找到文件。
PaddleCV/object_detection/mobilenet_ssd.py
浏览文件 @
679fa655
...
...
@@ -3,111 +3,124 @@ from paddle.fluid.initializer import MSRA
from
paddle.fluid.param_attr
import
ParamAttr
def
conv_bn
(
input
,
filter_size
,
num_filters
,
stride
,
padding
,
channels
=
None
,
num_groups
=
1
,
act
=
'relu'
,
use_cudnn
=
True
):
parameter_attr
=
ParamAttr
(
learning_rate
=
0.1
,
initializer
=
MSRA
())
conv
=
fluid
.
layers
.
conv2d
(
input
=
input
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
stride
,
padding
=
padding
,
groups
=
num_groups
,
act
=
None
,
use_cudnn
=
use_cudnn
,
param_attr
=
parameter_attr
,
bias_attr
=
False
)
return
fluid
.
layers
.
batch_norm
(
input
=
conv
,
act
=
act
)
class
MobileNetSSD
:
def
__init__
(
self
,
img
,
num_classes
,
img_shape
):
self
.
img
=
img
self
.
num_classes
=
num_classes
self
.
img_shape
=
img_shape
def
ssd_net
(
self
,
scale
=
1.0
):
# 300x300
tmp
=
self
.
conv_bn
(
self
.
img
,
3
,
int
(
32
*
scale
),
2
,
1
,
3
)
# 150x150
tmp
=
self
.
depthwise_separable
(
tmp
,
32
,
64
,
32
,
1
,
scale
)
tmp
=
self
.
depthwise_separable
(
tmp
,
64
,
128
,
64
,
2
,
scale
)
# 75x75
tmp
=
self
.
depthwise_separable
(
tmp
,
128
,
128
,
128
,
1
,
scale
)
tmp
=
self
.
depthwise_separable
(
tmp
,
128
,
256
,
128
,
2
,
scale
)
# 38x38
tmp
=
self
.
depthwise_separable
(
tmp
,
256
,
256
,
256
,
1
,
scale
)
tmp
=
self
.
depthwise_separable
(
tmp
,
256
,
512
,
256
,
2
,
scale
)
def
depthwise_separable
(
input
,
num_filters1
,
num_filters2
,
num_groups
,
stride
,
scale
):
depthwise_conv
=
conv_bn
(
input
=
input
,
filter_size
=
3
,
num_filters
=
int
(
num_filters1
*
scale
),
stride
=
stride
,
padding
=
1
,
num_groups
=
int
(
num_groups
*
scale
),
use_cudnn
=
False
)
# 19x19
for
i
in
range
(
5
):
tmp
=
self
.
depthwise_separable
(
tmp
,
512
,
512
,
512
,
1
,
scale
)
module11
=
tmp
tmp
=
self
.
depthwise_separable
(
tmp
,
512
,
1024
,
512
,
2
,
scale
)
pointwise_conv
=
conv_bn
(
input
=
depthwise_conv
,
filter_size
=
1
,
num_filters
=
int
(
num_filters2
*
scale
),
stride
=
1
,
padding
=
0
)
return
pointwise_conv
# 10x10
module13
=
self
.
depthwise_separable
(
tmp
,
1024
,
1024
,
1024
,
1
,
scale
)
module14
=
self
.
extra_block
(
module13
,
256
,
512
,
1
,
2
,
scale
)
# 5x5
module15
=
self
.
extra_block
(
module14
,
128
,
256
,
1
,
2
,
scale
)
# 3x3
module16
=
self
.
extra_block
(
module15
,
128
,
256
,
1
,
2
,
scale
)
# 2x2
module17
=
self
.
extra_block
(
module16
,
64
,
128
,
1
,
2
,
scale
)
mbox_locs
,
mbox_confs
,
box
,
box_var
=
fluid
.
layers
.
multi_box_head
(
inputs
=
[
module11
,
module13
,
module14
,
module15
,
module16
,
module17
],
image
=
self
.
img
,
num_classes
=
self
.
num_classes
,
min_ratio
=
20
,
max_ratio
=
90
,
min_sizes
=
[
60.0
,
105.0
,
150.0
,
195.0
,
240.0
,
285.0
],
max_sizes
=
[[],
150.0
,
195.0
,
240.0
,
285.0
,
300.0
],
aspect_ratios
=
[[
2.
],
[
2.
,
3.
],
[
2.
,
3.
],
[
2.
,
3.
],
[
2.
,
3.
],
[
2.
,
3.
]],
base_size
=
self
.
img_shape
[
2
],
offset
=
0.5
,
flip
=
True
)
def
extra_block
(
input
,
num_filters1
,
num_filters2
,
num_groups
,
stride
,
scale
):
# 1x1 conv
pointwise_conv
=
conv_bn
(
input
=
input
,
filter_size
=
1
,
num_filters
=
int
(
num_filters1
*
scale
),
stride
=
1
,
num_groups
=
int
(
num_groups
*
scale
),
padding
=
0
)
return
mbox_locs
,
mbox_confs
,
box
,
box_var
# 3x3 conv
normal_conv
=
conv_bn
(
input
=
pointwise_conv
,
filter_size
=
3
,
num_filters
=
int
(
num_filters2
*
scale
),
stride
=
2
,
num_groups
=
int
(
num_groups
*
scale
),
padding
=
1
)
return
normal_conv
def
conv_bn
(
self
,
input
,
filter_size
,
num_filters
,
stride
,
padding
,
channels
=
None
,
num_groups
=
1
,
act
=
'relu'
,
use_cudnn
=
True
):
parameter_attr
=
ParamAttr
(
learning_rate
=
0.1
,
initializer
=
MSRA
())
conv
=
fluid
.
layers
.
conv2d
(
input
=
input
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
stride
,
padding
=
padding
,
groups
=
num_groups
,
act
=
None
,
use_cudnn
=
use_cudnn
,
param_attr
=
parameter_attr
,
bias_attr
=
False
)
return
fluid
.
layers
.
batch_norm
(
input
=
conv
,
act
=
act
)
def
depthwise_separable
(
self
,
input
,
num_filters1
,
num_filters2
,
num_groups
,
stride
,
scale
):
depthwise_conv
=
self
.
conv_bn
(
input
=
input
,
filter_size
=
3
,
num_filters
=
int
(
num_filters1
*
scale
),
stride
=
stride
,
padding
=
1
,
num_groups
=
int
(
num_groups
*
scale
),
use_cudnn
=
False
)
def
mobile_net
(
num_classes
,
img
,
img_shape
,
scale
=
1.0
):
# 300x300
tmp
=
conv_bn
(
img
,
3
,
int
(
32
*
scale
),
2
,
1
,
3
)
# 150x150
tmp
=
depthwise_separable
(
tmp
,
32
,
64
,
32
,
1
,
scale
)
tmp
=
depthwise_separable
(
tmp
,
64
,
128
,
64
,
2
,
scale
)
# 75x75
tmp
=
depthwise_separable
(
tmp
,
128
,
128
,
128
,
1
,
scale
)
tmp
=
depthwise_separable
(
tmp
,
128
,
256
,
128
,
2
,
scale
)
# 38x38
tmp
=
depthwise_separable
(
tmp
,
256
,
256
,
256
,
1
,
scale
)
tmp
=
depthwise_separable
(
tmp
,
256
,
512
,
256
,
2
,
scale
)
pointwise_conv
=
self
.
conv_bn
(
input
=
depthwise_conv
,
filter_size
=
1
,
num_filters
=
int
(
num_filters2
*
scale
),
stride
=
1
,
padding
=
0
)
return
pointwise_conv
# 19x19
for
i
in
range
(
5
):
tmp
=
depthwise_separable
(
tmp
,
512
,
512
,
512
,
1
,
scale
)
module11
=
tmp
tmp
=
depthwise_separable
(
tmp
,
512
,
1024
,
512
,
2
,
scale
)
def
extra_block
(
self
,
input
,
num_filters1
,
num_filters2
,
num_groups
,
stride
,
scale
):
# 1x1 conv
pointwise_conv
=
self
.
conv_bn
(
input
=
input
,
filter_size
=
1
,
num_filters
=
int
(
num_filters1
*
scale
),
stride
=
1
,
num_groups
=
int
(
num_groups
*
scale
),
padding
=
0
)
# 10x10
module13
=
depthwise_separable
(
tmp
,
1024
,
1024
,
1024
,
1
,
scale
)
module14
=
extra_block
(
module13
,
256
,
512
,
1
,
2
,
scale
)
# 5x5
module15
=
extra_block
(
module14
,
128
,
256
,
1
,
2
,
scale
)
# 3x3
module16
=
extra_block
(
module15
,
128
,
256
,
1
,
2
,
scale
)
# 2x2
module17
=
extra_block
(
module16
,
64
,
128
,
1
,
2
,
scale
)
# 3x3 conv
normal_conv
=
self
.
conv_bn
(
input
=
pointwise_conv
,
filter_size
=
3
,
num_filters
=
int
(
num_filters2
*
scale
),
stride
=
2
,
num_groups
=
int
(
num_groups
*
scale
),
padding
=
1
)
return
normal_conv
mbox_locs
,
mbox_confs
,
box
,
box_var
=
fluid
.
layers
.
multi_box_head
(
inputs
=
[
module11
,
module13
,
module14
,
module15
,
module16
,
module17
],
image
=
img
,
num_classes
=
num_classes
,
min_ratio
=
20
,
max_ratio
=
90
,
min_sizes
=
[
60.0
,
105.0
,
150.0
,
195.0
,
240.0
,
285.0
],
max_sizes
=
[[],
150.0
,
195.0
,
240.0
,
285.0
,
300.0
],
aspect_ratios
=
[[
2.
],
[
2.
,
3.
],
[
2.
,
3.
],
[
2.
,
3.
],
[
2.
,
3.
],
[
2.
,
3.
]],
base_size
=
img_shape
[
2
],
offset
=
0.5
,
flip
=
True
)
return
mbox_locs
,
mbox_confs
,
box
,
box_var
def
build_mobilenet_ssd
(
img
,
num_classes
,
img_shape
):
ssd_model
=
MobileNetSSD
(
img
,
num_classes
,
img_shape
)
return
ssd_model
.
ssd_net
()
PaddleCV/object_detection/reader.py
浏览文件 @
679fa655
...
...
@@ -293,6 +293,7 @@ def train(settings,
coco_api
=
COCO
(
file_path
)
image_ids
=
coco_api
.
getImgIds
()
images
=
coco_api
.
loadImgs
(
image_ids
)
np
.
random
.
shuffle
(
images
)
n
=
int
(
math
.
ceil
(
len
(
images
)
//
num_workers
))
image_lists
=
[
images
[
i
:
i
+
n
]
for
i
in
range
(
0
,
len
(
images
),
n
)]
...
...
@@ -307,11 +308,11 @@ def train(settings,
data_dir
))
else
:
images
=
[
line
.
strip
()
for
line
in
open
(
file_path
)]
np
.
random
.
shuffle
(
images
)
n
=
int
(
math
.
ceil
(
len
(
images
)
//
num_workers
))
image_lists
=
[
images
[
i
:
i
+
n
]
for
i
in
range
(
0
,
len
(
images
),
n
)]
for
l
in
image_lists
:
readers
.
append
(
pascalvoc
(
settings
,
l
,
'train'
,
batch_size
,
shuffle
))
return
paddle
.
reader
.
multiprocess_reader
(
readers
,
False
)
...
...
@@ -341,7 +342,7 @@ def infer(settings, image_path):
"data path correctly."
%
image_path
)
img
=
Image
.
open
(
image_path
)
if
img
.
mode
==
'L'
:
img
=
im
.
convert
(
'RGB'
)
img
=
im
g
.
convert
(
'RGB'
)
im_width
,
im_height
=
img
.
size
img
=
img
.
resize
((
settings
.
resize_w
,
settings
.
resize_h
),
Image
.
ANTIALIAS
)
...
...
PaddleCV/object_detection/train.py
浏览文件 @
679fa655
...
...
@@ -10,7 +10,7 @@ import multiprocessing
import
paddle
import
paddle.fluid
as
fluid
import
reader
from
mobilenet_ssd
import
mobile_net
from
mobilenet_ssd
import
build_mobilenet_ssd
from
utility
import
add_arguments
,
print_arguments
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
...
...
@@ -92,7 +92,7 @@ def build_program(main_prog, startup_prog, train_params, is_train):
use_double_buffer
=
True
)
with
fluid
.
unique_name
.
guard
():
image
,
gt_box
,
gt_label
,
difficult
=
fluid
.
layers
.
read_file
(
py_reader
)
locs
,
confs
,
box
,
box_var
=
mobile_net
(
class_num
,
image
,
image_shape
)
locs
,
confs
,
box
,
box_var
=
build_mobilenet_ssd
(
image
,
class_num
,
image_shape
)
if
is_train
:
with
fluid
.
unique_name
.
guard
(
"train"
):
loss
=
fluid
.
layers
.
ssd_loss
(
locs
,
confs
,
gt_box
,
gt_label
,
box
,
...
...
@@ -228,6 +228,13 @@ def train(args,
total_time
=
0.0
for
epoc_id
in
range
(
epoc_num
):
train_reader
=
reader
.
train
(
data_args
,
train_file_list
,
batch_size_per_device
,
shuffle
=
is_shuffle
,
num_workers
=
num_workers
,
enable_ce
=
enable_ce
)
train_py_reader
.
decorate_paddle_reader
(
train_reader
)
epoch_idx
=
epoc_id
+
1
start_time
=
time
.
time
()
prev_start_time
=
start_time
...
...
@@ -255,9 +262,10 @@ def train(args,
end_time
=
time
.
time
()
total_time
+=
end_time
-
start_time
best_map
,
mean_map
=
test
(
epoc_id
,
best_map
)
print
(
"Best test map {0}"
.
format
(
best_map
))
if
epoc_id
%
10
==
0
or
epoc_id
==
epoc_num
-
1
:
best_map
,
mean_map
=
test
(
epoc_id
,
best_map
)
print
(
"Best test map {0}"
.
format
(
best_map
))
# save model
save_model
(
str
(
epoc_id
),
train_prog
)
if
enable_ce
:
...
...
@@ -275,7 +283,7 @@ def train(args,
(
devices_num
,
total_time
/
epoch_idx
))
if
__name__
==
'__main__'
:
def
main
()
:
args
=
parser
.
parse_args
()
print_arguments
(
args
)
...
...
@@ -318,3 +326,7 @@ if __name__ == '__main__':
train_parameters
[
dataset
],
train_file_list
=
train_file_list
,
val_file_list
=
val_file_list
)
if
__name__
==
'__main__'
:
main
()
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录