Skip to content

Commit 70cb3e1

Browse files
committed
updates
1 parent 7061abe commit 70cb3e1

File tree

4 files changed

+91
-21
lines changed

4 files changed

+91
-21
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
*.log
3+
*.pyc
4+
logs/
5+
examples/.ipynb_checkpoints/guided_grad_cam-checkpoint.ipynb
6+
core/.ipynb_checkpoints/ecf-checkpoint.py
7+
core/.ipynb_checkpoints/eckeras-checkpoint.py
8+
core/.ipynb_checkpoints/emri-checkpoint.py
9+
core/.ipynb_checkpoints/guided_grad_cam-checkpoint.py

core/ecf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def get_savepath(save_path=None):
124124

125125
# Search files with file name
126126
gl1 = glob.glob(f"{file_name}*{file_ext}") ; logger.debug(f"gl1: {gl1}")
127-
re1 = list(filter(lambda x:re.search(f"{file_name}(?:_[0-9]+){file_ext}$", x) is not None, gl1)) ; logger.debug(f"re1: {re1}")
127+
fn = file_name.replace(os.sep, "/")
128+
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}")
128129
re2 = re1 + glob.glob(save_path)
129130

130131
if len(re2) == 0:

core/eckeras.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,35 @@
1717
def get_model_memory_usage(batch_size, model):
1818
"""
1919
https://stackoverflow.com/questions/43137288/how-to-determine-needed-memory-of-keras-model
20+
2021
"""
21-
22+
2223
shapes_mem_count = 0
2324
internal_model_mem_count = 0
2425
for l in model.layers:
2526
layer_type = l.__class__.__name__
2627
if layer_type == 'Model':
2728
internal_model_mem_count += get_model_memory_usage(batch_size, l)
2829
single_layer_mem = 1
29-
for s in l.output_shape:
30+
out_shape = l.output_shape
31+
if type(out_shape) is list:
32+
out_shape = out_shape[0]
33+
for s in out_shape:
3034
if s is None:
3135
continue
3236
single_layer_mem *= s
3337
shapes_mem_count += single_layer_mem
3438

35-
trainable_count = np.sum([K.count_params(p) for p in set(model.trainable_weights)])
36-
non_trainable_count = np.sum([K.count_params(p) for p in set(model.non_trainable_weights)])
39+
trainable_count = np.sum([K.count_params(p) for p in model.trainable_weights])
40+
non_trainable_count = np.sum([K.count_params(p) for p in model.non_trainable_weights])
3741

3842
number_size = 4.0
3943
if K.floatx() == 'float16':
40-
number_size = 2.0
44+
number_size = 2.0
4145
if K.floatx() == 'float64':
42-
number_size = 8.0
43-
44-
total_memory = number_size*(batch_size*shapes_mem_count + trainable_count + non_trainable_count)
46+
number_size = 8.0
47+
48+
total_memory = number_size * (batch_size * shapes_mem_count + trainable_count + non_trainable_count)
4549
gbytes = np.round(total_memory / (1024.0 ** 3), 3) + internal_model_mem_count
4650
return gbytes
4751

core/emri.py

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,40 @@ def get_index_by_patient_number_old(plist, df1, seed_n = 42, mode = 'random', co
784784

785785
# Plot prediction result
786786
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):
787+
"""
788+
Plots a comparison figure with arrangement.
789+
790+
Parameters
791+
----------
792+
N : data numbers
793+
C : channels
794+
H : Height
795+
W : Width
796+
D : Depth
797+
798+
data : a numpy array
799+
(N, C, H, W, D) array, image data
800+
801+
labels : a numpy array
802+
(N, C, H, W, D) array, label data
803+
804+
preds : a numpy array
805+
(N, C, H, W, D) array, preds data
806+
807+
chunk_size : the number of data for one png file.
808+
809+
save_path : png save path.
810+
811+
arrangement : a list of strings
812+
e.g.
813+
'd1' means data channel 1
814+
'd1-l2' means overlaying label channel 2 on data channel 1
815+
'd1-p1-ph' means the height is what has the widest prediction area, pred channel 1 overlayed on data channel 1
816+
817+
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.
818+
819+
820+
"""
787821
ca = arange(0, len(data), chunk_size)
788822

789823
for i in range(len(ca)):
@@ -1066,15 +1100,37 @@ def show_mri(img, label, pred = None, logged = None, arrangement = ['d1', 'd1-l1
10661100
height=None, cmap='Greys_r', data_order=['x_test', 'y_test', 'y_pred', 'x_test', 'y_pred'], nums = None,
10671101
thresh=0.5, save_path = None, pad = 5, subplot_adjust_left = 0.33, max_samples = 1000, img_types = None, initial_num = 1, **kwargs):
10681102
"""
1069-
Paramters
1070-
---------
1071-
arrangement : a list.
1072-
e.g. ['d1', 'd2', 'd1-l1', 'd1-ph', 'd1-ph-p1']
1073-
d1 = data - channel 1
1074-
l2 = label - channel 2
1075-
p2 = pred - channel 2
1076-
ph = height based on prediction(not label)
1077-
1103+
1104+
Parameters
1105+
----------
1106+
N : data numbers
1107+
C : channels
1108+
H : Height
1109+
W : Width
1110+
D : Depth
1111+
1112+
data : a numpy array
1113+
(N, C, H, W, D) array, image data
1114+
1115+
labels : a numpy array
1116+
(N, C, H, W, D) array, label data
1117+
1118+
preds : a numpy array
1119+
(N, C, H, W, D) array, preds data
1120+
1121+
chunk_size : the number of data for one png file.
1122+
1123+
save_path : png save path.
1124+
1125+
arrangement : a list of strings
1126+
e.g.
1127+
'd1' means data channel 1
1128+
'd1-l2' means overlaying label channel 2 on data channel 1
1129+
'd1-p1-ph' means the height is what has the widest prediction area, pred channel 1 overlayed on data channel 1
1130+
1131+
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.
1132+
1133+
10781134
"""
10791135

10801136
# Set paramters
@@ -1254,14 +1310,14 @@ def truncate_trainset_size(*args, batch_size):
12541310

12551311

12561312
def make_model(build_model, input_shape, output_channels, n_gpu, test_mode = True, seed_number = 42):
1257-
tf.set_random_seed(seed_number)
1313+
tf.random.set_seed(seed_number)
12581314
np.random.seed(seed_number)
12591315
logger.info(f"{seed_number} was set to seed number, and seed of tensorflow and numpy was set to the number.")
12601316

1261-
model, template_model, opt, lg, lv, dc = build_model(input_shape=input_shape,
1317+
model, opt, lg, lv, dc = build_model(input_shape=input_shape,
12621318
output_channels=output_channels, n_gpu=n_gpu, test_mode=True)
12631319

1264-
return model, template_model, opt, lg, lv, dc
1320+
return model, opt, lg, lv, dc
12651321

12661322

12671323
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,

0 commit comments

Comments
 (0)