Пример #1
0
    def average_out_pred(r=2):
        model_name = 'ti-unet'

        path = os.path.join(
            folder_base,
            f'net_weight/{data}/{model_name}_d1_k{k}_n80/w_{1}.h5')
        model_i = load_model_quick(path)

        neural_net_i = NeuralNet(model_i, w_ext=10, norm_x=True)

        y_pred_lst = []
        r = 2
        for epoch_i in range(epoch - r, epoch + r + 1):  # epochs

            neural_net_i.load(path.rsplit('/', 1)[0], epoch_i)  # Load

            try:
                y_pred_i = neural_net_i.predict(img_x)
            except Exception as e:
                print(e)
                continue

            y_pred_lst.append(y_pred_i[..., 1])
        y_pred_avg = np.mean(y_pred_lst, axis=0)

        return y_pred_avg
Пример #2
0
def eval_3outputs():

    folder_base = 'C:/Users/admin/Data/ghent_altar/' if os.name == 'nt' else '/scratch/lameeus/data/ghent_altar/'

    assert data == '19botrightcrack3'

    k = 9
    epoch = 25
    model_name = 'ti-unet'

    folder_base = 'C:/Users/admin/Data/ghent_altar/' if os.name == 'nt' else '/scratch/lameeus/data/ghent_altar/'
    path = os.path.join(
        folder_base,
        f'net_weight/{data}/{model_name}_d1_k{k}_n80/w_{epoch}.h5')

    model = load_model_quick(path)
    neural_net = NeuralNet(model, w_ext=10, norm_x=True)

    from scripts.journal_paper.comparison_sh.shared import load_data
    a = load_data("19botright", 80)
    img_x, y_eval = a.get_x_train(), a.get_y_test()

    y_pred = neural_net.predict(img_x)

    assert y_pred.shape[-1] == 3
    y_pred2 = np.stack([1 - y_pred[..., 1], y_pred[..., 1]], axis=-1)

    data_i = _eval_func_single(y_eval, y_pred2)

    print(data_i)

    return
Пример #3
0
    def __init__(self):

        # data
        self.data()

        # Load model(s)

        model_name = 'unet'  # ['simple', 'ti-unet', 'unet']:

        folder = f'C:/Users/admin/Data/ghent_altar/net_weight/{model_name}_d1_k9_n80'
        epoch = 1
        path = f'C:/Users/admin/Data/ghent_altar/net_weight/{model_name}_d1_k9_n80/w_{epoch}.h5'

        from scripts.scripts_performance.main_performance import load_model_quick
        model = load_model_quick(path)
        neural_net = NeuralNet(model, w_ext=10, norm_x=True)

        model.summary()

        for epoch in range(1, 10 + 1):
            print('epoch', epoch)
            neural_net.load(folder, epoch)

            # Predict
            y_pred = neural_net.predict(self.img_x)

            if 0:
                plt.imshow(y_pred[..., 0])
                plt.show()

            for val_name in self.val:
                print(val_name)

                y_true_val = self.val[val_name]

                data_i = _eval_func_single(y_true_val, y_pred)

                print(data_i)

        # TODO best performing (ti-unet: 4)
        neural_net.load(folder, 4)

        y_pred = neural_net.predict(self.img_x)

        from performance.testing import get_y_pred_thresh
        y_pred_thresh = get_y_pred_thresh(y_pred, data_i['thresh'])

        concurrent([
            self.img_x[..., :3], self.img_y[..., 0], y_pred[..., 0],
            y_pred_thresh[..., 0]
        ])

        y_pred
Пример #4
0
def pred_epochs():
    img_x, img_y_val = data_lamb()

    d = 2
    k = 10
    model_name = 'ti-unet'

    train_data = '1319_10nat'

    w_ext = 10 if d == 1 else 26

    y_pred_lst = []
    n = []
    for epoch in range(10, 101, 10):
        print(epoch)

        epoch_start = 50
        epoch_corr = epoch + epoch_start if train_data[:5] == '1319_' else epoch
        path = f'C:/Users/admin/Data/ghent_altar/net_weight/{train_data}/{model_name}_d{d}_k{k}/w_{epoch_corr}.h5'

        try:
            model = load_model_quick(path)
        except Exception as e:
            print(e)
            continue

        neural_net = NeuralNet(model, w_ext=w_ext, norm_x=True)

        y_pred = neural_net.predict(img_x)

        if 0:
            data_i = _eval_func_single(img_y_val, y_pred, metric='kappa')
            print(data_i)
            data_i = _eval_func_single(img_y_val, y_pred, metric='jaccard')
            print(data_i)

        y_pred_lst.append(y_pred)
        n.append(epoch)

    concurrent([a[..., 1] for a in y_pred_lst], n)
    plt.show()

    return 1
Пример #5
0
def main():

    b_encoder_fixed = False
    info_enc_fixed = '_enc_fixed'

    folder_weights = '/scratch/lameeus/data/ghent_altar/net_weight/10lamb_kfold_pretrained'
    folder_save = '/home/lameeus/data/ghent_altar/dataframes'
    filename_single = f'pretrained_unet_10lamb_kfold_single'
    filename_avg_pred = f'pretrained_unet_10lamb_kfold_avgpred'
    folder_weights += info_enc_fixed if b_encoder_fixed else ''
    filename_single += info_enc_fixed if b_encoder_fixed else ''
    filename_avg_pred += info_enc_fixed if b_encoder_fixed else ''

    fold_range = range(6)
    # fold_range = [0, 1]

    k = 10
    epoch_range = range(1, 40 + 1)

    w_ext_in = 28

    k_fold_train_data = get_10lamb_6patches(5)  # 5 is the number of modalities
    train_data_all = k_fold_train_data.get_train_data_all()
    img_x = train_data_all.get_x_train()
    img_x = rescale0to1(img_x)
    img_clean = img_x[..., :3]
    img_y_all = train_data_all.get_y_train()

    b_plot = False

    for i_fold in fold_range:

        print(i_fold)

        img_y_te = k_fold_train_data.k_split_i(i_fold).get_y_test()

        # Init for range epochs
        lst_data_single = []
        lst_data_avg_pred = []
        list_y_pred = []
        model = None

        for epoch in np.sort(epoch_range)[::-1]:

            filepath_model = os.path.join(
                folder_weights, f'unet_enc_k{k}_ifold{i_fold}/w_{epoch}.h5')

            model = load_model_quick(filepath_model, model=model)
            n = NeuralNet(model, w_ext=w_ext_in)
            y_pred = n.predict(img_x)
            """
            Average out predictions
            """
            list_y_pred.append(y_pred)
            y_avg_pred = np.mean(list_y_pred, axis=0)

            thresh_single = optimal_test_thresh_equal_distribution(
                img_y_all, y_pred)
            thresh_avg_pred = optimal_test_thresh_equal_distribution(
                img_y_all, y_avg_pred)

            y_pred_bin = np.greater_equal(y_pred[..., 1], thresh_single)

            dict_perf = foo_performance(img_y_te, y_pred, thresh_single)
            print(dict_perf)

            if b_plot:
                concurrent([
                    y_pred_bin, img_clean,
                    semi_transparant(img_clean, y_pred_bin),
                    semi_transparant(img_clean, img_y_te[..., 1].astype(bool))
                ])

            data_single_i = {'k': k, 'i_fold': i_fold, 'epoch': epoch}
            data_avg_pred_i = {
                'k': k,
                'i_fold': i_fold,
                'epoch_start': epoch,
                'epoch_end': max(epoch_range)
            }

            data_single_i.update(dict_perf)
            data_avg_pred_i.update(
                foo_performance(img_y_te, y_avg_pred, thresh_avg_pred))

            lst_data_single.append(data_single_i)
            lst_data_avg_pred.append(data_avg_pred_i)

        df_single = pd.DataFrame(lst_data_single)
        df_avg_pred = pd.DataFrame(lst_data_avg_pred)

        path_single = os.path.join(folder_save, filename_single + '.csv')
        path_avg_pred = os.path.join(folder_save, filename_avg_pred + '.csv')

        pandas_save(path_single, df_single, append=True)
        pandas_save(path_avg_pred, df_avg_pred, append=True)

    return
Пример #6
0
def transfer_learning(
        epoch=25,  # Could check a few
        b_plot=False):

    d = 2  # 1, 2

    img_x, img_y_val = data_lamb()

    k = 10

    model_name = 'ti-unet'

    w_ext = 10 if d == 1 else 26

    # train_data:
    y_pred_lst = []
    n = ['clean']

    # train_data_lst = ['1319_10', '10', '1319', '1319_101319']
    train_data_lst = ['10nat', '1319_10nat', '1319_10nat1319', '1319']

    data_i_lst = {}

    for train_data in train_data_lst:
        print(train_data)

        epoch_start = 50
        epoch_corr = epoch + epoch_start if train_data[:5] == '1319_' else epoch
        if train_data == '1319':
            epoch_corr = 50
        path = f'C:/Users/admin/Data/ghent_altar/net_weight/{train_data}/{model_name}_d{d}_k{k}/w_{epoch_corr}.h5'

        try:
            model = load_model_quick(path)
        except Exception as e:
            print(e)
            continue

        neural_net = NeuralNet(model, w_ext=w_ext, norm_x=True)

        y_pred = neural_net.predict(img_x)

        # baseline
        data_i = _eval_func_single(img_y_val, y_pred, metric='kappa')
        print(data_i)

        if 0:
            """
            Checking which 
            
            baseline ~ .22
            i = 0: .268, Remove huge improvement  ( a lot of "green" background annotated as paint loss)
            i = 1: .228 Keep!
            i = 2: .179 keep! Drop (keep!!
            i = 3: .159 keep! Even more important
            i = 4: .252 Remove (huge problem right top)
            i = 5: .233 Keep, quit relevant
            """

            from datasets.default_trainingsets import get_10lamb_6patches
            kFoldTrainData = get_10lamb_6patches(5)

            _eval_func_single(
                kFoldTrainData.k_split_i(0).get_y_train(),
                y_pred,
                metric='kappa')  # Check what is influence without!

        data_i_lst[train_data] = data_i

        data_i = _eval_func_single(img_y_val, y_pred, metric='jaccard')
        print(data_i)

        y_pred_lst.append(y_pred)
        n.append(train_data)

    # plt.imshow(neural_net.predict(img_x[::2,::2,:])[..., 1])

    if b_plot:
        concurrent([img_x[..., :3]] + [a[..., 1] for a in y_pred_lst], n)

    if 0:
        from figures_paper.overlay import semi_transparant
        from data.datatools import imread, imsave

        t = [data_i_lst[n_i]['thresh'] for n_i in train_data_lst]
        p = []
        for i, train_data in enumerate(train_data_lst):
            b = np.greater_equal(y_pred_lst[i][..., 1], t[i])

            k = semi_transparant(img_x[..., :3], b, 0)
            p.append(k)

            imsave(
                os.path.join(
                    "C:/Users/admin/OneDrive - ugentbe/data/images_paper",
                    train_data + '.png'), k)

        concurrent(p)

    return data_i_lst
Пример #7
0
                                  f'{data}_{"ti-unet"}_n_per_class.csv'),
                     delimiter=';')

    i_max = df['kappa'].idxmax()

    k, epoch = map(int, df.iloc[i_max][['k', 'epoch']])

    model_name = 'ti-unet'

    folder_base = 'C:/Users/admin/Data/ghent_altar/' if os.name == 'nt' else '/scratch/lameeus/data/ghent_altar/'

    path = os.path.join(
        folder_base,
        f'net_weight/{data}/{model_name}_d1_k{k}_n80/w_{epoch}.h5')

    model = load_model_quick(path)
    neural_net = NeuralNet(model, w_ext=10, norm_x=True)

    # Image

    from scripts.journal_paper.comparison_sh.shared import load_data

    if data == '1319botright':
        a = load_data("19botright", n_per_class=80)
    else:
        a = load_data(data, n_per_class=80)
    img_x, img_y = a.get_x_train(), a.get_y_test()

    y_pred = neural_net.predict(img_x)

    if 1: