Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
seminar-breakout
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
seminar-breakout
Commits
2766bb5a
Commit
2766bb5a
authored
Nov 14, 2017
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[FasterRCNN] code clean-up and docs
parent
191d4691
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
56 deletions
+51
-56
examples/FasterRCNN/data.py
examples/FasterRCNN/data.py
+3
-1
examples/FasterRCNN/model.py
examples/FasterRCNN/model.py
+16
-14
examples/FasterRCNN/train.py
examples/FasterRCNN/train.py
+32
-41
No files found.
examples/FasterRCNN/data.py
View file @
2766bb5a
...
@@ -11,7 +11,7 @@ from tensorpack.utils import logger
...
@@ -11,7 +11,7 @@ from tensorpack.utils import logger
from
tensorpack.utils.argtools
import
memoized
,
log_once
from
tensorpack.utils.argtools
import
memoized
,
log_once
from
tensorpack.dataflow
import
(
from
tensorpack.dataflow
import
(
MapData
,
imgaug
,
TestDataSpeed
,
MapData
,
imgaug
,
TestDataSpeed
,
MapDataComponent
,
DataFromList
)
MapDataComponent
,
DataFromList
,
PrefetchDataZMQ
)
import
tensorpack.utils.viz
as
tpviz
import
tensorpack.utils.viz
as
tpviz
from
tensorpack.utils.viz
import
interactive_imshow
from
tensorpack.utils.viz
import
interactive_imshow
...
@@ -258,6 +258,7 @@ def get_train_dataflow(add_mask=False):
...
@@ -258,6 +258,7 @@ def get_train_dataflow(add_mask=False):
return
ret
return
ret
ds
=
MapData
(
ds
,
preprocess
)
ds
=
MapData
(
ds
,
preprocess
)
ds
=
PrefetchDataZMQ
(
ds
,
1
)
return
ds
return
ds
...
@@ -271,6 +272,7 @@ def get_eval_dataflow():
...
@@ -271,6 +272,7 @@ def get_eval_dataflow():
assert
im
is
not
None
,
fname
assert
im
is
not
None
,
fname
return
im
return
im
ds
=
MapDataComponent
(
ds
,
f
,
0
)
ds
=
MapDataComponent
(
ds
,
f
,
0
)
ds
=
PrefetchDataZMQ
(
ds
,
1
)
return
ds
return
ds
...
...
examples/FasterRCNN/model.py
View file @
2766bb5a
...
@@ -91,8 +91,7 @@ def rpn_losses(anchor_labels, anchor_boxes, label_logits, box_logits):
...
@@ -91,8 +91,7 @@ def rpn_losses(anchor_labels, anchor_boxes, label_logits, box_logits):
box_loss
,
box_loss
,
tf
.
cast
(
nr_valid
,
tf
.
float32
),
name
=
'box_loss'
)
tf
.
cast
(
nr_valid
,
tf
.
float32
),
name
=
'box_loss'
)
for
k
in
[
label_loss
,
box_loss
,
nr_valid
,
nr_pos
]
+
summaries
:
add_moving_summary
(
*
([
label_loss
,
box_loss
,
nr_valid
,
nr_pos
]
+
summaries
))
add_moving_summary
(
k
)
return
label_loss
,
box_loss
return
label_loss
,
box_loss
...
@@ -165,6 +164,7 @@ def generate_rpn_proposals(boxes, scores, img_shape):
...
@@ -165,6 +164,7 @@ def generate_rpn_proposals(boxes, scores, img_shape):
boxes: kx4 float
boxes: kx4 float
scores: k logits
scores: k logits
"""
"""
assert
boxes
.
shape
.
ndims
==
2
,
boxes
.
shape
if
get_current_tower_context
()
.
is_training
:
if
get_current_tower_context
()
.
is_training
:
PRE_NMS_TOPK
=
config
.
TRAIN_PRE_NMS_TOPK
PRE_NMS_TOPK
=
config
.
TRAIN_PRE_NMS_TOPK
POST_NMS_TOPK
=
config
.
TRAIN_POST_NMS_TOPK
POST_NMS_TOPK
=
config
.
TRAIN_POST_NMS_TOPK
...
@@ -213,6 +213,8 @@ def generate_rpn_proposals(boxes, scores, img_shape):
...
@@ -213,6 +213,8 @@ def generate_rpn_proposals(boxes, scores, img_shape):
@
under_name_scope
()
@
under_name_scope
()
def
proposal_metrics
(
iou
):
def
proposal_metrics
(
iou
):
"""
"""
Add summaries for RPN proposals.
Args:
Args:
iou: nxm, #proposal x #gt
iou: nxm, #proposal x #gt
"""
"""
...
@@ -233,6 +235,8 @@ def proposal_metrics(iou):
...
@@ -233,6 +235,8 @@ def proposal_metrics(iou):
@
under_name_scope
()
@
under_name_scope
()
def
sample_fast_rcnn_targets
(
boxes
,
gt_boxes
,
gt_labels
):
def
sample_fast_rcnn_targets
(
boxes
,
gt_boxes
,
gt_labels
):
"""
"""
Sample some ROIs from all proposals for training.
Args:
Args:
boxes: nx4 region proposals, floatbox
boxes: nx4 region proposals, floatbox
gt_boxes: mx4, floatbox
gt_boxes: mx4, floatbox
...
@@ -240,8 +244,9 @@ def sample_fast_rcnn_targets(boxes, gt_boxes, gt_labels):
...
@@ -240,8 +244,9 @@ def sample_fast_rcnn_targets(boxes, gt_boxes, gt_labels):
Returns:
Returns:
sampled_boxes: tx4 floatbox, the rois
sampled_boxes: tx4 floatbox, the rois
target_boxes: tx4 encoded box, the regression target
sampled_labels: t labels, in [0, #class-1]. Positive means foreground.
labels: t labels
fg_inds_wrt_gt: #fg indices, each in range [0, m-1].
It contains the matching GT of each foreground roi.
"""
"""
iou
=
pairwise_iou
(
boxes
,
gt_boxes
)
# nxm
iou
=
pairwise_iou
(
boxes
,
gt_boxes
)
# nxm
proposal_metrics
(
iou
)
proposal_metrics
(
iou
)
...
@@ -287,8 +292,8 @@ def sample_fast_rcnn_targets(boxes, gt_boxes, gt_labels):
...
@@ -287,8 +292,8 @@ def sample_fast_rcnn_targets(boxes, gt_boxes, gt_labels):
@
under_name_scope
()
@
under_name_scope
()
def
crop_and_resize
(
image
,
boxes
,
box_ind
,
crop_size
):
def
crop_and_resize
(
image
,
boxes
,
box_ind
,
crop_size
):
"""
"""
Better-aligned version of tf.image.crop_and_resize,
Better-aligned version of tf.image.crop_and_resize,
following our definition of floating point boxes.
following our definition of floating point boxes.
Args:
Args:
image: NCHW
image: NCHW
boxes: nx4, x1y1x2y2
boxes: nx4, x1y1x2y2
...
@@ -349,9 +354,6 @@ def roi_align(featuremap, boxes, output_shape):
...
@@ -349,9 +354,6 @@ def roi_align(featuremap, boxes, output_shape):
Returns:
Returns:
NxCxoHxoW
NxCxoHxoW
"""
"""
image_shape
=
tf
.
shape
(
featuremap
)[
2
:]
boxes
=
tf
.
stop_gradient
(
boxes
)
# TODO
boxes
=
tf
.
stop_gradient
(
boxes
)
# TODO
# sample 4 locations per roi bin
# sample 4 locations per roi bin
ret
=
crop_and_resize
(
ret
=
crop_and_resize
(
...
@@ -407,6 +409,7 @@ def fastrcnn_losses(labels, label_logits, fg_boxes, fg_box_logits):
...
@@ -407,6 +409,7 @@ def fastrcnn_losses(labels, label_logits, fg_boxes, fg_box_logits):
tf
.
to_int32
(
fg_labels
)
-
1
],
axis
=
1
)
# #fgx2
tf
.
to_int32
(
fg_labels
)
-
1
],
axis
=
1
)
# #fgx2
fg_box_logits
=
tf
.
gather_nd
(
fg_box_logits
,
indices
)
fg_box_logits
=
tf
.
gather_nd
(
fg_box_logits
,
indices
)
# some metrics to summarize
fg_label_pred
=
tf
.
argmax
(
tf
.
gather
(
label_logits
,
fg_inds
),
axis
=
1
)
fg_label_pred
=
tf
.
argmax
(
tf
.
gather
(
label_logits
,
fg_inds
),
axis
=
1
)
num_zero
=
tf
.
reduce_sum
(
tf
.
to_int32
(
tf
.
equal
(
fg_label_pred
,
0
)),
name
=
'num_zero'
)
num_zero
=
tf
.
reduce_sum
(
tf
.
to_int32
(
tf
.
equal
(
fg_label_pred
,
0
)),
name
=
'num_zero'
)
false_negative
=
tf
.
truediv
(
num_zero
,
num_fg
,
name
=
'false_negative'
)
false_negative
=
tf
.
truediv
(
num_zero
,
num_fg
,
name
=
'false_negative'
)
...
@@ -418,8 +421,7 @@ def fastrcnn_losses(labels, label_logits, fg_boxes, fg_box_logits):
...
@@ -418,8 +421,7 @@ def fastrcnn_losses(labels, label_logits, fg_boxes, fg_box_logits):
box_loss
=
tf
.
truediv
(
box_loss
=
tf
.
truediv
(
box_loss
,
tf
.
to_float
(
tf
.
shape
(
labels
)[
0
]),
name
=
'box_loss'
)
box_loss
,
tf
.
to_float
(
tf
.
shape
(
labels
)[
0
]),
name
=
'box_loss'
)
for
k
in
[
label_loss
,
box_loss
,
accuracy
,
fg_accuracy
,
false_negative
]:
add_moving_summary
(
label_loss
,
box_loss
,
accuracy
,
fg_accuracy
,
false_negative
)
add_moving_summary
(
k
)
return
label_loss
,
box_loss
return
label_loss
,
box_loss
...
@@ -456,10 +458,10 @@ def fastrcnn_predictions(boxes, probs):
...
@@ -456,10 +458,10 @@ def fastrcnn_predictions(boxes, probs):
box
,
prob
,
config
.
RESULTS_PER_IM
,
config
.
FASTRCNN_NMS_THRESH
)
box
,
prob
,
config
.
RESULTS_PER_IM
,
config
.
FASTRCNN_NMS_THRESH
)
selection
=
tf
.
to_int32
(
tf
.
gather
(
ids
,
selection
))
selection
=
tf
.
to_int32
(
tf
.
gather
(
ids
,
selection
))
# sort available in TF>1.4.0
# sort available in TF>1.4.0
# selection = tf.contrib.framework.sort(selection, direction='ASCENDING')
# s
orted_s
election = tf.contrib.framework.sort(selection, direction='ASCENDING')
sorted_selection
,
_
=
tf
.
nn
.
top_k
(
-
selection
,
k
=
tf
.
size
(
selection
))
sorted_selection
=
-
tf
.
nn
.
top_k
(
-
selection
,
k
=
tf
.
size
(
selection
))[
0
]
mask
=
tf
.
sparse_to_dense
(
mask
=
tf
.
sparse_to_dense
(
sparse_indices
=
-
sorted_selection
,
sparse_indices
=
sorted_selection
,
output_shape
=
output_shape
,
output_shape
=
output_shape
,
sparse_values
=
True
,
sparse_values
=
True
,
default_value
=
False
)
default_value
=
False
)
...
...
examples/FasterRCNN/train.py
View file @
2766bb5a
...
@@ -15,11 +15,9 @@ import tensorflow as tf
...
@@ -15,11 +15,9 @@ import tensorflow as tf
os
.
environ
[
'TENSORPACK_TRAIN_API'
]
=
'v2'
# will become default soon
os
.
environ
[
'TENSORPACK_TRAIN_API'
]
=
'v2'
# will become default soon
from
tensorpack
import
*
from
tensorpack
import
*
import
tensorpack.tfutils.symbolic_functions
as
symbf
from
tensorpack.tfutils.summary
import
add_moving_summary
from
tensorpack.tfutils.summary
import
add_moving_summary
from
tensorpack.tfutils
import
optimizer
,
gradproc
from
tensorpack.tfutils
import
optimizer
,
gradproc
import
tensorpack.utils.viz
as
tpviz
import
tensorpack.utils.viz
as
tpviz
from
tensorpack.utils.concurrency
import
subproc_call
from
tensorpack.utils.gpu
import
get_nr_gpu
from
tensorpack.utils.gpu
import
get_nr_gpu
...
@@ -88,8 +86,6 @@ class Model(ModelDesc):
...
@@ -88,8 +86,6 @@ class Model(ModelDesc):
anchor_boxes_encoded
=
encode_bbox_target
(
anchor_boxes
,
fm_anchors
)
anchor_boxes_encoded
=
encode_bbox_target
(
anchor_boxes
,
fm_anchors
)
featuremap
=
pretrained_resnet_conv4
(
image
,
config
.
RESNET_NUM_BLOCK
[:
3
])
featuremap
=
pretrained_resnet_conv4
(
image
,
config
.
RESNET_NUM_BLOCK
[:
3
])
rpn_label_logits
,
rpn_box_logits
=
rpn_head
(
'rpn'
,
featuremap
,
1024
,
config
.
NUM_ANCHOR
)
rpn_label_logits
,
rpn_box_logits
=
rpn_head
(
'rpn'
,
featuremap
,
1024
,
config
.
NUM_ANCHOR
)
rpn_label_loss
,
rpn_box_loss
=
rpn_losses
(
anchor_labels
,
anchor_boxes_encoded
,
rpn_label_logits
,
rpn_box_logits
)
decoded_boxes
=
decode_bbox_target
(
rpn_box_logits
,
fm_anchors
)
# fHxfWxNAx4, floatbox
decoded_boxes
=
decode_bbox_target
(
rpn_box_logits
,
fm_anchors
)
# fHxfWxNAx4, floatbox
proposal_boxes
,
proposal_scores
=
generate_rpn_proposals
(
proposal_boxes
,
proposal_scores
=
generate_rpn_proposals
(
...
@@ -111,6 +107,11 @@ class Model(ModelDesc):
...
@@ -111,6 +107,11 @@ class Model(ModelDesc):
fastrcnn_label_logits
,
fastrcnn_box_logits
=
fastrcnn_head
(
'fastrcnn'
,
feature_fastrcnn
,
config
.
NUM_CLASS
)
fastrcnn_label_logits
,
fastrcnn_box_logits
=
fastrcnn_head
(
'fastrcnn'
,
feature_fastrcnn
,
config
.
NUM_CLASS
)
if
is_training
:
if
is_training
:
# rpn loss
rpn_label_loss
,
rpn_box_loss
=
rpn_losses
(
anchor_labels
,
anchor_boxes_encoded
,
rpn_label_logits
,
rpn_box_logits
)
# fastrcnn loss
fg_inds_wrt_sample
=
tf
.
reshape
(
tf
.
where
(
rcnn_labels
>
0
),
[
-
1
])
# fg inds w.r.t all samples
fg_inds_wrt_sample
=
tf
.
reshape
(
tf
.
where
(
rcnn_labels
>
0
),
[
-
1
])
# fg inds w.r.t all samples
fg_sampled_boxes
=
tf
.
gather
(
rcnn_sampled_boxes
,
fg_inds_wrt_sample
)
fg_sampled_boxes
=
tf
.
gather
(
rcnn_sampled_boxes
,
fg_inds_wrt_sample
)
...
@@ -142,7 +143,7 @@ class Model(ModelDesc):
...
@@ -142,7 +143,7 @@ class Model(ModelDesc):
tf
.
constant
(
config
.
FASTRCNN_BBOX_REG_WEIGHTS
),
anchors
)
tf
.
constant
(
config
.
FASTRCNN_BBOX_REG_WEIGHTS
),
anchors
)
decoded_boxes
=
tf
.
identity
(
decoded_boxes
,
name
=
'fastrcnn_all_boxes'
)
decoded_boxes
=
tf
.
identity
(
decoded_boxes
,
name
=
'fastrcnn_all_boxes'
)
# Nx2. Each index into (#proposal, #category)
#
indices:
Nx2. Each index into (#proposal, #category)
pred_indices
,
final_probs
=
fastrcnn_predictions
(
decoded_boxes
,
label_probs
)
pred_indices
,
final_probs
=
fastrcnn_predictions
(
decoded_boxes
,
label_probs
)
final_probs
=
tf
.
identity
(
final_probs
,
'final_probs'
)
final_probs
=
tf
.
identity
(
final_probs
,
'final_probs'
)
final_boxes
=
tf
.
gather_nd
(
decoded_boxes
,
pred_indices
,
name
=
'final_boxes'
)
final_boxes
=
tf
.
gather_nd
(
decoded_boxes
,
pred_indices
,
name
=
'final_boxes'
)
...
@@ -208,36 +209,18 @@ def visualize(model_path, nr_visualize=50, output_dir='output'):
...
@@ -208,36 +209,18 @@ def visualize(model_path, nr_visualize=50, output_dir='output'):
pbar
.
update
()
pbar
.
update
()
def
offline_evaluate
(
model_path
,
output_file
):
def
offline_evaluate
(
pred_func
,
output_file
):
pred
=
OfflinePredictor
(
PredictConfig
(
model
=
Model
(),
session_init
=
get_model_loader
(
model_path
),
input_names
=
[
'image'
],
output_names
=
[
'final_boxes'
,
'final_probs'
,
'final_labels'
,
]))
df
=
get_eval_dataflow
()
df
=
get_eval_dataflow
()
df
=
PrefetchDataZMQ
(
df
,
1
)
all_results
=
eval_on_dataflow
(
all_results
=
eval_on_dataflow
(
df
,
lambda
img
:
detect_one_image
(
img
,
pred
))
df
,
lambda
img
:
detect_one_image
(
img
,
pred_func
))
with
open
(
output_file
,
'w'
)
as
f
:
with
open
(
output_file
,
'w'
)
as
f
:
json
.
dump
(
all_results
,
f
)
json
.
dump
(
all_results
,
f
)
print_evaluation_scores
(
output_file
)
print_evaluation_scores
(
output_file
)
def
predict
(
model_path
,
input_file
):
def
predict
(
pred_func
,
input_file
):
pred
=
OfflinePredictor
(
PredictConfig
(
model
=
Model
(),
session_init
=
get_model_loader
(
model_path
),
input_names
=
[
'image'
],
output_names
=
[
'final_boxes'
,
'final_probs'
,
'final_labels'
,
]))
img
=
cv2
.
imread
(
input_file
,
cv2
.
IMREAD_COLOR
)
img
=
cv2
.
imread
(
input_file
,
cv2
.
IMREAD_COLOR
)
results
=
detect_one_image
(
img
,
pred
)
results
=
detect_one_image
(
img
,
pred
_func
)
final
=
draw_final_outputs
(
img
,
results
)
final
=
draw_final_outputs
(
img
,
results
)
viz
=
np
.
concatenate
((
img
,
final
),
axis
=
1
)
viz
=
np
.
concatenate
((
img
,
final
),
axis
=
1
)
tpviz
.
interactive_imshow
(
viz
)
tpviz
.
interactive_imshow
(
viz
)
...
@@ -247,10 +230,8 @@ class EvalCallback(Callback):
...
@@ -247,10 +230,8 @@ class EvalCallback(Callback):
def
_setup_graph
(
self
):
def
_setup_graph
(
self
):
self
.
pred
=
self
.
trainer
.
get_predictor
(
self
.
pred
=
self
.
trainer
.
get_predictor
(
[
'image'
],
[
'image'
],
[
'final_boxes'
,
[
'final_boxes'
,
'final_probs'
,
'final_labels'
])
'final_probs'
,
self
.
df
=
get_eval_dataflow
()
'final_labels'
])
self
.
df
=
PrefetchDataZMQ
(
get_eval_dataflow
(),
1
)
def
_before_train
(
self
):
def
_before_train
(
self
):
EVAL_TIMES
=
5
# eval 5 times during training
EVAL_TIMES
=
5
# eval 5 times during training
...
@@ -288,18 +269,29 @@ if __name__ == '__main__':
...
@@ -288,18 +269,29 @@ if __name__ == '__main__':
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
args
.
gpu
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
args
.
gpu
if
args
.
visualize
or
args
.
evaluate
or
args
.
predict
:
if
args
.
visualize
or
args
.
evaluate
or
args
.
predict
:
# autotune is too slow for inference
os
.
environ
[
'TF_CUDNN_USE_AUTOTUNE'
]
=
'0'
assert
args
.
load
assert
args
.
load
print_config
()
print_config
()
if
args
.
visualize
:
if
args
.
visualize
:
visualize
(
args
.
load
)
visualize
(
args
.
load
)
elif
args
.
evaluate
:
else
:
assert
args
.
evaluate
.
endswith
(
'.json'
)
pred
=
OfflinePredictor
(
PredictConfig
(
# autotune is too slow for inference
model
=
Model
(),
os
.
environ
[
'TF_CUDNN_USE_AUTOTUNE'
]
=
'0'
session_init
=
get_model_loader
(
args
.
load
),
offline_evaluate
(
args
.
load
,
args
.
evaluate
)
input_names
=
[
'image'
],
elif
args
.
predict
:
output_names
=
[
COCODetection
(
config
.
BASEDIR
,
'train2014'
)
# to load the class names into caches
'final_boxes'
,
predict
(
args
.
load
,
args
.
predict
)
'final_probs'
,
'final_labels'
,
]))
if
args
.
evaluate
:
assert
args
.
evaluate
.
endswith
(
'.json'
)
offline_evaluate
(
pred
,
args
.
evaluate
)
elif
args
.
predict
:
COCODetection
(
config
.
BASEDIR
,
'train2014'
)
# to load the class names into caches
predict
(
pred
,
args
.
predict
)
else
:
else
:
logger
.
set_logger_dir
(
args
.
logdir
)
logger
.
set_logger_dir
(
args
.
logdir
)
print_config
()
print_config
()
...
@@ -322,7 +314,6 @@ if __name__ == '__main__':
...
@@ -322,7 +314,6 @@ if __name__ == '__main__':
[(
warmup_epoch
*
factor
,
1e-2
),
[(
warmup_epoch
*
factor
,
1e-2
),
(
150000
*
factor
//
stepnum
,
1e-3
),
(
150000
*
factor
//
stepnum
,
1e-3
),
(
210000
*
factor
//
stepnum
,
1e-4
)]),
(
210000
*
factor
//
stepnum
,
1e-4
)]),
HumanHyperParamSetter
(
'learning_rate'
),
EvalCallback
(),
EvalCallback
(),
GPUUtilizationTracker
(),
GPUUtilizationTracker
(),
],
],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment