def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" # targets = bbox_transform(ex_rois, gt_rois[:, :5]).astype( # np.float32, copy=False) targets = encode_and_decode.encode_boxes_rotate( unencode_boxes=gt_rois, reference_boxes=ex_rois, scale_factors=cfgs.ANCHOR_SCALE_FACTORS) # targets = encode_and_decode.encode_boxes(ex_rois=ex_rois, # gt_rois=gt_rois, # scale_factor=None) return targets
def _compute_targets_r(ex_rois, gt_rois_r, labels): """Compute bounding-box regression targets for an image. that is : [label, tx, ty, tw, th] """ assert ex_rois.shape[0] == gt_rois_r.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois_r.shape[1] == 5 targets_r = encode_and_decode.encode_boxes_rotate( unencode_boxes=gt_rois_r, reference_boxes=ex_rois, scale_factors=cfgs.ROI_SCALE_FACTORS) return np.hstack((labels[:, np.newaxis], targets_r)).astype(np.float32, copy=False)
def fast_rcnn_loss(self): with tf.variable_scope('fast_rcnn_loss'): minibatch_indices, minibatch_reference_boxes_mattached_gtboxes, \ minibatch_reference_boxes_mattached_gtboxes_rotate, minibatch_object_mask, \ minibatch_label_one_hot = self.fast_rcnn_minibatch(self.fast_rcnn_all_level_proposals) minibatch_reference_boxes = tf.gather(self.fast_rcnn_all_level_proposals, minibatch_indices) minibatch_encode_boxes = tf.gather(self.fast_rcnn_encode_boxes, minibatch_indices) # [minibatch_size, num_classes*4] minibatch_encode_boxes_rotate = tf.gather(self.fast_rcnn_encode_boxes_rotate, minibatch_indices) # [minibatch_size, num_classes*5] minibatch_scores = tf.gather(self.fast_rcnn_scores, minibatch_indices) minibatch_scores_rotate = tf.gather(self.fast_rcnn_scores_rotate, minibatch_indices) # encode gtboxes minibatch_encode_gtboxes = \ encode_and_decode.encode_boxes( unencode_boxes=minibatch_reference_boxes_mattached_gtboxes, reference_boxes=minibatch_reference_boxes, scale_factors=self.scale_factors ) minibatch_encode_gtboxes_rotate = encode_and_decode.encode_boxes_rotate( unencode_boxes=minibatch_reference_boxes_mattached_gtboxes_rotate, reference_boxes=minibatch_reference_boxes, scale_factors=self.scale_factors ) # [minibatch_size, num_classes*4] minibatch_encode_gtboxes = tf.tile(minibatch_encode_gtboxes, [1, self.num_classes]) # [minibatch_size, num_classes*5] minibatch_encode_gtboxes_rotate = tf.tile(minibatch_encode_gtboxes_rotate, [1, self.num_classes]) class_weights_list = [] category_list = tf.unstack(minibatch_label_one_hot, axis=1) for i in range(1, self.num_classes+1): tmp_class_weights = tf.ones(shape=[tf.shape(minibatch_encode_boxes)[0], 4], dtype=tf.float32) tmp_class_weights = tmp_class_weights * tf.expand_dims(category_list[i], axis=1) class_weights_list.append(tmp_class_weights) class_weights = tf.concat(class_weights_list, axis=1) # [minibatch_size, num_classes*4] class_weights_list_rotate = [] category_list_rotate = tf.unstack(minibatch_label_one_hot, axis=1) for i in range(1, self.num_classes + 1): tmp_class_weights_rotate = tf.ones(shape=[tf.shape(minibatch_encode_boxes_rotate)[0], 5], dtype=tf.float32) tmp_class_weights_rotate = tmp_class_weights_rotate * tf.expand_dims(category_list_rotate[i], axis=1) class_weights_list_rotate.append(tmp_class_weights_rotate) class_weights_rotate = tf.concat(class_weights_list_rotate, axis=1) # [minibatch_size, num_classes*5] # loss with tf.variable_scope('fast_rcnn_classification_loss'): fast_rcnn_classification_loss = slim.losses.softmax_cross_entropy(logits=minibatch_scores, onehot_labels=minibatch_label_one_hot) with tf.variable_scope('fast_rcnn_location_loss'): fast_rcnn_location_loss = losses.l1_smooth_losses(predict_boxes=minibatch_encode_boxes, gtboxes=minibatch_encode_gtboxes, object_weights=minibatch_object_mask, classes_weights=class_weights) slim.losses.add_loss(fast_rcnn_location_loss) with tf.variable_scope('fast_rcnn_classification_rotate_loss'): fast_rcnn_classification_rotate_loss = slim.losses.softmax_cross_entropy(logits=minibatch_scores_rotate, onehot_labels=minibatch_label_one_hot) with tf.variable_scope('fast_rcnn_location_rotate_loss'): fast_rcnn_location_rotate_loss = losses.l1_smooth_losses(predict_boxes=minibatch_encode_boxes_rotate, gtboxes=minibatch_encode_gtboxes_rotate, object_weights=minibatch_object_mask, classes_weights=class_weights_rotate) slim.losses.add_loss(fast_rcnn_location_rotate_loss) return fast_rcnn_location_loss, fast_rcnn_classification_loss, \ fast_rcnn_location_rotate_loss, fast_rcnn_classification_rotate_loss
def fast_rcnn_loss(self): with tf.variable_scope('fast_rcnn_loss'): minibatch_indices, minibatch_reference_boxes_mattached_gtboxes, \ minibatch_reference_boxes_mattached_gtboxes_rotate, \ minibatch_reference_boxes_mattached_head_quadrant, minibatch_object_mask, \ minibatch_label_one_hot = self.fast_rcnn_minibatch(self.fast_rcnn_all_level_proposals) minibatch_reference_boxes = tf.gather( self.fast_rcnn_all_level_proposals, minibatch_indices) minibatch_encode_boxes = tf.gather( self.fast_rcnn_encode_boxes, minibatch_indices) # [minibatch_size, num_classes*4] minibatch_encode_boxes_rotate = tf.gather( self.fast_rcnn_encode_boxes_rotate, minibatch_indices) # [minibatch_size, num_classes*5] minibatch_head_quadrant = tf.gather(self.fast_rcnn_head_quadrant, minibatch_indices) minibatch_scores = tf.gather(self.fast_rcnn_scores, minibatch_indices) minibatch_scores_rotate = tf.gather(self.fast_rcnn_scores_rotate, minibatch_indices) # encode gtboxes minibatch_encode_gtboxes = \ encode_and_decode.encode_boxes( unencode_boxes=minibatch_reference_boxes_mattached_gtboxes, reference_boxes=minibatch_reference_boxes, scale_factors=self.scale_factors ) minibatch_encode_gtboxes_rotate = encode_and_decode.encode_boxes_rotate( unencode_boxes= minibatch_reference_boxes_mattached_gtboxes_rotate, reference_boxes=minibatch_reference_boxes, scale_factors=self.scale_factors) ############### Class-agnostic Without tile # [minibatch_size, num_classes*4] # minibatch_encode_gtboxes = tf.tile(minibatch_encode_gtboxes, [1, self.num_classes]) ############### Class-agnostic Without tile # [minibatch_size, num_classes*5] # minibatch_encode_gtboxes_rotate = tf.tile(minibatch_encode_gtboxes_rotate, [1, self.num_classes]) ############### Class-agnostic Without tile # minibatch_gt_head_quadrant = tf.tile(minibatch_reference_boxes_mattached_head_quadrant, [1, self.num_classes]) minibatch_gt_head_quadrant = minibatch_reference_boxes_mattached_head_quadrant class_weights_list = [] category_list = tf.unstack(minibatch_label_one_hot, axis=1) for i in range(1, self.num_classes + 1): tmp_class_weights = tf.ones( shape=[tf.shape(minibatch_encode_boxes)[0], 4], dtype=tf.float32) tmp_class_weights = tmp_class_weights * tf.expand_dims( category_list[i], axis=1) class_weights_list.append(tmp_class_weights) class_weights = tf.concat( class_weights_list, axis=1) # [minibatch_size, num_classes*4] class_weights_list_rotate = [] category_list_rotate = tf.unstack(minibatch_label_one_hot, axis=1) for i in range(1, self.num_classes + 1): tmp_class_weights_rotate = tf.ones( shape=[tf.shape(minibatch_encode_boxes_rotate)[0], 5], dtype=tf.float32) tmp_class_weights_rotate = tmp_class_weights_rotate * tf.expand_dims( category_list_rotate[i], axis=1) class_weights_list_rotate.append(tmp_class_weights_rotate) class_weights_rotate = tf.concat( class_weights_list_rotate, axis=1) # [minibatch_size, num_classes*5] class_weights_list_head = [] category_list_head = tf.unstack(minibatch_label_one_hot, axis=1) for i in range(1, self.num_classes + 1): tmp_class_weights_head = tf.ones( shape=[tf.shape(minibatch_head_quadrant)[0], 4], dtype=tf.float32) tmp_class_weights_head = tmp_class_weights_head * tf.expand_dims( category_list_head[i], axis=1) class_weights_list_head.append(tmp_class_weights_head) class_weights_head = tf.concat(class_weights_list_head, axis=1) # loss with tf.variable_scope('fast_rcnn_classification_loss'): # fast_rcnn_classification_loss = slim.losses.softmax_cross_entropy(logits=minibatch_scores, # onehot_labels=minibatch_label_one_hot) fast_rcnn_classification_loss = losses.focal_loss( prediction_tensor=minibatch_scores, target_tensor=minibatch_label_one_hot) slim.losses.add_loss(fast_rcnn_classification_loss) with tf.variable_scope('fast_rcnn_location_loss'): # fast_rcnn_location_loss = losses.l1_smooth_losses(predict_boxes=minibatch_encode_boxes, # gtboxes=minibatch_encode_gtboxes, # object_weights=minibatch_object_mask, # classes_weights=class_weights) # Class-agnostic regression fast_rcnn_location_loss = losses.l1_smooth_losses( predict_boxes=minibatch_encode_boxes, gtboxes=minibatch_encode_gtboxes, object_weights=minibatch_object_mask, classes_weights=None) slim.losses.add_loss(fast_rcnn_location_loss) with tf.variable_scope('fast_rcnn_classification_rotate_loss'): # fast_rcnn_classification_rotate_loss = slim.losses.softmax_cross_entropy(logits=minibatch_scores_rotate, # onehot_labels=minibatch_label_one_hot) fast_rcnn_classification_rotate_loss = losses.focal_loss( prediction_tensor=minibatch_scores_rotate, target_tensor=minibatch_label_one_hot) slim.losses.add_loss(fast_rcnn_classification_rotate_loss) with tf.variable_scope('fast_rcnn_location_rotate_loss'): # fast_rcnn_location_rotate_loss = losses.l1_smooth_losses(predict_boxes=minibatch_encode_boxes_rotate, # gtboxes=minibatch_encode_gtboxes_rotate, # object_weights=minibatch_object_mask, # classes_weights=class_weights_rotate) # Class-agnostic regression fast_rcnn_location_rotate_loss = losses.l1_smooth_losses( predict_boxes=minibatch_encode_boxes_rotate, gtboxes=minibatch_encode_gtboxes_rotate, object_weights=minibatch_object_mask, classes_weights=None) slim.losses.add_loss(fast_rcnn_location_rotate_loss) with tf.variable_scope('fast_rcnn_head_quadrant_loss'): # fast_rcnn_head_quadrant_loss = losses.l1_smooth_losses(predict_boxes=minibatch_head_quadrant, # gtboxes=minibatch_gt_head_quadrant, # object_weights=minibatch_object_mask, # classes_weights=class_weights_head) # Class-agnostic regression fast_rcnn_head_quadrant_loss = losses.l1_smooth_losses( predict_boxes=minibatch_head_quadrant, gtboxes=minibatch_gt_head_quadrant, object_weights=minibatch_object_mask, classes_weights=None) slim.losses.add_loss( fast_rcnn_head_quadrant_loss * 10) # More importance by the bigger weight return fast_rcnn_location_loss, fast_rcnn_classification_loss, \ fast_rcnn_location_rotate_loss, fast_rcnn_classification_rotate_loss, fast_rcnn_head_quadrant_loss * 10