Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
05cd6ce3
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看板
未验证
提交
05cd6ce3
编写于
1月 18, 2021
作者:
Z
Zhen Wang
提交者:
GitHub
1月 18, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add new pure fp16 training for ResNet50. (#5047)
上级
7a0bff7c
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
51 addition
and
71 deletion
+51
-71
PaddleCV/image_classification/build_model.py
PaddleCV/image_classification/build_model.py
+26
-39
PaddleCV/image_classification/dali.py
PaddleCV/image_classification/dali.py
+1
-1
PaddleCV/image_classification/scripts/train/ResNet50_fp16.sh
PaddleCV/image_classification/scripts/train/ResNet50_fp16.sh
+2
-7
PaddleCV/image_classification/train.py
PaddleCV/image_classification/train.py
+14
-5
PaddleCV/image_classification/utils/optimizer.py
PaddleCV/image_classification/utils/optimizer.py
+6
-13
PaddleCV/image_classification/utils/utility.py
PaddleCV/image_classification/utils/utility.py
+2
-6
未找到文件。
PaddleCV/image_classification/build_model.py
浏览文件 @
05cd6ce3
...
...
@@ -37,20 +37,18 @@ def _calc_label_smoothing_loss(softmax_out, label, class_dim, epsilon):
def
_basic_model
(
data
,
model
,
args
,
is_train
):
image
=
data
[
0
]
label
=
data
[
1
]
if
args
.
model
in
AMP_MODEL_LIST
:
image_data
=
(
fluid
.
layers
.
cast
(
image
,
'float16'
)
if
args
.
use_pure_fp16
and
not
args
.
use_dali
else
image
)
with
paddle
.
static
.
amp
.
fp16_guard
():
image_transpose
=
fluid
.
layers
.
transpose
(
image_data
,
[
0
,
2
,
3
,
1
])
if
args
.
data_format
==
'NHWC'
else
image_data
image
,
[
0
,
2
,
3
,
1
])
if
args
.
data_format
==
'NHWC'
else
image
image_transpose
.
stop_gradient
=
image
.
stop_gradient
net_out
=
model
.
net
(
input
=
image_transpose
,
class_dim
=
args
.
class_dim
,
data_format
=
args
.
data_format
)
else
:
net_out
=
model
.
net
(
input
=
image
,
class_dim
=
args
.
class_dim
)
if
args
.
use_pure_fp16
:
net_out
=
fluid
.
layers
.
cast
(
x
=
net_out
,
dtype
=
"float32"
)
softmax_out
=
fluid
.
layers
.
softmax
(
net_out
,
use_cudnn
=
False
)
if
is_train
and
args
.
use_label_smoothing
:
...
...
@@ -59,12 +57,11 @@ def _basic_model(data, model, args, is_train):
else
:
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
softmax_out
,
label
=
label
)
target_cost
=
(
fluid
.
layers
.
reduce_sum
(
cost
)
if
args
.
use_pure_fp16
else
fluid
.
layers
.
mean
(
cost
))
avg_cost
=
fluid
.
layers
.
mean
(
cost
)
acc_top1
=
fluid
.
layers
.
accuracy
(
input
=
softmax_out
,
label
=
label
,
k
=
1
)
acc_top5
=
fluid
.
layers
.
accuracy
(
input
=
softmax_out
,
label
=
label
,
k
=
min
(
5
,
args
.
class_dim
))
return
[
target
_cost
,
acc_top1
,
acc_top5
]
return
[
avg
_cost
,
acc_top1
,
acc_top5
]
def
_googlenet_model
(
data
,
model
,
args
,
is_train
):
...
...
@@ -103,22 +100,18 @@ def _mixup_model(data, model, args, is_train):
lam
=
data
[
3
]
if
args
.
model
in
AMP_MODEL_LIST
:
image_data
=
(
fluid
.
layers
.
cast
(
image
,
'float16'
)
if
args
.
use_pure_fp16
and
not
args
.
use_dali
else
image
)
with
paddle
.
static
.
amp
.
fp16_guard
():
image_transpose
=
fluid
.
layers
.
transpose
(
image_data
,
[
0
,
2
,
3
,
1
])
if
args
.
data_format
==
'NHWC'
else
image_data
image
,
[
0
,
2
,
3
,
1
])
if
args
.
data_format
==
'NHWC'
else
image
image_transpose
.
stop_gradient
=
image
.
stop_gradient
net_out
=
model
.
net
(
input
=
image_transpose
,
class_dim
=
args
.
class_dim
,
data_format
=
args
.
data_format
)
else
:
net_out
=
model
.
net
(
input
=
image
,
class_dim
=
args
.
class_dim
)
if
args
.
use_pure_fp16
:
net_out_fp32
=
fluid
.
layers
.
cast
(
x
=
net_out
,
dtype
=
"float32"
)
softmax_out
=
fluid
.
layers
.
softmax
(
net_out_fp32
,
use_cudnn
=
False
)
else
:
softmax_out
=
fluid
.
layers
.
softmax
(
net_out
,
use_cudnn
=
False
)
if
not
args
.
use_label_smoothing
:
loss_a
=
fluid
.
layers
.
cross_entropy
(
input
=
softmax_out
,
label
=
y_a
)
loss_b
=
fluid
.
layers
.
cross_entropy
(
input
=
softmax_out
,
label
=
y_b
)
...
...
@@ -128,17 +121,11 @@ def _mixup_model(data, model, args, is_train):
loss_b
=
_calc_label_smoothing_loss
(
softmax_out
,
y_b
,
args
.
class_dim
,
args
.
label_smoothing_epsilon
)
if
args
.
use_pure_fp16
:
target_loss_a
=
fluid
.
layers
.
reduce_sum
(
x
=
loss_a
)
target_loss_b
=
fluid
.
layers
.
reduce_sum
(
x
=
loss_b
)
cost
=
lam
*
target_loss_a
+
(
1
-
lam
)
*
target_loss_b
target_cost
=
fluid
.
layers
.
reduce_sum
(
x
=
cost
)
else
:
target_loss_a
=
fluid
.
layers
.
mean
(
x
=
loss_a
)
target_loss_b
=
fluid
.
layers
.
mean
(
x
=
loss_b
)
cost
=
lam
*
target_loss_a
+
(
1
-
lam
)
*
target_loss_b
target_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
return
[
target_cost
]
loss_a_mean
=
fluid
.
layers
.
mean
(
x
=
loss_a
)
loss_b_mean
=
fluid
.
layers
.
mean
(
x
=
loss_b
)
cost
=
lam
*
loss_a_mean
+
(
1
-
lam
)
*
loss_b_mean
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
return
[
avg_cost
]
def
create_model
(
model
,
args
,
is_train
):
...
...
PaddleCV/image_classification/dali.py
浏览文件 @
05cd6ce3
...
...
@@ -165,7 +165,7 @@ def build(settings, mode='train'):
min_area
=
settings
.
lower_scale
lower
=
settings
.
lower_ratio
upper
=
settings
.
upper_ratio
output_dtype
=
types
.
FLOAT16
if
settings
.
use_pure_fp16
else
types
.
FLOAT
output_dtype
=
types
.
FLOAT16
if
(
settings
.
use_amp
and
settings
.
use_pure_fp16
)
else
types
.
FLOAT
interp
=
settings
.
interpolation
or
1
# default to linear
interp_map
=
{
...
...
PaddleCV/image_classification/scripts/train/ResNet50_fp16.sh
浏览文件 @
05cd6ce3
...
...
@@ -4,13 +4,10 @@ export FLAGS_conv_workspace_size_limit=4000 #MB
export
FLAGS_cudnn_exhaustive_search
=
1
export
FLAGS_cudnn_batchnorm_spatial_persistent
=
1
DATA_DIR
=
"Your image dataset path, e.g. /work/datasets/ILSVRC2012/"
DATA_FORMAT
=
"NHWC"
USE_AMP
=
true
#whether to use amp
USE_PURE_FP16
=
false
MULTI_PRECISION
=
${
USE_PURE_FP16
}
USE_PURE_FP16
=
true
USE_DALI
=
true
USE_ADDTO
=
true
...
...
@@ -34,7 +31,6 @@ python train.py \
--lr_strategy
=
piecewise_decay
\
--use_amp
=
${
USE_AMP
}
\
--use_pure_fp16
=
${
USE_PURE_FP16
}
\
--multi_precision
=
${
MULTI_PRECISION
}
\
--scale_loss
=
128.0
\
--use_dynamic_loss_scaling
=
true
\
--data_format
=
${
DATA_FORMAT
}
\
...
...
@@ -48,6 +44,5 @@ python train.py \
--reader_thread
=
10
\
--reader_buf_size
=
4000
\
--use_dali
=
${
USE_DALI
}
\
--lr
=
0.1
\
--random_seed
=
2020
--lr
=
0.1
PaddleCV/image_classification/train.py
浏览文件 @
05cd6ce3
...
...
@@ -75,6 +75,7 @@ def build_program(is_train, main_prog, startup_prog, args):
use_se
=
use_se
)
else
:
model
=
models
.
__dict__
[
args
.
model
]()
optimizer
=
None
with
fluid
.
program_guard
(
main_prog
,
startup_prog
):
if
args
.
random_seed
or
args
.
enable_ce
:
main_prog
.
random_seed
=
args
.
random_seed
...
...
@@ -91,10 +92,12 @@ def build_program(is_train, main_prog, startup_prog, args):
loss_out
.
append
(
global_lr
)
if
args
.
use_amp
:
optimizer
=
fluid
.
contrib
.
mixed_precision
.
decorate
(
optimizer
=
paddle
.
static
.
amp
.
decorate
(
optimizer
,
init_loss_scaling
=
args
.
scale_loss
,
use_dynamic_loss_scaling
=
args
.
use_dynamic_loss_scaling
)
use_dynamic_loss_scaling
=
args
.
use_dynamic_loss_scaling
,
use_pure_fp16
=
args
.
use_pure_fp16
,
use_fp16_guard
=
True
)
optimizer
.
minimize
(
avg_cost
)
if
args
.
use_ema
:
...
...
@@ -105,7 +108,7 @@ def build_program(is_train, main_prog, startup_prog, args):
ema
.
update
()
loss_out
.
append
(
ema
)
loss_out
.
append
(
data_loader
)
return
loss_out
return
loss_out
,
optimizer
def
validate
(
args
,
...
...
@@ -178,7 +181,7 @@ def train(args):
"""
startup_prog
=
fluid
.
Program
()
train_prog
=
fluid
.
Program
()
train_out
=
build_program
(
train_out
,
optimizer
=
build_program
(
is_train
=
True
,
main_prog
=
train_prog
,
startup_prog
=
startup_prog
,
...
...
@@ -194,7 +197,7 @@ def train(args):
if
args
.
validate
:
test_prog
=
fluid
.
Program
()
test_out
=
build_program
(
test_out
,
_
=
build_program
(
is_train
=
False
,
main_prog
=
test_prog
,
startup_prog
=
startup_prog
,
...
...
@@ -216,6 +219,12 @@ def train(args):
#init model by checkpoint or pretrianed model.
init_model
(
exe
,
args
,
train_prog
)
if
args
.
use_amp
:
optimizer
.
amp_init
(
place
,
scope
=
paddle
.
static
.
global_scope
(),
test_program
=
test_prog
if
args
.
validate
else
None
)
num_trainers
=
int
(
os
.
environ
.
get
(
'PADDLE_TRAINERS_NUM'
,
1
))
if
args
.
use_dali
:
import
dali
...
...
PaddleCV/image_classification/utils/optimizer.py
浏览文件 @
05cd6ce3
...
...
@@ -160,9 +160,7 @@ class Optimizer(object):
self
.
decay_epochs
=
args
.
decay_epochs
self
.
decay_rate
=
args
.
decay_rate
self
.
total_images
=
args
.
total_images
self
.
multi_precision
=
args
.
multi_precision
self
.
rescale_grad
=
(
1.0
/
(
args
.
batch_size
/
len
(
fluid
.
cuda_places
()))
if
args
.
use_pure_fp16
else
1.0
)
self
.
multi_precision
=
args
.
use_pure_fp16
self
.
step
=
int
(
math
.
ceil
(
float
(
self
.
total_images
)
/
self
.
batch_size
))
...
...
@@ -179,8 +177,7 @@ class Optimizer(object):
learning_rate
=
learning_rate
,
momentum
=
self
.
momentum_rate
,
regularization
=
fluid
.
regularizer
.
L2Decay
(
self
.
l2_decay
),
multi_precision
=
self
.
multi_precision
,
rescale_grad
=
self
.
rescale_grad
)
multi_precision
=
self
.
multi_precision
)
return
optimizer
def
cosine_decay
(
self
):
...
...
@@ -198,8 +195,7 @@ class Optimizer(object):
learning_rate
=
learning_rate
,
momentum
=
self
.
momentum_rate
,
regularization
=
fluid
.
regularizer
.
L2Decay
(
self
.
l2_decay
),
multi_precision
=
self
.
multi_precision
,
rescale_grad
=
self
.
rescale_grad
)
multi_precision
=
self
.
multi_precision
)
return
optimizer
def
cosine_decay_warmup
(
self
):
...
...
@@ -218,8 +214,7 @@ class Optimizer(object):
learning_rate
=
learning_rate
,
momentum
=
self
.
momentum_rate
,
regularization
=
fluid
.
regularizer
.
L2Decay
(
self
.
l2_decay
),
multi_precision
=
self
.
multi_precision
,
rescale_grad
=
self
.
rescale_grad
)
multi_precision
=
self
.
multi_precision
)
return
optimizer
def
exponential_decay_warmup
(
self
):
...
...
@@ -257,8 +252,7 @@ class Optimizer(object):
learning_rate
=
learning_rate
,
momentum
=
self
.
momentum_rate
,
regularization
=
fluid
.
regularizer
.
L2Decay
(
self
.
l2_decay
),
multi_precision
=
self
.
multi_precision
,
rescale_grad
=
self
.
rescale_grad
)
multi_precision
=
self
.
multi_precision
)
return
optimizer
...
...
@@ -301,8 +295,7 @@ class Optimizer(object):
learning_rate
=
self
.
lr
,
momentum
=
self
.
momentum_rate
,
regularization
=
fluid
.
regularizer
.
L2Decay
(
self
.
l2_decay
),
multi_precision
=
self
.
multi_precision
,
rescale_grad
=
self
.
rescale_grad
)
multi_precision
=
self
.
multi_precision
)
return
optimizer
...
...
PaddleCV/image_classification/utils/utility.py
浏览文件 @
05cd6ce3
...
...
@@ -141,7 +141,6 @@ def parse_args():
add_arg
(
'validate'
,
bool
,
True
,
"whether to validate when training."
)
add_arg
(
'use_amp'
,
bool
,
False
,
"Whether to enable mixed precision training with fp16."
)
add_arg
(
'use_pure_fp16'
,
bool
,
False
,
"Whether to enable all half precision training with fp16."
)
add_arg
(
'multi_precision'
,
bool
,
False
,
"Whether to enable multi-precision training with fp16."
)
add_arg
(
'scale_loss'
,
float
,
1.0
,
"The value of scale_loss for fp16."
)
add_arg
(
'use_dynamic_loss_scaling'
,
bool
,
True
,
"Whether to use dynamic loss scaling."
)
add_arg
(
'data_format'
,
str
,
"NCHW"
,
"Tensor data format when training."
)
...
...
@@ -379,13 +378,10 @@ def create_data_loader(is_train, args):
data_loader and the input data of net,
"""
image_shape
=
args
.
image_shape
image_dtype
=
"float32"
if
args
.
model
==
"ResNet50"
and
args
.
use_pure_fp16
and
args
.
use_dali
:
image_dtype
=
"float16"
feed_image
=
fluid
.
data
(
name
=
"feed_image"
,
shape
=
[
None
]
+
image_shape
,
dtype
=
image_dtype
,
dtype
=
"float32"
,
lod_level
=
0
)
feed_label
=
fluid
.
data
(
...
...
@@ -399,7 +395,7 @@ def create_data_loader(is_train, args):
feed_y_b
=
fluid
.
data
(
name
=
"feed_y_b"
,
shape
=
[
None
,
1
],
dtype
=
"int64"
,
lod_level
=
0
)
feed_lam
=
fluid
.
data
(
name
=
"feed_lam"
,
shape
=
[
None
,
1
],
dtype
=
image_dtype
,
lod_level
=
0
)
name
=
"feed_lam"
,
shape
=
[
None
,
1
],
dtype
=
"float32"
,
lod_level
=
0
)
data_loader
=
fluid
.
io
.
DataLoader
.
from_generator
(
feed_list
=
[
feed_image
,
feed_y_a
,
feed_y_b
,
feed_lam
],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录