Skip to content

Commit

Permalink
add resnet101
Browse files Browse the repository at this point in the history
  • Loading branch information
philokey committed Mar 1, 2017
1 parent 2566038 commit 28129df
Show file tree
Hide file tree
Showing 22 changed files with 806 additions and 28,325 deletions.
14 changes: 14 additions & 0 deletions experiments/cfgs/res101.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
EXP_DIR: res101
TRAIN:
HAS_RPN: True
IMS_PER_BATCH: 1
BBOX_NORMALIZE_TARGETS_PRECOMPUTED: True
RPN_POSITIVE_OVERLAP: 0.7
RPN_BATCHSIZE: 256
PROPOSAL_METHOD: gt
BG_THRESH_LO: 0.0
DISPLAY: 20
BATCH_SIZE: 256
TEST:
HAS_RPN: True
POOLING_MODE: crop
69 changes: 69 additions & 0 deletions experiments/scripts/test_faster_rcnn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

set -x
set -e

export PYTHONUNBUFFERED="True"

GPU_ID=$1
DATASET=$2
NET=$3

array=( $@ )
len=${#array[@]}
EXTRA_ARGS=${array[@]:3:$len}
EXTRA_ARGS_SLUG=${EXTRA_ARGS// /_}

case ${DATASET} in
pascal_voc)
TRAIN_IMDB="voc_2007_trainval"
TEST_IMDB="voc_2007_test"
ITERS=70000
;;
pascal_voc_0712)
TRAIN_IMDB="voc_2007_trainval+voc_2012_trainval"
TEST_IMDB="voc_2007_test"
ITERS=25000
;;
coco)
TRAIN_IMDB="coco_2014_train+coco_2014_valminusminival"
TEST_IMDB="coco_2014_minival"
ITERS=490000
;;
*)
echo "No dataset given"
exit
;;
esac

LOG="experiments/logs/test_${NET}_${TRAIN_IMDB}_${EXTRA_ARGS_SLUG}.txt.`date +'%Y-%m-%d_%H-%M-%S'`"
exec &> >(tee -a "$LOG")
echo Logging output to "$LOG"

set +x
if [[ ! -z ${EXTRA_ARGS_SLUG} ]]; then
NET_FINAL=output/${NET}/${TRAIN_IMDB}/${EXTRA_ARGS_SLUG}/${NET}_faster_rcnn_iter_${ITERS}.ckpt
else
NET_FINAL=output/${NET}/${TRAIN_IMDB}/default/${NET}_faster_rcnn_iter_${ITERS}.ckpt
fi
set -x

if [[ ! -z ${EXTRA_ARGS_SLUG} ]]; then
CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/test_net.py \
--imdb ${TEST_IMDB} \
--weight data/imagenet_weights/${NET}.weights \
--model ${NET_FINAL} \
--cfg experiments/cfgs/${NET}.yml \
--tag ${EXTRA_ARGS_SLUG} \
--net ${NET} \
--set ${EXTRA_ARGS}
else
CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/test_net.py \
--imdb ${TEST_IMDB} \
--weight data/imagenet_weights/${NET}.weights \
--model ${NET_FINAL} \
--cfg experiments/cfgs/${NET}.yml \
--net ${NET} \
--set ${EXTRA_ARGS}
fi

77 changes: 77 additions & 0 deletions experiments/scripts/train_faster_rcnn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

set -x
set -e

export PYTHONUNBUFFERED="True"

GPU_ID=$1
DATASET=$2
NET=$3

array=( $@ )
len=${#array[@]}
EXTRA_ARGS=${array[@]:3:$len}
EXTRA_ARGS_SLUG=${EXTRA_ARGS// /_}

case ${DATASET} in
pascal_voc)
TRAIN_IMDB="voc_2007_trainval"
TEST_IMDB="voc_2007_test"
STEPSIZE=50000
ITERS=70000
;;
pascal_voc_0712)
TRAIN_IMDB="voc_2007_trainval+voc_2012_trainval"
TEST_IMDB="voc_2007_test"
STEPSIZE=50000
ITERS=70000
;;
coco)
TRAIN_IMDB="coco_2014_train+coco_2014_valminusminival"
TEST_IMDB="coco_2014_minival"
STEPSIZE=350000
ITERS=490000
;;
*)
echo "No dataset given"
exit
;;
esac

LOG="experiments/logs/${NET}_${TRAIN_IMDB}_${EXTRA_ARGS_SLUG}_${NET}.txt.`date +'%Y-%m-%d_%H-%M-%S'`"
exec &> >(tee -a "$LOG")
echo Logging output to "$LOG"

set +x
if [[ ! -z ${EXTRA_ARGS_SLUG} ]]; then
NET_FINAL=output/${NET}/${TRAIN_IMDB}/${EXTRA_ARGS_SLUG}/${NET}_faster_rcnn_iter_${ITERS}.ckpt
else
NET_FINAL=output/${NET}/${TRAIN_IMDB}/default/${NET}_faster_rcnn_iter_${ITERS}.ckpt
fi
set -x

if [ ! -f ${NET_FINAL}.index ]; then
if [[ ! -z ${EXTRA_ARGS_SLUG} ]]; then
CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/trainval_net.py \
--weight data/imagenet_weights/${NET}.ckpt \
--imdb ${TRAIN_IMDB} \
--imdbval ${TEST_IMDB} \
--iters ${ITERS} \
--cfg experiments/cfgs/${NET}.yml \
--tag ${EXTRA_ARGS_SLUG} \
--net ${NET} \
--set TRAIN.STEPSIZE ${STEPSIZE} ${EXTRA_ARGS}
else
CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/trainval_net.py \
--weight data/imagenet_weights/${NET}.ckpt \
--imdb ${TRAIN_IMDB} \
--imdbval ${TEST_IMDB} \
--iters ${ITERS} \
--cfg experiments/cfgs/${NET}.yml \
--net ${NET} \
--set TRAIN.STEPSIZE ${STEPSIZE} ${EXTRA_ARGS}
fi
fi

./experiments/scripts/test_faster_rcnn.sh $@
4 changes: 2 additions & 2 deletions experiments/scripts/vgg16.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ set -x
if [ ! -f ${NET_FINAL}.index ]; then
if [[ ! -z ${EXTRA_ARGS_SLUG} ]]; then
CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/trainval_vgg16_net.py \
--weight data/imagenet_weights/vgg16.weights \
--weight data/imagenet_weights/vgg_16.ckpt \
--imdb ${TRAIN_IMDB} \
--imdbval ${TEST_IMDB} \
--iters ${ITERS} \
Expand All @@ -56,7 +56,7 @@ if [ ! -f ${NET_FINAL}.index ]; then
--set TRAIN.STEPSIZE ${STEPSIZE} ${EXTRA_ARGS}
else
CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/trainval_vgg16_net.py \
--weight data/imagenet_weights/vgg16.weights \
--weight data/imagenet_weights/vgg_16.ckpt \
--imdb ${TRAIN_IMDB} \
--imdbval ${TEST_IMDB} \
--iters ${ITERS} \
Expand Down
1 change: 0 additions & 1 deletion lib/datasets/pascal_voc.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def gt_roidb(self):
cache_file = os.path.join(self.cache_path, self.name + '_gt_roidb.pkl')
if os.path.exists(cache_file):
with open(cache_file, 'rb') as fid:
roidb = pickle.load(fid)
try:
roidb = pickle.load(fid)
except:
Expand Down
1 change: 0 additions & 1 deletion lib/layer_utils/proposal_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def proposal_layer(rpn_cls_prob, rpn_bbox_pred, im_info, cfg_key, _feat_stride,
scales = np.array(anchor_scales)
num_anchors = scales.shape[0] * 3
im_info = im_info[0]

# Get the scores and bounding boxes
scores = rpn_cls_prob[:, :, :, num_anchors:]
rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4))
Expand Down
3 changes: 3 additions & 0 deletions lib/layer_utils/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Licensed under The MIT License [see LICENSE for details]
# Written by Xinlei Chen
# --------------------------------------------------------
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import numpy.random as npr
Expand Down
3 changes: 1 addition & 2 deletions lib/model/bbox_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def bbox_transform_inv(boxes, deltas):
return np.zeros((0, deltas.shape[1]), dtype=deltas.dtype)

boxes = boxes.astype(deltas.dtype, copy=False)

widths = boxes[:, 2] - boxes[:, 0] + 1.0
heights = boxes[:, 3] - boxes[:, 1] + 1.0
ctr_x = boxes[:, 0] + 0.5 * widths
Expand All @@ -47,7 +46,7 @@ def bbox_transform_inv(boxes, deltas):
dy = deltas[:, 1::4]
dw = deltas[:, 2::4]
dh = deltas[:, 3::4]

pred_ctr_x = dx * widths[:, np.newaxis] + ctr_x[:, np.newaxis]
pred_ctr_y = dy * heights[:, np.newaxis] + ctr_y[:, np.newaxis]
pred_w = np.exp(dw) * widths[:, np.newaxis]
Expand Down
2 changes: 1 addition & 1 deletion lib/model/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@

# solver.prototxt specifies the snapshot path prefix, this adds an optional
# infix to yield the path: <prefix>[_<infix>]_iters_XYZ.caffemodel
__C.TRAIN.SNAPSHOT_PREFIX = 'vgg16_faster_rcnn'
__C.TRAIN.SNAPSHOT_PREFIX = 'res101_faster_rcnn'
# __C.TRAIN.SNAPSHOT_INFIX = ''

# Use a prefetch thread in roi_data_layer.layer
Expand Down
4 changes: 3 additions & 1 deletion lib/model/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def im_detect(sess, net, im):
_, scores, bbox_pred, rois = net.test_image(sess, blobs['data'], blobs['im_info'])

boxes = rois[:, 1:5] / im_scales[0]

# print(scores.shape, bbox_pred.shape, rois.shape, boxes.shape)
scores = np.reshape(scores, [scores.shape[0], -1])
bbox_pred = np.reshape(bbox_pred, [bbox_pred.shape[0], -1])
if cfg.TEST.BBOX_REG:
# Apply bounding-box regression deltas
box_deltas = bbox_pred
Expand Down
25 changes: 22 additions & 3 deletions lib/model/train_val.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import time

import tensorflow as tf
from tensorflow.python import pywrap_tensorflow


class SolverWrapper(object):
Expand Down Expand Up @@ -79,6 +80,17 @@ def snapshot(self, sess, iter):

return filename, nfilename

def get_variables_in_checkpoint_file(self, file_name):
try:
reader = pywrap_tensorflow.NewCheckpointReader(file_name)
var_to_shape_map = reader.get_variable_to_shape_map()
return var_to_shape_map
except Exception as e: # pylint: disable=broad-except
print(str(e))
if "corrupted compressed block contents" in str(e):
print("It's likely that your checkpoint file has been compressed "
"with SNAPPY.")

def train_model(self, sess, max_iters):
# Build data layers for both training and validation set
self.data_layer = RoIDataLayer(self.roidb, self.imdb.num_classes)
Expand All @@ -99,11 +111,11 @@ def train_model(self, sess, max_iters):
tag='default', anchor_scales=anchors)
# Define the loss
loss = layers['total_loss']

# Set learning rate and momentum
lr = tf.Variable(cfg.TRAIN.LEARNING_RATE, trainable=False)
momentum = cfg.TRAIN.MOMENTUM
self.optimizer = tf.train.MomentumOptimizer(lr, momentum)

# Compute the gradients wrt the loss
gvs = self.optimizer.compute_gradients(loss)
# Double the gradient of the bias if set
Expand Down Expand Up @@ -148,10 +160,17 @@ def train_model(self, sess, max_iters):
# Fresh train directly from VGG weights
print('Loading initial model weights from {:s}'.format(self.pretrained_model))
variables = tf.global_variables()

# Only initialize the variables that were not initialized when the graph was built
for vbs in self.net._initialized:
variables.remove(vbs)
sess.run(tf.variables_initializer(variables, name='init'))
var_keep_dic = self.get_variables_in_checkpoint_file(self.pretrained_model)
variables_to_restore = []
# print(var_keep_dic)
for v in variables:
if v.name.split(':')[0] in var_keep_dic:
variables_to_restore.append(v)
restorer = tf.train.Saver(variables_to_restore)
restorer.restore(sess, self.pretrained_model)
print('Loaded.')
sess.run(tf.assign(lr, cfg.TRAIN.LEARNING_RATE))
last_snapshot_iter = 0
Expand Down
Loading

0 comments on commit 28129df

Please sign in to comment.