diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8b1b30 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ + +*.log +*.pyc +logs/ +examples/.ipynb_checkpoints/guided_grad_cam-checkpoint.ipynb +core/.ipynb_checkpoints/ecf-checkpoint.py +core/.ipynb_checkpoints/eckeras-checkpoint.py +core/.ipynb_checkpoints/emri-checkpoint.py +core/.ipynb_checkpoints/guided_grad_cam-checkpoint.py diff --git a/core/ecf.py b/core/ecf.py index 97088d0..a980c47 100644 --- a/core/ecf.py +++ b/core/ecf.py @@ -124,7 +124,8 @@ def get_savepath(save_path=None): # Search files with file name gl1 = glob.glob(f"{file_name}*{file_ext}") ; logger.debug(f"gl1: {gl1}") - re1 = list(filter(lambda x:re.search(f"{file_name}(?:_[0-9]+){file_ext}$", x) is not None, gl1)) ; logger.debug(f"re1: {re1}") + fn = file_name.replace(os.sep, "/") + re1 = list(filter(lambda x:re.search(f"{fn}(?:_[0-9]+){file_ext}$", os.path.normpath(x).replace(os.sep, "/")) is not None, gl1)) ; logger.debug(f"re1: {re1}") re2 = re1 + glob.glob(save_path) if len(re2) == 0: diff --git a/core/eckeras.py b/core/eckeras.py index 7a47982..a75baa8 100644 --- a/core/eckeras.py +++ b/core/eckeras.py @@ -17,8 +17,9 @@ def get_model_memory_usage(batch_size, model): """ https://stackoverflow.com/questions/43137288/how-to-determine-needed-memory-of-keras-model + """ - + shapes_mem_count = 0 internal_model_mem_count = 0 for l in model.layers: @@ -26,22 +27,25 @@ def get_model_memory_usage(batch_size, model): if layer_type == 'Model': internal_model_mem_count += get_model_memory_usage(batch_size, l) single_layer_mem = 1 - for s in l.output_shape: + out_shape = l.output_shape + if type(out_shape) is list: + out_shape = out_shape[0] + for s in out_shape: if s is None: continue single_layer_mem *= s shapes_mem_count += single_layer_mem - trainable_count = np.sum([K.count_params(p) for p in set(model.trainable_weights)]) - non_trainable_count = np.sum([K.count_params(p) for p in set(model.non_trainable_weights)]) + trainable_count = np.sum([K.count_params(p) for p in model.trainable_weights]) + non_trainable_count = np.sum([K.count_params(p) for p in model.non_trainable_weights]) number_size = 4.0 if K.floatx() == 'float16': - number_size = 2.0 + number_size = 2.0 if K.floatx() == 'float64': - number_size = 8.0 - - total_memory = number_size*(batch_size*shapes_mem_count + trainable_count + non_trainable_count) + number_size = 8.0 + + total_memory = number_size * (batch_size * shapes_mem_count + trainable_count + non_trainable_count) gbytes = np.round(total_memory / (1024.0 ** 3), 3) + internal_model_mem_count return gbytes diff --git a/core/emri.py b/core/emri.py index 4ac8ebf..f9981ac 100644 --- a/core/emri.py +++ b/core/emri.py @@ -784,6 +784,40 @@ def get_index_by_patient_number_old(plist, df1, seed_n = 42, mode = 'random', co # Plot prediction result def plot_prediction_result(data, labels, preds, logged = None, chunk_size = 80, save_path = None, arrangement = ['d1', 'd1-l1', 'd1-p1', 'd1-ph', 'd1-l1-ph', 'd1-p1-ph'], **kwargs): + """ + Plots a comparison figure with arrangement. + + Parameters + ---------- + N : data numbers + C : channels + H : Height + W : Width + D : Depth + + data : a numpy array + (N, C, H, W, D) array, image data + + labels : a numpy array + (N, C, H, W, D) array, label data + + preds : a numpy array + (N, C, H, W, D) array, preds data + + chunk_size : the number of data for one png file. + + save_path : png save path. + + arrangement : a list of strings + e.g. + 'd1' means data channel 1 + 'd1-l2' means overlaying label channel 2 on data channel 1 + 'd1-p1-ph' means the height is what has the widest prediction area, pred channel 1 overlayed on data channel 1 + + If you set arrangement to ['d1', 'd1-l1', 'd1-p1', 'd1-ph', 'd1-l1-ph', 'd1-p1-ph'], the figure will be (samples, 6) plot. + + + """ ca = arange(0, len(data), chunk_size) for i in range(len(ca)): @@ -1066,15 +1100,37 @@ def show_mri(img, label, pred = None, logged = None, arrangement = ['d1', 'd1-l1 height=None, cmap='Greys_r', data_order=['x_test', 'y_test', 'y_pred', 'x_test', 'y_pred'], nums = None, thresh=0.5, save_path = None, pad = 5, subplot_adjust_left = 0.33, max_samples = 1000, img_types = None, initial_num = 1, **kwargs): """ - Paramters - --------- - arrangement : a list. - e.g. ['d1', 'd2', 'd1-l1', 'd1-ph', 'd1-ph-p1'] - d1 = data - channel 1 - l2 = label - channel 2 - p2 = pred - channel 2 - ph = height based on prediction(not label) - + + Parameters + ---------- + N : data numbers + C : channels + H : Height + W : Width + D : Depth + + data : a numpy array + (N, C, H, W, D) array, image data + + labels : a numpy array + (N, C, H, W, D) array, label data + + preds : a numpy array + (N, C, H, W, D) array, preds data + + chunk_size : the number of data for one png file. + + save_path : png save path. + + arrangement : a list of strings + e.g. + 'd1' means data channel 1 + 'd1-l2' means overlaying label channel 2 on data channel 1 + 'd1-p1-ph' means the height is what has the widest prediction area, pred channel 1 overlayed on data channel 1 + + If you set arrangement to ['d1', 'd1-l1', 'd1-p1', 'd1-ph', 'd1-l1-ph', 'd1-p1-ph'], the figure will be (samples, 6) plot. + + """ # Set paramters @@ -1254,14 +1310,14 @@ def truncate_trainset_size(*args, batch_size): def make_model(build_model, input_shape, output_channels, n_gpu, test_mode = True, seed_number = 42): - tf.set_random_seed(seed_number) + tf.random.set_seed(seed_number) np.random.seed(seed_number) logger.info(f"{seed_number} was set to seed number, and seed of tensorflow and numpy was set to the number.") - model, template_model, opt, lg, lv, dc = build_model(input_shape=input_shape, + model, opt, lg, lv, dc = build_model(input_shape=input_shape, output_channels=output_channels, n_gpu=n_gpu, test_mode=True) - return model, template_model, opt, lg, lv, dc + return model, opt, lg, lv, dc def set_callbacks(model, save_dir, fold_number = None, min_delta = 0.01, patience = 10, baseline = None, a0 = 1e-5, lr_schedule_total_epoch = 300,