From f1ea64dc171a0a35919cfd468a7b701cb54683e5 Mon Sep 17 00:00:00 2001 From: Yoshitomo Matsubara Date: Sat, 25 May 2024 10:55:36 -0700 Subject: [PATCH 1/2] Disable an auto-configuration for def_logger --- examples/hf_transformers/text_classification.py | 3 ++- examples/torchvision/image_classification.py | 3 ++- examples/torchvision/object_detection.py | 3 ++- examples/torchvision/semantic_segmentation.py | 3 ++- torchdistill/common/constant.py | 6 ------ torchdistill/misc/log.py | 9 +++++++++ 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/hf_transformers/text_classification.py b/examples/hf_transformers/text_classification.py index 9344e71e..31b1e696 100644 --- a/examples/hf_transformers/text_classification.py +++ b/examples/hf_transformers/text_classification.py @@ -43,7 +43,7 @@ from torchdistill.core.training import get_training_box from torchdistill.datasets import util from torchdistill.datasets.registry import register_collate_func -from torchdistill.misc.log import setup_log_file, SmoothedValue, MetricLogger +from torchdistill.misc.log import set_basic_log_config, setup_log_file, SmoothedValue, MetricLogger logger = def_logger.getChild(__name__) @@ -206,6 +206,7 @@ def predict_private(model, dataset_dict, label_names_dict, is_regression, accele def main(args): + set_basic_log_config() log_file_path = args.run_log if is_main_process() and log_file_path is not None: setup_log_file(os.path.expanduser(log_file_path)) diff --git a/examples/torchvision/image_classification.py b/examples/torchvision/image_classification.py index 6958ac2f..98239649 100644 --- a/examples/torchvision/image_classification.py +++ b/examples/torchvision/image_classification.py @@ -16,7 +16,7 @@ from torchdistill.core.distillation import get_distillation_box from torchdistill.core.training import get_training_box from torchdistill.datasets.util import build_data_loader -from torchdistill.misc.log import setup_log_file, SmoothedValue, MetricLogger +from torchdistill.misc.log import set_basic_log_config, setup_log_file, SmoothedValue, MetricLogger from torchdistill.models.official import get_image_classification_model from torchdistill.models.registry import get_model @@ -158,6 +158,7 @@ def train(teacher_model, student_model, dataset_dict, src_ckpt_file_path, dst_ck def main(args): + set_basic_log_config() log_file_path = args.run_log if is_main_process() and log_file_path is not None: setup_log_file(os.path.expanduser(log_file_path)) diff --git a/examples/torchvision/object_detection.py b/examples/torchvision/object_detection.py index 4d292e76..9ae7f7de 100644 --- a/examples/torchvision/object_detection.py +++ b/examples/torchvision/object_detection.py @@ -22,7 +22,7 @@ from torchdistill.core.distillation import get_distillation_box from torchdistill.core.training import get_training_box from torchdistill.datasets.util import build_data_loader -from torchdistill.misc.log import setup_log_file, SmoothedValue, MetricLogger +from torchdistill.misc.log import set_basic_log_config, setup_log_file, SmoothedValue, MetricLogger from torchdistill.models.official import get_object_detection_model from torchdistill.models.registry import get_model @@ -198,6 +198,7 @@ def train(teacher_model, student_model, dataset_dict, src_ckpt_file_path, dst_ck def main(args): + set_basic_log_config() log_file_path = args.run_log if is_main_process() and log_file_path is not None: setup_log_file(os.path.expanduser(log_file_path)) diff --git a/examples/torchvision/semantic_segmentation.py b/examples/torchvision/semantic_segmentation.py index 8997719c..879fb794 100644 --- a/examples/torchvision/semantic_segmentation.py +++ b/examples/torchvision/semantic_segmentation.py @@ -17,7 +17,7 @@ from torchdistill.core.distillation import get_distillation_box from torchdistill.core.training import get_training_box from torchdistill.datasets.util import build_data_loader -from torchdistill.misc.log import setup_log_file, SmoothedValue, MetricLogger +from torchdistill.misc.log import set_basic_log_config, setup_log_file, SmoothedValue, MetricLogger from torchdistill.models.official import get_semantic_segmentation_model from torchdistill.models.registry import get_model from utils.eval import SegEvaluator @@ -151,6 +151,7 @@ def train(teacher_model, student_model, dataset_dict, src_ckpt_file_path, dst_ck def main(args): + set_basic_log_config() log_file_path = args.run_log if is_main_process() and log_file_path is not None: setup_log_file(os.path.expanduser(log_file_path)) diff --git a/torchdistill/common/constant.py b/torchdistill/common/constant.py index 9fa50254..3324956d 100644 --- a/torchdistill/common/constant.py +++ b/torchdistill/common/constant.py @@ -2,10 +2,4 @@ SELF_MODULE_PATH = '.' LOGGING_FORMAT = '%(asctime)s\t%(levelname)s\t%(name)s\t%(message)s' - -logging.basicConfig( - format=LOGGING_FORMAT, - datefmt='%Y/%m/%d %H:%M:%S', - level=logging.INFO, -) def_logger = logging.getLogger() diff --git a/torchdistill/misc/log.py b/torchdistill/misc/log.py index 658b15b2..3730410b 100644 --- a/torchdistill/misc/log.py +++ b/torchdistill/misc/log.py @@ -1,4 +1,5 @@ import datetime +import logging import time from collections import defaultdict, deque from logging import FileHandler, Formatter @@ -13,6 +14,14 @@ logger = def_logger.getChild(__name__) +def set_basic_log_config(): + logging.basicConfig( + format=LOGGING_FORMAT, + datefmt='%Y/%m/%d %H:%M:%S', + level=logging.INFO + ) + + def setup_log_file(log_file_path): """ Sets a file handler with ``log_file_path`` to write a log file. From 027d688c9a7ae295b4ed5b2c32f067cc730ee1b1 Mon Sep 17 00:00:00 2001 From: Yoshitomo Matsubara Date: Sat, 25 May 2024 10:57:15 -0700 Subject: [PATCH 2/2] Add a docstring --- torchdistill/misc/log.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/torchdistill/misc/log.py b/torchdistill/misc/log.py index 3730410b..3bfb5ad7 100644 --- a/torchdistill/misc/log.py +++ b/torchdistill/misc/log.py @@ -15,6 +15,9 @@ def set_basic_log_config(): + """ + Sets a default basic configuration for logging. + """ logging.basicConfig( format=LOGGING_FORMAT, datefmt='%Y/%m/%d %H:%M:%S',