Exemplo n.º 1
0
def test(test_idx=None):

    # Get parameters
    net_rpn, pxl_rpn, ids_rpn = load_net(cfg.NET_DIR_RPN, cfg.NET_NAME_RPN)
    net_clf, pxl_clf, ids_clf = load_net(cfg.NET_DIR_CLF, cfg.NET_NAME_CLF)
    w_b = np.load(os.path.join("data", "cancer-du-sein", "w_b.npy"))

    # Load test db
    db_dir = os.path.join("/", "data", "train_extracts", "radio_extractions")
    test_db = np.load(os.path.join(db_dir, "imdb_val_cds_rpn_20200928.npy"))

    # Init test metrics
    stats = {'ok': 0, 'cmpt': 0}
    roc = {"scores": [], "labels": []}

    # Loop over test
    for idx, im_roidb in enumerate(test_db):
        # DEBUG
        if test_idx is not None:
            im_roidb = test_db[test_idx]
        print(idx, im_roidb["name"])

        # Load image
        img = np.load(im_roidb["name"])

        # Detect nodules
        rois, ids, scores = get_rpn_rois(img, net_rpn, pxl_rpn, ids_rpn,
                                         cfg.NMS_THRESH, cfg.NMS_THRESH_CLS, cfg.CONF_THRESH)

        # Classify detected nodules
        clf_ids = ["malin"]
        if len(rois) > 0:
            rois = rois[np.argmax(scores)][np.newaxis, :]
            clf_ids, clf_scores = classify_rois_cds(img, rois, net_clf, pxl_clf, ids_clf)
            # clf_id, clf_score = classify_rois_cds_svm(img, w_b, rois, net_clf, pxl_clf, ids_clf)
            # import ipdb; ipdb.set_trace()
        else:
            print("no detected nodule")

        # Get ground truth
        gt_clf = load_diagnosis("cancer-du-sein", im_roidb["photo_id"])

        # Update stats
        stats["cmpt"] += 1
        if gt_clf == clf_ids[0]:
        # if gt_clf == clf_id:
            stats["ok"] += 1
        else:
            print([[gt_clf], [clf_ids[0]]])
        # plot_rectangle(img, rois)
        # import ipdb; ipdb.set_trace()
        # Update roc
        roc["labels"].append(int(gt_clf == "malin"))
        roc["scores"].append(clf_scores[0, 1])
        # roc["scores"].append(clf_score[0])

    # Print results
    print_results(stats)
    # Get AUC score
    compute_test_score(roc)
Exemplo n.º 2
0
def analyze_image(img_path):
    """
    Analyze an image coming from the self-checkout machine
        - Extract possible region of interest (roi) using both ycnn algo
        and circle detection.
        - Classfify each roi using either matching, svm or size classification.

    Input:
        - im_path:  string      path to image 'media/analysis/im_name.jpg'
    """

    # Get parameters
    net_rpn, pxl_rpn, ids_rpn = load_net(cfg.NET_DIR_RPN, cfg.NET_NAME_RPN)
    net_clf, pxl_clf, ids_clf = load_net(cfg.NET_DIR_CLF, cfg.NET_NAME_CLF)

    # Load image
    img = np.squeeze(load_image(img_path, tile_image=False, transpose=False))
    img = np.tile(img[:, :, np.newaxis], (1, 1, 3))

    # Detect menisques
    rois, ids, scores = get_rpn_rois(img, net_rpn, pxl_rpn, ids_rpn,
                                     cfg.NMS_THRESH, cfg.NMS_THRESH_CLS,
                                     cfg.CONF_THRESH)

    # Classify each menisque
    clf_ids = classify_rois_cds(img, rois, net_clf, pxl_clf, ids_clf)

    # Plot results if needed
    if PLOT and img is not None:
        plot_rectangle(img, rois, clf_ids)

    return rois, clf_ids
Exemplo n.º 3
0
def analyze_image(im_path):

    """
    Analyze an image coming from the self-checkout machine
        - Extract possible region of interest (roi) using both ycnn algo
        and circle detection.
        - Classfify each roi using either matching, svm or size classification.

    Input:
        - im_path:  string      path to image 'media/analysis/im_name.jpg'
    """

    # Get parameters
    CACHE_MANAGER = CacheManager()
    net_rpn, pxl_rpn, ids_rpn = CACHE_MANAGER.get_net_rpn()
    net_clf, pxl_clf, ids_clf = CACHE_MANAGER.get_net_clf()

    # Load image
    im = load_image(im_path)

    # Detect menisques
    rois, ids, scores = get_rpn_rois(im, net_rpn, pxl_rpn, ids_rpn,
                                     cfg.NMS_THRESH, cfg.NMS_THRESH_CLS, cfg.CONF_THRESH)

    # Classify each menisque
    clf_ids = classify_rois(im, rois, net_clf, pxl_clf, ids_clf)

    # Plot results if needed
    if PLOT and im is not None:
        plot_rectangle(im, rois, clf_ids)

    return rois, clf_ids
Exemplo n.º 4
0
def export_results():

    # Init paths
    root_dir = os.path.join("/", "home", "yann", "radioAdvisor")
    data_dir = os.path.join(root_dir, "data", "cancer-du-sein",
                            "sifem_validation")
    im_dir = os.path.join(root_dir, "data", "cancer-du-sein", "test-images")

    # Load db
    db = os.listdir(data_dir)

    # Get parameters
    net_rpn, pxl_rpn, ids_rpn = load_net(cfg.NET_DIR_RPN, cfg.NET_NAME_RPN)
    net_clf, pxl_clf, ids_clf = load_net(cfg.NET_DIR_CLF, cfg.NET_NAME_CLF)

    # Test images
    # exam_ids, exam_scores = [], []
    results = []
    for idx, filename in enumerate(db):

        if not filename.endswith(".nii.gz") or "._" in filename:
            continue
        print(idx, filename)

        # Load image if needed
        exam_id = filename.split(".")[0]
        data_path = os.path.join(data_dir, filename)
        img_path = os.path.join(im_dir, "%s.npy" % exam_id)
        if not os.path.exists(img_path):
            img = np.squeeze(
                load_image(data_path, tile_image=False, transpose=False))
            img = np.tile(img[:, :, np.newaxis], (1, 1, 3))
            np.save(img_path, img)
        else:
            img = np.load(img_path)

        # Detect nodules
        rois, _, det_scores = get_rpn_rois(img, net_rpn, pxl_rpn, ids_rpn,
                                           cfg.NMS_THRESH, cfg.NMS_THRESH_CLS,
                                           cfg.CONF_THRESH)

        # Classify detected nodules
        clf_scores = np.array([[0.5, 0.5]])
        if len(rois) > 0:
            rois = rois[np.argmax(det_scores)][np.newaxis, :]
            _, clf_scores = classify_rois_cds(img, rois, net_clf, pxl_clf,
                                              ids_clf)

        # Compute area
        results.append({"examen": exam_id, "prediction": clf_scores[0, 1]})

    # Create csv
    create_csv(results)
Exemplo n.º 5
0
def test(csv_name=None):

    # Get parameters
    CACHE_MANAGER = CacheManager()
    net_rpn, pxl_rpn, ids_rpn = CACHE_MANAGER.get_net_rpn()
    net_f_clf, pxl_f_clf, ids_f_clf = CACHE_MANAGER.get_net_f_clf()
    net_o_clf, pxl_o_clf, ids_o_clf = CACHE_MANAGER.get_net_o_clf()

    # Load test db
    data_dir = os.path.join("data", "test_data")
    data = {
        filename.split(".")[0]: []
        for filename in os.listdir(data_dir)
        if filename.endswith(".nii.gz") and "._" not in filename
    }
    if csv_name is not None:
        data = parse_csv(csv_name)

    # Loop over test
    test_names = []
    test_f_scores, test_l_scores, test_o_scores = np.zeros(0), np.zeros(
        (0, 2)), np.zeros((0, 2))
    for idx, im_name in enumerate(data.keys()):
        # DEBUG
        print idx, im_name

        # Load image
        im_dir = "test_data" if csv_name is None else "raw_data"
        im_path = os.path.join("data", im_dir, "%s.nii.gz" % im_name)
        im = load_image(im_path)

        # Detect menisques
        rois, _, _ = get_rpn_rois(im, net_rpn, pxl_rpn, ids_rpn,
                                  cfg.NMS_THRESH, cfg.NMS_THRESH_CLS,
                                  cfg.CONF_THRESH)

        # Classify each menisque
        if cfg.NET_DIR_F_CLF is not None:
            clf_ids, f_score, l_scores, o_scores = classify_rois(
                im, rois, net_f_clf, pxl_f_clf, ids_f_clf, net_o_clf,
                pxl_o_clf, ids_o_clf)
        else:
            clf_ids, f_score, l_scores, o_scores = classify_rois_(
                im, rois, net_o_clf, pxl_o_clf, ids_o_clf)

        # Store results
        test_names.append(im_name)
        test_f_scores = np.hstack((test_f_scores, f_score))
        test_l_scores = np.vstack((test_l_scores, l_scores))
        test_o_scores = np.vstack((test_o_scores, o_scores))

    create_csv(test_names, test_f_scores, test_l_scores, test_o_scores)
Exemplo n.º 6
0
def test(test_idx=None):

    # Get parameters
    CACHE_MANAGER = CacheManager()
    net_rpn, pxl_rpn, ids_rpn = CACHE_MANAGER.get_net_rpn()
    net_f_clf, pxl_f_clf, ids_f_clf = CACHE_MANAGER.get_net_f_clf()
    net_o_clf, pxl_o_clf, ids_o_clf = CACHE_MANAGER.get_net_o_clf()

    # Load test db
    test_db = np.load(
        os.path.join("database", "clf", "imdb_test_clf_radio_v2.npy"))

    # Init test metrics
    stats = {
        'well_cl': 0,
        'bad_cl': 0,
        'cmpt_cl': 0,
        'well_det': 0,
        'bad_det': 0,
        'fp': 0,
        'cmpt_det': 0,
        'all_well': 0,
        'all_cmpt': 0
    }
    roc = {
        "fissure": {
            "scores": [],
            "label": []
        },
        "localisation": {
            "scores": [],
            "label": []
        },
        "orientation": {
            "scores": [],
            "label": []
        }
    }

    # Loop over test
    for idx, im_roidb in enumerate(test_db):
        # DEBUG
        if test_idx is not None:
            im_roidb = test_db[test_idx]
        print idx, im_roidb["name"]

        # Load image
        im_name = im_roidb["name"].split("/")[-1].split(".")[0]
        im_path = os.path.join("data", "raw_data", "%s.nii.gz" % im_name)
        im = load_image(im_path)

        # Detect menisques
        rois, _, _ = get_rpn_rois(im, net_rpn, pxl_rpn, ids_rpn,
                                  cfg.NMS_THRESH, cfg.NMS_THRESH_CLS,
                                  cfg.CONF_THRESH)

        # Classify each menisque
        if cfg.NET_DIR_F_CLF is not None:
            clf_ids, f_score, l_scores, o_scores = classify_rois(
                im, rois, net_f_clf, pxl_f_clf, ids_f_clf, net_o_clf,
                pxl_o_clf, ids_o_clf)
        else:
            clf_ids, f_score, l_scores, o_scores = classify_rois_(
                im, rois, net_o_clf, pxl_o_clf, ids_o_clf)

        # Evaluate results
        evaluate_results(im, im_roidb["boxes"], rois, clf_ids, stats)

        # Evaluate roc
        evaluate_roc(im, im_roidb["boxes"], f_score, l_scores, o_scores, roc)

    # Print results
    print_results(stats)
    # Get AUC score
    compute_test_score(roc)