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
def __init__(self): self.set_data() if 1: y_lst = [] for i_fold in range(6): for k in [12, 13]: for epoch in [99, 100]: y = self.predict(i_fold=0, k=k, epoch=epoch) y_lst.append(y) y_avg = np.mean(y_lst, axis=0) y_avg1 = y_avg[..., 1] plt.imshow(y_avg1) if 0: imsave( f'/home/lameeus/data/ghent_altar/output/hierarchy/13_small/pred_transfer_kfoldenc{self.fixed_enc}_avg.png', y_avg1) if 0: self.predict_average() if 0: self.predict_compare() if 0: # regular TI-UNet self.predict_compare_regular()
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
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)
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
def combine_stitches(): folder = '/home/lameeus/data/ghent_altar/output/hierarchy/10_lamb/inpainting' im_base = imread(os.path.join(folder, 'inpainting_stitch_f2_g3_fixed_v0_linear_c256.png')) im_leftbot = imread(os.path.join(folder, 'inpainting_stitch_f2_g3_fixed_v1_linear_c256_manual.png')) im_base[3*256:, :(3)*256, :] = im_leftbot[3*256:, :(3)*256, :] if 0: plt.imshow(im_base) imsave(os.path.join(folder, 'inpainting_comb.png'), im_base) from figures_paper.overlay import semi_transparant im_clean = imread('/home/lameeus/data/ghent_altar/input/hierarchy/10_lamb/clean.png') im_paintloss = imread('/home/lameeus/data/ghent_altar/output/hierarchy/10_lamb/detection_updated.png') im_overlay = semi_transparant(im_clean, im_paintloss) if 1: plt.imshow(im_overlay) folder = '/home/lameeus/data/ghent_altar/output/hierarchy/10_lamb/fancy' imsave(os.path.join(folder, 'det_overlay.png'), im_overlay)
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 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])
resolution_scale = 16 if (modality == 'irr') & (panel_nr == 13): resolution_scale = 15 im = get_im(panel_nr=panel_nr, resolution_scale=resolution_scale, modality=modality, b_grid=True) if modality in ('irr', 'ir', 'xray'): if len(im.shape ) == 3: # In case the grayscale is loaded as 3 colour channels. im = np.mean(im, axis=2) if os.name == 'nt': # Laptop windows if panel_nr == 13: folder_save = f'C:/Users/Laurens_laptop_w/OneDrive - UGent/data/13_small/webscrape' else: folder_save = None raise NotImplementedError() else: # Desktop Linux if 0: folder_save = f'C:/Users/Laurens_laptop_w/OneDrive - UGent/data/{panel_nr}_small/webscrape' folder_save = f'/scratch/lameeus/data/ghent_altar/webscrape/{panel_nr}' assert os.path.exists(folder_save) path_save = os.path.join(folder_save, f'{modality}_{resolution_scale}.png') imsave(path_save, im, b_check_duplicate=True) print("Done")
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)
"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) imsave(os.path.join(f, "10treated2.png"), im_treated_recolour) l = []
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
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
l = [im_clean] for i, b in enumerate([b_annot, b_sh, b_pred, b_pred_unet]): if i == 0: # Probably not really interesting to change the colour l.append( semi_transparant(im_clean, b.astype(bool), color1='cyan', color2='grey', transparency=0, transparancy2=.5)) else: l.append( semi_transparant(im_clean, b.astype(bool), color2='grey', transparency=0, transparancy2=.5)) concurrent(l) folder_out = 'C:/Users/admin/OneDrive - ugentbe/data/images_paper' imsave(os.path.join(folder_out, f'{data}_clean.png'), im_clean) imsave(os.path.join(folder_out, f'{data}_annot.png'), l[1]) imsave(os.path.join(folder_out, f'{data}_src.png'), l[2]) imsave(os.path.join(folder_out, f'{data}_tiunet.png'), l[3]) imsave(os.path.join(folder_out, f'{data}_unet.png'), l[4])