def main_train(self, train_data, steps_per_epoch=None): if steps_per_epoch is None: steps_per_epoch = self.steps_per_epoch from main_general import get_training_data from preprocessing.image import get_flow # TODO train x_train, y_train, x_val, y_val = get_training_data(train_data) # Generator flow_tr = get_flow(x_train, y_train, w_patch=self.w_patch, w_ext_in=self.w_ext_in) flow_va = get_flow(x_val, y_val, w_patch=self.w_patch, w_ext_in=self.w_ext_in) epochs = 1 self.neural_net.train( flow_tr, validation=flow_va, epochs=epochs, steps_per_epoch=steps_per_epoch, info=f'{set_net["name"]}_d{self.d}_k{self.k}_n{self.n_per_class}')
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 set_flow(train_data): x_train, y_train, _, _ = get_training_data(train_data) global flow_tr flow_tr = get_flow(x_train, y_train, w_patch=w_patch, w_ext_in=w_ext_in)
def get_flow_xy(xy): if isinstance(xy, tuple): x, y = map(batch2img, xy) flow = get_flow(batch2img(x), batch2img(y)) return flow elif isinstance(xy, (NumpyArrayIterator, )): return xy else: raise TypeError(f'Unkown type for xy: {type(xy)}')
def set_flow(self): # w_out should be 2+4*n w_checker = 512 if self.ae_set_nr is not None: img_x = [] for set_nr in self.ae_set_nr: if set_nr == 10: df = get_10lamb() elif set_nr == 13: df = get_13zach() elif set_nr == 19: df = get_19hand() img_x_i = xy_from_df(df, mod)[0] img_x.append() else: raise NotImplementedError() img_x_lst = [self.img_x] h_x, w_x = img_x.shape[:2] x_ae_tr = [] x_ae_te = [] for i in range(int(np.ceil(h_x / w_checker))): for j in range(int(np.ceil(w_x / w_checker))): h0 = i * w_checker w0 = j * w_checker crop_x = img_x[h0:h0 + w_checker, w0:w0 + w_checker, ...] if (i + j) % 2 == 0: x_ae_tr.append(crop_x) else: x_ae_te.append(crop_x) self.flow_ae_tr = get_flow( x_ae_tr, x_ae_tr, w_patch=self.w_patch, w_ext_in=self.w_ext_in_ae, ) self.flow_ae_te = get_flow( x_ae_te, x_ae_te, w_patch=self.w_patch, w_ext_in=self.w_ext_in_ae, ) """ Data for Segmentation of 10 lamb """ self.k_fold_train_data = get_10lamb_6patches(5) self.flow_segm = get_flow( self.img_x, self.k_fold_train_data.get_train_data_all().get_y_train(), w_patch=self.w_patch, w_ext_in=self.w_ext_in_ti if self.ti else self.w_ext_in_ae, )
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')
def train_segm(self): folder_save = '/home/lameeus/data/ghent_altar/dataframes' info_batchnorm = '_batchnorm' if self.batch_norm else '' info_fixed = '_encfixed' if self.fixed_enc == 1 else '_prefixed' if self.fixed_enc == 2 else '' info_model = 'tiunet' if self.ti else 'unet' filename_single = f'pretrained/{info_model}_10lamb_kfold{info_fixed}{info_batchnorm}/d{self.depth}_single' path_single = os.path.join(folder_save, filename_single + '.csv') get_info = lambda: f'10lamb_kfold_pretrained{info_fixed}{info_batchnorm}/{info_model}_d{self.depth}_k{self.k}_ifold{i_fold}' img_y_all = self.k_fold_train_data.get_train_data_all().get_y_train() def get_model(): if self.ti: model = self.get_tiunet_preenc(k=self.k, lr=self.lr_opt) else: model = self.get_unet_preenc(k=self.k, lr=self.lr_opt) if self.fixed_enc == 2: n_temp = NeuralNet(model) folder_weights = '/scratch/lameeus/data/ghent_altar/net_weight' folder1 = f'10lamb_kfold_pretrained{"_encfixed"}{info_batchnorm}' folder2 = f'{info_model}_d{self.depth}_k{self.k}_ifold{i_fold}' n_temp.load(os.path.join(folder_weights, folder1, folder2), 100) del (n_temp) return model w_ext = self.w_ext_in_ti if self.ti else self.w_ext_in_ae if not self.lr_opt: model_segm = get_model() find_learning_rate(model_segm, self.flow_segm, lr1=1e0) for i_fold in range(6): print(f'i_fold = {i_fold}') model_segm = get_model() n_segm = NeuralNet(model_segm, w_ext=w_ext) train_data_i = self.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() flow_tr = get_flow(self.img_x, img_y_tr, w_patch=self.w_patch, w_ext_in=w_ext) flow_te = get_flow(self.img_x, img_y_te, w_patch=self.w_patch, w_ext_in=w_ext) info = get_info() for epoch in range(self.epochs): n_segm.train(flow_tr, flow_te, epochs=1, verbose=2, info=info) y_pred = n_segm.predict(self.img_x) thresh_single = optimal_test_thresh_equal_distribution( img_y_all, y_pred) data_single_i = {'k': self.k, 'i_fold': i_fold, 'epoch': epoch} data_single_i.update( foo_performance(img_y_te, y_pred, thresh_single)) lst_data_single = [data_single_i] df_single = pd.DataFrame(lst_data_single) pandas_save(path_single, df_single, append=True) return
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 w_ext = neuralNet0(mod=mod, k=1, verbose=1).w_ext flow_tr = get_flow(x[0], y_tr[0], w_patch=10, # Comes from 10 w_ext_in=w_ext ) flow_te = get_flow(x_te[0], y_te[0], w_patch=10, # Comes from 10 w_ext_in=w_ext ) b = 1 class_weight = (1, 1) if b: # Balance the data class_weight_tr = get_class_weights(flow_tr) class_weight = tuple(c_i * c_j for c_i, c_j in zip(class_weight, class_weight_tr))