Exemplo n.º 1
0
def folds_annot():
    train_data = get_10lamb_old(5)
    img_x, _, _, _ = get_training_data(train_data)
    
    img_clean = img_x[..., :3]

    lst_get = [get_borders1, get_borders2, get_borders3, get_borders4, get_borders5, get_borders6]

    for i_fold in range(6):
        
        img_annot = imread(f'/home/lameeus/data/ghent_altar/input/hierachy/10_lamb/annotations/kfold/annot_{i_fold+1}.png')

        y1 = annotations2y(img_annot, thresh=.8)[..., 1]

        a = semi_transparant(img_clean, y1.astype(bool))
    
        w0, w1, h0, h1 = lst_get[i_fold]()
        clean_annot_crop = a[h0:h1, w0:w1, :]

        img_clean_crop = img_clean[h0:h1, w0:w1, :]
        
        if 0: concurrent([img_clean_crop, clean_annot_crop])
        
        folder_save = '/scratch/lameeus/data/ghent_altar/input/hierarchy/10lamb/ifolds'

        imsave(os.path.join(folder_save, f'clean_crop_ifold{i_fold}.png'), img_clean_crop)
        imsave(os.path.join(folder_save, f'clean_annot_crop_ifold{i_fold}.png'), clean_annot_crop)
        
        pass
Exemplo n.º 2
0
    def predict_average(self):

        y_lst = []
        for i_fold in range(6):
            for k in range(18, 19 + 1):
                for epoch in range(99, 100 + 1):

                    y = self.predict(i_fold=i_fold, k=k, epoch=epoch)
                    y_lst.append(y)
        y_avg = np.mean(y_lst, axis=0)

        y_avg_bin = self.get_bin(y_avg)

        img_clean = self.img_x[..., :3]

        y_avg_bin_overlay = semi_transparant(img_clean, y_avg_bin)

        concurrent([y_avg[..., 1], y_avg_bin, y_avg_bin_overlay, img_clean])

        if 0:
            path_save = f'/scratch/lameeus/data/ghent_altar/output/hierarchy/10_lamb/' \
                        f'paintloss_tiunet_enc{self.fixed_enc}.png'
            imsave(path_save, y_avg_bin)
        if 1:
            path_save = f'/scratch/lameeus/data/ghent_altar/output/hierarchy/10_lamb/' \
                        f'paintloss_overlay_tiunet_enc{self.fixed_enc}.png'
            imsave(path_save, y_avg_bin_overlay)

        return 1
Exemplo n.º 3
0
    def plot(self):
        imgs = self.get()

        keys = imgs.keys()
        img_list = [imgs[key] for key in keys]

        concurrent(img_list, titles=keys)
Exemplo n.º 4
0
    def predict_compare_regular(self):

        img_y_all = self.k_fold_train_data.get_train_data_all().get_y_train()
        lst_get = [
            get_borders1, get_borders2, get_borders3, get_borders4,
            get_borders5, get_borders6
        ]
        img_clean = self.img_x[..., :3]

        for i_fold in range(6):

            y_lst = []
            # Average prediction
            print(f'i_fold = {i_fold}')
            for k in [17, 18, 19]:
                print(f'k = {k}')
                for epoch in [36, 37, 38, 39, 40]:
                    print(f'epoch = {epoch}')
                    y = self.predict_regular(i_fold=i_fold, k=k, epoch=epoch)
                    y_lst.append(y)

            y_avg = np.mean(y_lst, axis=0)
            y_avg_bin = self.get_bin(y_avg)

            # Performance
            img_y_te = self.k_fold_train_data.k_split_i(i_fold).get_y_test()

            thresh = optimal_test_thresh_equal_distribution(img_y_all, y_avg)

            perf = foo_performance(img_y_te, y_avg, thresh)

            # CROP

            w0, w1, h0, h1 = lst_get[i_fold]()

            y_avg_bin_crop = y_avg_bin[h0:h1, w0:w1]
            clean_crop = img_clean[h0:h1, w0:w1, :]

            y_avg_bin_transparent_crop = semi_transparant(
                clean_crop, y_avg_bin_crop)
            if 0:
                concurrent(
                    [clean_crop, y_avg_bin_crop, y_avg_bin_transparent_crop])

            folder_save = '/scratch/lameeus/data/ghent_altar/output/hierarchy/10_lamb/ifolds_regular_tiunet'
            filename = f'_tiunet_ifold{i_fold}_jacc{perf["jaccard"]:.3f}.png'
            # Save y_bin

            imsave(os.path.join(folder_save, 'binpred' + filename),
                   y_avg_bin_crop,
                   b_check_duplicate=False)

            # Save overlay
            imsave(os.path.join(folder_save, 'overlay' + filename),
                   y_avg_bin_transparent_crop,
                   b_check_duplicate=False)
Exemplo n.º 5
0
    def predict_regular(self, i_fold=None, k=None, epoch=None):

        n = self.get_n_model_regular(i_fold=i_fold, k=k, epoch=epoch)

        y = n.predict(self.img_x)

        if 0:
            concurrent([y[..., 1], self.img_x[..., :3]])

        return y
Exemplo n.º 6
0
def local_thresholding(x_img, ext:int=200):
    gray = np.mean(x_img, axis=2)
    
    x_threshold = filters.threshold_local(gray, block_size=ext*2+1)
    
    # Debugging:
    if 1:
        from plotting import concurrent
        concurrent([gray, x_threshold])
    
    return np.greater_equal(gray, x_threshold)
Exemplo n.º 7
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
    def ae_results(self, n_ae):
        """
        AE Reconstruction
        """

        img_x_ae = n_ae.predict(self.img_x)

        y_denorm = _undo_norm_input(img_x_ae)
        y_clean = y_denorm[..., :3]
        y_rgb = y_denorm[..., 3:6]
        y_ir = y_denorm[..., 6]
        y_irr = y_denorm[..., 7]
        y_xray = y_denorm[..., 8]
        concurrent([y_clean, y_rgb, y_ir, y_irr, y_xray],
                   ['clean', 'rgb', 'ir', 'irr', 'xray'])
Exemplo n.º 9
0
    def main_eval(
        self,
        train_data,
        b_plot=False,
    ):

        x = train_data.get_x_test()

        y_pred = self.neural_net.predict(x)

        if b_plot:
            concurrent([x[..., :3], y_pred[..., 1]], ['input', 'prediction'])

        val_datas = [{'y': train_data.get_y_test()}] + self.val_datas

        return _eval_func(y_pred, val_datas, b_plot=b_plot)
Exemplo n.º 10
0
def continues_learning():
    folder = '/home/lameeus/data/ghent_altar/input/hierarchy/13_small'
    im_clean = imread(os.path.join(folder, 'clean.png'))[..., :3]
    im_annot0 = imread(os.path.join(folder, 'annot.tif'))
    im_annot1 = imread(os.path.join(folder, 'clean_annot_practical.png'))

    y_true = annotations2y(im_annot0)
    y_true_extra = annotations2y(im_annot1, thresh=.9)
    
    folder = '/home/lameeus/data/ghent_altar/output/hierarchy/13_small/practical_annotations'
    y_pred0 = imread(os.path.join(folder, 'pred_transfer_kfoldenc2_ifold0_avg.png'))
    
    folder = '/home/lameeus/data/ghent_altar/output/hierarchy/13_small'
    y_pred1 = imread(os.path.join(folder, 'pred_transfer_kfoldenc2_ifold0_avg_epoch50_J0427.png'))
    
    from performance.testing import optimal_test_thresh_equal_distribution
    from scripts.scripts_performance.main_performance import foo_performance
    from figures_paper.overlay import semi_transparant
    def get_bin(y_pred):

        assert len(y_pred.shape) == 2
        y_pred01 = np.stack([1-y_pred, y_pred], axis=-1)
        thresh = optimal_test_thresh_equal_distribution(y_true, y_pred01)

        print(foo_performance(y_true, y_pred01, thresh))

        y_pred_bin = y_pred >= thresh
        
        return y_pred_bin

    y_pred0_bin = get_bin(y_pred0)
    y_pred1_bin = get_bin(y_pred1)

    y_pred0_bin_fancy = semi_transparant(im_clean, y_pred0_bin)
    y_pred1_bin_fancy = semi_transparant(im_clean, y_pred1_bin)
    
    concurrent([im_clean, y_true[..., 0], y_true_extra[..., 0], y_pred0, y_pred1, y_pred0_bin, y_pred1_bin,
                y_pred0_bin_fancy,
                y_pred1_bin_fancy])
    
    folder_save = '/home/lameeus/data/ghent_altar/output/hierarchy/13_small/fancy'
    imsave(os.path.join(folder_save, 'overalytrain10.png'), y_pred0_bin_fancy)
    imsave(os.path.join(folder_save, 'overalytrain10train13.png'), y_pred1_bin_fancy)
    
    return
Exemplo n.º 11
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
        def foo(n_segm, b=0):
            y_pred = n_segm.predict(self.img_x)

            thresh_single = optimal_test_thresh_equal_distribution(
                self.img_y_te, y_pred)
            # data_single_i = {'k': self.k,
            #                  'i_fold': i_fold,
            #                  'epoch': epoch}
            print(foo_performance(self.img_y_te, y_pred, thresh_single))

            img_clean = self.img_x[..., :3]
            concurrent([
                img_clean, y_pred[..., 1], y_pred[..., 1] >= thresh_single,
                semi_transparant(img_clean, y_pred[..., 1] >= thresh_single)
            ])

            if b:
                from data.datatools import imsave

                folder = '/home/lameeus/data/ghent_altar/output/hierarchy/'
                info_epoch = f'_epoch{n_segm.epoch}' if n_segm.epoch > 0 else ''
                filename = folder + f'13_small/pred_transfer_kfoldenc{self.fixed_enc}_ifold{self.i_fold}_avg{info_epoch}.png'
                imsave(filename, y_pred[..., 1])
Exemplo n.º 13
0
def main():
    ### Settings
    
    mod=5
    panel_nr = 19
    
    i_start ,i_end = 1, epochs_tot
    # i_start ,i_end = 1, 2
    
    k_lst = np.arange(1, 21)
    # k_lst = [1, 2]
    
    verbose=0
    b_plot = False
    
    ###
    
    if panel_nr == 13:
        train_data = get_13botleftshuang(mod=mod)
        folder_weights = '/scratch/lameeus/data/ghent_altar/net_weight/lamb_segmentation'
    elif panel_nr == 19:
        train_data = get_19SE_shuang(mod=mod)
        folder_weights = '/scratch/lameeus/data/ghent_altar/net_weight/19_hand_SE'
    else:
        raise ValueError(panel_nr)


    x, y_tr, _, y_te = get_training_data(train_data)

    (y_tr, y_te) = map(batch2img, (y_tr, y_te))
    


    assert i_end >= i_start
    
    if b_plot:
        # plotting
        pred_lst = []
        info_lst = []
    
    lst_data = []
    lst_data_avg_pred = []
    
    for k in k_lst:
    
        model = None
        
        pred_lst = []
        
        for epoch in np.arange(i_start, i_end + 1)[::-1]:
    
            info = f'settings: k {k}; epoch {epoch}'
            print('\n\t'+info)
            
            filepath_model = os.path.join(folder_weights, f'ti_unet_k{k}_imbalanced/w_{epoch}.h5')
    
            if epoch == i_end:
                model = load_model(filepath_model, custom_objects={'loss': loss,
                                                                   'accuracy_with0': accuracy_with0,
                                                                   'jaccard_with0': jaccard_with0,
                                                                   'kappa_loss': kappa_loss
                                                                   })
        
            else:
                model.load_weights(filepath_model)

            n = NeuralNet(model, w_ext=10)
    
            y_pred = n.predict(x)
            o = y_pred[..., 1]

            pred_lst.append(o)
            
            def print_conf(y_true, y_pred):
                    y_true = batch2img(y_true)
                    y_pred = batch2img(y_pred)
                    
                    b_annot = np.sum(y_true, axis=-1).astype(bool)
                    
                    y_true_annot = y_true[b_annot, :].argmax(axis=-1)
                    y_pred_annot = y_pred[b_annot, :].argmax(axis=-1)
                    
                    """
                    T0; predicted 1, but is 0
                    predicted 0, but is 1; T1
                    """
                    conf_mat = confusion_matrix(y_true_annot, y_pred_annot)
                    print(conf_mat)
                
            if 1:   # Single prediction
                
                if verbose == 1:
                    print_conf(y_tr, y_pred)
                    print_conf(y_te, y_pred)
                    
                if b_plot:
                    pred_lst.append(o)
                    info_lst.append(info)
    
                test_thresh = test_thresh_incremental(y_pred, y_tr, y_te, n=5, verbose=0)
                
                pred_thresh = np.greater_equal(o, test_thresh)
    
                pred_thresh_bin = np.stack([1-pred_thresh, pred_thresh], axis=-1)
    
                y_te_flat, y_pred_flat = filter_non_zero(y_te, pred_thresh_bin)
                y_te_argmax = np.argmax(y_te_flat, axis=-1)
                y_pred_argmax = np.argmax(y_pred_flat, axis=-1)
                acc, jacc, kappa = _get_scores(y_te_argmax, y_pred_argmax)
            
                if verbose == 1:
                    print_conf(y_tr, pred_thresh_bin)
                    print_conf(y_te, pred_thresh_bin)
    
                if 0: concurrent([pred_thresh])
                
                data_i = {'k':k,
                          'epoch':epoch,
                          'test_thresh':test_thresh,
                          'kappa':kappa,
                          'accuracy':acc,
                          'jaccard':jacc
                          }
                lst_data.append(data_i)
            
            if 1:   # avg prediction
    
                pred_i_average = np.mean(pred_lst, axis=0)
    
                # optimizing threshold prediction
                test_thresh = test_thresh_incremental(np.stack([1 - pred_i_average, pred_i_average], axis=-1), y_tr, y_te, n=5,
                                                      verbose=0)
                pred_thresh = np.greater_equal(pred_i_average, test_thresh)
                pred_thresh_bin = np.stack([1 - pred_thresh, pred_thresh], axis=-1)
    
                y_te_flat, y_pred_flat = filter_non_zero(y_te, pred_thresh_bin)
                y_te_argmax = np.argmax(y_te_flat, axis=-1)
                y_pred_argmax = np.argmax(y_pred_flat, axis=-1)
                acc, jacc, kappa = _get_scores(y_te_argmax, y_pred_argmax)
    
                data_i = {'k': k,
                          'epoch_start': epoch,
                          'test_thresh': test_thresh,
                          'kappa': kappa,
                          'accuracy': acc,
                          'jaccard': jacc
                          }
    
                lst_data_avg_pred.append(data_i)
            
    b = True
    if b:
        df = pd.DataFrame(lst_data)
        filename_save = f'tiunet_1pool_shaoguang{panel_nr}_imbalanced'
        filename_path = f'/scratch/lameeus/data/ghent_altar/dataframes/{filename_save}.csv'
        df.to_csv(filename_path, sep=';')
    
        df = pd.DataFrame(lst_data_avg_pred)
        filename_save = f'tiunet_1pool_shaoguang{panel_nr}_imbalanced_averaging'
        df.to_csv(f'/scratch/lameeus/data/ghent_altar/dataframes/{filename_save}.csv', sep=';')

    if b_plot:
        concurrent(pred_lst, info_lst)
    
    plt.show()
    
    return
Exemplo n.º 14
0
            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

    if 1:
        # Average out prediction

        y_pred_avg = average_out_pred()

        if 0:
            concurrent([y_pred_avg])

        y_pred_avg2 = np.stack([1 - y_pred_avg, y_pred_avg], axis=-1)

        data_i = _eval_func_single(get_crop(img_y), get_crop(y_pred_avg2))
        print(data_i)

    from performance.testing import get_y_pred_thresh

    thresh = df.iloc[i_max]['thresh']

    y_thresh = get_y_pred_thresh(y_pred, thresh=thresh)

    concurrent([img_x[..., :3], y_pred[..., 0], y_thresh[..., 0]])

    im_pred = get_crop(y_thresh)
Exemplo n.º 15
0
    def predict_compare(self):

        img_y_all = self.k_fold_train_data.get_train_data_all().get_y_train()
        lst_get = [
            get_borders1, get_borders2, get_borders3, get_borders4,
            get_borders5, get_borders6
        ]
        img_clean = self.img_x[..., :3]

        for i_fold in range(6):
            for i_fixed_enc in range(3):
                self.fixed_enc = i_fixed_enc

                y_lst = []
                # Average prediction
                for k in [17, 18, 19]:
                    for epoch in [96, 97, 98, 99, 100]:

                        y = self.predict(i_fold=i_fold, k=k, epoch=epoch)
                        y_lst.append(y)

                y_avg = np.mean(y_lst, axis=0)
                y_avg_bin = self.get_bin(y_avg)

                # Performance
                img_y_te = self.k_fold_train_data.k_split_i(
                    i_fold).get_y_test()

                thresh = optimal_test_thresh_equal_distribution(
                    img_y_all, y_avg)

                perf = foo_performance(img_y_te, y_avg, thresh)

                # CROP

                w0, w1, h0, h1 = lst_get[i_fold]()

                y_avg_bin_crop = y_avg_bin[h0:h1, w0:w1]
                clean_crop = img_clean[h0:h1, w0:w1, :]

                y_avg_bin_transparent_crop = semi_transparant(
                    clean_crop, y_avg_bin_crop)
                if 0:
                    concurrent([
                        clean_crop, y_avg_bin_crop, y_avg_bin_transparent_crop
                    ])

                # Save
                if self.fixed_enc == 0: info_enc = 'Train'
                elif self.fixed_enc == 1: info_enc = 'Fixed'
                elif self.fixed_enc == 2: info_enc = 'FixedTrain'

                folder_save = '/scratch/lameeus/data/ghent_altar/output/hierarchy/10_lamb/ifolds'
                filename = f'_enc{info_enc}_ifold{i_fold}_jacc{perf["jaccard"]:.3f}.png'
                # Save y_bin

                imsave(os.path.join(folder_save, 'binpred' + filename),
                       y_avg_bin_crop,
                       b_check_duplicate=False)

                # Save overlay
                imsave(os.path.join(folder_save, 'overlay' + filename),
                       y_avg_bin_transparent_crop,
                       b_check_duplicate=False)
Exemplo n.º 16
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
Exemplo n.º 17
0
    if 0:
        a = get_19hand()
        b = False
        if b:
            a.plot()

        ### Training/Validation data
        img_y = a.get('annot')
        y = annotations2y(img_y)
        y_annot = y2bool_annot(y)

        b = False
        if b:
            y_annot_tr, y_annot_te = panel19withoutRightBot(y_annot)
        
            concurrent([a.get('clean'), y_annot, y_annot_tr, y_annot_te],
                       ['clean', 'annotation', 'a annot', 'test annot'])

    if 0:
        train_data = get_train19_topleft(mod=mod)
    elif dataset_name == '19_hand_SE':
        train_data = get_19SE_shuang(mod=mod)
    else:
        train_data = get_13botleftshuang(mod=mod)

    # TODO normalise inputs This seems to be super important...
    # train_data.x = (1/255. * train_data.x).astype(np.float16)
    # train_data.x = (255. * train_data.x).astype(np.float16)

    x, y_tr, x_te, y_te = get_training_data(train_data)

    # To get w_ext
Exemplo n.º 18
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
    # Read paint loss detection
    b_mask = detect_colour(
        imread(
            "C:/Users/admin/OneDrive - ugentbe/data/images_paper/1319_10nat_V3.png"
        ), "cyan")
    if 0:
        plt.imshow(b_mask)

    # Read inpaint
    im_inpaint = imread(os.path.join(f, "inpainting_comb.jpg"))

    im_inpainted_new = inpaint_replacer(im_orig, b_mask, im_inpaint)
    plt.imshow(im_inpainted_new)

    concurrent([im_orig, im_inpainted_new, im_inpaint, b_mask],
               ['orig', 'new', 'old', 'mask'])

    # Save
    imsave(os.path.join(f, "inpaint_v2.png"), im_inpainted_new)

    # Crop?
    im_treated = imread(os.path.join(f, "restored_SR.jpg"))

    # Trying to rescale to similar colour pallet
    im_treated_recolour = im_treated.astype(float)
    im_treated_recolour = (im_treated_recolour - im_treated.mean(
        (0, 1))) * im_inpainted_new.std((0, 1)) / im_treated.std(
            (0, 1)) + im_inpainted_new.mean((0, 1))
    im_treated_recolour = np.clip(im_treated_recolour, 0, 255)
    im_treated_recolour = im_treated_recolour.astype(np.uint8)
def main():
    """

    :return:
    """

    ### Settings
    mod = 5

    w_patch = 16 * 2
    """
    Data (all important modalities)
    """

    # folder_windows = r'C:\Users\Laurens_laptop_w\OneDrive - UGent\data\10lamb'
    train_data = get_10lamb_old(mod)
    img_x, img_y_tr, _, _ = get_training_data(train_data)
    # Normalise the input!
    img_x = rescale0to1(img_x)
    """
    Train segmentation
        1) reuse everything
        2) fix encoder
    """

    if 1:

        if 1:
            b_encoder_fixed = False

            info_enc_fixed = '_enc_fixed' if b_encoder_fixed else ''
            get_info = lambda: f'10lamb_kfold_pretrained{info_enc_fixed}/unet_enc_k{k}_ifold{i_fold}'

            n_epochs = 40

            k = 10

            if k == 10:
                epoch_w = 100
            else:
                raise NotImplementedError()

            ### Settings you don't have to change:

            w_patch = 50
            w_ext_in = 28
            b_double = False
            padding = 'valid'

            # TODO flag for converting encoder to dilated conv

            def get_unet_pretrained_encoder():

                model_encoder = get_model_encoder()

                encoder_inputs = model_encoder.input

                decoder_outputs = decoder(model_encoder, f_out=2)

                model_pretrained_unet = Model(encoder_inputs, decoder_outputs)
                from methods.examples import compile_segm
                compile_segm(model_pretrained_unet, lr=1e-4)

                model_pretrained_unet.summary()

                return model_pretrained_unet

            """
            Train
            """

            k_fold_train_data = get_10lamb_6patches(5)
            for i_fold in range(6):
                """
                Get a new network (not trained yet for segmentation)
                """

                model_pretrained_unet = get_unet_pretrained_encoder()
                n_pretrained_unet = NeuralNet(model_pretrained_unet)
                """
                The data
                """

                train_data_i = k_fold_train_data.k_split_i(i_fold)

                info = get_info()

                img_y_tr = train_data_i.get_y_train()
                img_y_te = train_data_i.get_y_test()

                flow_tr = get_flow(
                    img_x,
                    img_y_tr,
                    w_patch=w_patch,  # Comes from 10
                    w_ext_in=w_ext_in)

                flow_te = get_flow(
                    img_x,
                    img_y_te,
                    w_patch=w_patch,  # Comes from 10
                    w_ext_in=w_ext_in)

                n_pretrained_unet.train(flow_tr,
                                        flow_te,
                                        epochs=n_epochs,
                                        verbose=1,
                                        info=info)
                """
                Prediction
                """

                n_pretrained_unet.w_ext = w_ext_in
                y_pred = n_pretrained_unet.predict(img_x)

                concurrent([y_pred[..., 1]])
    """
    Classification
    """

    if 1:
        im_clean = img_x[..., :3]

        k = 8
        i_fold = 3
        epoch_last = 40

        from methods.examples import kappa_loss, weighted_categorical_crossentropy
        from performance.metrics import accuracy_with0, jaccard_with0
        loss = weighted_categorical_crossentropy((1, 1))

        list_y_pred = []

        ### K fold validation
        k_fold_train_data = get_10lamb_6patches(5)
        train_data_i = k_fold_train_data.k_split_i(i_fold)
        img_y_tr = train_data_i.get_y_train()
        img_y_te = train_data_i.get_y_test()

        for epoch in np.arange(31, epoch_last + 1):
            filepath_model = f'/scratch/lameeus/data/ghent_altar/net_weight/10lamb_kfold/ti_unet_k{k}_kfold{i_fold}/w_{epoch}.h5'

            model = load_model(filepath_model,
                               custom_objects={
                                   'loss': loss,
                                   'accuracy_with0': accuracy_with0,
                                   'jaccard_with0': jaccard_with0,
                                   'kappa_loss': kappa_loss
                               })

            n = NeuralNet(model, w_ext=10)
            y_pred = n.predict(img_x)

            list_y_pred.append(y_pred)

        y_pred_mean = np.mean(list_y_pred, axis=0)
        q1 = y_pred_mean[..., 1]
        concurrent([q1, q1.round(), im_clean])
        """
        Optimal threshold (making conf matrix symmetric, not based on maximising kappa)
        """
        y_gt = np.any([img_y_tr, img_y_te], axis=0)

        from performance.testing import _get_scores, filter_non_zero

        def foo_performance(y_true, y_pred, thresh):
            # is basically argmax
            y_pred_thresh_arg = np.greater_equal(y_pred[..., 1], thresh)

            y_true_flat, y_pred_thresh_arg_flat = filter_non_zero(
                y_true, y_pred_thresh_arg)
            y_te_argmax = np.argmax(y_true_flat, axis=-1)

            # Kappa
            return _get_scores(y_te_argmax, y_pred_thresh_arg_flat)[-1]

        """
        1. BEST? PERFORMANCE based on test set
        """

        print('1. Test distribution optimization')

        thresh = optimal_test_thresh_equal_distribution(img_y_te, y_pred_mean)
        q1_thresh = np.greater_equal(q1, thresh)
        concurrent([q1, q1_thresh, im_clean])

        print(f'thresh: {thresh}')

        # Test, train, both
        print('Kappa performance:')
        print('\ttrain:', foo_performance(img_y_tr, y_pred_mean, thresh))
        print('\ttestset:', foo_performance(img_y_te, y_pred_mean, thresh))
        print('\tboth:', foo_performance(y_gt, y_pred_mean, thresh))

        print('\nIncremental optimization on test set')

        test_thresh2 = test_thresh_incremental(y_pred_mean,
                                               img_y_tr,
                                               img_y_te,
                                               n=5,
                                               verbose=0)

        print('Kappa performance:')
        print('\ttrain:', foo_performance(img_y_tr, y_pred_mean, test_thresh2))
        print('\ttestset:', foo_performance(img_y_te, y_pred_mean,
                                            test_thresh2))
        print('\tboth:', foo_performance(y_gt, y_pred_mean, test_thresh2))
        """
        2. based on train
        """

        print('\n2. Training distribution optimization')

        thresh = optimal_test_thresh_equal_distribution(img_y_tr, y_pred_mean)
        q1_thresh = np.greater_equal(q1, thresh)
        concurrent([q1, q1_thresh, im_clean])

        print(f'thresh: {thresh}')

        # Test, train, both
        print('Kappa performance:')
        print('\ttrain:', foo_performance(img_y_tr, y_pred_mean, thresh))
        print('\ttestset:', foo_performance(img_y_te, y_pred_mean, thresh))
        print('\tboth:', foo_performance(y_gt, y_pred_mean, thresh))
        """
        3. CONSISTENT: based on train+set
        """

        print('\n3. all GT distribution optimization')

        thresh = optimal_test_thresh_equal_distribution(y_gt, y_pred_mean)
        q1_thresh = np.greater_equal(q1, thresh)
        concurrent([q1, q1_thresh, im_clean])

        print(f'thresh: {thresh}')

        # Test, train, both
        print('Kappa performance:')
        print('\ttrain:', foo_performance(img_y_tr, y_pred_mean, thresh))
        print('\ttestset:', foo_performance(img_y_te, y_pred_mean, thresh))
        print('\tboth:', foo_performance(y_gt, y_pred_mean, thresh))

        if 0:
            """
            4. DUMB/Not needed: Based on prediction of whole panel
            """

            thresh = optimal_test_thresh_equal_distribution(y_gt,
                                                            y_pred_mean,
                                                            mask_true=False)
            q1_thresh = np.greater_equal(q1, thresh)
            concurrent([q1, q1_thresh, im_clean])

    print('Done')