Skip to content

Commit

Permalink
Logging format fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
torzdf committed Sep 25, 2019
1 parent 8ccb1f5 commit 174e695
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/faces_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ def load_aligned(self, image, size=256, dtype=None):
padding)
self.aligned["face"] = face if dtype is None else face.astype(dtype)

logger.trace("Loaded aligned face: %s", {key: val
for key, val in self.aligned.items()
if key != "face"})
logger.trace("Loaded aligned face: %s", {k: str(v) if isinstance(v, np.ndarray) else v
for k, v in self.aligned.items()
if k != "face"})

def _padding_from_coverage(self, size, coverage_ratio):
""" Return the image padding for a face from coverage_ratio set against a
Expand Down
39 changes: 27 additions & 12 deletions lib/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import logging
from logging.handlers import RotatingFileHandler
import os
import re
import sys
import traceback

from datetime import datetime
from tqdm import tqdm

from numpy import ndarray

class FaceswapLogger(logging.Logger):
""" Create custom logger with custom levels """
Expand Down Expand Up @@ -39,20 +39,35 @@ def trace(self, msg, *args, **kwargs):


class FaceswapFormatter(logging.Formatter):
""" Override formatter to strip newlines and multiple spaces from logger
Messages that begin with "R|" should be handled as is
"""
""" Override formatter to strip newlines from logger arguments """
def format(self, record):
if isinstance(record.msg, str):
if record.msg.startswith("R|"):
record.msg = record.msg[2:]
record.strip_spaces = False
elif record.strip_spaces:
record.msg = re.sub(" +",
" ",
record.msg.replace("\n", "\\n").replace("\r", "\\r"))
if isinstance(record.msg, str) and ("\n" in record.msg or "\r" in record.msg):
record.msg = record.msg.replace("\n", "\\n").replace("\r", "\\r")
if any(self.reformat_check(arg) for arg in record.args):
record.args = self.reformat_args(record.args)
return super().format(record)

@staticmethod
def reformat_check(arg):
""" Check if the argument should be reformatted
The argument is a string with a line break
The argument is a numpy array
"""
return ((isinstance(arg, str) and ("\n" in arg or "\r" in arg))
or isinstance(arg, ndarray))

@staticmethod
def reformat_args(args):
""" Reformat args that require new lines removing """
new_args = []
for arg in args:
if isinstance(arg, ndarray):
# Convert numpy arrays to string for reformatting
arg = str(ndarray)
if isinstance(arg, str) and ("\n" in arg or "\r" in arg):
arg = arg.replace("\n", "\\n").replace("\r", "\\r")
new_args.append(arg)
return tuple(new_args)

class RollingBuffer(collections.deque):
"""File-like that keeps a certain number of lines of text in memory."""
Expand Down
7 changes: 4 additions & 3 deletions lib/training_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def _get_landmarks(self, filenames, batch, side):
""" Obtains the 68 Point Landmarks for the images in this batch. This is only called if
config item ``warp_to_landmarks`` is ``True`` or if :attr:`mask_type` is not ``None``. If
the landmarks for an image cannot be found, then an error is raised. """
logger.trace("Retrieving landmarks: (filenames: '%s', side: '%s'", filenames, side)
logger.trace("Retrieving landmarks: (filenames: %s, side: '%s')", filenames, side)
src_points = [self._landmarks[side].get(sha1(face).hexdigest(), None) for face in batch]

# Raise error on missing alignments
Expand All @@ -278,7 +278,7 @@ def _get_landmarks(self, filenames, batch, side):
"alignments".format(missing))
raise FaceswapError(msg)

logger.trace("Returning: (src_points: %s)", src_points)
logger.trace("Returning: (src_points: %s)", [str(src) for src in src_points])
return np.array(src_points)

def _get_closest_match(self, filenames, side, batch_src_points):
Expand Down Expand Up @@ -423,7 +423,8 @@ def initialize(self, training_size):
warp_lm_edge_anchors=edge_anchors,
warp_lm_grids=grids)
self.initialized = True
logger.debug("Initialized constants: %s", self._constants)
logger.debug("Initialized constants: %s", {k:str(v) if isinstance(v, np.ndarray) else v
for k, v in self._constants.items()})

# <<< TARGET IMAGES >>> #
def get_targets(self, batch):
Expand Down
4 changes: 2 additions & 2 deletions plugins/train/model/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,12 @@ def log_summary(self):
return
for side in sorted(list(self.predictors.keys())):
logger.verbose("[%s %s Summary]:", self.name.title(), side.upper())
self.predictors[side].summary(print_fn=lambda x: logger.verbose("R|%s", x))
self.predictors[side].summary(print_fn=lambda x: logger.verbose("%s", x))
for name, nnmeta in self.networks.items():
if nnmeta.side is not None and nnmeta.side != side:
continue
logger.verbose("%s:", name.title())
nnmeta.network.summary(print_fn=lambda x: logger.verbose("R|%s", x))
nnmeta.network.summary(print_fn=lambda x: logger.verbose("%s", x))

def do_snapshot(self):
""" Perform a model snapshot """
Expand Down
12 changes: 6 additions & 6 deletions scripts/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ def monitor(self, thread):
""" Monitor the console, and generate + monitor preview if requested """
is_preview = self.args.preview
logger.debug("Launching Monitor")
logger.info("R|===================================================")
logger.info("R| Starting")
logger.info("===================================================")
logger.info(" Starting")
if is_preview:
logger.info("R| Using live preview")
logger.info("R| Press '%s' to save and quit",
logger.info(" Using live preview")
logger.info(" Press '%s' to save and quit",
"Terminate" if self.args.redirect_gui else "ENTER")
if not self.args.redirect_gui:
logger.info("R| Press 'S' to save model weights immediately")
logger.info("R|===================================================")
logger.info(" Press 'S' to save model weights immediately")
logger.info("===================================================")

keypress = KBHit(is_gui=self.args.redirect_gui)
err = False
Expand Down

0 comments on commit 174e695

Please sign in to comment.