Skip to content

Commit

Permalink
add moving average
Browse files Browse the repository at this point in the history
  • Loading branch information
dengdan committed Jul 8, 2017
1 parent 9fc4924 commit 6a33bca
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 13 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,4 +1,3 @@
scripts/*.sh
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
39 changes: 31 additions & 8 deletions eval_seglink.py
Expand Up @@ -19,24 +19,35 @@
# model threshold parameters
# =========================================================================== #
tf.app.flags.DEFINE_string('train_with_ignored', False,
'whether to use ignored bbox (in ic15) in training.')
'whether to use ignored bbox (in ic15) in training.')
tf.app.flags.DEFINE_boolean('do_grid_search', False,
'whether to do grid search to find a best combinations of seg_conf_threshold and link_conf_threshold.')
tf.app.flags.DEFINE_float('seg_loc_loss_weight', 1.0, 'the loss weight of segment localization')
tf.app.flags.DEFINE_float('link_cls_loss_weight', 1.0, 'the loss weight of linkage classification loss')
'whether to do grid search to find a best combinations of \
seg_conf_threshold and link_conf_threshold.')
tf.app.flags.DEFINE_float('seg_loc_loss_weight', 1.0,
'the loss weight of segment localization')
tf.app.flags.DEFINE_float('link_cls_loss_weight', 1.0,
'the loss weight of linkage classification loss')

tf.app.flags.DEFINE_float('seg_conf_threshold', 0.9,
'the threshold on the confidence of segment')
'the threshold on the confidence of segment')
tf.app.flags.DEFINE_float('link_conf_threshold', 0.7,
'the threshold on the confidence of linkage')
'the threshold on the confidence of linkage')


# =========================================================================== #
# Checkpoint and running Flags
# =========================================================================== #
tf.app.flags.DEFINE_string('checkpoint_path', None,
'the path of checkpoint to be evaluated. If it is a directory containing many checkpoints, the lastest will be evaluated.')
tf.app.flags.DEFINE_float('gpu_memory_fraction', 0.1, 'the gpu memory fraction to be used. If less than 0, allow_growth = True is used.')
'the path of checkpoint to be evaluated. \
If it is a directory containing many checkpoints, \
the lastest will be evaluated.')
tf.app.flags.DEFINE_float('gpu_memory_fraction', 0.1,
'the gpu memory fraction to be used. \
If less than 0, allow_growth = True is used.')
tf.app.flags.DEFINE_bool('using_moving_average', False,
'Whether to use ExponentionalMovingAverage')
tf.app.flags.DEFINE_float('moving_average_decay', 0.9999,
'The decay rate of ExponentionalMovingAverage')

# =========================================================================== #
# I/O and preprocessing Flags.
Expand Down Expand Up @@ -218,18 +229,30 @@ def eval(dataset):
elif FLAGS.gpu_memory_fraction > 0:
sess_config.gpu_options.per_process_gpu_memory_fraction = FLAGS.gpu_memory_fraction;

# Variables to restore: moving avg. or normal weights.
if FLAGS.using_moving_average:
variable_averages = tf.train.ExponentialMovingAverage(
FLAGS.moving_average_decay)
variables_to_restore = variable_averages.variables_to_restore(
slim.get_model_variables())
variables_to_restore[global_step.op.name] = global_step
else:
variables_to_restore = slim.get_variables_to_restore()

if util.io.is_dir(FLAGS.checkpoint_path):
slim.evaluation.evaluation_loop(
master = '',
eval_op=list(names_to_updates.values()),
num_evals=dataset.num_samples,
variables_to_restore=variables_to_restore,
checkpoint_dir = checkpoint_dir,
logdir = logdir,
session_config=sess_config)
else:
slim.evaluation.evaluate_once(
master = '',
eval_op=list(names_to_updates.values()),
variables_to_restore=variables_to_restore,
num_evals=2,#dataset.num_samples,
checkpoint_path = FLAGS.checkpoint_path,
logdir = logdir,
Expand Down
2 changes: 1 addition & 1 deletion push.sh
@@ -1,4 +1,4 @@
git add . --all
git commit -m "add train without ignored gt."
git commit -m "add moving average"
git push -u origin master

45 changes: 45 additions & 0 deletions scripts/eval.sh
@@ -0,0 +1,45 @@
set -x
set -e
# ./scripts/eval.sh 1 icdar2013 train 384 384 ckpt
# ./scripts/eval.sh 1 icdar2013 train 512 512 ckpt

export CUDA_VISIBLE_DEVICES=$1
DATASET=$2
SPLIT=$3
WIDTH=$4
HEIGHT=$5
CHECKPOINT_PATH=$6

if [ $DATASET == 'synthtext' ]
then
DATA_PATH=SynthText
elif [ $DATASET == 'scut' ]
then
DATA_PATH=SCUT
elif [ $DATASET == 'icdar2013' ]
then
DATA_PATH=ICDAR
elif [ $DATASET == 'icdar2015' ]
then
DATA_PATH=ICDAR
else
echo invalid dataset: $DATASET
exit
fi

DATASET_DIR=$HOME/dataset/SSD-tf/${DATA_PATH}

python eval_seglink.py \
--checkpoint_path=${CHECKPOINT_PATH} \
--dataset_dir=${DATASET_DIR} \
--dataset_name=${DATASET} \
--dataset_split_name=$SPLIT \
--eval_image_width=${WIDTH} \
--eval_image_height=${HEIGHT} \
--gpu_memory_fraction=0.4 \
--do_grid_search=$7 \
--using_moving_average=0




22 changes: 22 additions & 0 deletions scripts/test.sh
@@ -0,0 +1,22 @@
set -x
set -e
# ./scripts/test.sh 1 icdar2013 train 384 384 ckpt
# ./scripts/test.sh 1 icdar2013 train 512 512 ckpt
# ./scripts/test.sh 1 icdar2015 train 1280 768 ckpt

export CUDA_VISIBLE_DEVICES=$1
CHECKPOINT_PATH=$2



python test_seglink.py \
--checkpoint_path=${CHECKPOINT_PATH} \
--gpu_memory_fraction=-1 \
--seg_conf_threshold=0.8 \
--link_conf_threshold=0.5






57 changes: 57 additions & 0 deletions scripts/train.sh
@@ -0,0 +1,57 @@
set -x
set -e
# ./scripts/train.sh 0 18 synthtext
export CUDA_VISIBLE_DEVICES=$1
IMG_PER_GPU=$2
DATASET=$3

CHKPT_PATH=${HOME}/models/seglink/seglink_synthtext
TRAIN_DIR=${HOME}/models/seglink/seglink_icdar2015_384
#TRAIN_DIR=${HOME}/temp/no-use/seglink/seglink_icdar2015_384
#CHKPT_PATH=${HOME}/models/ssd-pretrain/seglink

# get the number of gpus
OLD_IFS="$IFS"
IFS=","
gpus=($CUDA_VISIBLE_DEVICES)
IFS="$OLD_IFS"
NUM_GPUS=${#gpus[@]}

# batch_size = num_gpus * IMG_PER_GPU
BATCH_SIZE=`expr $NUM_GPUS \* $IMG_PER_GPU`

#dataset
if [ $DATASET == 'synthtext' ]
then
DATA_PATH=SynthText
elif [ $DATASET == 'scut' ]
then
DATA_PATH=SCUT
elif [ $DATASET == 'icdar2013' ]
then
DATA_PATH=ICDAR
elif [ $DATASET == 'icdar2015' ]
then
DATA_PATH=ICDAR
else
echo invalid dataset: $DATASET
exit
fi

DATASET_DIR=$HOME/dataset/SSD-tf/${DATA_PATH}

python train_seglink.py \
--train_dir=${TRAIN_DIR} \
--num_gpus=${NUM_GPUS} \
--learning_rate=0.0001 \
--gpu_memory_fraction=-1 \
--train_image_width=384 \
--train_image_height=384 \
--batch_size=${BATCH_SIZE}\
--dataset_dir=${DATASET_DIR} \
--dataset_name=${DATASET} \
--dataset_split_name=train \
--train_with_ignored=0 \
--checkpoint_path=${CHKPT_PATH} \
--using_moving_average=0

19 changes: 19 additions & 0 deletions scripts/vis.sh
@@ -0,0 +1,19 @@
#python visualize_detection_result.py \
# --image=~/dataset/ICDAR2015/Challenge2.Task123/Challenge2_Test_Task12_Images/ \
# --gt=~/dataset/ICDAR2015/Challenge2.Task123/Challenge2_Test_Task1_GT/ \
# --det=~/temp/no-use/seglink_debug_icdar2013/eval/icdar2013_test/model.ckpt-48176/txt/ \
# --output=~/temp/no-use/seglink_result

#python visualize_detection_result.py \
# --image=~/dataset/ICDAR2015/Challenge2.Task123/Challenge2_Training_Task12_Images/ \
# --gt=~/dataset/ICDAR2015/Challenge2.Task123/Challenge2_Training_Task1_GT/ \
# --det=~/temp/no-use/seglink_debug_icdar2013/eval/icdar2013_train/model.ckpt-48176/txt/ \
# --output=~/temp/no-use/seglink_result


python visualize_detection_result.py \
--image=~/dataset/ICDAR2015/Challenge4/ch4_training_images/ \
--gt=~/dataset/ICDAR2015/Challenge4/ch4_training_localization_transcription_gt/ \
--det=~/models/seglink/seglink_icdar2015_without_ignored/eval/icdar2015_train/model.ckpt-72885/seg_link_conf_th_0.900000_0.700000/txt \
--output=~/temp/no-use/seglink_result_512_train

19 changes: 16 additions & 3 deletions train_seglink.py
Expand Up @@ -44,7 +44,8 @@
tf.app.flags.DEFINE_float('learning_rate', 0.001, 'learning rate.')
tf.app.flags.DEFINE_float('momentum', 0.9, 'The momentum for the MomentumOptimizer')
tf.app.flags.DEFINE_float('weight_decay', 0.0005, 'The weight decay on the model weights.')

tf.app.flags.DEFINE_bool('using_moving_average', False, 'Whether to use ExponentionalMovingAverage')
tf.app.flags.DEFINE_float('moving_average_decay', 0.9999, 'The decay rate of ExponentionalMovingAverage')

# =========================================================================== #
# I/O and preprocessing Flags.
Expand Down Expand Up @@ -219,7 +220,19 @@ def create_clones(batch_queue):
averaged_gradients = sum_gradients(gradients)

update_op = optimizer.apply_gradients(averaged_gradients, global_step=global_step)
train_op = control_flow_ops.with_dependencies([update_op], seglink_loss, name='train_op')

train_ops = [update_op]

# moving average
if FLAGS.using_moving_average:
tf.logging.info('using moving average in training, \
with decay = %f'%(FLAGS.moving_average_decay))
ema = tf.train.ExponentialMovingAverage(FLAGS.moving_average_decay)
ema_op = ema.apply(tf.trainable_variables())
with tf.control_dependencies([update_op]):# ema after updating
train_ops.append(tf.group(ema_op))

train_op = control_flow_ops.with_dependencies(train_ops, seglink_loss, name='train_op')
return train_op


Expand All @@ -240,7 +253,7 @@ def train(train_op):
logdir = FLAGS.train_dir,
init_fn = init_fn,
summary_op = summary_op,
number_of_steps = FLAGS.max_number_of_steps,
number_of_steps = 100,#, FLAGS.max_number_of_steps,
log_every_n_steps = FLAGS.log_every_n_steps,
save_summaries_secs = 60,
saver = saver,
Expand Down

0 comments on commit 6a33bca

Please sign in to comment.