Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Crispy13 committed Nov 15, 2020
1 parent 7061abe commit 70cb3e1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 21 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion core/ecf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 12 additions & 8 deletions core/eckeras.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,35 @@
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:
layer_type = l.__class__.__name__
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

Expand Down
80 changes: 68 additions & 12 deletions core/emri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 70cb3e1

Please sign in to comment.