def on_epoch_end(self, epoch, logs={}): if epoch > 0: patients_per_batch = IMGS_PER_ENS_BATCH num_batches = num_un_labeled_train // patients_per_batch remainder = num_un_labeled_train % patients_per_batch num_batches = num_batches if remainder is 0 else num_batches + 1 for b_no in np.arange(num_batches): actual_batch_size = patients_per_batch if ( b_no <= num_batches - 1 and remainder == 0) else remainder start = (b_no * patients_per_batch) + num_labeled_train end = (start + actual_batch_size) imgs = get_array(self.imgs_path, start, end) ensemble_prediction = get_array(self.ensemble_path, start, end, dtype='float32') supervised_flag = get_array(self.supervised_flag_path, start, end, dtype='float16') inp = [imgs, ensemble_prediction, supervised_flag] del imgs, supervised_flag cur_pred = np.zeros( (actual_batch_size, 32, 168, 168, NUM_CLASS)) model_out = model.predict(inp, batch_size=2, verbose=1) del inp cur_pred[:, :, :, :, 0] = model_out[0] cur_pred[:, :, :, :, 1] = model_out[1] cur_pred[:, :, :, :, 2] = model_out[2] cur_pred[:, :, :, :, 3] = model_out[3] cur_pred[:, :, :, :, 4] = model_out[4] del model_out # Z = αZ + (1 - α)z ensemble_prediction = alpha * ensemble_prediction + ( 1 - alpha) * cur_pred save_array(self.ensemble_path, ensemble_prediction, start, end) del ensemble_prediction if 'cur_pred' in locals(): del cur_pred
def on_epoch_end(self, epoch, logs={}): # print(time() - self.starttime) # model_temp = model sup_count = self.count pz_save, self.val_pz_dice_coef = self.shall_save(logs['val_pz_dice_coef'], self.val_pz_dice_coef) cz_save, self.val_cz_dice_coef = self.shall_save(logs['val_cz_dice_coef'], self.val_cz_dice_coef) us_save, self.val_us_dice_coef = self.shall_save(logs['val_us_dice_coef'], self.val_us_dice_coef) afs_save, self.val_afs_dice_coef = self.shall_save(logs['val_afs_dice_coef'], self.val_afs_dice_coef) bg_save, self.val_bg_dice_coef = self.shall_save(logs['val_bg_dice_coef'], self.val_bg_dice_coef) if epoch > 0: patients_per_batch = IMGS_PER_ENS_BATCH num_batches = num_un_labeled_train // patients_per_batch remainder = num_un_labeled_train % patients_per_batch num_batches = num_batches if remainder is 0 else num_batches + 1 for b_no in np.arange(num_batches): actual_batch_size = patients_per_batch if ( b_no <= num_batches - 1 and remainder == 0) else remainder start = (b_no * patients_per_batch) + num_labeled_train end = (start + actual_batch_size) imgs = get_array(self.imgs_path, start, end) ensemble_prediction = get_array(self.ensemble_path, start, end, dtype='float32') supervised_flag = get_array(self.supervised_flag_path, start, end, dtype='float16') inp = [imgs, ensemble_prediction, supervised_flag] del imgs, supervised_flag cur_pred = np.zeros((actual_batch_size, 32, 168, 168, NUM_CLASS)) # cur_sigmoid_pred = np.zeros((actual_batch_size, 32, 168, 168, NUM_CLASS)) model_out = model.predict(inp, batch_size=2, verbose=1) # 1 # model_out = np.add(model_out, train.predict(inp, batch_size=2, verbose=1)) # 2 del inp cur_pred[:, :, :, :, 0] = model_out[0] if pz_save else ensemble_prediction[:, :, :, :, 0] cur_pred[:, :, :, :, 1] = model_out[1] if cz_save else ensemble_prediction[:, :, :, :, 1] cur_pred[:, :, :, :, 2] = model_out[2] if us_save else ensemble_prediction[:, :, :, :, 2] cur_pred[:, :, :, :, 3] = model_out[3] if afs_save else ensemble_prediction[:, :, :, :, 3] cur_pred[:, :, :, :, 4] = model_out[4] if bg_save else ensemble_prediction[:, :, :, :, 4] del model_out # Z = αZ + (1 - α)z ensemble_prediction = alpha * ensemble_prediction + (1 - alpha) * cur_pred save_array(self.ensemble_path, ensemble_prediction, start, end) del ensemble_prediction # flag = np.where(np.reshape(np.max(ensemble_prediction, axis=-1),supervised_flag.shape) >= THRESHOLD, np.ones_like(supervised_flag),np.zeros_like(supervised_flag)) # dont consider background # cur_pred[:, :, :, :, 4] = np.zeros((actual_batch_size, 32, 168, 168)) argmax_pred_ravel = np.ravel(np.argmax(cur_pred, axis=-1)) max_pred_ravel = np.ravel(np.max(cur_pred, axis=-1)) indices = None del cur_pred for zone in np.arange(5): final_max_ravel = np.where(argmax_pred_ravel == zone, np.zeros_like(max_pred_ravel), max_pred_ravel) zone_indices = np.argpartition(final_max_ravel, -self.confident_pixels_no)[ -self.confident_pixels_no:] if zone == 0: indices = zone_indices else: indices = np.unique(np.concatenate((zone_indices, indices))) mask = np.ones(max_pred_ravel.shape, dtype=bool) mask[indices] = False max_pred_ravel[mask] = 0 max_pred_ravel = np.where(max_pred_ravel > 0, np.ones_like(max_pred_ravel) * 2, np.zeros_like(max_pred_ravel)) flag = np.reshape(max_pred_ravel, (actual_batch_size, 32, 168, 168)) # flag = np.reshape(max_pred_ravel, (IMGS_PER_ENS_BATCH, 32, 168, 168)) del max_pred_ravel, indices save_array(self.supervised_flag_path, flag, start, end) sup_count = sup_count + np.count_nonzero(flag) del flag if 'cur_pred' in locals(): del cur_pred
def on_epoch_end(self, epoch, logs={}): # print(time() - self.starttime) # model_temp = model bg_save, self.val_bg_dice_coef = self.shall_save( logs['val_bg_dice_coef'], self.val_bg_dice_coef) z1_save, self.val_z1_dice_coef = self.shall_save( logs['val_z1_dice_coef'], self.val_z1_dice_coef) z2_save, self.val_z2_dice_coef = self.shall_save( logs['val_z2_dice_coef'], self.val_z2_dice_coef) if epoch > 0: patients_per_batch = IMGS_PER_ENS_BATCH num_batches = num_un_labeled_train // patients_per_batch remainder = num_un_labeled_train % patients_per_batch remainder_pixels = remainder * DIM[0] * DIM[1] * DIM[2] confident_pixels_no_per_batch = (PERCENTAGE_OF_PIXELS * patients_per_batch * DIM[0] * DIM[1] * DIM[2]) // 100 if remainder_pixels < confident_pixels_no_per_batch: patients_per_last_batch = patients_per_batch + remainder else: patients_per_last_batch = remainder num_batches = num_batches + 1 for b_no in np.arange(num_batches): actual_batch_size = patients_per_batch if ( b_no < num_batches - 1) else patients_per_last_batch confident_pixels_no = (PERCENTAGE_OF_PIXELS * DIM[0] * DIM[1] * DIM[2] * actual_batch_size) // 100 start = (b_no * patients_per_batch) + num_labeled_train end = (start + actual_batch_size) imgs = get_array(self.data_path + '/imgs/', start, end) ensemble_prediction = get_array(self.ensemble_path + '/ens_gt/', start, end, dtype='float32') supervised_flag = get_array(self.ensemble_path + '/flag/', start, end, dtype='float16') inp = [imgs, ensemble_prediction, supervised_flag] del imgs, supervised_flag cur_pred = np.zeros( (actual_batch_size, DIM[0], DIM[1], DIM[2], 3)) model_out = model.predict(inp, batch_size=batch_size, verbose=1) # 1 del inp cur_pred[:, :, :, :, 0] = model_out[ 0] if bg_save else ensemble_prediction[:, :, :, :, 0] cur_pred[:, :, :, :, 1] = model_out[ 1] if z1_save else ensemble_prediction[:, :, :, :, 1] cur_pred[:, :, :, :, 2] = model_out[ 2] if z2_save else ensemble_prediction[:, :, :, :, 2] del model_out # Z = αZ + (1 - α)z ensemble_prediction = alpha * ensemble_prediction + ( 1 - alpha) * cur_pred save_array(os.path.join(self.ensemble_path, 'ens_gt'), ensemble_prediction, start, end) del ensemble_prediction argmax_pred_ravel = np.ravel(np.argmax(cur_pred, axis=-1)) max_pred_ravel = np.ravel(np.max(cur_pred, axis=-1)) indices = None del cur_pred for zone in np.arange(3): final_max_ravel = np.where( argmax_pred_ravel == zone, np.zeros_like(max_pred_ravel), max_pred_ravel) zone_indices = np.argpartition( final_max_ravel, -confident_pixels_no)[-confident_pixels_no:] if zone == 0: indices = zone_indices else: indices = np.unique( np.concatenate((zone_indices, indices))) mask = np.ones(max_pred_ravel.shape, dtype=bool) mask[indices] = False max_pred_ravel[mask] = 0 max_pred_ravel = np.where(max_pred_ravel > 0, np.ones_like(max_pred_ravel) * 2, np.zeros_like(max_pred_ravel)) flag = np.reshape( max_pred_ravel, (actual_batch_size, DIM[0], DIM[1], DIM[2])) # flag = np.reshape(max_pred_ravel, (IMGS_PER_ENS_BATCH, 32, 168, 168)) del max_pred_ravel, indices save_array(os.path.join(self.ensemble_path, 'flag'), flag, start, end) ##sup_count = sup_count + np.count_nonzero(flag) del flag if 'cur_pred' in locals(): del cur_pred
def on_epoch_end(self, epoch, logs={}): model_MC.set_weights(model.get_weights()) # print(time() - self.starttime) # model_temp = model bg_save, self.val_bg_dice_coef = self.shall_save(logs['val_bg_dice_coef'], self.val_bg_dice_coef) z1_save, self.val_z1_dice_coef = self.shall_save(logs['val_z1_dice_coef'], self.val_z1_dice_coef) z2_save, self.val_z2_dice_coef = self.shall_save(logs['val_z2_dice_coef'], self.val_z2_dice_coef) if epoch > 0: patients_per_batch = IMGS_PER_ENS_BATCH num_batches = num_un_labeled_train // patients_per_batch remainder = num_un_labeled_train % patients_per_batch remainder_pixels = remainder * DIM[0] * DIM[1] * DIM[2] confident_pixels_no_per_batch = (PERCENTAGE_OF_PIXELS * patients_per_batch * DIM[0] * DIM[1] * DIM[ 2]) // 100 if remainder_pixels < confident_pixels_no_per_batch: patients_per_last_batch = patients_per_batch + remainder else: patients_per_last_batch = remainder num_batches = num_batches + 1 for b_no in np.arange(num_batches): actual_batch_size = patients_per_batch if ( b_no < num_batches - 1) else patients_per_last_batch confident_pixels_no = (PERCENTAGE_OF_PIXELS * DIM[0] * DIM[1] * DIM[2] * actual_batch_size) // 100 start = (b_no * patients_per_batch) + num_labeled_train end = (start + actual_batch_size) imgs = get_array(self.data_path + '/imgs/', start, end) ensemble_prediction = get_array(self.ensemble_path + '/ens_gt/', start, end, dtype='float32') supervised_flag = get_array(self.ensemble_path + '/flag/', start, end, dtype='float16') inp = [imgs, ensemble_prediction, supervised_flag] del imgs, supervised_flag cur_pred = np.zeros((actual_batch_size, DIM[0], DIM[1], DIM[2], 3)) model_out = model.predict(inp, batch_size=batch_size, verbose=1) # 1 cur_pred[:, :, :, :, 0] = model_out[0] if bg_save else ensemble_prediction[:, :, :, :, 0] cur_pred[:, :, :, :, 1] = model_out[1] if z1_save else ensemble_prediction[:, :, :, :, 1] cur_pred[:, :, :, :, 2] = model_out[2] if z2_save else ensemble_prediction[:, :, :, :, 2] del model_out # Z = αZ + (1 - α)z ensemble_prediction = alpha * ensemble_prediction + (1 - alpha) * cur_pred save_array(os.path.join(self.ensemble_path, 'ens_gt'), ensemble_prediction, start, end) del ensemble_prediction # mc dropout chnages mc_pred = np.zeros((actual_batch_size, DIM[0], DIM[1], DIM[2], 3)) T = 20 for i in np.arange(T): model_out = model_MC.predict(inp, batch_size=batch_size, verbose=1) mc_pred[:, :, :, :, 0] = np.add(model_out[0], mc_pred[:, :, :, :, 0]) mc_pred[:, :, :, :, 1] = np.add(model_out[1], mc_pred[:, :, :, :, 1]) mc_pred[:, :, :, :, 2] = np.add(model_out[2], mc_pred[:, :, :, :, 2]) # avg_pred = mc_pred / T# entropy = None for z in np.arange(3): if z == 0: entropy = (mc_pred[:, :, :, :, z] / T) * np.log((mc_pred[:, :, :, :, z] / T) + 1e-5) else: entropy = entropy + (mc_pred[:, :, :, :, z] / T) * np.log( (mc_pred[:, :, :, :, z] / T) + 1e-5) entropy = -entropy del mc_pred, inp, model_out argmax_pred_ravel = np.ravel(np.argmin(cur_pred, axis=-1)) max_pred_ravel = np.ravel(np.max(cur_pred, axis=-1)) indices = None del cur_pred for zone in np.arange(3): entropy_zone = np.ravel(entropy[:, :, :, :]) final_max_ravel = np.where(argmax_pred_ravel == zone, np.zeros_like(entropy_zone), entropy_zone) zone_indices = np.argpartition(final_max_ravel, -confident_pixels_no)[ -confident_pixels_no:] if zone == 0: indices = zone_indices else: indices = np.unique(np.concatenate((zone_indices, indices))) mask = np.ones(entropy_zone.shape, dtype=bool) mask[indices] = False entropy_zone[mask] = 0 entropy_zone = np.where(entropy_zone > 0, np.ones_like(entropy_zone) * 2, np.zeros_like(entropy_zone)) flag = np.reshape(max_pred_ravel, (actual_batch_size, DIM[0], DIM[1], DIM[2])) del entropy_zone, indices save_array(self.ensemble_path + '/flag/', flag, start, end) del flag if 'cur_pred' in locals(): del cur_pred
def on_epoch_end(self, epoch, logs={}): sup_count = self.count pz_save, self.val_pz_dice_coef = self.shall_save( logs['val_pz_dice_coef'], self.val_pz_dice_coef) cz_save, self.val_cz_dice_coef = self.shall_save( logs['val_cz_dice_coef'], self.val_cz_dice_coef) us_save, self.val_us_dice_coef = self.shall_save( logs['val_us_dice_coef'], self.val_us_dice_coef) afs_save, self.val_afs_dice_coef = self.shall_save( logs['val_afs_dice_coef'], self.val_afs_dice_coef) bg_save, self.val_bg_dice_coef = self.shall_save( logs['val_bg_dice_coef'], self.val_bg_dice_coef) if epoch > 0: patients_per_batch = IMGS_PER_ENS_BATCH num_batches = num_un_labeled_train // patients_per_batch remainder = num_un_labeled_train % patients_per_batch num_batches = num_batches if remainder is 0 else num_batches + 1 update_flag = True if logs[ 'val_loss'] < self.val_loss else False for b_no in np.arange(num_batches): actual_batch_size = patients_per_batch if ( b_no <= num_batches - 1 and remainder == 0) else remainder start = (b_no * patients_per_batch) + num_labeled_train end = (start + actual_batch_size) imgs = get_array(self.imgs_path, start, end) ensemble_prediction = get_array(self.ensemble_path, start, end, dtype='float32') supervised_flag = get_array(self.supervised_flag_path, start, end, dtype='float16') inp = [imgs, ensemble_prediction, supervised_flag] del imgs, supervised_flag cur_pred = np.zeros( (actual_batch_size, 32, 168, 168, NUM_CLASS)) mc_pred = np.zeros( (actual_batch_size, 32, 168, 168, NUM_CLASS)) mc_pred_sq = np.zeros( (actual_batch_size, 32, 168, 168, NUM_CLASS)) model_out = model.predict(inp, batch_size=2, verbose=1) # 1 # model_out = np.add(model_out, train.predict(inp, batch_size=2, verbose=1)) # 2 # del inp cur_pred[:, :, :, :, 0] = model_out[ 0] if pz_save else ensemble_prediction[:, :, :, :, 0] cur_pred[:, :, :, :, 1] = model_out[ 1] if cz_save else ensemble_prediction[:, :, :, :, 1] cur_pred[:, :, :, :, 2] = model_out[ 2] if us_save else ensemble_prediction[:, :, :, :, 2] cur_pred[:, :, :, :, 3] = model_out[ 3] if afs_save else ensemble_prediction[:, :, :, :, 3] cur_pred[:, :, :, :, 4] = model_out[ 4] if bg_save else ensemble_prediction[:, :, :, :, 4] mc_pred[:, :, :, :, 0] = model_out[5] mc_pred[:, :, :, :, 1] = model_out[6] mc_pred[:, :, :, :, 2] = model_out[7] mc_pred[:, :, :, :, 3] = model_out[8] mc_pred[:, :, :, :, 4] = model_out[9] mc_pred_sq[:, :, :, :, 0] = model_out[5] * model_out[5] mc_pred_sq[:, :, :, :, 1] = model_out[6] * model_out[6] mc_pred_sq[:, :, :, :, 2] = model_out[7] * model_out[7] mc_pred_sq[:, :, :, :, 3] = model_out[8] * model_out[8] mc_pred_sq[:, :, :, :, 4] = model_out[9] * model_out[9] del model_out # Z = αZ + (1 - α)z ensemble_prediction = alpha * ensemble_prediction + ( 1 - alpha) * cur_pred[:, :, :, :, 0:5] # del cur_pred save_array(self.ensemble_path, ensemble_prediction, start, end) del ensemble_prediction # if update_flag: if True: self.val_loss = logs['val_loss'] T = 20 for i in np.arange(T - 1): model_out = model.predict(inp, batch_size=2, verbose=1) mc_pred[:, :, :, :, 0] = np.add(model_out[5], mc_pred[:, :, :, :, 0]) mc_pred[:, :, :, :, 1] = np.add(model_out[6], mc_pred[:, :, :, :, 1]) mc_pred[:, :, :, :, 2] = np.add(model_out[7], mc_pred[:, :, :, :, 2]) mc_pred[:, :, :, :, 3] = np.add(model_out[8], mc_pred[:, :, :, :, 3]) mc_pred[:, :, :, :, 4] = np.add(model_out[9], mc_pred[:, :, :, :, 4]) mc_pred_sq[:, :, :, :, 0] = np.add(mc_pred_sq[:, :, :, :, 0], model_out[5] * model_out[5]) mc_pred_sq[:, :, :, :, 1] = np.add(mc_pred_sq[:, :, :, :, 1], model_out[6] * model_out[6]) mc_pred_sq[:, :, :, :, 2] = np.add(mc_pred_sq[:, :, :, :, 2], model_out[7] * model_out[7]) mc_pred_sq[:, :, :, :, 3] = np.add(mc_pred_sq[:, :, :, :, 3], model_out[8] * model_out[8]) mc_pred_sq[:, :, :, :, 4] = np.add(mc_pred_sq[:, :, :, :, 4], model_out[9] * model_out[9]) var = (mc_pred_sq / T) - ((mc_pred / T) * (mc_pred / T)) del mc_pred, mc_pred_sq, inp, model_out argmax_pred_ravel = np.ravel( np.argmin(cur_pred, axis=-1)) max_pred_ravel = np.ravel(np.max(cur_pred, axis=-1)) indices = None del cur_pred for zone in np.arange(4): var_zone = 1 - np.ravel(var[:, :, :, :, zone]) final_max_ravel = np.where( argmax_pred_ravel == zone, np.zeros_like(var_zone), var_zone) zone_indices = np.argpartition( final_max_ravel, -self.confident_pixels_no )[-self.confident_pixels_no:] if zone == 0: indices = zone_indices else: indices = np.unique( np.concatenate((zone_indices, indices))) mask = np.ones(var_zone.shape, dtype=bool) mask[indices] = False var_zone[mask] = 0 var_zone = np.where(var_zone > 0, np.ones_like(var_zone) * 2, np.zeros_like(var_zone)) flag = np.reshape(var_zone, (IMGS_PER_ENS_BATCH, 32, 168, 168)) del var_zone, indices save_array(self.supervised_flag_path, flag, start, end) sup_count = sup_count + np.count_nonzero(flag) del flag if 'cur_pred' in locals(): del cur_pred