in models/vision/detection/awsdet/core/bbox/bbox_target.py [0:0]
def _build_single_target(self, proposals, gt_boxes, gt_class_ids, img_shape):
'''
Args
---
proposals: [num_proposals, (y1, x1, y2, x2)] in regular coordinates.
gt_boxes: [num_gt_boxes, (y1, x1, y2, x2)]
gt_class_ids: [num_gt_boxes]
img_shape: np.ndarray. [2]. (img_height, img_width)
Returns
---
rois: [num_rois, (y1, x1, y2, x2)]
target_matchs: [num_positive_rois]
target_deltas: [num_positive_rois, (dy, dx, log(dh), log(dw))]
'''
# remove padded proposals and gt boxes if any
proposals, _ = trim_zeros(proposals)
gt_boxes, non_zeros = trim_zeros(gt_boxes)
gt_boxes = tf.cast(gt_boxes, proposals.dtype)
gt_labels = tf.boolean_mask(gt_class_ids, non_zeros)
proposals_gt = tf.concat([proposals, gt_boxes], axis=0)
iou = geometry.compute_overlaps(proposals_gt, gt_boxes)
max_overlaps = tf.reduce_max(iou, axis=1)
gt_assignment = tf.argmax(iou, axis=1)
labels = tf.gather(gt_labels, gt_assignment)
# get FG and BG
fg_inds = tf.where(max_overlaps >= self.pos_iou_thr)[:, 0]
bg_inds = tf.where(tf.logical_and(max_overlaps < self.pos_iou_thr,
max_overlaps >= self.neg_iou_thr))[:, 0]
# filter FG/BG
if tf.size(fg_inds) > self._max_pos_samples:
fg_inds = tf.random.shuffle(fg_inds)[:self._max_pos_samples]
remaining = self.num_rcnn_deltas - tf.size(fg_inds)
num_bg = tf.size(bg_inds)
if tf.greater_equal(num_bg, remaining):
bg_inds = tf.random.shuffle(bg_inds)[:remaining]
else:
# sample with replacement from very poor overlaps if number of backgrounds is not enough
bg_inds = tf.where(max_overlaps < self.pos_iou_thr)[:, 0]
bg_inds = tf.random.shuffle(bg_inds)[:remaining]
num_bg = tf.size(bg_inds)
while remaining > num_bg:
dups = remaining - num_bg
dup_bgs = tf.random.shuffle(bg_inds)[:dups]
bg_inds = tf.concat([bg_inds, dup_bgs], axis=0)
num_bg = tf.size(bg_inds)
# tf.print('proposal target generated %d fgs and %d bgs.' % (tf.size(fg_inds), tf.size(bg_inds)))
keep_inds = tf.concat([fg_inds, bg_inds], axis=0)
final_rois = tf.gather(proposals_gt, keep_inds) # rois[keep_inds]
final_labels = tf.gather(labels, keep_inds) # labels[keep_inds]
zero_indices = tf.expand_dims(tf.range(tf.size(fg_inds), tf.size(keep_inds), dtype=tf.int32), axis=1)
zero_labels = tf.zeros(tf.shape(zero_indices)[0], dtype=tf.int32)
final_labels = tf.tensor_scatter_nd_update(final_labels, zero_indices, zero_labels)
# inside weights - positive examples are set, rest are zeros
bbox_inside_weights = tf.zeros((tf.size(keep_inds), self.num_classes, 4), dtype=tf.float32)
if tf.size(fg_inds) > 0:
if self.reg_class_agnostic:
cur_index = tf.transpose(tf.stack([tf.range(tf.size(fg_inds)), tf.zeros(tf.size(fg_inds), dtype=tf.int32)]))
else:
cur_index = tf.stack([tf.range(tf.size(fg_inds)), tf.gather(labels, fg_inds)], axis=1)
bbox_inside_weights = tf.tensor_scatter_nd_update(bbox_inside_weights,
cur_index,
tf.ones([tf.size(fg_inds), 4], bbox_inside_weights.dtype))
bbox_inside_weights = tf.reshape(bbox_inside_weights, [-1, self.num_classes * 4])
final_bbox_targets = tf.zeros((tf.size(keep_inds), self.num_classes, 4), dtype=tf.float32)
if tf.size(fg_inds) > 0:
bbox_targets = transforms.bbox2delta(
tf.gather(final_rois, tf.range(tf.size(fg_inds))),
tf.gather(gt_boxes, tf.gather(gt_assignment, fg_inds)),
target_stds=self.target_stds, target_means=self.target_means)
if self.reg_class_agnostic:
final_bbox_targets = tf.tensor_scatter_nd_update(
final_bbox_targets,
tf.transpose(tf.stack([tf.range(tf.size(fg_inds)),
tf.zeros(tf.size(fg_inds), dtype=tf.int32)])),
bbox_targets)
else:
final_bbox_targets = tf.tensor_scatter_nd_update(
final_bbox_targets,
tf.stack([tf.range(tf.size(fg_inds)),
tf.gather(labels, fg_inds)], axis=1), bbox_targets)
final_bbox_targets = tf.reshape(final_bbox_targets, [-1, self.num_classes * 4])
bbox_outside_weights = tf.ones_like(bbox_inside_weights, dtype=bbox_inside_weights.dtype) * 1.0 / self.num_rcnn_deltas
fg_assignments = tf.gather(gt_assignment, keep_inds)
return (tf.stop_gradient(final_rois), tf.stop_gradient(final_labels), tf.stop_gradient(final_bbox_targets),
tf.stop_gradient(bbox_inside_weights), tf.stop_gradient(bbox_outside_weights), tf.stop_gradient(fg_assignments))