提交 bca39465 编写于 作者: J jerrywgz

add imagenet model

上级 a2f4ad99
set -e
if [ "x${IMAGENET_USERNAME}" == x -o "x${IMAGENET_ACCESS_KEY}" == x ];then
echo "Please create an account on image-net.org."
echo "It will provide you a pair of username and accesskey to download imagenet data."
read -p "Username: " IMAGENET_USERNAME
read -p "Accesskey: " IMAGENET_ACCESS_KEY
fi
root_url=http://www.image-net.org/challenges/LSVRC/2012/nnoupb
valid_tar=ILSVRC2012_img_val.tar
train_tar=ILSVRC2012_img_train.tar
train_folder=train/
valid_folder=val/
echo "Download imagenet training data..."
mkdir -p ${train_folder}
wget -nd -c ${root_url}/${train_tar}
tar xf ${train_tar} -C ${train_folder}
cd ${train_folder}
for x in `ls *.tar`
do
filename=`basename $x .tar`
mkdir -p $filename
tar -xf $x -C $filename
rm -rf $x
done
cd -
echo "Download imagenet validation data..."
mkdir -p ${valid_folder}
wget -nd -c ${root_url}/${valid_tar}
tar xf ${valid_tar} -C ${valid_folder}
echo "Download imagenet label file: val_list.txt & train_list.txt"
label_file=ImageNet_label.tgz
label_url=http://imagenet-data.bj.bcebos.com/${label_file}
wget -nd -c ${label_url}
tar zxf ${label_file}
...@@ -93,7 +93,8 @@ class Cell(): ...@@ -93,7 +93,8 @@ class Cell():
dropout_implementation='upscale_in_train') dropout_implementation='upscale_in_train')
s = h3 + h4 s = h3 + h4
out += [s] out += [s]
return fluid.layers.concat([out[i] for i in self._concat], axis=1) concat_ = fluid.layers.concat([out[i] for i in self._concat], axis=1, name=name+'concat')
return concat_
def AuxiliaryHeadCIFAR(input, num_classes, aux_name='auxiliary_head'): def AuxiliaryHeadCIFAR(input, num_classes, aux_name='auxiliary_head'):
...@@ -337,7 +338,7 @@ class NetworkCIFAR(object): ...@@ -337,7 +338,7 @@ class NetworkCIFAR(object):
return lrc_loss_mean return lrc_loss_mean
def AuxiliaryHeadImageNet(input, num_classes, aux_name='auxiliary_head'): def AuxiliaryHeadImageNet(input, num_classes, aux_name='auxiliary_head'):
relu_a = fluid.layers.relu(input, inplace=True) relu_a = fluid.layers.relu(input, inplace=False)
#relu_a.persistable = True #relu_a.persistable = True
#print(relu_a) #print(relu_a)
pool_a = fluid.layers.pool2d(relu_a, 5, 'avg', pool_stride=2) pool_a = fluid.layers.pool2d(relu_a, 5, 'avg', pool_stride=2)
...@@ -405,10 +406,11 @@ def Stem0Conv(input, C_out): ...@@ -405,10 +406,11 @@ def Stem0Conv(input, C_out):
bias_attr=ParamAttr( bias_attr=ParamAttr(
initializer=Constant(0.), name='stem0.1.bias'), initializer=Constant(0.), name='stem0.1.bias'),
moving_mean_name='stem0.1.running_mean', moving_mean_name='stem0.1.running_mean',
moving_variance_name='stem0.1.running_var') moving_variance_name='stem0.1.running_var',
relu_a = fluid.layers.relu(bn_a, inplace=True) act='relu')
#relu_a = fluid.layers.relu(bn_a,inplace=True)
conv_b = fluid.layers.conv2d( conv_b = fluid.layers.conv2d(
relu_a, bn_a,
C_out, C_out,
3, 3,
padding=1, padding=1,
...@@ -428,7 +430,7 @@ def Stem0Conv(input, C_out): ...@@ -428,7 +430,7 @@ def Stem0Conv(input, C_out):
return bn_b return bn_b
def Stem1Conv(input, C_out): def Stem1Conv(input, C_out):
relu_a = fluid.layers.relu(input, inplace=True) relu_a = fluid.layers.relu(input,inplace=False)
conv_a = fluid.layers.conv2d( conv_a = fluid.layers.conv2d(
relu_a, relu_a,
C_out, C_out,
......
...@@ -140,7 +140,7 @@ def train(args): ...@@ -140,7 +140,7 @@ def train(args):
:rtype: callable :rtype: callable
""" """
return reader_creator_filepath(args.data, 'train.txt', True) return reader_creator_filepath(args.data, 'debug.txt', True)
def test(args): def test(args):
......
...@@ -163,14 +163,19 @@ def train(model, args, im_shape, steps_one_epoch, num_gpu): ...@@ -163,14 +163,19 @@ def train(model, args, im_shape, steps_one_epoch, num_gpu):
# return os.path.exists(os.path.join(args.pretrained_model, var.name)) # return os.path.exists(os.path.join(args.pretrained_model, var.name))
# fluid.io.load_vars(exe, args.pretrained_model, main_program=train_prog, predicate=if_exist) # fluid.io.load_vars(exe, args.pretrained_model, main_program=train_prog, predicate=if_exist)
#build_strategy = fluid.BuildStrategy()
#build_strategy.enable_inplace = False
#build_strategy.memory_optimize = False
train_fetch_list = [loss_train]
fluid.memory_optimize(train_prog, skip_opt_set=set(train_fetch_list))
exec_strategy = fluid.ExecutionStrategy() exec_strategy = fluid.ExecutionStrategy()
exec_strategy.num_threads = 1 #exec_strategy.num_threads = 1
train_exe = fluid.ParallelExecutor( train_exe = fluid.ParallelExecutor(
main_program=train_prog, main_program=train_prog,
use_cuda=True, use_cuda=True,
loss_name=loss_train.name, loss_name=loss_train.name,
exec_strategy=exec_strategy) exec_strategy=exec_strategy)
train_batch_size = args.batch_size train_batch_size = args.batch_size
test_batch_size = 256 test_batch_size = 256
...@@ -182,8 +187,7 @@ def train(model, args, im_shape, steps_one_epoch, num_gpu): ...@@ -182,8 +187,7 @@ def train(model, args, im_shape, steps_one_epoch, num_gpu):
test_py_reader.decorate_paddle_reader(test_reader) test_py_reader.decorate_paddle_reader(test_reader)
fluid.clip.set_gradient_clip(fluid.clip.GradientClipByGlobalNorm(args.grad_clip), program=train_prog) fluid.clip.set_gradient_clip(fluid.clip.GradientClipByGlobalNorm(args.grad_clip), program=train_prog)
train_fetch_list = [loss_train] print(train_prog.to_string(True))
fluid.memory_optimize(train_prog, skip_opt_set=set(train_fetch_list))
def save_model(postfix, main_prog): def save_model(postfix, main_prog):
model_path = os.path.join(args.save_model_path, postfix) model_path = os.path.join(args.save_model_path, postfix)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册