未验证 提交 334ad371 编写于 作者: Z zhaoyuchen2018 提交者: GitHub

Transform benchmark resnext50 changing to models (#2412)

This changing can improve performance

test=develop
上级 83b367d7
...@@ -69,7 +69,8 @@ class SE_ResNeXt(): ...@@ -69,7 +69,8 @@ class SE_ResNeXt():
pool_size=3, pool_size=3,
pool_stride=2, pool_stride=2,
pool_padding=1, pool_padding=1,
pool_type='max') pool_type='max',
use_cudnn=False)
elif layers == 101: elif layers == 101:
cardinality = 32 cardinality = 32
reduction_ratio = 16 reduction_ratio = 16
...@@ -88,7 +89,8 @@ class SE_ResNeXt(): ...@@ -88,7 +89,8 @@ class SE_ResNeXt():
pool_size=3, pool_size=3,
pool_stride=2, pool_stride=2,
pool_padding=1, pool_padding=1,
pool_type='max') pool_type='max',
use_cudnn=False)
elif layers == 152: elif layers == 152:
cardinality = 64 cardinality = 64
reduction_ratio = 16 reduction_ratio = 16
...@@ -118,7 +120,7 @@ class SE_ResNeXt(): ...@@ -118,7 +120,7 @@ class SE_ResNeXt():
name='conv3') name='conv3')
conv = fluid.layers.pool2d( conv = fluid.layers.pool2d(
input=conv, pool_size=3, pool_stride=2, pool_padding=1, \ input=conv, pool_size=3, pool_stride=2, pool_padding=1, \
pool_type='max') pool_type='max', use_cudnn=False)
n = 1 if layers == 50 or layers == 101 else 3 n = 1 if layers == 50 or layers == 101 else 3
for block in range(len(depth)): for block in range(len(depth)):
n += 1 n += 1
...@@ -132,7 +134,11 @@ class SE_ResNeXt(): ...@@ -132,7 +134,11 @@ class SE_ResNeXt():
name=str(n) + '_' + str(i + 1)) name=str(n) + '_' + str(i + 1))
pool = fluid.layers.pool2d( pool = fluid.layers.pool2d(
input=conv, pool_size=7, pool_type='avg', global_pooling=True) input=conv,
pool_size=7,
pool_type='avg',
global_pooling=True,
use_cudnn=False)
drop = fluid.layers.dropout( drop = fluid.layers.dropout(
x=pool, dropout_prob=0.5, seed=self.params['dropout_seed']) x=pool, dropout_prob=0.5, seed=self.params['dropout_seed'])
stdv = 1.0 / math.sqrt(drop.shape[1] * 1.0) stdv = 1.0 / math.sqrt(drop.shape[1] * 1.0)
...@@ -224,7 +230,11 @@ class SE_ResNeXt(): ...@@ -224,7 +230,11 @@ class SE_ResNeXt():
reduction_ratio, reduction_ratio,
name=None): name=None):
pool = fluid.layers.pool2d( pool = fluid.layers.pool2d(
input=input, pool_size=0, pool_type='avg', global_pooling=True) input=input,
pool_size=0,
pool_type='avg',
global_pooling=True,
use_cudnn=False)
stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0) stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0)
squeeze = fluid.layers.fc( squeeze = fluid.layers.fc(
input=pool, input=pool,
......
...@@ -22,10 +22,23 @@ import time ...@@ -22,10 +22,23 @@ import time
import sys import sys
import functools import functools
import math import math
def set_paddle_flags(flags):
for key, value in flags.items():
if os.environ.get(key, None) is None:
os.environ[key] = str(value)
# NOTE(paddle-dev): All of these flags should be
# set before `import paddle`. Otherwise, it would
# not take any effect.
set_paddle_flags({
'FLAGS_eager_delete_tensor_gb': 0, # enable gc
'FLAGS_fraction_of_gpu_memory_to_use': 0.98
})
import argparse import argparse
import functools import functools
import subprocess import subprocess
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.dataset.flowers as flowers import paddle.dataset.flowers as flowers
...@@ -50,6 +63,7 @@ add_arg('class_dim', int, 1000, "Class number.") ...@@ -50,6 +63,7 @@ add_arg('class_dim', int, 1000, "Class number.")
add_arg('image_shape', str, "3,224,224", "input image size") add_arg('image_shape', str, "3,224,224", "input image size")
add_arg('model_save_dir', str, "output", "model save directory") add_arg('model_save_dir', str, "output", "model save directory")
add_arg('with_mem_opt', bool, True, "Whether to use memory optimization or not.") add_arg('with_mem_opt', bool, True, "Whether to use memory optimization or not.")
add_arg('with_inplace', bool, True, "Whether to use inplace memory optimization.")
add_arg('pretrained_model', str, None, "Whether to use pretrained model.") add_arg('pretrained_model', str, None, "Whether to use pretrained model.")
add_arg('checkpoint', str, None, "Whether to resume checkpoint.") add_arg('checkpoint', str, None, "Whether to resume checkpoint.")
add_arg('lr', float, 0.1, "set learning rate.") add_arg('lr', float, 0.1, "set learning rate.")
...@@ -412,10 +426,20 @@ def train(args): ...@@ -412,10 +426,20 @@ def train(args):
# use_ngraph is for CPU only, please refer to README_ngraph.md for details # use_ngraph is for CPU only, please refer to README_ngraph.md for details
use_ngraph = os.getenv('FLAGS_use_ngraph') use_ngraph = os.getenv('FLAGS_use_ngraph')
if not use_ngraph: if not use_ngraph:
build_strategy = fluid.BuildStrategy()
build_strategy.memory_optimize = args.with_mem_opt
build_strategy.enable_inplace = args.with_inplace
build_strategy.fuse_all_reduce_ops=1
exec_strategy = fluid.ExecutionStrategy()
exec_strategy.num_iteration_per_drop_scope = 10
train_exe = fluid.ParallelExecutor( train_exe = fluid.ParallelExecutor(
main_program=train_prog, main_program=train_prog,
use_cuda=bool(args.use_gpu), use_cuda=bool(args.use_gpu),
loss_name=train_cost.name) loss_name=train_cost.name,
build_strategy=build_strategy,
exec_strategy=exec_strategy)
else: else:
train_exe = exe train_exe = exe
...@@ -429,6 +453,7 @@ def train(args): ...@@ -429,6 +453,7 @@ def train(args):
test_info = [[], [], []] test_info = [[], [], []]
train_time = [] train_time = []
batch_id = 0 batch_id = 0
time_record=[]
try: try:
while True: while True:
t1 = time.time() t1 = time.time()
...@@ -450,6 +475,7 @@ def train(args): ...@@ -450,6 +475,7 @@ def train(args):
t2 = time.time() t2 = time.time()
period = t2 - t1 period = t2 - t1
time_record.append(period)
loss = np.mean(np.array(loss)) loss = np.mean(np.array(loss))
train_info[0].append(loss) train_info[0].append(loss)
...@@ -457,6 +483,8 @@ def train(args): ...@@ -457,6 +483,8 @@ def train(args):
train_time.append(period) train_time.append(period)
if batch_id % 10 == 0: if batch_id % 10 == 0:
period = np.mean(time_record)
time_record=[]
if use_mixup: if use_mixup:
print("Pass {0}, trainbatch {1}, loss {2}, lr {3}, time {4}" print("Pass {0}, trainbatch {1}, loss {2}, lr {3}, time {4}"
.format(pass_id, batch_id, "%.5f"%loss, "%.5f" %lr, "%2.2f sec" % period)) .format(pass_id, batch_id, "%.5f"%loss, "%.5f" %lr, "%2.2f sec" % period))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册