예제 #1
0
def _test_one_sentence(test_arguments, sentence, rois, rcnn, this_sentence_len, info):
    roi_num = rois.shape[0]
    ridx = np.zeros(roi_num).astype(int)
    original_predict_cls_score, predict_target_bbox = rcnn(sentence, rois, ridx)
    predict_cls_score = F.softmax(original_predict_cls_score, dim=1).data.cpu().numpy()  # softmax score for each row
    argmax_softmax = np.argmax(predict_cls_score, axis=1)
    if test_arguments.loss_weight_lambda == 0:
        predict_bbox = rois
        write_result(predict_bbox, argmax_softmax, np.max(predict_cls_score, axis=1),
                     test_arguments.result_output, info['gt_str'], None, None, 'TOI')
    else:
        predict_target_bbox = predict_target_bbox.data.cpu().numpy()
        if test_arguments.normalize:
            predict_target_bbox = denorm(predict_target_bbox, test_arguments)
        if test_arguments.dx_compute_method == "left_boundary":
            predict_bbox = lb_reg_to_bbox(this_sentence_len, predict_target_bbox, rois)
        else:
            predict_bbox = reg_to_bbox(this_sentence_len, predict_target_bbox, rois)

        new_predict_bbox = np.zeros(
            (predict_bbox.shape[1], predict_bbox.shape[0]))  # only select the meaningful bbox regression.
        for i in range(predict_bbox.shape[1]):  # for n region
            new_predict_bbox[i, :] = predict_bbox[:, i, argmax_softmax[i]]
        write_result(int_bbox(new_predict_bbox, this_sentence_len).astype(np.int32), argmax_softmax,
                     np.max(predict_cls_score, axis=1),
                     test_arguments.result_output, info['gt_str'], None, None, 'TOI')

        predict_bbox, rois, predict_cls_score = check_regression(new_predict_bbox, rois,
                                                                 original_predict_cls_score.data.cpu().numpy(),
                                                                 this_sentence_len)
        write_result(predict_bbox.astype(np.int32), np.argmax(predict_cls_score, axis=1),
                     np.max(predict_cls_score, axis=1),
                     test_arguments.result_output, info['gt_str'], None, None, 'after check LR')

    result_bbox = []
    result_cls = []
    drop_bbox = []
    drop_cls = []
    original_roi_ls = []
    c_score_ls = []

    if len(predict_bbox) > 0:
        # if True:
        argmax_softmax = np.argmax(predict_cls_score, axis=1)
        # predict_bbox, argmax_softmax = select_meaningful_bbox_regression(predict_bbox, predict_cls_score)

        score_indexes = score_filter(predict_cls_score, score_th=test_arguments.score_threshold)

        predict_bbox = predict_bbox[score_indexes, :]
        argmax_softmax = argmax_softmax[score_indexes]
        predict_cls_score = predict_cls_score[score_indexes]
        rois = rois[score_indexes]
        write_result(predict_bbox.astype(np.int32), argmax_softmax, np.max(predict_cls_score, axis=1),
                     test_arguments.result_output, info['gt_str'], None, None, 'after filter')
        for c in range(1, classes_num + 1):
            cls_index = np.where(argmax_softmax == c)
            if len(cls_index[0]) == 0:  # this cls type is empty
                continue
            c_cls_score = predict_cls_score[cls_index[0], c]
            c_bboxs = predict_bbox[cls_index[0], :]
            original_roi = rois[np.where(argmax_softmax == c)[0], :]
            boxes, _boxes, roi, c_score = non_maximum_suppression_fusion(c_cls_score, c_bboxs, original_roi,
                                                                         iou_threshold=test_arguments.th_nms_iou,
                                                                         score_threshold=test_arguments.score_threshold,
                                                                         info=info)
            result_bbox.extend(boxes)
            drop_bbox.extend(_boxes)
            original_roi_ls.extend(roi)
            c_score_ls.extend(c_score)
            result_cls.extend([c] * len(boxes))  # print the predict result of this sentence.
            drop_cls.extend([c] * len(_boxes))
    return np.array(result_bbox), np.array(result_cls), np.array(drop_bbox), np.array(drop_cls), np.array(
        original_roi_ls), np.array(c_score_ls)
예제 #2
0
def _test_one_sentence(file, test_arguments, sentence, rois, rcnn, fold_index,
                       this_sentence_len, info):
    roi_num = rois.shape[0]
    ridx = np.zeros(roi_num).astype(int)
    pred_cls_score, pred_tbbox = rcnn(sentence, rois, ridx)
    pred_cls_score = F.softmax(
        pred_cls_score, dim=1).data.cpu().numpy()  # softmax score for each row
    pred_tbbox = pred_tbbox.data.cpu().numpy()
    if test_arguments.normalize:
        pred_tbbox = denorm(pred_tbbox, fold_index, test_arguments)
    if test_arguments.dx_compute_method == "left_boundary":
        pred_bbox = lb_reg_to_bbox(this_sentence_len, pred_tbbox, rois)
    else:
        pred_bbox = reg_to_bbox(this_sentence_len, pred_tbbox, rois)
    original_pred_bbox = pred_bbox.copy()
    pred_bbox, argmax_softmax = select_meaningful_bbox_regression(
        pred_bbox, pred_cls_score)
    # rs =
    output_bbox = pred_bbox.T.copy()
    output_result_cls = np.argmax(pred_cls_score, axis=1).copy()
    int_bbox(output_bbox, len(info["gt_str"]))
    if test_arguments.output_flow:
        output_file_result(file, output_bbox, output_result_cls, rois, info,
                           np.max(pred_cls_score, axis=1))

    # if test_arguments.output_flow:
    #     output_detect_result(result_bbox, result_cls, original_rois, info, scores)

    # pred bbox shape is 2 * n_roi
    result_bbox = []
    result_cls = []
    drop_bbox = []
    drop_cls = []
    original_roi_ls = []
    c_score_ls = []
    for c in range(1, classes_num + 1):
        cls_index = np.where(argmax_softmax == c)
        if len(cls_index[0]) == 0:  # this cls type is empty
            continue
        c_cls_score = pred_cls_score[cls_index[0], c]
        c_bboxs = pred_bbox[:, cls_index[0]].T
        original_roi = rois[np.where(argmax_softmax == c)[0], :]

        boxes, _boxes, roi, c_score = non_maximum_suppression(
            c_cls_score,
            c_bboxs,
            original_roi,
            iou_threshold=test_arguments.th_nms_iou,
            score_threshold=test_arguments.score_threshold,
            info=info)
        result_bbox.extend(boxes)
        drop_bbox.extend(_boxes)
        original_roi_ls.extend(roi)
        c_score_ls.extend(c_score)
        result_cls.extend(
            [c] * len(boxes))  # print the predict result of this sentence.
        drop_cls.extend([c] * len(_boxes))

    return np.array(result_bbox), np.array(result_cls), np.array(
        drop_bbox), np.array(drop_cls), np.array(original_roi_ls), np.array(
            c_score_ls)
예제 #3
0
def _test_one_sentence(file_before, test_arguments, sentence, rois, rcnn, fold_index, this_sentence_len,
                       info):
    roi_num = rois.shape[0]
    ridx = np.zeros(roi_num).astype(int)
    if roi_num != 0:    # 候选框数量不为0
        original_pred_cls_score, pred_tbbox = rcnn(sentence, rois, ridx)
        pred_cls_score = F.softmax(original_pred_cls_score, dim=1).data.cpu().numpy()  # softmax score for each row
        argmax_softmax = np.argmax(pred_cls_score, axis=1)  # 预测类型
        if test_arguments.loss_weight_lambda == 0:
            pred_bbox = rois
            write_result(pred_bbox, argmax_softmax, np.max(pred_cls_score, axis=1),
                         test_arguments.result_output, info['gt_str'], None, None, 'TOI')   # 输出TOI
        else:   # LR模型需要对候选框进行回归
            pred_tbbox = pred_tbbox.data.cpu().numpy()
            if test_arguments.normalize:
                pred_tbbox = denorm(pred_tbbox, fold_index, test_arguments)
            if test_arguments.dx_compute_method == "left_boundary":
                pred_bbox = lb_reg_to_bbox(this_sentence_len, pred_tbbox, rois)
            else:
                pred_bbox = reg_to_bbox(this_sentence_len, pred_tbbox, rois)

            new_pred_bbox = np.zeros(
                (pred_bbox.shape[1], pred_bbox.shape[0]))  # only select the meaningful bbox regression.
            for i in range(pred_bbox.shape[1]):  # for n region
                new_pred_bbox[i, :] = pred_bbox[:, i, argmax_softmax[i]]
            write_result(int_bbox(new_pred_bbox, this_sentence_len).astype(np.int32), argmax_softmax,
                         np.max(pred_cls_score, axis=1),
                         test_arguments.result_output, info['gt_str'], None, None, 'TOI')   # 输出TOI

            # 去除回归过大的候选框,输出结果
            pred_bbox, rois, pred_cls_score = check_regression(new_pred_bbox, rois,
                                                               original_pred_cls_score.data.cpu().numpy(),
                                                               this_sentence_len, test_arguments.th_train_iou)
            write_result(pred_bbox.astype(np.int32), np.argmax(pred_cls_score, axis=1), np.max(pred_cls_score, axis=1),
                         test_arguments.result_output, info['gt_str'], None, None, 'after check LR')
    else:
        pred_bbox = np.array([])

    result_bbox = []
    result_cls = []
    drop_bbox = []
    drop_cls = []
    original_roi_ls = []
    c_score_ls = []

    if len(pred_bbox) > 0:
        # if True:
        argmax_softmax = np.argmax(pred_cls_score, axis=1)  # 各种处理过后,重新判断预测类型
        # pred_bbox, argmax_softmax = select_meaningful_bbox_regression(pred_bbox, pred_cls_score)

        # filter去除得分小于s_th的候选框,输出结果
        score_indexs = score_filter(pred_cls_score, score_th=test_arguments.score_threshold)
        pred_bbox = pred_bbox[score_indexs, :]
        argmax_softmax = argmax_softmax[score_indexs]
        pred_cls_score = pred_cls_score[score_indexs]
        rois = rois[score_indexs]
        write_result(pred_bbox.astype(np.int32), argmax_softmax, np.max(pred_cls_score, axis=1),
                     test_arguments.result_output, info['gt_str'], None, None, 'after filter')

        # output_bbox = pred_bbox.T.copy()
        # output_result_cls = np.argmax(pred_cls_score, axis=1).copy()
        # int_bbox(output_bbox, len(info["gt_str"]))
        # output_file_result(file_before, output_bbox, output_result_cls, rois, info, np.max(pred_cls_score, axis=1))
        # if test_arguments.output_flow:
        #     output_detect_result(output_bbox, output_result_cls, rois, info, np.max(pred_cls_score, axis=1))
        # 
        # if test_arguments.output_flow:
        #     output_detect_result(result_bbox, result_cls, original_rois, info, scores)

        # pred bbox shape is 2 * n_roi
        # 按照类型进行nms: mix、length、score
        for c in range(1, classes_num + 1):
            cls_index = np.where(argmax_softmax == c)
            if len(cls_index[0]) == 0:  # this cls type is empty
                continue
            c_cls_score = pred_cls_score[cls_index[0], c]
            c_bboxs = pred_bbox[cls_index[0], :]
            original_roi = rois[np.where(argmax_softmax == c)[0], :]
            if c == 1 or c == 7:
                boxes, _boxes, roi, c_score = non_maximum_suppression_mix(c_cls_score, c_bboxs, original_roi,
                                                                      iou_threshold=test_arguments.th_nms_iou,
                                                                      score_threshold=test_arguments.score_threshold,
                                                                      info=info)
            else:
                boxes, _boxes, roi, c_score = non_maximum_suppression_mix(c_cls_score, c_bboxs, original_roi,
                                                                      iou_threshold=test_arguments.th_nms_iou,
                                                                      score_threshold=test_arguments.score_threshold,
                                                                      info=info)
            result_bbox.extend(boxes)
            drop_bbox.extend(_boxes)
            original_roi_ls.extend(roi)
            c_score_ls.extend(c_score)
            result_cls.extend([c] * len(boxes))  # print the predict result of this sentence.
            drop_cls.extend([c] * len(_boxes))
    # output_file_result(file_after, result_bbox, result_cls, original_roi_ls, info, np.max(pred_cls_score, axis=1))

    return np.array(result_bbox), np.array(result_cls), np.array(drop_bbox), np.array(drop_cls), np.array(
        original_roi_ls), np.array(c_score_ls)
예제 #4
0
def _test_one_sentence_all_regression(test_arguments, sentence, rois, rcnn,
                                      fold_index, this_sentence_len, info):
    roi_num = rois.shape[0]
    ridx = np.zeros(roi_num).astype(int)
    pred_cls_score, pred_tbbox = rcnn(sentence, rois, ridx)
    pred_cls_score = F.softmax(
        pred_cls_score, dim=1).data.cpu().numpy()  # softmax score for each row
    pred_tbbox = pred_tbbox.data.cpu().numpy()
    if test_arguments.normalize:
        pred_tbbox = denorm(pred_tbbox, fold_index, test_arguments)
    if test_arguments.dx_compute_method == "left_boundary":
        pred_bbox = lb_reg_to_bbox(this_sentence_len, pred_tbbox, rois)
    else:
        pred_bbox = reg_to_bbox(this_sentence_len, pred_tbbox, rois)

    result_bbox = []
    result_cls = []
    drop_bbox = []
    drop_cls = []
    original_roi_ls = []
    c_score_ls = []
    for c in range(1, classes_num + 1):
        c_cls_score = pred_cls_score[:, c]
        c_bboxs = pred_bbox[:, :, c].T
        boxes, _boxes, roi, c_score = non_maximum_suppression_all_regression(
            c_cls_score,
            c_bboxs,
            rois.copy(),
            iou_threshold=test_arguments.th_nms_iou,
            score_threshold=test_arguments.score_threshold,
            info=info)
        result_bbox.extend(boxes)
        drop_bbox.extend(_boxes)
        original_roi_ls.extend(roi)
        c_score_ls.extend(c_score)
        result_cls.extend(
            [c] * len(boxes))  # print the predict result of this sentence.
        drop_cls.extend([c] * len(_boxes))

    if len(
            result_cls
    ) == 0:  # if the sentence without detecting anything!, we will lower the score criterion
        lower_than_th_count[0] += 1
        drop_bbox = []
        drop_cls = []
        for c in range(1, classes_num + 1):
            c_sc = pred_cls_score[:, c]
            c_bboxs = pred_bbox[:, :, c].T
            boxes, _boxes, roi, c_score = non_maximum_suppression(
                c_sc,
                c_bboxs,
                rois.copy(),
                iou_threshold=test_arguments.th_nms_iou,
                score_threshold=test_arguments.score_threshold / 6,
                info=info)
            result_bbox.extend(boxes)
            drop_bbox.extend(_boxes)
            original_roi_ls.extend(roi)
            c_score_ls.extend(c_score)
            result_cls.extend([c] * len(boxes))
            drop_cls.extend([c] * len(_boxes))
        # result_bbox = result_bbox[:1]
        # result_cls = result_cls[:1]
        # drop_bbox += result_bbox[1:]
        # drop_cls += result_cls[1:]
        # original_roi_ls = original_roi_ls[:1]
        # c_score_ls = c_score_ls[:1]

    return np.array(result_bbox), np.array(result_cls), np.array(
        drop_bbox), np.array(drop_cls), np.array(original_roi_ls), np.array(
            c_score_ls)