Skip to content

Commit

Permalink
Merge pull request #194 from AhmedFaisal95/fix/inference-logging
Browse files Browse the repository at this point in the history
Fix: Inference script logging
  • Loading branch information
af-a authored Sep 26, 2022
2 parents 745f929 + 57ea71b commit 9b1f350
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
5 changes: 3 additions & 2 deletions FastSurferCNN/run_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ def handle_cuda_memory_exception(exception: RuntimeError, exit_on_out_of_memory:
help="Name under which the conformed input image will be saved, in the same directory as the segmentation "
"(the input image is always conformed first, if it is not already conformed). "
"The original input image is saved in the output directory as $id/mri/orig/001.mgz. Default: mri/orig.mgz.")
parser.add_argument('--log_name', type=str, dest='log_name', default="",
help="Absolute path to file in which run logs will be saved. If not set, logs will not be saved.")
parser.add_argument("--out_dir", type=str, default=None,
help="Directory in which evaluation results should be written. "
"Will be created if it does not exist. Optional if full path is defined for --pred_name.")
Expand Down Expand Up @@ -349,8 +351,7 @@ def handle_cuda_memory_exception(exception: RuntimeError, exit_on_out_of_memory:

# Set up logging
from utils.logging import setup_logging
cfg = args2cfg(args)[0]
setup_logging(cfg.OUT_LOG_DIR, cfg.OUT_LOG_NAME)
setup_logging(args.log_name)

# Download checkpoints if they do not exist
# see utils/checkpoint.py for default paths
Expand Down
23 changes: 12 additions & 11 deletions FastSurferCNN/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,29 @@
# limitations under the License.

# IMPORTS
import os

from logging import *
from typing import Union as _Union
from sys import stdout as _stdout
from pathlib import Path as _Path
from os import makedirs as _makedirs
from os.path import join as _join


def setup_logging(output_dir: _Union[str, _Path], expr_num: str):
def setup_logging(log_file_path: str):
"""
Sets up the logging
"""
# Set up logging format.
_FORMAT = "[%(levelname)s: %(filename)s: %(lineno)4d]: %(message)s"
log_folder = _join(output_dir, "logs")
_makedirs(log_folder, exist_ok=True)
log_file = _join(log_folder, f"expr_{expr_num}.log")
handlers = [StreamHandler(_stdout)]

if log_file_path:
log_dir_path = os.path.dirname(log_file_path)
log_file_name = os.path.basename(log_file_path)
if not os.path.exists(log_dir_path):
os.makedirs(log_dir_path)

fh = FileHandler(filename=log_file, mode='a')
ch = StreamHandler(_stdout)
handlers.append(FileHandler(filename=log_file_path, mode='a'))

basicConfig(level=INFO, format=_FORMAT, handlers=[fh, ch])
basicConfig(level=INFO, format=_FORMAT, handlers=handlers)

# At this point, this is just an alias for compatibility’s sake
get_logger = getLogger
6 changes: 3 additions & 3 deletions run_fastsurfer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,14 @@ if [ "$surf_only" == "0" ]; then
# "============= Running FastSurferCNN (Creating Segmentation aparc.DKTatlas.aseg.mgz) ==============="
# use FastSurferCNN to create cortical parcellation + anatomical segmentation into 95 classes.
mkdir -p "$(dirname "$seg_log")"
echo "Log file for fastsurfercnn eval.py" > $seg_log
echo "Log file for fastsurfercnn run_prediction.py" > $seg_log
date |& tee -a $seg_log
echo "" |& tee -a $seg_log

pushd $fastsurfercnndir
cmd="$python run_prediction.py --orig_name $t1 --pred_name $seg --conf_name $conformed_name $hires --ckpt_sag $weights_sag --ckpt_ax $weights_ax --ckpt_cor $weights_cor --batch_size $batch_size --cfg_cor $config_cor --cfg_ax $config_ax --cfg_sag $config_sag --run_viewagg_on $viewagg --device $device"
cmd="$python run_prediction.py --orig_name $t1 --pred_name $seg --conf_name $conformed_name --log_name $seg_log $hires --ckpt_sag $weights_sag --ckpt_ax $weights_ax --ckpt_cor $weights_cor --batch_size $batch_size --cfg_cor $config_cor --cfg_ax $config_ax --cfg_sag $config_sag --run_viewagg_on $viewagg --device $device"
echo $cmd |& tee -a $seg_log
$cmd |& tee -a $seg_log
$cmd
if [ ${PIPESTATUS[0]} -ne 0 ] ; then exit 1 ; fi
popd
fi
Expand Down

0 comments on commit 9b1f350

Please sign in to comment.