Skip to content

Commit

Permalink
Minor fix logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamnitsask committed Jan 20, 2020
1 parent ebec7ba commit 91ca6d9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
12 changes: 11 additions & 1 deletion deepmedic/logging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,21 @@ def getMeanPerColOf2dListExclNA(list2d, notApplicPattern) :
return listWithMeanPerColumn



def datetime_now_str() :
#datetime returns in the format: YYYY-MM-DD HH:MM:SS.millis but ':' is not supported for Windows' naming convention.
dateTimeNowStr = str(datetime.datetime.now())
dateTimeNowStr = dateTimeNowStr.replace(':','.')
dateTimeNowStr = dateTimeNowStr.replace(' ','.')
return dateTimeNowStr

# TODO: Merge the two below
def print_progress_step_tr_val(log, n_batches, n_batches_done, batch_size, prefix_str):
progress_step = max(1, n_batches // 5)
if n_batches_done == 0 or (n_batches_done % progress_step) == 0 or (n_batches_done == n_batches):
log.print3(prefix_str + " Processed " + str(n_batches_done) + "/" + str(n_batches) + " batches for this subepoch...")

def print_progress_step_test(log, n_batches, n_batches_done, batch_size, n_tiles_for_subj):
progress_step = max(1, n_batches // 5)
if n_batches_done == 0 or (n_batches_done % progress_step) == 0 or (n_batches_done == n_batches):
log.print3("Processed " + str(n_batches_done * batch_size) + "/" + str(n_tiles_for_subj) + " segments.")

13 changes: 3 additions & 10 deletions deepmedic/routines/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
from deepmedic.dataManagement.io import savePredImgToNiiWithOriginalHdr, saveFmImgToNiiWithOriginalHdr, \
save4DImgWithAllFmsToNiiWithOriginalHdr
from deepmedic.dataManagement.preprocessing import unpad_3d_img

from deepmedic.neuralnet.pathwayTypes import PathwayTypes as pt
from deepmedic.logging.utils import strListFl4fNA, getMeanPerColOf2dListExclNA
from deepmedic.logging.utils import strListFl4fNA, getMeanPerColOf2dListExclNA, print_progress_step_test



Expand Down Expand Up @@ -202,13 +201,6 @@ def stitch_predicted_to_fms(array_fms_to_save, idx_next_tile_in_fm_vols,
return idx_next_tile_in_fm_vols, array_fms_to_save


def print_progress_step(log, n_batches, batch_i, batch_size, n_tiles_for_subj):
progress_step = max(1, n_batches // 5)

if batch_i == 0 or ((batch_i + 1) % progress_step) == 0 or (batch_i + 1) == n_batches:
log.print3("Processed " + str(batch_i * batch_size) + "/" + str(n_tiles_for_subj) + " segments.")


def prepare_feeds_dict(feeds, channs_of_tiles_per_path):
# TODO: Can we rename the input feeds so that they are easier to deal with?
feeds_dict = {feeds['x']: np.asarray(channs_of_tiles_per_path[0], dtype='float32')}
Expand Down Expand Up @@ -258,9 +250,9 @@ def predict_whole_volume_by_tiling(log, sessionTf, cnn3d,
idx_next_tile_in_fm_vols = 0
n_batches = n_tiles_for_subj // batchsize
t_fwd_pass_subj = 0 # time it took for forward pass over all tiles of subject.
print_progress_step_test(log, n_batches, 0, batchsize, n_tiles_for_subj)
for batch_i in range(n_batches):

print_progress_step(log, n_batches, batch_i, batchsize, n_tiles_for_subj)
# Extract data for the segments of this batch.
# ( I could modularize extractDataOfASegmentFromImagesUsingSampledSliceCoords()
# of training and use it here as well. )
Expand Down Expand Up @@ -307,6 +299,7 @@ def predict_whole_volume_by_tiling(log, sessionTf, cnn3d,
outp_pred_dims,
cnn3d.pathways,
idxs_fms_to_save)
print_progress_step_test(log, n_batches, batch_i + 1, batchsize, n_tiles_for_subj)
# Done with batch

log.print3("TIMING: Segmentation of subject: [Forward Pass:] {0:.2f}".format(t_fwd_pass_subj) + " secs.")
Expand Down
22 changes: 8 additions & 14 deletions deepmedic/routines/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from deepmedic.dataManagement.sampling import get_samples_for_subepoch
from deepmedic.routines.testing import inference_on_whole_volumes

from deepmedic.logging.utils import datetime_now_str
from deepmedic.logging.utils import datetime_now_str, print_progress_step_tr_val


def process_in_batches(log,
Expand All @@ -38,16 +38,12 @@ def process_in_batches(log,
# Each row of array below holds number of:
# Real Positives, Real Neg, True Predicted Pos, True Predicted Neg in subepoch, in this order.
arr_RpRnTpTn_per_class_in_subep = np.zeros([cnn3d.num_classes, 4], dtype="int32")

print_progress_step = max(1, n_batches // 5)

prefix_progress_str = '[TRAINING]' if train_or_val == 'train' else '[VALIDATION]'
print_progress_step_tr_val(log, n_batches, 0, batchsize, prefix_progress_str)
for batch_i in range(n_batches):

if train_or_val == "train":
if batch_i == 0 or ((batch_i + 1) % print_progress_step) == 0 or (batch_i + 1) == n_batches:
log.print3("[TRAINING] Trained on " + str(batch_i) + "/" + str(n_batches) +\
" batches for this subepoch...")

ops_to_fetch = cnn3d.get_main_ops('train')
list_of_ops = [ops_to_fetch['cost']] + ops_to_fetch['list_rp_rn_tp_tn'] +\
[ops_to_fetch['updates_grouped_op']]
Expand All @@ -68,12 +64,8 @@ def process_in_batches(log,

cost_this_batch = results_of_run[0]
list_RpRnPpPn_per_class = results_of_run[1:-1] # [-1] is from updates_grouped_op, returns nothing

else: # validation
if batch_i == 0 or ((batch_i + 1) % print_progress_step) == 0 or (batch_i + 1) == n_batches:
log.print3("[VALIDATION] Validated on " +
str(batch_i) + "/" + str(n_batches) + " batches for this subepoch...")

ops_to_fetch = cnn3d.get_main_ops('val')
list_of_ops = ops_to_fetch['list_rp_rn_tp_tn']

Expand All @@ -91,7 +83,7 @@ def process_in_batches(log,

cost_this_batch = 999 # placeholder in case of validation.
list_RpRnPpPn_per_class = results_of_run

# list_RpRnPpPn_per_class holds Real Pos, Real Neg, True Pred Pos, True Pred Neg ...
# ... for all classes, in this order, flattened. First RpRnTpTn are for 'WHOLE' class.
arr_RpRnTpTn_per_class = np.asarray(list_RpRnPpPn_per_class, dtype="int32")
Expand All @@ -101,6 +93,8 @@ def process_in_batches(log,
costs_of_batches.append(cost_this_batch) # only really used in training.
arr_RpRnTpTn_per_class_in_subep += arr_RpRnTpTn_per_class

print_progress_step_tr_val(log, n_batches, batch_i + 1, batchsize, prefix_progress_str)

# ======== Calculate and Report accuracy over subepoch
# In case of validation, mean_cost_subep is just a placeholder.
# Cause this does not get calculated and reported in this case.
Expand Down

0 comments on commit 91ca6d9

Please sign in to comment.