Merged
clean code #669
yyuseAdd on Sep 13, 2025
clean code #669
Merged
yyuseAdd on Sep 13, 2025
Merged fromrefs/pull/669/headintomaster
Total: 3 files changed+27-22
Mexamples/model_security/model_attacks/cv/yolov3_darknet53/src/logger.py+0-11File content is under review, please try again later
@@ -99,8 +99,8 @@ def mnist_inversion_attack(net):
99 LOGGER.info(TAG, 'The average L2 distance between original images and inverted images is: {}'.format(avg_l2_dis))99 LOGGER.info(TAG, 'The average L2 distance between original images and inverted images is: {}'.format(avg_l2_dis))
100 LOGGER.info(TAG, 'The average ssim value between original images and inverted images is: {}'.format(avg_ssim))100 LOGGER.info(TAG, 'The average ssim value between original images and inverted images is: {}'.format(avg_ssim))
101 LOGGER.info(TAG, 'The average prediction confidence on true labels of inverted images is: {}'.format(avg_confi))101 LOGGER.info(TAG, 'The average prediction confidence on true labels of inverted images is: {}'.format(avg_confi))
102- LOGGER.info(TAG, 'True labels of original images are: %s' % true_labels)102+ LOGGER.info(TAG, 'True labels of original images are: %s', true_labels)
103- LOGGER.info(TAG, 'Predicted labels of inverted images are: %s' % pred_labels)103+ LOGGER.info(TAG, 'Predicted labels of inverted images are: %s', pred_labels)
104 104 
105 # plot 10 images105 # plot 10 images
106 plot_num = min(sample_num, 10)106 plot_num = min(sample_num, 10)
@@ -37,7 +37,7 @@ class LogUtil:
37 """37 """
38 _instance = None38 _instance = None
39 _logger = None39 _logger = None
40- _extra_fmt = ' [%s] [%s] '40+ _extra_fmt = ' [{}] [{}] '
41 41 
42 def __init__(self):42 def __init__(self):
43 raise SyntaxError('can not instance, please use get_instance.')43 raise SyntaxError('can not instance, please use get_instance.')
@@ -109,7 +109,7 @@ class LogUtil:
109 109 
110 def debug(self, tag, msg, *args):110 def debug(self, tag, msg, *args):
111 """111 """
112- Log '[tag] msg % args' with severity 'DEBUG'.112+ Log '[tag] msg' with severity 'DEBUG'.
113 113 
114 Args:114 Args:
115 tag (str): Logger tag.115 tag (str): Logger tag.
@@ -118,11 +118,15 @@ class LogUtil:
118 """118 """
119 caller_info = _find_caller()119 caller_info = _find_caller()
120 file_info = ':'.join([caller_info[0], str(caller_info[1])])120 file_info = ':'.join([caller_info[0], str(caller_info[1])])
121- self._logger.debug(self._extra_fmt + msg, file_info, tag, *args)121+ formatted_msg = self._extra_fmt.format(file_info, tag) + str(msg)
122+ if args:
123+ self._logger.debug(formatted_msg, *args)
124+ else:
125+ self._logger.debug(formatted_msg)
122 126 
123 def info(self, tag, msg, *args):127 def info(self, tag, msg, *args):
124 """128 """
125- Log '[tag] msg % args' with severity 'INFO'.129+ Log '[tag] msg' with severity 'INFO'.
126 130 
127 Args:131 Args:
128 tag (str): Logger tag.132 tag (str): Logger tag.
@@ -131,11 +135,15 @@ class LogUtil:
131 """135 """
132 caller_info = _find_caller()136 caller_info = _find_caller()
133 file_info = ':'.join([caller_info[0], str(caller_info[1])])137 file_info = ':'.join([caller_info[0], str(caller_info[1])])
134- self._logger.info(self._extra_fmt + msg, file_info, tag, *args)138+ formatted_msg = self._extra_fmt.format(file_info, tag) + str(msg)
139+ if args:
140+ self._logger.info(formatted_msg, *args)
141+ else:
142+ self._logger.info(formatted_msg)
135 143 
136 def warn(self, tag, msg, *args):144 def warn(self, tag, msg, *args):
137 """145 """
138- Log '[tag] msg % args' with severity 'WARNING'.146+ Log '[tag] msg' with severity 'WARNING'.
139 147 
140 Args:148 Args:
141 tag (str): Logger tag.149 tag (str): Logger tag.
@@ -144,11 +152,15 @@ class LogUtil:
144 """152 """
145 caller_info = _find_caller()153 caller_info = _find_caller()
146 file_info = ':'.join([caller_info[0], str(caller_info[1])])154 file_info = ':'.join([caller_info[0], str(caller_info[1])])
147- self._logger.warning(self._extra_fmt + msg, file_info, tag, *args)155+ formatted_msg = self._extra_fmt.format(file_info, tag) + str(msg)
156+ if args:
157+ self._logger.warning(formatted_msg, *args)
158+ else:
159+ self._logger.warning(formatted_msg)
148 160 
149 def error(self, tag, msg, *args):161 def error(self, tag, msg, *args):
150 """162 """
151- Log '[tag] msg % args' with severity 'ERROR'.163+ Log '[tag] msg' with severity 'ERROR'.
152 164 
153 Args:165 Args:
154 tag (str): Logger tag.166 tag (str): Logger tag.
@@ -157,4 +169,8 @@ class LogUtil:
157 """169 """
158 caller_info = _find_caller()170 caller_info = _find_caller()
159 file_info = ':'.join([caller_info[0], str(caller_info[1])])171 file_info = ':'.join([caller_info[0], str(caller_info[1])])
160- self._logger.error(self._extra_fmt + msg, file_info, tag, *args)172+ formatted_msg = self._extra_fmt.format(file_info, tag) + str(msg)
173+ if args:
174+ self._logger.error(formatted_msg, *args)
175+ else:
176+ self._logger.error(formatted_msg)