-
Notifications
You must be signed in to change notification settings - Fork 60
add logger module #180
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
add logger module #180
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ system: | |
distribute: False | ||
amp_level: 'O0' | ||
seed: 42 | ||
log_interval: 100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 与默认值相同,可删去。 |
||
val_while_train: True | ||
drop_overflow_update: True | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ system: | |
distribute: True | ||
amp_level: 'O3' | ||
seed: 42 | ||
log_interval: 100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 与默认值相同,可删去。 |
||
val_while_train: True | ||
drop_overflow_update: False | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ system: | |
distribute: True | ||
amp_level: 'O3' | ||
seed: 42 | ||
log_interval: 100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 与默认值相同,可删去。 |
||
val_while_train: True | ||
drop_overflow_update: False | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"""Custom Logger.""" | ||
import os | ||
import sys | ||
import logging | ||
|
||
|
||
class Logger(logging.Logger): | ||
""" | ||
Logger. | ||
|
||
Args: | ||
logger_name: String. Logger name. | ||
rank: Integer. Rank id. | ||
""" | ||
def __init__(self, logger_name, rank=0, is_main_device=False, log_fn=None): | ||
super(Logger, self).__init__(logger_name) | ||
self.rank = rank | ||
self.log_fn = log_fn | ||
if is_main_device: | ||
console = logging.StreamHandler(sys.stdout) | ||
console.setLevel(logging.INFO) | ||
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s') | ||
console.setFormatter(formatter) | ||
self.addHandler(console) | ||
|
||
def setup_logging_file(self, log_dir): | ||
"""Setup logging file.""" | ||
if not os.path.exists(log_dir): | ||
os.makedirs(log_dir, exist_ok=True) | ||
log_name = 'log_%s.txt' % self.rank | ||
self.log_fn = os.path.join(log_dir, log_name) | ||
fh = logging.FileHandler(self.log_fn) | ||
fh.setLevel(logging.INFO) | ||
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s') | ||
fh.setFormatter(formatter) | ||
self.addHandler(fh) | ||
|
||
def info(self, msg, *args, **kwargs): | ||
if self.isEnabledFor(logging.INFO): | ||
self._log(logging.INFO, msg, args, **kwargs) | ||
|
||
def save_args(self, args): | ||
self.info('Args:') | ||
args_dict = vars(args) | ||
for key in args_dict.keys(): | ||
self.info('--> %s: %s', key, args_dict[key]) | ||
self.info('') | ||
|
||
def important_info(self, msg, *args, **kwargs): | ||
if self.isEnabledFor(logging.INFO) and self.rank == 0: | ||
line_width = 2 | ||
important_msg = '\n' | ||
important_msg += ('*'*70 + '\n')*line_width | ||
important_msg += ('*'*line_width + '\n')*2 | ||
important_msg += '*'*line_width + ' '*8 + msg + '\n' | ||
important_msg += ('*'*line_width + '\n')*2 | ||
important_msg += ('*'*70 + '\n')*line_width | ||
self.info(important_msg, *args, **kwargs) | ||
|
||
|
||
def get_logger(log_dir, rank, is_main_device): | ||
"""Get Logger.""" | ||
logger = Logger('mindocr', rank, is_main_device) | ||
logger.setup_logging_file(log_dir) | ||
return logger |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import sys | ||
sys.path.append('.') | ||
|
||
import pytest | ||
import yaml | ||
from addict import Dict | ||
from mindocr.utils.logger import get_logger | ||
|
||
|
||
@pytest.mark.parametrize('task', ['det', 'rec']) | ||
def test_logger(task): | ||
if task == 'det': | ||
config_fp = 'configs/det/dbnet/db_r50_icdar15.yaml' | ||
elif task=='rec': | ||
config_fp = 'configs/rec/crnn/crnn_icdar15.yaml' | ||
|
||
with open(config_fp) as fp: | ||
cfg = yaml.safe_load(fp) | ||
cfg = Dict(cfg) | ||
|
||
logger = get_logger(cfg.train.ckpt_save_dir, rank=0, is_main_device=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
与默认值相同,可删去。