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 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 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 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 __init__(self, folder): self._imgs = {} # TODO only read image on the spot (less memory intensive) for file in os.listdir(folder): root, ext = os.path.splitext(file) if ext.lower() in ['.png', '.tif', '.jpg']: path = os.path.join(folder, file) img = imread(path) self._imgs[root] = img super().__init__()
def get_19SE_shuang_crack(mod, n_per_class=80, debug=False, n_outputs=2): """ John the evangelist see Journal 2020 S. Huang :param mod: :param n_per_class: :return: """ img_x, img_y_full = xy_from_df(get_19hand(), mod) _, img_y = panel19withoutRightBot(img_y_full) img_y_tr, img_y_te = _split_train_n_per_class(img_y, n_per_class) from data.datatools import imread img_y_tr_crack300 = imread( "/home/lameeus/data/ghent_altar/input/hierarchy/19_small/grd_crack.png" ) # Init img_y_tr_crack = np.zeros(img_y.shape) # Watch out, both classes are now marked as one img_y_tr_crack[1083:1083 + 300, 1740:1740 + 300, :][img_y_tr_crack300 > 0, :] = 1 img_y_tr_crack, _ = _split_train_n_per_class(img_y_tr_crack, n_per_class) img_y_tr_crack[..., 1] = 0 # Fix from above! if n_outputs == 2: img_y_tr_dual = np.logical_or(img_y_tr, img_y_tr_crack) elif n_outputs == 3: # Add a third class: Cracks img_y_tr_dual = np.concatenate([img_y_tr, img_y_tr_crack[..., :1]], axis=-1) else: raise ValueError(n_per_class) assert 2 * n_per_class <= np.sum(img_y_tr_dual) <= 3 * n_per_class if debug: import matplotlib.pyplot as plt plt.imshow(np.sum(img_y_tr, axis=-1)) plt.show() train_data = TrainData(img_x, img_y_tr_dual, img_y_te) return train_data
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, )
# Replace paint loss in original with inpaint def inpaint_replacer(im_orig, b_mask, im_inpaint): assert im_orig.shape[:2] == b_mask.shape == im_inpaint.shape[:2] im_new = im_orig.copy() im_new[b_mask, :] = im_inpaint[b_mask, :] return im_new if __name__ == '__main__': f = "C:/Users/admin/Downloads" # Read original im_orig = imread( "C:/Users/admin/OneDrive - ugentbe/data/10_lamb/clean.png") # 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)
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
# TODO double check performance! import os folder = 'C:/Users/admin/OneDrive - ugentbe/Documents/2019journal paper/2020_04_06/images/results_compare' from data.datatools import imread, imsave # Annot if '13' in data: data_nr = 13 elif '19' in data: data_nr = 19 else: raise ValueError(data) im_clean = imread(os.path.join(folder, f'{data_nr}_3_clean.jpg')) im_annot = imread(os.path.join(folder, f'{data_nr}_3_annot.png')) im_sh = imread(os.path.join(folder, f'{data_nr}_3_src.png')) # TODO get binary from data.conversion_tools import detect_colour b_annot = detect_colour(im_annot, 'cyan', thresh=.9) b_sh = detect_colour(im_sh, 'cyan', thresh=.9) # Performance measures: