Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
2377b052
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看板
未验证
提交
2377b052
编写于
3月 12, 2020
作者:
littletomatodonkey
提交者:
GitHub
3月 12, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix gpu num check bug (#4406)
上级
a0a66616
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
73 addition
and
57 deletion
+73
-57
PaddleCV/metric_learning/train_elem.py
PaddleCV/metric_learning/train_elem.py
+35
-26
PaddleCV/metric_learning/train_pair.py
PaddleCV/metric_learning/train_pair.py
+38
-31
未找到文件。
PaddleCV/metric_learning/train_elem.py
浏览文件 @
2377b052
...
...
@@ -44,7 +44,7 @@ add_arg('test_batch_size', int, 50, "Minibatch size.")
add_arg
(
'image_shape'
,
str
,
"3,224,224"
,
"input image size"
)
add_arg
(
'class_dim'
,
int
,
11318
,
"Class number."
)
add_arg
(
'lr'
,
float
,
0.01
,
"set learning rate."
)
add_arg
(
'lr_strategy'
,
str
,
"piecewise_decay"
,
"Set the learning rate decay strategy."
)
add_arg
(
'lr_strategy'
,
str
,
"piecewise_decay"
,
"Set the learning rate decay strategy."
)
add_arg
(
'lr_steps'
,
str
,
"15000,25000"
,
"step of lr"
)
add_arg
(
'total_iter_num'
,
int
,
30000
,
"total_iter_num"
)
add_arg
(
'display_iter_step'
,
int
,
10
,
"display_iter_step."
)
...
...
@@ -63,15 +63,15 @@ add_arg('enable_ce', bool, False, "If set True, enable continuous evaluation job
model_list
=
[
m
for
m
in
dir
(
models
)
if
"__"
not
in
m
]
def
optimizer_setting
(
params
):
ls
=
params
[
"learning_strategy"
]
assert
ls
[
"name"
]
==
"piecewise_decay"
,
\
"learning rate strategy must be {},
\
but got {}"
.
format
(
"piecewise_decay"
,
lr
[
"name"
])
"learning rate strategy must be {}, but got {}"
.
format
(
"piecewise_decay"
,
lr
[
"name"
])
bd
=
[
int
(
e
)
for
e
in
ls
[
"lr_steps"
].
split
(
','
)]
base_lr
=
params
[
"lr"
]
lr
=
[
base_lr
*
(
0.1
**
i
)
for
i
in
range
(
len
(
bd
)
+
1
)]
lr
=
[
base_lr
*
(
0.1
**
i
)
for
i
in
range
(
len
(
bd
)
+
1
)]
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
fluid
.
layers
.
piecewise_decay
(
boundaries
=
bd
,
values
=
lr
),
...
...
@@ -81,30 +81,28 @@ def optimizer_setting(params):
def
net_config
(
image
,
label
,
model
,
args
,
is_train
):
assert
args
.
model
in
model_list
,
"{} is not in lists: {}"
.
format
(
args
.
model
,
model_list
)
assert
args
.
model
in
model_list
,
"{} is not in lists: {}"
.
format
(
args
.
model
,
model_list
)
out
=
model
.
net
(
input
=
image
,
embedding_size
=
args
.
embedding_size
)
if
not
is_train
:
return
None
,
None
,
None
,
out
if
args
.
loss_name
==
"softmax"
:
metricloss
=
SoftmaxLoss
(
class_dim
=
args
.
class_dim
,
)
metricloss
=
SoftmaxLoss
(
class_dim
=
args
.
class_dim
,
)
elif
args
.
loss_name
==
"arcmargin"
:
metricloss
=
ArcMarginLoss
(
class_dim
=
args
.
class_dim
,
margin
=
args
.
arc_margin
,
scale
=
args
.
arc_scale
,
easy_margin
=
args
.
arc_easy_margin
,
)
class_dim
=
args
.
class_dim
,
margin
=
args
.
arc_margin
,
scale
=
args
.
arc_scale
,
easy_margin
=
args
.
arc_easy_margin
,
)
cost
,
logit
=
metricloss
.
loss
(
out
,
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
acc_top1
=
fluid
.
layers
.
accuracy
(
input
=
logit
,
label
=
label
,
k
=
1
)
acc_top5
=
fluid
.
layers
.
accuracy
(
input
=
logit
,
label
=
label
,
k
=
5
)
return
avg_cost
,
acc_top1
,
acc_top5
,
out
def
build_program
(
is_train
,
main_prog
,
startup_prog
,
args
):
image_shape
=
[
int
(
m
)
for
m
in
args
.
image_shape
.
split
(
","
)]
model
=
models
.
__dict__
[
args
.
model
]()
...
...
@@ -119,11 +117,13 @@ def build_program(is_train, main_prog, startup_prog, args):
use_double_buffer
=
True
)
image
,
label
=
fluid
.
layers
.
read_file
(
py_reader
)
else
:
image
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
image
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
with
fluid
.
unique_name
.
guard
():
avg_cost
,
acc_top1
,
acc_top5
,
out
=
net_config
(
image
,
label
,
model
,
args
,
is_train
)
avg_cost
,
acc_top1
,
acc_top5
,
out
=
net_config
(
image
,
label
,
model
,
args
,
is_train
)
if
is_train
:
params
=
model
.
params
params
[
"lr"
]
=
args
.
lr
...
...
@@ -138,7 +138,7 @@ def build_program(is_train, main_prog, startup_prog, args):
"""
if
is_train
:
return
py_reader
,
avg_cost
,
acc_top1
,
acc_top5
,
global_lr
else
:
else
:
return
out
,
image
,
label
...
...
@@ -175,7 +175,9 @@ def train_async(args):
args
=
args
)
test_prog
=
tmp_prog
.
clone
(
for_test
=
True
)
train_fetch_list
=
[
global_lr
.
name
,
train_cost
.
name
,
train_acc1
.
name
,
train_acc5
.
name
]
train_fetch_list
=
[
global_lr
.
name
,
train_cost
.
name
,
train_acc1
.
name
,
train_acc5
.
name
]
test_fetch_list
=
[
test_feas
.
name
]
place
=
fluid
.
CUDAPlace
(
0
)
if
args
.
use_gpu
else
fluid
.
CPUPlace
()
...
...
@@ -196,13 +198,18 @@ def train_async(args):
fluid
.
io
.
load_vars
(
exe
,
pretrained_model
,
main_program
=
train_prog
,
predicate
=
if_exist
)
devicenum
=
get_gpu_num
()
if
args
.
use_gpu
:
devicenum
=
get_gpu_num
()
else
:
devicenum
=
int
(
os
.
environ
.
get
(
'CPU_NUM'
,
1
))
assert
(
args
.
train_batch_size
%
devicenum
)
==
0
train_batch_size
=
args
.
train_batch_size
//
devicenum
test_batch_size
=
args
.
test_batch_size
train_reader
=
paddle
.
batch
(
reader
.
train
(
args
),
batch_size
=
train_batch_size
,
drop_last
=
True
)
test_reader
=
paddle
.
batch
(
reader
.
test
(
args
),
batch_size
=
test_batch_size
,
drop_last
=
False
)
train_reader
=
paddle
.
batch
(
reader
.
train
(
args
),
batch_size
=
train_batch_size
,
drop_last
=
True
)
test_reader
=
paddle
.
batch
(
reader
.
test
(
args
),
batch_size
=
test_batch_size
,
drop_last
=
False
)
test_feeder
=
fluid
.
DataFeeder
(
place
=
place
,
feed_list
=
[
image
,
label
])
train_py_reader
.
decorate_paddle_reader
(
train_reader
)
...
...
@@ -239,12 +246,14 @@ def train_async(args):
train_info
=
[
0
,
0
,
0
,
0
]
totalruntime
+=
period
if
iter_no
%
args
.
test_iter_step
==
0
and
iter_no
!=
0
:
f
,
l
=
[],
[]
for
batch_id
,
data
in
enumerate
(
test_reader
()):
t1
=
time
.
time
()
[
feas
]
=
exe
.
run
(
test_prog
,
fetch_list
=
test_fetch_list
,
feed
=
test_feeder
.
feed
(
data
))
[
feas
]
=
exe
.
run
(
test_prog
,
fetch_list
=
test_fetch_list
,
feed
=
test_feeder
.
feed
(
data
))
label
=
np
.
asarray
([
x
[
1
]
for
x
in
data
])
f
.
append
(
feas
)
l
.
append
(
label
)
...
...
@@ -285,10 +294,10 @@ def initlogging():
logging
.
basicConfig
(
level
=
loglevel
,
# logger.BASIC_FORMAT,
format
=
"%(levelname)s:%(filename)s[%(lineno)s] %(name)s:%(funcName)s->%(message)s"
,
format
=
"%(levelname)s:%(filename)s[%(lineno)s] %(name)s:%(funcName)s->%(message)s"
,
datefmt
=
'%a, %d %b %Y %H:%M:%S'
)
def
main
():
args
=
parser
.
parse_args
()
print_arguments
(
args
)
...
...
PaddleCV/metric_learning/train_pair.py
浏览文件 @
2377b052
...
...
@@ -46,7 +46,7 @@ add_arg('test_batch_size', int, 50, "Minibatch size.")
add_arg
(
'image_shape'
,
str
,
"3,224,224"
,
"input image size"
)
add_arg
(
'class_dim'
,
int
,
11318
,
"Class number."
)
add_arg
(
'lr'
,
float
,
0.0001
,
"set learning rate."
)
add_arg
(
'lr_strategy'
,
str
,
"piecewise_decay"
,
"Set the learning rate decay strategy."
)
add_arg
(
'lr_strategy'
,
str
,
"piecewise_decay"
,
"Set the learning rate decay strategy."
)
add_arg
(
'lr_steps'
,
str
,
"100000"
,
"step of lr"
)
add_arg
(
'total_iter_num'
,
int
,
100000
,
"total_iter_num"
)
add_arg
(
'display_iter_step'
,
int
,
10
,
"display_iter_step."
)
...
...
@@ -64,15 +64,15 @@ add_arg('npairs_reg_lambda', float, 0.01, "npairs reg lambda.")
model_list
=
[
m
for
m
in
dir
(
models
)
if
"__"
not
in
m
]
def
optimizer_setting
(
params
):
ls
=
params
[
"learning_strategy"
]
assert
ls
[
"name"
]
==
"piecewise_decay"
,
\
"learning rate strategy must be {},
\
but got {}"
.
format
(
"piecewise_decay"
,
lr
[
"name"
])
"learning rate strategy must be {}, but got {}"
.
format
(
"piecewise_decay"
,
lr
[
"name"
])
bd
=
[
int
(
e
)
for
e
in
ls
[
"lr_steps"
].
split
(
','
)]
base_lr
=
params
[
"lr"
]
lr
=
[
base_lr
*
(
0.1
**
i
)
for
i
in
range
(
len
(
bd
)
+
1
)]
lr
=
[
base_lr
*
(
0.1
**
i
)
for
i
in
range
(
len
(
bd
)
+
1
)]
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
fluid
.
layers
.
piecewise_decay
(
boundaries
=
bd
,
values
=
lr
),
...
...
@@ -82,38 +82,34 @@ def optimizer_setting(params):
def
net_config
(
image
,
label
,
model
,
args
,
is_train
):
assert
args
.
model
in
model_list
,
"{} is not in lists: {}"
.
format
(
args
.
model
,
model_list
)
assert
args
.
model
in
model_list
,
"{} is not in lists: {}"
.
format
(
args
.
model
,
model_list
)
out
=
model
.
net
(
input
=
image
,
embedding_size
=
args
.
embedding_size
)
if
not
is_train
:
return
None
,
out
if
args
.
loss_name
==
"triplet"
:
metricloss
=
TripletLoss
(
margin
=
args
.
margin
,
)
metricloss
=
TripletLoss
(
margin
=
args
.
margin
,
)
elif
args
.
loss_name
==
"quadruplet"
:
metricloss
=
QuadrupletLoss
(
train_batch_size
=
args
.
train_batch_size
,
samples_each_class
=
args
.
samples_each_class
,
margin
=
args
.
margin
,
)
train_batch_size
=
args
.
train_batch_size
,
samples_each_class
=
args
.
samples_each_class
,
margin
=
args
.
margin
,
)
elif
args
.
loss_name
==
"eml"
:
metricloss
=
EmlLoss
(
train_batch_size
=
args
.
train_batch_size
,
samples_each_class
=
args
.
samples_each_class
,
)
train_batch_size
=
args
.
train_batch_size
,
samples_each_class
=
args
.
samples_each_class
,
)
elif
args
.
loss_name
==
"npairs"
:
metricloss
=
NpairsLoss
(
train_batch_size
=
args
.
train_batch_size
,
samples_each_class
=
args
.
samples_each_class
,
reg_lambda
=
args
.
npairs_reg_lambda
,
)
train_batch_size
=
args
.
train_batch_size
,
samples_each_class
=
args
.
samples_each_class
,
reg_lambda
=
args
.
npairs_reg_lambda
,
)
cost
=
metricloss
.
loss
(
out
,
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
return
avg_cost
,
out
def
build_program
(
is_train
,
main_prog
,
startup_prog
,
args
):
image_shape
=
[
int
(
m
)
for
m
in
args
.
image_shape
.
split
(
","
)]
model
=
models
.
__dict__
[
args
.
model
]()
...
...
@@ -128,7 +124,8 @@ def build_program(is_train, main_prog, startup_prog, args):
use_double_buffer
=
True
)
image
,
label
=
fluid
.
layers
.
read_file
(
py_reader
)
else
:
image
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
image
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
with
fluid
.
unique_name
.
guard
():
...
...
@@ -147,7 +144,7 @@ def build_program(is_train, main_prog, startup_prog, args):
"""
if
is_train
:
return
py_reader
,
avg_cost
,
global_lr
,
out
,
label
else
:
else
:
return
out
,
image
,
label
...
...
@@ -176,7 +173,9 @@ def train_async(args):
args
=
args
)
test_prog
=
tmp_prog
.
clone
(
for_test
=
True
)
train_fetch_list
=
[
global_lr
.
name
,
train_cost
.
name
,
train_feas
.
name
,
train_label
.
name
]
train_fetch_list
=
[
global_lr
.
name
,
train_cost
.
name
,
train_feas
.
name
,
train_label
.
name
]
test_fetch_list
=
[
test_feas
.
name
]
place
=
fluid
.
CUDAPlace
(
0
)
if
args
.
use_gpu
else
fluid
.
CPUPlace
()
...
...
@@ -197,13 +196,18 @@ def train_async(args):
fluid
.
io
.
load_vars
(
exe
,
pretrained_model
,
main_program
=
train_prog
,
predicate
=
if_exist
)
devicenum
=
get_gpu_num
()
if
args
.
use_gpu
:
devicenum
=
get_gpu_num
()
else
:
devicenum
=
int
(
os
.
environ
.
get
(
'CPU_NUM'
,
1
))
assert
(
args
.
train_batch_size
%
devicenum
)
==
0
train_batch_size
=
args
.
train_batch_size
/
devicenum
test_batch_size
=
args
.
test_batch_size
train_reader
=
paddle
.
batch
(
reader
.
train
(
args
),
batch_size
=
train_batch_size
,
drop_last
=
True
)
test_reader
=
paddle
.
batch
(
reader
.
test
(
args
),
batch_size
=
test_batch_size
,
drop_last
=
False
)
train_reader
=
paddle
.
batch
(
reader
.
train
(
args
),
batch_size
=
train_batch_size
,
drop_last
=
True
)
test_reader
=
paddle
.
batch
(
reader
.
test
(
args
),
batch_size
=
test_batch_size
,
drop_last
=
False
)
test_feeder
=
fluid
.
DataFeeder
(
place
=
place
,
feed_list
=
[
image
,
label
])
train_py_reader
.
decorate_paddle_reader
(
train_reader
)
...
...
@@ -238,12 +242,14 @@ def train_async(args):
train_info
=
[
0
,
0
,
0
]
totalruntime
+=
period
if
iter_no
%
args
.
test_iter_step
==
0
and
iter_no
!=
0
:
f
,
l
=
[],
[]
for
batch_id
,
data
in
enumerate
(
test_reader
()):
t1
=
time
.
time
()
[
feas
]
=
exe
.
run
(
test_prog
,
fetch_list
=
test_fetch_list
,
feed
=
test_feeder
.
feed
(
data
))
[
feas
]
=
exe
.
run
(
test_prog
,
fetch_list
=
test_fetch_list
,
feed
=
test_feeder
.
feed
(
data
))
label
=
np
.
asarray
([
x
[
1
]
for
x
in
data
])
f
.
append
(
feas
)
l
.
append
(
label
)
...
...
@@ -270,6 +276,7 @@ def train_async(args):
iter_no
+=
1
def
initlogging
():
for
handler
in
logging
.
root
.
handlers
[:]:
logging
.
root
.
removeHandler
(
handler
)
...
...
@@ -277,10 +284,10 @@ def initlogging():
logging
.
basicConfig
(
level
=
loglevel
,
# logger.BASIC_FORMAT,
format
=
"%(levelname)s:%(filename)s[%(lineno)s] %(name)s:%(funcName)s->%(message)s"
,
format
=
"%(levelname)s:%(filename)s[%(lineno)s] %(name)s:%(funcName)s->%(message)s"
,
datefmt
=
'%a, %d %b %Y %H:%M:%S'
)
def
main
():
args
=
parser
.
parse_args
()
print_arguments
(
args
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录