Exemplo n.º 1
0
def visual_results_jsrt_only(model_name, args):
    print "Visual results for model {} JSRT only".format(model_name)
    imgs, blobs = jsrt.load(set_name='jsrt140p')
    pred_blobs = detect.read_blobs('data/{}-jsrt140p-pred-blobs.pkl'.format(
        args.detector))
    masks = np.load('data/aam-jsrt140p-pred-masks.npy')
    rois = create_rois(imgs, masks, pred_blobs, args)
    folds = KFold(n_splits=5, shuffle=True,
                  random_state=util.FOLDS_SEED).split(imgs)
    fold_idx = 0
    for tr, te in folds:
        model.load('data/' + model.name + '.fold-{}'.format(fold_idx + 1))
        model = neural.create_network(model_name, args,
                                      (1, args.roi_size, args.roi_size))
        X_tr, Y_tr, X_te, Y_te = neural.create_train_test_sets(
            real_blobs_tr, pred_blobs_tr, rois_tr, real_blobs_te,
            pred_blobs_te, rois_te)

        print 'load weights {}'.format(model.name)
        model.network.load_weights('data/{}_weights.h5'.format(model.name))
        # FIX: remove and add zmuv mean and zmuv std no Preprocessor augment.py
        if not hasattr(model.preprocessor, 'zmuv_mean'):
            model.preprocessor.fit(X_tr, Y_tr)

        model.save('data/' + model.name)
        pred_blobs_te, probs_te = neural.predict_proba(model, pred_blobs_te,
                                                       rois_te)
        util.save_rois_with_probs(rois_te, probs_te)
        fold_idx += 1
Exemplo n.º 2
0
def exp_eval_ots_lidc_jsrt(args):
    # load LIDC & JSRT-positives data
    imgs_tr, blobs_tr = lidc.load()
    pred_blobs_tr = detect.read_blobs('data/{}-lidc-pred-blobs.pkl'.format(
        args.detector))
    masks_tr = np.load('data/aam-lidc-pred-masks.npy')
    imgs_te, blobs_te = jsrt.load(set_name='jsrt140p')
    pred_blobs_te = detect.read_blobs('data/{}-jsrt140p-pred-blobs.pkl'.format(
        args.detector))
    masks_te = np.load('data/aam-jsrt140p-pred-masks.npy')
    rois_tr = lnd.create_rois(imgs_tr, masks_tr, pred_blobs_tr, args)
    rois_te = lnd.create_rois(imgs_te, masks_te, pred_blobs_te, args)

    # Create rois dataset
    rois_tr, Y_tr, _, _ = neural.create_train_test_sets(
        blobs_tr, pred_blobs_tr, rois_tr, None, None, None)
    generator = augment.get_default_generator((args.roi_size, args.roi_size))
    rois_tr, Y_tr = augment.balance_and_perturb(rois_tr, Y_tr, generator)
    range_tr = np.max(rois_tr), np.min(rois_tr)
    print "range {}".format(range_tr)

    # Extract features
    network = VGG16(mode='ots-feat', pool_layer=args.pool_layer)
    feats_tr = extract_convfeats(network, rois_tr, range_tr)
    feats_te = extract_convfeats(network, rois_te, range_tr)
    np.save('data/{}-lidc-feats.npy'.format(args.detector, fold_idx), feats_tr)
    np.save('data/{}-jsrt140p-feats.npy'.format(args.detector, fold_idx),
            feats_te)

    # Eval classifier
    clf = LinearSVC(C=args.svm_c)
    froc = evaluate_classifier(clf, feats_tr, Y_tr, blobs_te, pred_blobs_te,
                               feats_te)
Exemplo n.º 3
0
def model_output(model_name, args):
    print "Model Outputs"
    imgs, blobs = jsrt.load(set_name='jsrt140p')
    pred_blobs = detect.read_blobs('data/{}-jsrt140p-pred-blobs.pkl'.format(
        args.detector))
    masks = np.load('data/aam-jsrt140p-pred-masks.npy')
    rois = create_rois(imgs, masks, pred_blobs, args)
    folds = KFold(n_splits=5, shuffle=True,
                  random_state=util.FOLDS_SEED).split(imgs)

    fold_idx = 0
    frocs = []
    legends = ['Fold {}'.format(i + 1) for i in range(5)]

    index = np.array(range(len(imgs)))
    for tr, te in folds:
        X_tr, Y_tr, _, _ = neural.create_train_test_sets(
            blobs[tr], pred_blobs[tr], rois[tr], blobs[te], pred_blobs[te],
            rois[te])
        model = neural.create_network(model_name, args,
                                      (1, args.roi_size, args.roi_size))
        model.name = model.name + '-{}-lidc.fold-{}'.format(
            args.detector, fold_idx + 1)
        model.network.load_weights('data/{}_weights.h5'.format(model.name))
        if not hasattr(model.preprocessor, 'zmuv_mean'):
            model.preprocessor.fit(X_tr, Y_tr)

        print "Predict ..."
        pred_blobs_te, probs_te, rois_te = neural.predict_proba(
            model, pred_blobs[te], rois[te])

        print "Save ..."
        eval.save_outputs(imgs[te], blobs[te], pred_blobs_te, probs_te,
                          rois_te, index[te])
Exemplo n.º 4
0
def model_evaluation_tr_lidc_te_jsrt(model_name, args):
    imgs_tr, blobs_tr = lidc.load()
    pred_blobs_tr = detect.read_blobs('data/{}-lidc-pred-blobs.pkl'.format(
        args.detector))
    masks_tr = np.load('data/aam-lidc-pred-masks.npy')
    imgs_te, blobs_te = jsrt.load(set_name='jsrt140p')
    pred_blobs_te = detect.read_blobs('data/{}-jsrt140p-pred-blobs.pkl'.format(
        args.detector))
    masks_te = np.load('data/aam-jsrt140p-pred-masks.npy')

    rois_tr = create_rois(imgs_tr, masks_tr, pred_blobs_tr, args)
    rois_te = create_rois(imgs_te, masks_te, pred_blobs_te, args)

    model = neural.create_network(model_name, args,
                                  (1, args.roi_size, args.roi_size))
    model.name += '-{}-lidc'.format(args.detector)
    froc = evaluate_model(model, blobs_tr, pred_blobs_tr, rois_tr, blobs_te,
                          pred_blobs_te, rois_te)
    froc = eval.average_froc([froc])

    legends = ['Test FROC (JSRT positives)']
    util.save_froc([froc],
                   'data/{}-{}-lidc-jsrt-froc'.format(model.name,
                                                      args.detector),
                   legends,
                   with_std=False)
Exemplo n.º 5
0
def eval(model_name, args):
    images_tr, _ = jsrt.load(set_name='jsrt_od')
    landmarks_tr = scr.load(set_name='jsrt_od')
    masks_tr = jsrt.masks(set_name='jsrt_od', join_masks=False)
    images_te, _ = jsrt.load(set_name='jsrt_ev')
    landmarks_te = scr.load(set_name='jsrt_ev')
    masks_te = jsrt.masks(set_name='jsrt_ev', join_masks=False)
    
    scales = [args.scale1, args.scale2]
    ious_ev = eval_by_IOU(model_name, 'jsrt_od', images_tr, landmarks_tr, images_te, masks_te, scales=scales)
    ious_od = eval_by_IOU(model_name, 'jsrt_ev', images_te, landmarks_te, images_tr, masks_tr, scales=scales)

    ious = ious_ev + ious_od
    ious.sort()
    ious = np.array(ious)
    
    q1 = len(ious)/4
    med = len(ious)/2
    q3 = med + q1
    results = np.array([ious.mean(), ious.std(), ious.min(), ious[q1], ious[med], ious[q3], ious.max()])
    results = np.round(results, decimals=3)
    np.savetxt('{}_sc_{}_{}_eval.txt'.format(model_name, args.scale1, args.scale2), results)
Exemplo n.º 6
0
def train(model_name):
    images, _ = jsrt.load(set_name='jsrt140n')
    landmarks = scr.load(set_name='jsrt140n')
    # TODO: check the masks returned by join masks
    model = get_shape_model(model_name)

    print 'Training model ...' 
    model.fit(images, landmarks)

    print 'Saving model ...'
    model_file = open('data/{}-{}-model.pkl'.format(model_name, 'jsrt140n'), 'wb')
    pickle.dump(model, model_file, -1)
    model_file.close()
Exemplo n.º 7
0
def segment_datasets(model_name):
    model = pickle.load(open('data/{}-{}-model.pkl'.format(model_name, 'jsrt140n'), 'rb'))

    print('Segment lidc')
    lidc_images, _ = lidc.load()
    pred_masks = model.transform(lidc_images)
    np.save('data/{}-{}-pred-masks'.format(model_name, 'lidc'), np.array(pred_masks))

    print('Segment jsrt positives')
    jsrt_images, _ = jsrt.load(set_name='jsrt140p')
    pred_masks = model.transform(jsrt_images)
    np.save('data/{}-{}-pred-masks'.format(model_name, 'jsrt140p'), np.array(pred_masks))

    '''
Exemplo n.º 8
0
def save_blobs(method, args):
    images, blobs = lidc.load()
    masks = np.load('data/aam-lidc-pred-masks.npy')
    print 'masks shape {}'.format(masks.shape)
    pred_blobs, probs = detect_blobs(images,
                                     masks,
                                     args.threshold,
                                     real_blobs=blobs,
                                     method=method)
    write_blobs(
        pred_blobs,
        'data/{}-{}-aam-lidc-pred-blobs.pkl'.format(method, args.threshold))

    froc = eval.froc(blobs, pred_blobs, probs, distance='rad')
    froc = eval.average_froc([froc], DETECTOR_FPPI_RANGE)
    abpi = average_bppi(pred_blobs)

    util.save_froc([froc],
                   'data/{}-{}-lidc-blobs-froc'.format(method, args.threshold),
                   ['{} FROC on LIDC dataset'.format(method)],
                   fppi_max=100)
    np.savetxt('data/{}-{}-lidc-blobs-abpi.txt'.format(method, args.threshold),
               np.array([abpi]))
    print('Average blobs per image LIDC {}'.format(abpi))

    images, blobs = jsrt.load(set_name='jsrt140p')
    masks = np.load('data/aam-jsrt140p-pred-masks.npy')
    pred_blobs, probs = detect_blobs(images,
                                     masks,
                                     args.threshold,
                                     real_blobs=blobs,
                                     method=method)
    write_blobs(
        pred_blobs, 'data/{}-{}-aam-jsrt140p-pred-blobs.pkl'.format(
            method, args.threshold))

    froc = eval.froc(blobs, pred_blobs, probs, distance='rad')
    froc = eval.average_froc([froc], DETECTOR_FPPI_RANGE)
    abpi = average_bppi(pred_blobs)

    util.save_froc([froc],
                   'data/{}-{}-jsrt140p-blobs-froc'.format(
                       method, args.threshold),
                   ['{} FROC on JSRT140 positives dataset'.format(method)],
                   fppi_max=100)
    np.savetxt(
        'data/{}-{}-jsrt140p-blobs-abpi.txt'.format(method, args.threshold),
        np.array([abpi]))
    print('Average blobs per image JSRT positives {}'.format(abpi))
Exemplo n.º 9
0
def save_rois(args):
    imgs_tr, blobs_tr = lidc.load(pts=False)
    pred_blobs_tr = detect.read_blobs('data/sbf-aam-lidc-pred-blobs.pkl')
    masks_tr = np.load('data/aam-lidc-pred-masks.npy')

    imgs_te, blobs_te = jsrt.load(set_name='jsrt140p')
    pred_blobs_te = detect.read_blobs('data/sbf-aam-jsrt140p-pred-blobs.pkl')
    masks_te = np.load('data/aam-jsrt140p-pred-masks.npy')

    rois_tr = create_rois(imgs_tr,
                          masks_tr,
                          pred_blobs_tr,
                          args,
                          real_blobs=blobs_tr)
    rois_te = create_rois(imgs_te,
                          masks_te,
                          pred_blobs_te,
                          args,
                          real_blobs=blobs_te)
    X_tr, Y_tr, X_te, Y_te = neural.create_train_test_sets(
        blobs_tr, pred_blobs_tr, rois_tr, blobs_te, pred_blobs_te, rois_te)
    X_tr, Y_tr = util.split_data_pos_neg(X_tr, Y_tr)
    X_te, Y_te = util.split_data_pos_neg(X_te, Y_te)

    X_pos = X_tr[0]
    idx = np.random.randint(0, len(X_tr[1]), len(X_pos))
    X_neg = X_tr[1][idx]

    print len(X_pos), len(X_neg)
    for i in range(len(X_pos)):
        util.imwrite('data/lidc/roi{}p.jpg'.format(i), X_pos[i][0])
        np.save('data/lidc/roi{}p.npy'.format(i), X_pos[i])
        util.imwrite('data/lidc/roi{}n.jpg'.format(i), X_neg[i][0])
        np.save('data/lidc/roi{}n.npy'.format(i), X_neg[i])

    X_pos = X_te[0]
    idx = np.random.randint(0, len(X_te[1]), len(X_pos))
    X_neg = X_te[1][idx]

    print len(X_pos), len(X_neg)
    for i in range(len(X_pos)):
        util.imwrite('data/jsrt140/roi{}p.jpg'.format(i), X_pos[i][0])
        np.save('data/jsrt140/roi{}p.npy'.format(i), X_pos[i])
        util.imwrite('data/jsrt140/roi{}n.jpg'.format(i), X_neg[i][0])
        np.save('data/jsrt140/roi{}n.npy'.format(i), X_neg[i])
Exemplo n.º 10
0
def exp_eval_ots_jsrt_only(args):
    # load LIDC & JSRT-positives data
    network = VGG16(mode='ots-feat', pool_layer=args.pool_layer)
    print "Model Evaluation Protocol 2"
    imgs, blobs = jsrt.load(set_name='jsrt140p')
    pred_blobs = detect.read_blobs('data/{}-jsrt140p-pred-blobs.pkl'.format(
        args.detector))
    masks = np.load('data/aam-jsrt140p-pred-masks.npy')
    rois = lnd.create_rois(imgs, masks, pred_blobs, args)
    folds = KFold(n_splits=5, shuffle=True,
                  random_state=util.FOLDS_SEED).split(imgs)

    fold_idx = 0
    frocs = []
    legends = ['Fold {}'.format(i + 1) for i in range(5)]
    for tr, te in folds:
        # Eval classifier
        rois_tr, Y_tr, _, _ = neural.create_train_test_sets(
            blobs_tr, pred_blobs_tr, rois[tr], None, None, None)
        generator = augment.get_default_generator(
            (args.roi_size, args.roi_size))
        rois_tr, Y_tr = augment.balance_and_perturb(rois_tr, Y_tr, generator)

        clf = LinearSVC(C=args.svm_c)
        froc = evaluate_classifier(clf, rois_tr, Y_tr, blobs[te],
                                   pred_blobs[te], feats[te])
        frocs.append(froc)

        current_frocs = [eval.average_froc([froc_i]) for froc_i in frocs]
        util.save_froc(current_frocs,
                       'data/lsvm-{}-jsrtonly-folds'.format(args.detector),
                       legends[:len(frocs)],
                       with_std=False)
        fold_idx += 1

    froc = eval.average_froc(frocs)
    legends = ['Test FROC (JSRT positives)']
    util.save_froc([froc],
                   'data/lsvm-{}-jsrtonly'.format(args.detector),
                   legends,
                   with_std=True)
Exemplo n.º 11
0
def model_evaluation_jsrt_only(model_name, args):
    print "Model Evaluation Protocol 2"
    imgs, blobs = jsrt.load(set_name='jsrt140p')
    pred_blobs = detect.read_blobs('data/{}-jsrt140p-pred-blobs.pkl'.format(
        args.detector))
    masks = np.load('data/aam-jsrt140p-pred-masks.npy')
    rois = create_rois(imgs, masks, pred_blobs, args)
    folds = KFold(n_splits=5, shuffle=True,
                  random_state=util.FOLDS_SEED).split(imgs)

    fold_idx = 0
    frocs = []
    legends = ['Fold {}'.format(i + 1) for i in range(5)]
    for tr, te in folds:
        model = neural.create_network(model_name, args,
                                      (1, args.roi_size, args.roi_size))
        model.name = model.name + '-{}-lidc.fold-{}'.format(
            args.detector, fold_idx + 1)
        froc = evaluate_model(model, blobs[tr], pred_blobs[tr], rois[tr],
                              blobs[te], pred_blobs[te], rois[te])
        frocs.append(froc)

        current_frocs = [eval.average_froc([froc_i]) for froc_i in frocs]
        util.save_froc(current_frocs,
                       'data/{}-{}-only-jsrt-folds'.format(
                           model.name[:-7], args.detector),
                       legends[:len(frocs)],
                       with_std=False)
        model.save('data/' + model.name)
        fold_idx += 1

    froc = eval.average_froc(frocs)
    legends = ['Test FROC (JSRT positives)']
    util.save_froc([froc],
                   'data/{}-{}-only-jsrt'.format(model.name[:-7],
                                                 args.detector),
                   legends,
                   with_std=True)