Skip to content

Commit

Permalink
Minor code/comment styling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vierja committed Jul 13, 2017
1 parent dee261b commit 2c8035d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
11 changes: 6 additions & 5 deletions frcnn/rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,23 @@ def _build(self, pooled_layer, proposals, gt_boxes, im_shape):

prediction_dict = {}

# We treat num proposals as batch number so that when flattening we actually
# We treat num proposals as batch number so that when flattening we
# get a (num_proposals, flatten_pooled_feature_map_size) Tensor.
flatten_net = tf.contrib.layers.flatten(pooled_layer)
net = tf.identity(flatten_net)

if self._debug:
prediction_dict['flatten_net'] = net # TODO: debug tmp

# After flattening we are lef with a (num_proposals, pool_height * pool_width * 512) tensor.
# After flattening we are lef with a
# (num_proposals, pool_height * pool_width * 512) tensor.
# The first dimension works as batch size when applied to snt.Linear.
for i, layer in enumerate(self._layers):
net = layer(net)
prediction_dict['layer_{}_out'.format(i)] = net # TODO: debug tmp
net = self._activation(net)
net = tf.nn.dropout(net, keep_prob=self._dropout_keep_prob)
variable_summaries(layer.w, 'layer_{}_W'.format(i), ['RCNN'])
# TODO: megadebug, low performance: variable_summaries(layer.w, 'layer_{}_W'.format(i), ['RCNN'])

cls_score = self._classifier_layer(net)
prob = tf.nn.softmax(cls_score, dim=1)
Expand Down Expand Up @@ -132,8 +133,8 @@ def loss(self, prediction_dict):
bbox_offsets: shape (num_proposals, num_classes * 4)
Has the offset for each proposal for each class.
We have to compare only the proposals labeled with the offsets
for that label.
We have to compare only the proposals labeled with the
offsets for that label.
bbox_offsets_target: shape (num_proposals, 4)
Has the true offset of each proposal for the true label.
Expand Down
19 changes: 11 additions & 8 deletions frcnn/roi_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,37 @@ def __init__(self, pooling_mode=CROP, pooled_width=7, pooled_height=7,

def _get_bboxes(self, roi_proposals, im_shape):
"""
Get normalized coordinates for RoIs (betweetn 0 and 1 for easy cropping)
Get normalized coordinates for RoIs (between 0 and 1 for easy cropping)
in TF order (y1, x1, y2, x2)
"""
im_shape = tf.cast(im_shape, tf.float32)

_, x1, y1, x2, y2 = tf.split(value=roi_proposals, num_or_size_splits=5, axis=1)
_, x1, y1, x2, y2 = tf.unstack(value=roi_proposals, num_or_size_splits=5, axis=1)

x1 = x1 / im_shape[1]
y1 = y1 / im_shape[0]
x2 = x2 / im_shape[1]
y2 = y2 / im_shape[0]

# Won't be backpropagated to rois anyway, but to save time TODO: Remove?
# Won't be backpropagated to rois anyway, but to save time TODO: Remove
bboxes = tf.concat([y1, x1, y2, x2], axis=1)

return bboxes

def _roi_crop(self, roi_proposals, pretrained, im_shape):
bboxes = self._get_bboxes(roi_proposals, im_shape)
# TODO: Why?!!?
# batch_ids = tf.squeeze(tf.slice(roi_proposals, [0, 0], [-1, 1], name="batch_id"), [1])
bboxes_shape = tf.shape(bboxes)
batch_ids = tf.zeros((bboxes_shape[0], ), dtype=tf.int32)
crops = tf.image.crop_and_resize(
pretrained, bboxes, batch_ids, [self._pooled_width * 2, self._pooled_height * 2], name="crops"
pretrained, bboxes, batch_ids,
[self._pooled_width * 2, self._pooled_height * 2], name="crops"
)

prediction_dict = {
'roi_pool': slim.max_pool2d(crops, [2, 2], stride=2)
'roi_pool': tf.nn.max_pool(
crops, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
padding='VALID'
),
}

if self._debug:
Expand All @@ -68,4 +70,5 @@ def _build(self, roi_proposals, pretrained, im_shape):
elif self._pooling_mode == ROI_POOLING:
return self._roi_pooling(roi_proposals, pretrained, im_shape)
else:
raise NotImplemented('Pooling mode {} does not exist.'.format(self._pooling_mode))
raise NotImplemented(
'Pooling mode {} does not exist.'.format(self._pooling_mode))
8 changes: 3 additions & 5 deletions frcnn/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@


def normalize_bboxes(image, bboxes):
batch, x1, y1, x2, y2 = tf.split(value=bboxes, num_or_size_splits=5, axis=1)
batch, x1, y1, x2, y2 = tf.unstack(bboxes, axis=1)

image_shape = tf.cast(tf.shape(image), tf.float32)
x1 = x1 / image_shape[2]
y1 = y1 / image_shape[1]
x2 = x2 / image_shape[2]
y2 = y2 / image_shape[1]

bboxes = tf.concat([batch, y1, x1, y2, x2], axis=1)
bboxes = tf.stack([batch, y1, x1, y2, x2], axis=1)
bboxes = tf.expand_dims(bboxes, 0)

return bboxes


def draw_bboxes(image, bboxes, topn=10, normalize=True):
# change fucking order
#. we asume bboxes has batch
bboxes = tf.slice(bboxes, [0, 0], [topn, 5])
if normalize:
bboxes = normalize_bboxes(image, bboxes)

return tf.image.draw_bounding_boxes(image, bboxes)
25 changes: 13 additions & 12 deletions frcnn/utils/image_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,27 @@ def draw_batch_proposals(pred_dict):

if target == 1:
fill = (0, 0, 255, 10)
outline_fill = (0, 0, 255, 50)
else:
fill = (255, 0, 0, 5)
outline_fill = (255, 0, 0, 30)

draw.rectangle(list(proposal), fill=fill, outline=outline_fill)
x, y = list(proposal)[:2]
x = max(x, 0)
y = max(y, 0)

score = float(score)
if score > 0.5:
font_fill = (0, 255, 0, 255)
outline_fill = (0, 255, 0, 50)
else:
font_fill = (255, 0, 0, 255)
outline_fill = (255, 0, 0, 50)

if np.isclose(score, 1.0) or score > 1.0:
font_txt = '1'
else:
font_txt = '{:.2f}'.format(score)[1:]

draw.rectangle(list(proposal), fill=fill, outline=outline_fill)
x, y = list(proposal)[:2]
x = max(x, 0)
y = max(y, 0)

score = float(score)
draw.text(tuple([x, y]), text=font_txt, font=font, fill=font_fill)

gt_boxes = pred_dict['gt_boxes']
Expand All @@ -222,12 +222,13 @@ def draw_batch_proposals(pred_dict):

imgcat_pil(image_pil)


def draw_top_nms_proposals(pred_dict, min_score=0.8):
print('Top NMS proposals')
scores = pred_dict['rpn_prediction']['scores']
proposals = pred_dict['rpn_prediction']['proposals']
# Remove batch id
proposals = proposals[:,1:]
proposals = proposals[:, 1:]
top_scores_mask = scores > min_score
scores = scores[top_scores_mask]
proposals = proposals[top_scores_mask]
Expand Down Expand Up @@ -477,9 +478,9 @@ def draw_rcnn_cls_batch_errors(pred_dict, foreground=True, background=True, wors
imgcat_pil(image_pil)


def draw_rcnn_reg_batch_errors(pred_dict, worst=True):
print('Show errors in batch used for training classifier.')
print('blue => GT, green => foreground, red => background')
def draw_rcnn_reg_batch_errors(pred_dict):
print('Show errors in batch used for training classifier regressor.')
print('blue => GT, green => foreground, r`regression_loss` - c`classification_loss`.')

proposals = pred_dict['rpn_prediction']['proposals'][:,1:]
cls_targets = pred_dict['classification_prediction']['cls_target']
Expand Down

0 comments on commit 2c8035d

Please sign in to comment.