Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Inference script logging #194

Merged
merged 4 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions run_fastsurfer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ if [ "$surf_only" == "0" ]; then
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
af-a marked this conversation as resolved.
Show resolved Hide resolved
$cmd |& tee -a $seg_log
$cmd
if [ ${PIPESTATUS[0]} -ne 0 ] ; then exit 1 ; fi
dkuegler marked this conversation as resolved.
Show resolved Hide resolved
popd
fi
Expand Down