Ejemplo n.º 1
0
 def call(self, inputs):
     rpn_bbox_deltas = inputs[0]
     rpn_labels = inputs[1]
     anchors = inputs[2]
     gt_boxes = inputs[3]
     #
     total_pos_bboxes = self.hyper_params["total_pos_bboxes"]
     total_neg_bboxes = self.hyper_params["total_neg_bboxes"]
     total_bboxes = total_pos_bboxes + total_neg_bboxes
     anchors_shape = tf.shape(anchors)
     batch_size, total_anchors = anchors_shape[0], anchors_shape[1]
     rpn_bbox_deltas = tf.reshape(rpn_bbox_deltas, (batch_size, total_anchors, 4))
     rpn_labels = tf.reshape(rpn_labels, (batch_size, total_anchors, 1))
     #
     rpn_bboxes = helpers.get_bboxes_from_deltas(anchors, rpn_bbox_deltas)
     rpn_bboxes = tf.reshape(rpn_bboxes, (batch_size, total_anchors, 1, 4))
     nms_bboxes, _, _, _ = helpers.non_max_suppression(rpn_bboxes, rpn_labels,
                                                       max_output_size_per_class=self.hyper_params["nms_topn"],
                                                       max_total_size=self.hyper_params["nms_topn"])
     ################################################################################################################
     pos_bbox_indices, neg_bbox_indices, gt_box_indices = helpers.get_selected_indices(nms_bboxes, gt_boxes, total_pos_bboxes, total_neg_bboxes)
     #
     pos_roi_bboxes = tf.gather(nms_bboxes, pos_bbox_indices, batch_dims=1)
     neg_roi_bboxes = tf.zeros((batch_size, total_neg_bboxes, 4), tf.float32)
     roi_bboxes = tf.concat([pos_roi_bboxes, neg_roi_bboxes], axis=1)
     return tf.stop_gradient(roi_bboxes), tf.stop_gradient(gt_box_indices)
Ejemplo n.º 2
0
 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["post_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 = helpers.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, _, _, _ = helpers.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)
Ejemplo n.º 3
0
VOC_test_data = VOC_test_data.padded_batch(batch_size, padded_shapes=padded_shapes, padding_values=padding_values)

base_model = VGG16(include_top=False)
if hyper_params["stride"] == 16:
    base_model = Sequential(base_model.layers[:-1])
rpn_model = rpn.get_model(base_model, hyper_params)

frcnn_model_path = helpers.get_model_path("frcnn", hyper_params["stride"])
rpn_model_path = helpers.get_model_path("rpn", hyper_params["stride"])
model_path = frcnn_model_path if load_weights_from_frcnn else rpn_model_path
rpn_model.load_weights(model_path, by_name=True)

for image_data in VOC_test_data:
    img, gt_boxes, gt_labels = image_data
    input_img, anchors = rpn.get_step_data(image_data, hyper_params, preprocess_input, mode="inference")
    rpn_bbox_deltas, rpn_labels = rpn_model.predict_on_batch(input_img)
    #
    anchors_shape = tf.shape(anchors)
    batch_size, anchor_row_size = anchors_shape[0], anchors_shape[1]
    rpn_bbox_deltas = tf.reshape(rpn_bbox_deltas, (batch_size, anchor_row_size, 4))
    rpn_labels = tf.reshape(rpn_labels, (batch_size, anchor_row_size, 1))
    #
    rpn_bboxes = helpers.get_bboxes_from_deltas(anchors, rpn_bbox_deltas)
    rpn_bboxes = tf.reshape(rpn_bboxes, (batch_size, anchor_row_size, 1, 4))
    #
    nms_bboxes, _, _, _ = helpers.non_max_suppression(rpn_bboxes, rpn_labels,
                                                max_output_size_per_class=hyper_params["nms_topn"],
                                                max_total_size=hyper_params["nms_topn"])
    img_float32 = tf.image.convert_image_dtype(img, tf.float32)
    helpers.draw_bboxes(img_float32, nms_bboxes)
Ejemplo n.º 4
0
rpn_model = rpn.get_model(base_model, hyper_params)
frcnn_model = faster_rcnn.get_model(base_model,
                                    rpn_model,
                                    hyper_params,
                                    mode=mode)
#
frcnn_model_path = helpers.get_model_path("frcnn", hyper_params["stride"])
frcnn_model.load_weights(frcnn_model_path)

for image_data in VOC_test_data:
    img, gt_boxes, gt_labels = image_data
    input_img, anchors = rpn.get_step_data(image_data,
                                           hyper_params,
                                           preprocess_input,
                                           mode=mode)
    frcnn_pred = frcnn_model.predict_on_batch([input_img, anchors, gt_boxes])
    roi_bboxes, rpn_reg_pred, rpn_cls_pred, frcnn_reg_pred, frcnn_cls_pred = frcnn_pred
    # We remove background predictions and reshape outputs for non max suppression
    valid_pred_bboxes, valid_pred_labels = faster_rcnn.get_valid_predictions(
        roi_bboxes, frcnn_reg_pred, frcnn_cls_pred,
        hyper_params["total_labels"])

    nms_bboxes, nmsed_scores, nmsed_classes, valid_detections = helpers.non_max_suppression(
        valid_pred_bboxes,
        valid_pred_labels,
        max_output_size_per_class=3,
        max_total_size=12,
        score_threshold=0.7)
    helpers.draw_bboxes_with_labels(img[0], nms_bboxes[0], nmsed_classes[0],
                                    nmsed_scores[0], labels)