コード例 #1
0
    def config_filenames(self, net_type, use_core, keep_spatial_dim=False):
        # init file managers
        Weights = File.Weights(net_type, output_dir=input_dir)
        if use_core:
            if keep_spatial_dim:
                Embed = File.Embed('SP_' + net_type, output_dir=output_dir)
            else:
                Embed = File.Embed(net_type, output_dir=output_dir)
        else:
            if net_type == 'dir':
                Embed = File.Pred(type='malig', pre='dir', output_dir=output_dir)
            elif net_type == 'dirR':
                Embed = File.Pred(type='rating', pre='dirR', output_dir=output_dir)
            elif net_type == 'dirS':
                Embed = File.Pred(type='size', pre='dirS', output_dir=output_dir)
            elif net_type == 'dirRS':
                # assert False # save rating and size in seperate files
                Embed = {}
                Embed['R'] = File.Pred(type='rating', pre='dirRS', output_dir=output_dir)
                Embed['S'] = File.Pred(type='size', pre='dirRS', output_dir=output_dir)
            else:
                print('{} not recognized'.format(net_type))
                assert False

        return Weights, Embed
コード例 #2
0
def dir_rating_view(run, post, epochs, net_type='dirR', factor=1.0):
    # load
    #images, predict, meta_data, labels, masks = pred_loader.load(run, epochs[-1], post)
    PredFile = FileManager.Pred(type='rating', pre=net_type)

    predict, epochs, meta_data, images, classes, labels, masks, _, _, _ = PredFile.load(
        run=run + 'c0', dset=post)
    # prepare
    images = np.squeeze(images)
    labels = np.array([np.mean(l, axis=0) for l in labels])
    labels = np.round(factor * labels).astype('int')
    predict = np.round(factor * predict[-1]).astype('int')
    #plot
    select = [5, 23, 27, 51]
    plt.figure('view_' + run + '_' + post)
    for pid, i in enumerate(select):
        plt.subplot(2, 2, pid + 1)
        plt.imshow(images[i])
        plt.title(
            np.array2string(labels[i], prefix='L') + '\n' +
            np.array2string(predict[i], prefix='P'))
        plt.xticks([])
        plt.yticks([])
        if pid >= 0:
            dl = l2(labels[i], labels[select[0]])
            dp = l2(predict[i], predict[select[0]])
            plt.ylabel("{:.1f}\n{:.1f}".format(dl, dp))
コード例 #3
0
def dir_rating_accuracy(run, post, net_type, epochs, n_groups=5):
    #images, predict, meta_data, labels, masks = pred_loader.load(run, epochs[-1], post)
    rating_property = [
        'Subtlety', 'Internalstructure', 'Calcification', 'Sphericity',
        'Margin', 'Lobulation', 'Spiculation', 'Texture', 'Malignancy'
    ]
    PredFile = FileManager.Pred(type='rating', pre=net_type)
    acc = np.zeros([len(epochs), n_groups, len(rating_property)])
    for c, run_config in enumerate(
        [run + 'c{}'.format(config) for config in range(n_groups)]):
        predict, valid_epochs, images, meta_data, classes, labels, masks = PredFile.load(
            run=run_config, dset=post)
        labels = np.array([np.mean(t, axis=0) for t in labels])
        for i, e in enumerate(epochs):
            try:
                idx = int(np.argwhere(valid_epochs == e))
            except:
                print('skip epoch {}'.format(e))
                continue
            for ridx, r in enumerate(rating_property):
                acc[i, c, ridx] = accuracy(labels[:, ridx], predict[idx, :,
                                                                    ridx])
    acc = np.mean(acc, axis=1)
    plt.figure()
    plt.title('Rating Acc')
    plt.plot(epochs, acc)
    plt.legend(rating_property)

    return acc
コード例 #4
0
def dir_rating_correlate(run,
                         post,
                         epochs,
                         rating_norm='none',
                         clustered_rating_distance=True,
                         n_groups=5):
    pear_corr = [[] for i in range(n_groups)]
    kend_corr = [[] for i in range(n_groups)]
    plot_data_filename = './Plots/Data/rating_correlation_{}{}.p'.format(
        'dirR', run)
    try:
        print('SKIPING')
        assert False
        pear_corr, kend_corr = pickle.load(open(plot_data_filename, 'br'))
        print("Loaded results for {}".format(run))
    except:
        print("Evaluating Rating Correlation for {}".format(run))
        for c, run_config in enumerate(
            [run + 'c{}'.format(config) for config in range(n_groups)]):
            PredFile = FileManager.Pred(type='rating', pre='dirR')
            Reg = RatingCorrelator(PredFile(run=run_config, dset=post),
                                   multi_epoch=True)
            for e in epochs:
                Reg.evaluate_embed_distance_matrix(
                    method='euclidean',
                    epoch=e,
                    round=(rating_norm == 'Round'))
                Reg.evaluate_rating_space(norm=rating_norm)
                Reg.evaluate_rating_distance_matrix(
                    method='euclidean',
                    clustered_rating_distance=clustered_rating_distance)

                Reg.linear_regression()
                # Reg.scatter('embed', 'rating', xMethod="euclidean", yMethod='euclidean', sub=False)
                p, s, k = Reg.correlate_retrieval(
                    'embed',
                    'rating',
                    round=(rating_norm == 'Round'),
                    verbose=False)
                pear_corr[c].append(p)
                kend_corr[c].append(k)

            pear_corr[c] = np.array(pear_corr[c])
            kend_corr[c] = np.array(kend_corr[c])

        pear_corr = np.mean(pear_corr, axis=0)
        kend_corr = np.mean(kend_corr, axis=0)
        print('NO DUMP')
        #pickle.dump((pear_corr, kend_corr), open(plot_data_filename, 'bw'))

    pear_corr = smooth(pear_corr[:, 0]), smooth(pear_corr[:, 1])
    kend_corr = smooth(kend_corr[:, 0]), smooth(kend_corr[:, 1])
    epochs = np.array(epochs)

    plt.figure('Rating2Rating:' + run + '-' + post)
    q = plt.plot(epochs, pear_corr[0])
    plt.plot(epochs,
             pear_corr[0] + pear_corr[1],
             color=q[0].get_color(),
             ls='--',
             alpha=alpha)
    plt.plot(epochs,
             pear_corr[0] - pear_corr[1],
             color=q[0].get_color(),
             ls='--',
             alpha=alpha)

    q = plt.plot(epochs, kend_corr[0])
    plt.plot(epochs,
             kend_corr[0] + kend_corr[1],
             color=q[0].get_color(),
             ls='--',
             alpha=alpha)
    plt.plot(epochs,
             kend_corr[0] - kend_corr[1],
             color=q[0].get_color(),
             ls='--',
             alpha=alpha)

    plt.grid(which='major', axis='y')
    plt.title('rating_' + run + '_' + post)
    plt.xlabel('epochs')
    plt.ylabel('correlation')
    plt.legend(['pearson', '', '', 'kendall', '', ''])
コード例 #5
0
def dir_size_rmse(run,
                  post,
                  epochs,
                  net_type,
                  dist='RMSE',
                  weighted=False,
                  n_groups=5):

    plot_data_filename = './Plots/Data/size{}_{}{}.p'.format(
        dist, net_type, run)
    try:
        assert False
        R = pickle.load(open(plot_data_filename, 'br'))
        print("Loaded results for {}".format(run))
    except:
        print("Evaluating Size RMSE for {}".format(run))
        PredFile = FileManager.Pred(type='size', pre=net_type)
        R = np.zeros([len(epochs), n_groups])

        for c, run_config in enumerate(
            [run + 'c{}'.format(config) for config in range(n_groups)]):
            predict, valid_epochs, images, meta_data, classes, labels, masks = PredFile.load(
                run=run_config, dset=post)
            labels = np.array(labels)
            for i, e in enumerate(epochs):
                print(" Epoch {}:".format(e))
                try:
                    idx = int(np.argwhere(valid_epochs == e))
                except:
                    print('skip epoch {}'.format(e))
                    continue
                pred = predict[idx]
                '''
                W = np.ones(labels.shape[0])
                if weighted:
                    assert False
                    w = np.histogram(labels[:, r], bins=np.array(range(64))+0.5)[0]
                    w = 1 - w / np.sum(w)
                    pred_w = np.minimum(np.maximum(pred[:, r], 1.0), max_val)
                    W = w[np.round(pred_w - 1).astype('int')]
                if dist=='RMSE':
                    err = W.dot((pred - labels)**2)
                    err = np.sqrt(err/np.sum(W))
                elif dist=='ABS':
                    err = W.dot(np.abs(pred - labels)) / np.sum(W)
                else:
                    print('{} unrecognized distance'.format(dist))
                    assert False
                '''
                rmse = np.sqrt(np.mean(np.sum((pred - labels)**2, axis=1)))
                R[i, c] = rmse
        R = np.mean(R, axis=1)
        pickle.dump(R, open(plot_data_filename, 'bw'))

    # smooth
    R = smooth(R)

    plt.figure(dist + ' ' + net_type + run + '-' + post)
    plt.title('Size ' + dist)
    plt.plot(epochs, R)
    #plt.legend(rating_property+['Overall'])
    plt.grid(True, axis='y')

    return R
コード例 #6
0
def dir_rating_params_correlate(run,
                                post,
                                epochs,
                                net_type,
                                rating_norm='none',
                                configurations=list(range(5)),
                                USE_CACHE=True,
                                DUMP=True):

    reference = [0.7567, 0.5945, 0.7394, 0.5777, 0.6155, 0.7445,
                 0.6481]  # 0, 0,
    rating_property = [
        'Subtlety', 'Sphericity', 'Margin', 'Lobulation', 'Spiculation',
        'Texture', 'Malignancy'
    ]  # 'Internalstructure', 'Calcification',
    mask = [True, False, False, True, True, True, True, True, True]

    pear_corr = [[] for i in configurations]
    plot_data_filename = './Plots/Data/rating_params_correlation_{}{}.p'.format(
        net_type, run)
    try:
        if USE_CACHE is False:
            print('SKIPPING')
            assert False
        pear_corr = pickle.load(open(plot_data_filename, 'br'))
        print("Loaded results for {}".format(run))
    except:
        print("Evaluating Rating Correlation for {}".format(run))
        for c, run_config in enumerate(
            [run + 'c{}'.format(config) for config in configurations]):
            PredFile = FileManager.Pred(type='rating', pre=net_type)
            Reg = RatingCorrelator(PredFile(run=run_config, dset=post),
                                   multi_epoch=True,
                                   conf=c)
            Reg.evaluate_rating_space(norm=rating_norm)
            #valid_epochs = []
            for e in epochs:
                p = Reg.correlate_to_ratings(epoch=e,
                                             round=(rating_norm == 'Round'))
                if not np.all(np.isfinite(p[mask])):
                    print('nan at: conf={}, epoch={}'.format(c, e))
                pear_corr[c].append(p[mask])
                #valid_epochs.append(e)

            pear_corr[c] = np.array(pear_corr[c])

        pear_corr = np.mean(pear_corr, axis=0)
        if DUMP:
            pickle.dump(pear_corr, open(plot_data_filename, 'bw'))
        else:
            print('NO DUMP')

    for i, e in enumerate(epochs):
        print("=" * 20)
        print(" Epoch {}:".format(e))
        print("-" * 20)
        for p, property in enumerate(rating_property):
            print("\t{}: \t{:.2f}".format(property, pear_corr[i, p]))
        #print("\t" + ("-" * 10))
        #print("\toverall: \t{:.2f}".format(R[i, 9]))

    for p in range(pear_corr.shape[1]):
        pear_corr[:, p] = smooth(pear_corr[:, p], window_length=5, polyorder=2)
    epochs = np.array(epochs)

    plt.figure('RatingParams2Rating:' + run + '-' + post)
    q = plt.plot(epochs, pear_corr, linewidth=2.5)
    for line, ref in zip(q, reference):
        plt.plot(epochs,
                 ref * np.ones_like(epochs),
                 color=line.get_color(),
                 ls='--',
                 linewidth=4,
                 alpha=0.6)

    plt.grid(which='major', axis='y')
    plt.title('rating_' + run + '_' + post)
    plt.xlabel('epochs')
    plt.ylabel('correlation')
    plt.legend(rating_property)
コード例 #7
0
def dir_rating_rmse(run,
                    post,
                    epochs,
                    net_type,
                    dist='RMSE',
                    weighted=False,
                    configurations=list(range(5)),
                    USE_CACHE=True,
                    DUMP=True):
    #images, predict, meta_data, labels, masks = pred_loader.load(run, epochs[-1], post)
    rating_property = [
        'Subtlety', 'Internalstructure', 'Calcification', 'Sphericity',
        'Margin', 'Lobulation', 'Spiculation', 'Texture', 'Malignancy'
    ]

    plot_data_filename = './Plots/Data/{}_{}{}.p'.format(dist, net_type, run)
    try:
        if USE_CACHE is False:
            print("skipping...")
            assert False
        R = pickle.load(open(plot_data_filename, 'br'))
        print("Loaded results for {}".format(run))
    except:
        print("Evaluating RMSE for {}".format(run))
        PredFile = FileManager.Pred(type='rating', pre=net_type)
        R = np.zeros([len(epochs), 10, len(configurations)])

        for c, run_config in enumerate(
            [run + 'c{}'.format(config) for config in configurations]):
            predict, valid_epochs, images, meta_data, classes, labels, masks, conf, rating_weights, z = PredFile.load(
                run=run_config, dset=post)
            labels = np.array([np.mean(l, axis=0) for l in labels])
            for i, e in enumerate(epochs):
                #print("=" * 20)
                #print(" Epoch {}:".format(e))
                #print("-" * 20)
                try:
                    idx = int(np.argwhere(valid_epochs == e))
                except:
                    print('skip epoch {}'.format(e))
                    continue
                pred = predict[idx]

                for r, max_val in zip(range(9), [5, 5, 6, 5, 5, 5, 5, 5, 5]):
                    #print("{}:".format(rating_property[r]))
                    W = np.ones(labels.shape[0])
                    if weighted:
                        w = np.histogram(labels[:, r],
                                         bins=np.array(range(max_val + 1)) +
                                         0.5)[0]
                        #print("\tcounts - {}".format(w))
                        w = 1 - w / np.sum(w)
                        w /= (len(w) - 1)
                        assert np.abs(w.sum() - 1) < 1e-6
                        #print("\tweighted by {}".format(w))
                        #pred_w = np.minimum(np.maximum(pred[:, r], 1.0), max_val)
                        W = w[np.round(labels[:, r] - 1).astype('int')]
                    if dist == 'RMSE':
                        err = W.dot((pred[:, r] - labels[:, r])**2)
                        err = np.sqrt(err / np.sum(W))
                    elif dist == 'ABS':
                        err = W.dot(
                            np.abs(pred[:, r] - labels[:, r])) / np.sum(W)
                    else:
                        print('{} unrecognized distance'.format(dist))
                        assert False
                    #print("rmse: \t{:.2f}".format(err))
                    R[i, r, c] = err
                rmse = np.sqrt(np.mean(np.sum((pred - labels)**2, axis=1)))
                #print("=" * 20)
                #print("overall: \t{:.2f}".format(rmse))
                R[i, 9, c] = rmse
        R = np.mean(R, axis=2)
        for i, e in enumerate(epochs):
            print("=" * 20)
            print(" Epoch {}:".format(e))
            print("-" * 20)
            for p, property in enumerate(rating_property):
                print("\t{}: \t{:.2f}".format(property, R[i, p]))
            print("\t" + ("-" * 10))
            print("\toverall: \t{:.2f}".format(R[i, 9]))

        if DUMP:
            pickle.dump(R, open(plot_data_filename, 'bw'))
        else:
            print("No Dump")

    # smooth
    for r in range(9):
        R[:, r] = smooth(R[:, r])
    plt.figure(dist + ' ' + run + '-' + post)
    plt.title('Rating ' + dist)
    plt.plot(epochs, R)
    plt.legend(rating_property + ['Overall'])
    plt.grid(True, axis='y')

    return R
コード例 #8
0
    for conf in [1]:  # range(n_groups):
        run = run_id + 'c{}'.format(conf)
        if True:
            print("Predicting Rating for " + run)
            PredRating = PredictRating(pooling=pooling)
            PredRating.load_dataset(data_subset_id=DataSubSet,
                                    full=full,
                                    include_unknown=include_unknown,
                                    size=128,
                                    rating_scale=rating_scale,
                                    configuration=conf)
            preds = []
            valid_epochs = []
            for e in epochs:
                WeightsFile = FileManager.Weights('dirR').name(run, epoch=e)
                PredFile = FileManager.Pred(type='rating', pre='dirR')
                out_file = PredFile(run=run, dset=post)

                data, out_filename = PredRating.predict_rating(
                    WeightsFile, out_file)
                images_test, pred, meta_test, classes_test, labels_test, masks_test = data
                preds.append(np.expand_dims(pred, axis=0))
            preds = np.concatenate(preds, axis=0)
            pickle.dump((preds, np.array(epochs), meta_test, images_test,
                         classes_test, labels_test, masks_test),
                        open(out_filename, 'bw'))
        else:
            print("Predicting Malignancy for " + run)
            PredMal = PredictMal(pooling=pooling)
            for e in epochs:
                WeightsFile = FileManager.Weights('dir').name(run, epoch=e)