def call(self, inputs):
     rpn_bbox_deltas = inputs[0]
     rpn_labels = inputs[1]
     anchors = self.anchors
     #
     pre_nms_topn = self.hyper_params["pre_nms_topn"]
     post_nms_topn = self.hyper_params["train_nms_topn"] if self.mode == "training" else self.hyper_params[
         "test_nms_topn"]
     nms_iou_threshold = self.hyper_params["nms_iou_threshold"]
     variances = self.hyper_params["variances"]
     total_anchors = anchors.shape[0]
     batch_size = tf.shape(rpn_bbox_deltas)[0]
     rpn_bbox_deltas = tf.reshape(rpn_bbox_deltas, (batch_size, total_anchors, 4))
     rpn_labels = tf.reshape(rpn_labels, (batch_size, total_anchors))
     #
     rpn_bbox_deltas *= variances
     rpn_bboxes = bbox_utils.get_bboxes_from_deltas(anchors, rpn_bbox_deltas)
     #
     _, pre_indices = tf.nn.top_k(rpn_labels, pre_nms_topn)
     #
     pre_roi_bboxes = tf.gather(rpn_bboxes, pre_indices, batch_dims=1)
     pre_roi_labels = tf.gather(rpn_labels, pre_indices, batch_dims=1)
     #
     pre_roi_bboxes = tf.reshape(pre_roi_bboxes, (batch_size, pre_nms_topn, 1, 4))
     pre_roi_labels = tf.reshape(pre_roi_labels, (batch_size, pre_nms_topn, 1))
     #
     roi_bboxes, _, _, _ = bbox_utils.non_max_suppression(pre_roi_bboxes, pre_roi_labels,
                                                          max_output_size_per_class=post_nms_topn,
                                                          max_total_size=post_nms_topn,
                                                          iou_threshold=nms_iou_threshold)
     #
     return tf.stop_gradient(roi_bboxes)
 def call(self, inputs):
     roi_bboxes = inputs[0]
     pred_deltas = inputs[1]
     pred_label_probs = inputs[2]
     batch_size = tf.shape(pred_deltas)[0]
     #
     pred_deltas = tf.reshape(pred_deltas, (batch_size, -1, self.total_labels, 4))
     pred_deltas *= self.variances
     #
     expanded_roi_bboxes = tf.tile(tf.expand_dims(roi_bboxes, -2), (1, 1, self.total_labels, 1))
     pred_bboxes = bbox_utils.get_bboxes_from_deltas(expanded_roi_bboxes, pred_deltas)
     #
     pred_labels_map = tf.expand_dims(tf.argmax(pred_label_probs, -1), -1)
     pred_labels = tf.where(tf.not_equal(pred_labels_map, 0), pred_label_probs, tf.zeros_like(pred_label_probs))
     #
     final_bboxes, final_scores, final_labels, _ = bbox_utils.non_max_suppression(
         pred_bboxes, pred_labels,
         max_output_size_per_class=self.max_total_size,
         max_total_size=self.max_total_size,
         score_threshold=self.score_threshold)
     #
     return final_bboxes, final_labels, final_scores
Example #3
0
for image_data in test_data:
    img, _, _ = image_data
    pred_bbox_deltas, pred_labels = ssd_model.predict_on_batch(img)
    #
    pred_bbox_deltas *= hyper_params["variances"]
    pred_bboxes = bbox_utils.get_bboxes_from_deltas(prior_boxes,
                                                    pred_bbox_deltas)
    pred_bboxes = tf.clip_by_value(pred_bboxes, 0, 1)
    #
    pred_labels = tf.cast(pred_labels, tf.float32)
    reshaped_pred_bboxes = tf.reshape(
        pred_bboxes, (batch_size, pred_bbox_deltas.shape[1], 1, 4))
    # Remove background predictions
    pred_labels_map = tf.argmax(pred_labels, 2, output_type=tf.int32)
    valid_cond = tf.not_equal(pred_labels_map, bg_id)
    #
    valid_bboxes = tf.expand_dims(reshaped_pred_bboxes[valid_cond], 0)
    valid_labels = tf.expand_dims(pred_labels[valid_cond], 0)
    #
    nms_bboxes, nmsed_scores, nmsed_classes, valid_detections = bbox_utils.non_max_suppression(
        valid_bboxes,
        valid_labels,
        max_output_size_per_class=10,
        max_total_size=200,
        score_threshold=0.5)
    denormalized_bboxes = bbox_utils.denormalize_bboxes(
        nms_bboxes[0], img_size, img_size)
    drawing_utils.draw_bboxes_with_labels(img[0], denormalized_bboxes,
                                          nmsed_classes[0], nmsed_scores[0],
                                          labels)