예제 #1
0
def val_batch(batch_num, b, evaluator, evaluator_multiple_preds, evaluator_list, evaluator_multiple_preds_list, thrs=(20, 50, 100)):
    det_res = detector[b]
    if conf.num_gpus == 1:
        det_res = [det_res]

    for i, (boxes_i, objs_i, obj_scores_i, rels_i, pred_scores_i) in enumerate(det_res):
        gt_entry = {
            'gt_classes': val.gt_classes[batch_num + i].copy(),
            'gt_relations': val.relationships[batch_num + i].copy(),
            'gt_boxes': val.gt_boxes[batch_num + i].copy(),
        }
        assert np.all(objs_i[rels_i[:,0]] > 0) and np.all(objs_i[rels_i[:,1]] > 0)
        # assert np.all(rels_i[:,2] > 0)

        pred_entry = {
            'pred_boxes': boxes_i * BOX_SCALE/IM_SCALE,
            'pred_classes': objs_i,
            'pred_rel_inds': rels_i,
            'obj_scores': obj_scores_i,
            'rel_scores': pred_scores_i,
        }
        all_pred_entries.append(pred_entry)

        eval_entry(conf.mode, gt_entry, pred_entry, evaluator, evaluator_multiple_preds, 
                   evaluator_list, evaluator_multiple_preds_list)
예제 #2
0
파일: main.py 프로젝트: donggu-kang/sgg
def val_batch(batch_num, b, evaluator, eval_m, val_dataset, evaluator_list,
              evaluator_multiple_preds_list):

    if conf.detector == 'mrcnn':
        scale = 1.
        box_threshs = [0.2, 0.05, 0.01]
    else:
        scale = BOX_SCALE / IM_SCALE
        box_threshs = [None]

    pred_entries = []
    for box_score_thresh in box_threshs:
        detector.set_box_score_thresh(box_score_thresh)
        try:
            det_res = detector.forward_parallel(
                b)  # keep as it was in the original code

            if conf.num_gpus == 1:
                det_res = [det_res]

            for i, (boxes_i, objs_i, obj_scores_i, rels_i,
                    pred_scores_i) in enumerate(det_res):

                if conf.split == 'stanford':
                    w, h = b[i][1][0, :2]
                    scale_gt = 1. / (BOX_SCALE / max(w, h))
                else:
                    scale_gt = 1.

                gt_entry = {
                    'gt_classes':
                    val_dataset.gt_classes[batch_num + i].copy(),
                    'gt_relations':
                    val_dataset.relationships[batch_num + i].copy(),
                    'gt_boxes':
                    val_dataset.gt_boxes[batch_num + i].copy() * scale_gt,
                }

                pred_entry = {
                    'pred_boxes': boxes_i * scale,
                    'pred_classes': objs_i,
                    'pred_rel_inds': rels_i,
                    'obj_scores': obj_scores_i,
                    'rel_scores': pred_scores_i,  # hack for now.
                }
                pred_entries.append(pred_entry)

                for sfx in ['', '_nogc']:
                    evaluator[eval_m + sfx].evaluate_scene_graph_entry(
                        gt_entry, pred_entry)

                if evaluator_list is not None and len(evaluator_list) > 0:
                    eval_entry(eval_m, gt_entry, pred_entry, evaluator_list,
                               evaluator_multiple_preds_list)

            return pred_entries

        except (ValueError, IndexError) as e:
            print('no objects or relations found'.upper(), e, b[0][-1],
                  'trying a smaller threshold')
예제 #3
0
def val_batch(batch_num, b, evaluator, evaluator_multiple_preds,
              evaluator_list, evaluator_multiple_preds_list):
    det_res = detector[b]

    # pdb.set_trace()

    if conf.num_gpus == 1:
        det_res = [det_res]

    for i, (boxes_i, objs_i, obj_scores_i, rels_i,
            pred_scores_i) in enumerate(det_res):

        gt_entry = {
            'gt_classes': val.gt_classes[batch_num + i].copy(),  #(23,) (16,)
            'gt_relations':
            val.relationships[batch_num + i].copy(),  #(29, 3) (6, 3)
            'gt_boxes': val.gt_boxes[batch_num + i].copy(),  #(23, 4) (16, 4)
        }

        assert np.all(objs_i[rels_i[:, 0]] > 0) and np.all(
            objs_i[rels_i[:, 1]] > 0)

        pred_entry = {
            'pred_boxes': boxes_i * BOX_SCALE / IM_SCALE,  #(23, 4) (16, 4)
            'pred_classes': objs_i,  #(23,) (16,)
            'pred_rel_inds': rels_i,  #(506, 2) (240, 2)
            'obj_scores': obj_scores_i,  #(23,) (16,)
            'rel_scores': pred_scores_i,  # hack for now. (506, 51) (240, 51)
        }

        gt_entry, pred_entry = glat_postprocess(gt_entry,
                                                pred_entry,
                                                if_predicting=True)

        eval_entry(conf.mode, gt_entry, pred_entry, evaluator,
                   evaluator_multiple_preds, evaluator_list,
                   evaluator_multiple_preds_list)
예제 #4
0
        continue
    evaluator_list.append((index, name, BasicSceneGraphEvaluator.all_modes()))
    evaluator_multiple_preds_list.append((index, name, BasicSceneGraphEvaluator.all_modes(multiple_preds=True)))

if conf.cache is not None and os.path.exists(conf.cache):
    print("Found {}! Loading from it".format(conf.cache))
    with open(conf.cache,'rb') as f:
        all_pred_entries = pkl.load(f)
    for i, pred_entry in enumerate(tqdm(all_pred_entries)):
        gt_entry = {
            'gt_classes': val.gt_classes[i].copy(),
            'gt_relations': val.relationships[i].copy(),
            'gt_boxes': val.gt_boxes[i].copy(),
        }

        eval_entry(conf.mode, gt_entry, pred_entry, evaluator, evaluator_multiple_preds, 
                   evaluator_list, evaluator_multiple_preds_list)

    recall = evaluator[conf.mode].print_stats()
    recall_mp = evaluator_multiple_preds[conf.mode].print_stats()
    
    mean_recall = calculate_mR_from_evaluator_list(evaluator_list, conf.mode, save_file=conf.save_rel_recall)
    mean_recall_mp = calculate_mR_from_evaluator_list(evaluator_multiple_preds_list, conf.mode, multiple_preds=True, save_file=conf.save_rel_recall)

else:
    detector.eval()
    for val_b, batch in enumerate(tqdm(val_loader)):
        val_batch(conf.num_gpus*val_b, batch, evaluator, evaluator_multiple_preds, evaluator_list, evaluator_multiple_preds_list)

    recall = evaluator[conf.mode].print_stats()
    recall_mp = evaluator_multiple_preds[conf.mode].print_stats()