diff --git a/FastSurferCNN/run_prediction.py b/FastSurferCNN/run_prediction.py index fc2fcda0..3f3669f0 100644 --- a/FastSurferCNN/run_prediction.py +++ b/FastSurferCNN/run_prediction.py @@ -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.") @@ -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 diff --git a/FastSurferCNN/utils/logging.py b/FastSurferCNN/utils/logging.py index e7ded8f2..f8ab95eb 100644 --- a/FastSurferCNN/utils/logging.py +++ b/FastSurferCNN/utils/logging.py @@ -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 diff --git a/run_fastsurfer.sh b/run_fastsurfer.sh index 7838bcbf..8761bceb 100755 --- a/run_fastsurfer.sh +++ b/run_fastsurfer.sh @@ -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