Example #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
Example #2
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
Example #3
0
    def _data10nat():
        # Sparse annotations
        train_data = get_10lamb_old(5)

        from datasets.examples import get_10lamb
        from data.conversion_tools import annotations2y
        train_data.y_tr = annotations2y(get_10lamb().get("annot_tflearning"))

        return train_data
Example #4
0
def class_imbalance():
    
    y = annotations2y(imread('/home/lameeus/data/ghent_altar/input/hierarchy/10_lamb/annotations/kfold/annot_comb.png'))

    n = np.sum(y, axis=(0, 1))
    print(n)
    print('f:', n/np.sum(n))

    return
    def set_img_x(self):
        if self.set_nr == 13:
            train_data = get_13(self.mod)

            from data.datatools import imread
            from data.conversion_tools import annotations2y
            train_data.y_te = np.copy(train_data.y_tr)
            train_data.y_tr = annotations2y(imread(
                '/home/lameeus/data/ghent_altar/input/hierarchy/13_small/clean_annot_practical.png'
            ),
                                            thresh=.9)

            img_x, img_y, _, img_y_te = get_training_data(train_data)

        # Normalise the input!
        img_x = rescale0to1(img_x)
        self.img_x = img_x
        self.img_y_tr = img_y
        self.img_y_te = img_y_te

        train_data_10 = get_10lamb_6patches(self.mod).get_train_data_all()
        img_x_10, img_y_10, _, _ = get_training_data(train_data_10)
        # Normalise the input!
        img_x_10 = rescale0to1(img_x_10)

        self.flow_tr_set = get_flow(self.img_x,
                                    self.img_y_tr,
                                    w_patch=self.w_patch,
                                    w_ext_in=self.w_ext_in_ti)
        self.flow_tr_10 = get_flow(img_x_10,
                                   img_y_10,
                                   w_patch=self.w_patch,
                                   w_ext_in=self.w_ext_in_ti)
        n_multiply = 10
        self.flow_tr_set_10 = get_flow([self.img_x] * n_multiply + [img_x_10],
                                       [self.img_y_tr] * n_multiply +
                                       [img_y_10],
                                       w_patch=self.w_patch,
                                       w_ext_in=self.w_ext_in_ti)

        self.flow_ae_tr = get_flow(
            self.img_x,
            self.img_x,
            w_patch=self.w_patch,
            w_ext_in=self.w_ext_in_ae,
        )
def data_lamb():
    img_x, _ = xy_from_df(get_10lamb(), 5)

    if 0:
        img_y_val = annotations2y(get_10lamb_kfold().get("annot_clean_comb"))
    else:
        # "Improved" evaluation set

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

        img_y_val_lst = []

        for i in [1, 2, 3, 5]:
            y_i = kFoldTrainData.k_split_i(i).get_y_test()

            img_y_val_lst.append(y_i)

        img_y_val = np.sum(img_y_val_lst, axis=0)

    return img_x, img_y_val
def lamb():

    folder_in = '/net/gaimfs/research/lameeus/data/Nina/'

    im_clean = imread(os.path.join(folder_in, 'clean.png'))
    im_detection = imread(os.path.join(folder_in, 'paintloss_tiunet_enc0.png'))

    extra1 = imread(
        '/scratch/lameeus/data/ghent_altar/input/hierarchy/10lamb/mask_pred_comb.png'
    )
    b1 = np.equal(extra1, 255)

    im_update = np.copy(im_clean)

    extra2 = imread('/home/lameeus/Desktop/extra_annot.png')

    cyan = [0, 255, 255]

    im_update[im_detection.astype(bool), :] = cyan

    im_update[b1, :] = cyan

    y_extra2 = annotations2y(extra2)

    y_extra2_0 = y_extra2[..., 0].astype(bool)
    y_extra2_1 = y_extra2[..., 1].astype(bool)
    im_update[y_extra2_0, :] = im_clean[y_extra2_0, :]
    im_update[y_extra2_1, :] = cyan

    plt.imshow(im_update)

    from data.conversion_tools import detect_colour
    paintloss_updated = detect_colour(im_update, 'cyan')

    imsave('/net/gaimfs/research/lameeus/data/Nina/detection_updated.png',
           paintloss_updated)

    return im_update
def crop_left_bot_script():

    panel_nr = 19  # 13, 19
    modality = 'annot'
    modality = 'src'

    folder = f'/home/lameeus/data/ghent_altar/input/hierachy/{panel_nr}_small'
    if modality == 'annot':
        path_mod = os.path.join(folder, 'annot' + '.tif')
    elif modality == 'src':
        folder_src = f'/scratch/lameeus/data/ghent_altar/output/hierarchy/{panel_nr}_small/fancy'
        path_mod = os.path.join(folder_src, f'{panel_nr}_3_src-kf.jpg')

    path_clean = os.path.join(folder, 'clean.tif')

    img_clean = get_image(path_clean)[..., :3]
    img_mod = get_image(path_mod)

    img_clean = get_crop(img_clean)
    if modality == 'annot':
        img_mod = get_crop(img_mod)
    elif modality == 'src':
        img_mod = resize(img_mod, (300, 300))

    y_mod = annotations2y(img_mod, thresh=.7)

    if modality == 'src':
        plt.imshow(y_mod[..., 0])

    cyan = [0, 255, 255]
    green = [0, 127, 0]
    lime = [50, 205, 50]
    gray = [127, 127, 127]
    white = [255, 255, 255]
    black = [0, 0, 0]

    b1 = y_mod[..., 1].astype(bool)

    img_clean_y = np.copy(img_clean)
    img_clean_y[b1, :] = cyan

    # background
    def grayer(img, img_clean, b1):
        frac = .5
        img[~b1, :] = (frac * img_clean[~b1, :] +
                       (1 - frac) * np.asarray(gray)).astype(img.dtype)

    def darker(img, img_clean, b1):
        frac = .5
        img[~b1, :] = (frac * img_clean[~b1, :] +
                       (1 - frac) * np.asarray(black)).astype(img.dtype)

    grayer(img_clean_y, img_clean, b1)
    # darker(img, img_clean, b1)

    plt.imshow(img_clean_y)
    plt.show()

    if 0:
        path_save = f'/scratch/lameeus/data/ghent_altar/output/hierarchy/{panel_nr}_small/fancy/shuang_{modality}.png'
        if os.path.exists(path_save) == False:
            save_img(path_save, img_clean_y)
        else:
            print(f"didn't save: {path_save}")

    return img_clean_y
Example #9
0
def xy_from_df(df, mod):
    img_x = x_from_df(df, mod)
    img_y = annotations2y(df.get('annot'))

    return img_x, img_y
Example #10
0
def combine_annot(b_save=True):
    ext = 100

    red = [255, 0, 0]
    blue = [0, 0, 255]

    folder_annots = '/home/lameeus/data/ghent_altar/input/hierachy/10_lamb/annotations/lameeus'
    folder_save = '/home/lameeus/data/ghent_altar/input/hierachy/10_lamb/annotations/kfold'

    # Clean image
    im_clean = imread(
        '/home/lameeus/data/ghent_altar/input/hierachy/10_lamb/clean.png')
    im_white = 255 * np.ones(shape=im_clean.shape, dtype=np.uint8)

    im_annot = np.copy(im_white)
    im_annot_clean = np.copy(im_clean)

    def apply_redblue(im, h0, h1, w0, w1, b0, b1):
        # red to red
        # rest to blue
        im[h0:h1, w0:w1, :][~b1, :] = blue
        im[h0:h1, w0:w1, :][b1, :] = red

    for i in range(6):
        im = imread(os.path.join(folder_annots,
                                 f'crop{i+1}_annotation_lm.png'))

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

        im_crop = im[ext:-ext, ext:-ext, :]
        y_crop = annotations2y(im_crop, thresh=.9)
        b1 = y_crop[..., 1].astype(bool)

        if 0:
            plt.figure()
            plt.imshow(im_crop)
            plt.show()

        if b_save:
            im_annot_i = np.copy(im_white)
            im_annot_clean_i = np.copy(im_clean)

            apply_redblue(im_annot_i, h0, h1, w0, w1, ~b1, b1)
            apply_redblue(im_annot_clean_i, h0, h1, w0, w1, ~b1, b1)

            imsave(os.path.join(folder_save, f'annot_{i+1}.png'), im_annot_i)
            imsave(os.path.join(folder_save, f'annot_clean_{i+1}.png'),
                   im_annot_clean_i)

        apply_redblue(im_annot, h0, h1, w0, w1, ~b1, b1)
        apply_redblue(im_annot_clean, h0, h1, w0, w1, ~b1, b1)

    plt.figure()
    plt.imshow(im_annot)
    plt.show()

    if b_save:
        imsave(os.path.join(folder_save, 'annot_comb.png'), im_annot)
        imsave(os.path.join(folder_save, 'annot_clean_comb.png'),
               im_annot_clean)

    return