2022-09-14 07:40:25 +00:00
|
|
|
import logging
|
2022-10-11 02:50:11 +00:00
|
|
|
import logging.config
|
2022-09-17 05:21:20 +00:00
|
|
|
import re
|
2022-11-11 18:16:59 +00:00
|
|
|
import time
|
2022-10-11 02:50:11 +00:00
|
|
|
import warnings
|
2022-09-14 07:40:25 +00:00
|
|
|
|
|
|
|
_CURRENT_LOGGING_CONTEXT = None
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-09-17 05:21:20 +00:00
|
|
|
def log_conditioning(conditioning, description):
|
|
|
|
if _CURRENT_LOGGING_CONTEXT is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
_CURRENT_LOGGING_CONTEXT.log_conditioning(conditioning, description)
|
|
|
|
|
|
|
|
|
2022-09-14 07:40:25 +00:00
|
|
|
def log_latent(latents, description):
|
|
|
|
if _CURRENT_LOGGING_CONTEXT is None:
|
|
|
|
return
|
2022-09-21 04:15:19 +00:00
|
|
|
|
2022-11-11 18:16:59 +00:00
|
|
|
if latents is None:
|
|
|
|
return
|
|
|
|
|
2022-09-14 07:40:25 +00:00
|
|
|
_CURRENT_LOGGING_CONTEXT.log_latents(latents, description)
|
|
|
|
|
|
|
|
|
2022-09-18 13:07:07 +00:00
|
|
|
def log_img(img, description):
|
|
|
|
if _CURRENT_LOGGING_CONTEXT is None:
|
|
|
|
return
|
|
|
|
_CURRENT_LOGGING_CONTEXT.log_img(img, description)
|
|
|
|
|
|
|
|
|
2022-11-14 06:51:23 +00:00
|
|
|
def log_progress_latent(latent):
|
|
|
|
if _CURRENT_LOGGING_CONTEXT is None:
|
|
|
|
return
|
|
|
|
_CURRENT_LOGGING_CONTEXT.log_progress_latent(latent)
|
|
|
|
|
|
|
|
|
2022-10-14 03:49:48 +00:00
|
|
|
def log_tensor(t, description=""):
|
|
|
|
if _CURRENT_LOGGING_CONTEXT is None:
|
|
|
|
return
|
|
|
|
_CURRENT_LOGGING_CONTEXT.log_img(t, description)
|
|
|
|
|
|
|
|
|
2022-11-13 03:24:03 +00:00
|
|
|
def increment_step():
|
|
|
|
if _CURRENT_LOGGING_CONTEXT is None:
|
|
|
|
return
|
|
|
|
_CURRENT_LOGGING_CONTEXT.step_count += 1
|
|
|
|
|
|
|
|
|
2022-11-11 18:16:59 +00:00
|
|
|
class TimingContext:
|
|
|
|
def __init__(self, logging_context, description):
|
|
|
|
self.logging_context = logging_context
|
|
|
|
self.description = description
|
|
|
|
self.start_time = None
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.start_time = time.time()
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
self.logging_context.timings[self.description] = time.time() - self.start_time
|
|
|
|
|
|
|
|
|
2022-09-17 05:21:20 +00:00
|
|
|
class ImageLoggingContext:
|
2022-11-14 06:51:23 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
prompt,
|
|
|
|
model,
|
|
|
|
debug_img_callback=None,
|
|
|
|
img_outdir=None,
|
|
|
|
progress_img_callback=None,
|
|
|
|
progress_img_interval_steps=3,
|
|
|
|
progress_img_interval_min_s=0.1,
|
2023-01-28 01:18:42 +00:00
|
|
|
progress_latent_callback=None,
|
2022-11-14 06:51:23 +00:00
|
|
|
):
|
2022-09-14 07:40:25 +00:00
|
|
|
self.prompt = prompt
|
|
|
|
self.model = model
|
|
|
|
self.step_count = 0
|
2022-11-13 03:24:03 +00:00
|
|
|
self.image_count = 0
|
2022-11-14 06:51:23 +00:00
|
|
|
self.debug_img_callback = debug_img_callback
|
2022-09-17 05:21:20 +00:00
|
|
|
self.img_outdir = img_outdir
|
2022-11-14 06:51:23 +00:00
|
|
|
self.progress_img_callback = progress_img_callback
|
|
|
|
self.progress_img_interval_steps = progress_img_interval_steps
|
|
|
|
self.progress_img_interval_min_s = progress_img_interval_min_s
|
2023-01-28 01:18:42 +00:00
|
|
|
self.progress_latent_callback = progress_latent_callback
|
2022-11-14 06:51:23 +00:00
|
|
|
|
2022-11-11 18:16:59 +00:00
|
|
|
self.start_ts = time.perf_counter()
|
|
|
|
self.timings = {}
|
2022-11-14 06:51:23 +00:00
|
|
|
self.last_progress_img_ts = 0
|
|
|
|
self.last_progress_img_step = -1000
|
2022-09-14 07:40:25 +00:00
|
|
|
|
2023-02-12 02:23:45 +00:00
|
|
|
self._prev_log_context = None
|
|
|
|
|
2022-09-14 07:40:25 +00:00
|
|
|
def __enter__(self):
|
2023-09-29 08:13:50 +00:00
|
|
|
global _CURRENT_LOGGING_CONTEXT
|
2023-02-12 02:23:45 +00:00
|
|
|
self._prev_log_context = _CURRENT_LOGGING_CONTEXT
|
2022-09-14 07:40:25 +00:00
|
|
|
_CURRENT_LOGGING_CONTEXT = self
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
2023-09-29 08:13:50 +00:00
|
|
|
global _CURRENT_LOGGING_CONTEXT
|
2023-02-12 02:23:45 +00:00
|
|
|
_CURRENT_LOGGING_CONTEXT = self._prev_log_context
|
2022-09-14 07:40:25 +00:00
|
|
|
|
2022-11-11 18:16:59 +00:00
|
|
|
def timing(self, description):
|
|
|
|
return TimingContext(self, description)
|
|
|
|
|
|
|
|
def get_timings(self):
|
|
|
|
self.timings["total"] = time.perf_counter() - self.start_ts
|
|
|
|
return self.timings
|
|
|
|
|
2022-09-17 05:21:20 +00:00
|
|
|
def log_conditioning(self, conditioning, description):
|
2022-11-14 06:51:23 +00:00
|
|
|
if not self.debug_img_callback:
|
2022-09-17 05:21:20 +00:00
|
|
|
return
|
|
|
|
img = conditioning_to_img(conditioning)
|
|
|
|
|
2022-11-14 06:51:23 +00:00
|
|
|
self.debug_img_callback(
|
2022-11-13 03:24:03 +00:00
|
|
|
img, description, self.image_count, self.step_count, self.prompt
|
|
|
|
)
|
2022-09-17 05:21:20 +00:00
|
|
|
|
2022-09-15 02:40:50 +00:00
|
|
|
def log_latents(self, latents, description):
|
2023-09-29 08:13:50 +00:00
|
|
|
from imaginairy.img_utils import model_latents_to_pillow_imgs
|
2022-10-11 02:50:11 +00:00
|
|
|
|
2022-11-14 06:51:23 +00:00
|
|
|
if "predicted_latent" in description:
|
2023-01-28 01:18:42 +00:00
|
|
|
if self.progress_latent_callback is not None:
|
|
|
|
self.progress_latent_callback(latents)
|
2022-11-14 06:51:23 +00:00
|
|
|
if (
|
|
|
|
self.step_count - self.last_progress_img_step
|
2023-09-29 08:13:50 +00:00
|
|
|
) > self.progress_img_interval_steps and (
|
|
|
|
time.perf_counter() - self.last_progress_img_ts
|
|
|
|
> self.progress_img_interval_min_s
|
|
|
|
):
|
|
|
|
self.log_progress_latent(latents)
|
|
|
|
self.last_progress_img_step = self.step_count
|
|
|
|
self.last_progress_img_ts = time.perf_counter()
|
2022-11-14 06:51:23 +00:00
|
|
|
|
|
|
|
if not self.debug_img_callback:
|
2022-09-14 07:40:25 +00:00
|
|
|
return
|
2022-09-15 02:40:50 +00:00
|
|
|
if latents.shape[1] != 4:
|
2022-09-14 07:40:25 +00:00
|
|
|
# logger.info(f"Didn't save tensor of shape {samples.shape} for {description}")
|
|
|
|
return
|
2022-11-13 03:24:03 +00:00
|
|
|
|
2022-10-14 03:49:48 +00:00
|
|
|
try:
|
|
|
|
shape_str = ",".join(tuple(latents.shape))
|
|
|
|
except TypeError:
|
|
|
|
shape_str = str(latents.shape)
|
|
|
|
description = f"{description}-{shape_str}"
|
2022-09-24 05:58:48 +00:00
|
|
|
for img in model_latents_to_pillow_imgs(latents):
|
2022-11-13 03:24:03 +00:00
|
|
|
self.image_count += 1
|
2022-11-14 06:51:23 +00:00
|
|
|
self.debug_img_callback(
|
2022-11-13 03:24:03 +00:00
|
|
|
img, description, self.image_count, self.step_count, self.prompt
|
|
|
|
)
|
2022-09-17 05:21:20 +00:00
|
|
|
|
2022-09-18 13:07:07 +00:00
|
|
|
def log_img(self, img, description):
|
2022-11-14 06:51:23 +00:00
|
|
|
if not self.debug_img_callback:
|
2022-09-18 13:07:07 +00:00
|
|
|
return
|
2023-02-03 05:43:04 +00:00
|
|
|
import torch
|
|
|
|
from torchvision.transforms import ToPILImage
|
|
|
|
|
2022-11-13 03:24:03 +00:00
|
|
|
self.image_count += 1
|
2022-09-18 13:07:07 +00:00
|
|
|
if isinstance(img, torch.Tensor):
|
|
|
|
img = ToPILImage()(img.squeeze().cpu().detach())
|
|
|
|
img = img.copy()
|
2022-11-14 06:51:23 +00:00
|
|
|
self.debug_img_callback(
|
2022-11-13 03:24:03 +00:00
|
|
|
img, description, self.image_count, self.step_count, self.prompt
|
|
|
|
)
|
2022-09-18 13:07:07 +00:00
|
|
|
|
2022-11-14 06:51:23 +00:00
|
|
|
def log_progress_latent(self, latent):
|
2023-09-29 08:13:50 +00:00
|
|
|
from imaginairy.img_utils import model_latents_to_pillow_imgs
|
2022-11-14 06:51:23 +00:00
|
|
|
|
|
|
|
if not self.progress_img_callback:
|
|
|
|
return
|
|
|
|
for img in model_latents_to_pillow_imgs(latent):
|
|
|
|
self.progress_img_callback(img)
|
|
|
|
|
2022-10-14 03:49:48 +00:00
|
|
|
def log_tensor(self, t, description=""):
|
2022-11-14 06:51:23 +00:00
|
|
|
if not self.debug_img_callback:
|
2022-10-14 03:49:48 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if len(t.shape) == 2:
|
|
|
|
self.log_img(t, description)
|
|
|
|
|
|
|
|
def log_indexed_graph_of_tensor(self):
|
|
|
|
pass
|
|
|
|
|
2022-09-17 05:21:20 +00:00
|
|
|
# def img_callback(self, img, description, step_count, prompt):
|
|
|
|
# steps_path = os.path.join(self.img_outdir, "steps", f"{self.file_num:08}_S{prompt.seed}")
|
|
|
|
# os.makedirs(steps_path, exist_ok=True)
|
|
|
|
# filename = f"{self.file_num:08}_S{prompt.seed}_step{step_count:04}_{filesafe_text(description)[:40]}.jpg"
|
|
|
|
# destination = os.path.join(steps_path, filename)
|
|
|
|
# draw = ImageDraw.Draw(img)
|
|
|
|
# draw.text((10, 10), str(description))
|
|
|
|
# img.save(destination)
|
|
|
|
|
|
|
|
|
|
|
|
def filesafe_text(t):
|
|
|
|
return re.sub(r"[^a-zA-Z0-9.,\[\]() -]+", "_", t)[:130]
|
|
|
|
|
|
|
|
|
|
|
|
def conditioning_to_img(conditioning):
|
2023-02-03 05:43:04 +00:00
|
|
|
from torchvision.transforms import ToPILImage
|
|
|
|
|
2022-09-17 05:21:20 +00:00
|
|
|
return ToPILImage()(conditioning)
|
2022-10-11 02:50:11 +00:00
|
|
|
|
|
|
|
|
2023-11-24 16:27:36 +00:00
|
|
|
class ColorIndentingFormatter(logging.Formatter):
|
|
|
|
RED = "\033[31m"
|
|
|
|
GREEN = "\033[32m"
|
|
|
|
RESET = "\033[0m"
|
|
|
|
|
2022-10-11 03:13:32 +00:00
|
|
|
def format(self, record):
|
|
|
|
s = super().format(record)
|
2023-11-24 16:27:36 +00:00
|
|
|
color = ""
|
|
|
|
reset = ""
|
|
|
|
if record.levelno >= logging.ERROR:
|
|
|
|
color = self.RED
|
|
|
|
|
2022-10-11 03:13:32 +00:00
|
|
|
if _CURRENT_LOGGING_CONTEXT is not None:
|
|
|
|
s = f" {s}"
|
2023-11-24 16:27:36 +00:00
|
|
|
|
|
|
|
if not s.startswith(" "):
|
|
|
|
color = self.GREEN
|
|
|
|
|
|
|
|
if color:
|
|
|
|
reset = self.RESET
|
|
|
|
s = f"{color}{s}{reset}"
|
2022-10-11 03:13:32 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
|
2022-10-11 02:50:11 +00:00
|
|
|
def configure_logging(level="INFO"):
|
|
|
|
fmt = "%(message)s"
|
|
|
|
if level == "DEBUG":
|
|
|
|
fmt = "%(asctime)s [%(levelname)s] %(name)s:%(lineno)d: %(message)s"
|
|
|
|
|
|
|
|
LOGGING_CONFIG = {
|
|
|
|
"version": 1,
|
|
|
|
"disable_existing_loggers": True,
|
|
|
|
"formatters": {
|
2022-10-11 03:13:32 +00:00
|
|
|
"standard": {
|
|
|
|
"format": fmt,
|
2023-11-24 16:27:36 +00:00
|
|
|
"class": "imaginairy.log_utils.ColorIndentingFormatter",
|
2022-10-11 03:13:32 +00:00
|
|
|
},
|
2022-10-11 02:50:11 +00:00
|
|
|
},
|
|
|
|
"handlers": {
|
|
|
|
"default": {
|
2023-05-29 20:27:06 +00:00
|
|
|
"level": level,
|
2022-10-11 02:50:11 +00:00
|
|
|
"formatter": "standard",
|
|
|
|
"class": "logging.StreamHandler",
|
|
|
|
"stream": "ext://sys.stdout", # Default is stderr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"loggers": {
|
|
|
|
"": { # root logger
|
|
|
|
"handlers": ["default"],
|
|
|
|
"level": "WARNING",
|
|
|
|
"propagate": False,
|
|
|
|
},
|
|
|
|
"imaginairy": {"handlers": ["default"], "level": level, "propagate": False},
|
|
|
|
"transformers.modeling_utils": {
|
|
|
|
"handlers": ["default"],
|
|
|
|
"level": "ERROR",
|
|
|
|
"propagate": False,
|
|
|
|
},
|
2023-05-30 05:57:21 +00:00
|
|
|
# disable https://github.com/huggingface/transformers/blob/17a55534f5e5df10ac4804d4270bf6b8cc24998d/src/transformers/models/clip/configuration_clip.py#L330
|
|
|
|
"transformers.models.clip.configuration_clip": {
|
|
|
|
"handlers": ["default"],
|
|
|
|
"level": "ERROR",
|
|
|
|
"propagate": False,
|
|
|
|
},
|
2023-05-08 03:37:15 +00:00
|
|
|
# disable the stupid triton is not available messages
|
|
|
|
# https://github.com/facebookresearch/xformers/blob/6425fd0cacb1a6579aa2f0c4a570b737cb10e9c3/xformers/__init__.py#L52
|
|
|
|
"xformers": {"handlers": ["default"], "level": "ERROR", "propagate": False},
|
2022-10-11 02:50:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
suppress_annoying_logs_and_warnings()
|
|
|
|
logging.config.dictConfig(LOGGING_CONFIG)
|
|
|
|
|
|
|
|
|
|
|
|
def disable_transformers_custom_logging():
|
2023-02-03 05:43:04 +00:00
|
|
|
from transformers.modeling_utils import logger as modeling_logger
|
|
|
|
from transformers.utils.logging import _configure_library_root_logger
|
|
|
|
|
2022-10-11 02:50:11 +00:00
|
|
|
_configure_library_root_logger()
|
|
|
|
_logger = modeling_logger.parent
|
|
|
|
_logger.handlers = []
|
|
|
|
_logger.propagate = True
|
|
|
|
_logger.setLevel(logging.NOTSET)
|
2022-10-16 23:42:46 +00:00
|
|
|
modeling_logger.handlers = []
|
|
|
|
modeling_logger.propagate = True
|
|
|
|
modeling_logger.setLevel(logging.ERROR)
|
2022-10-11 02:50:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def disable_pytorch_lighting_custom_logging():
|
2023-02-03 05:43:04 +00:00
|
|
|
from pytorch_lightning import _logger as pytorch_logger
|
|
|
|
|
2023-01-17 06:48:27 +00:00
|
|
|
try:
|
2023-09-29 08:13:50 +00:00
|
|
|
from pytorch_lightning.utilities.seed import log
|
2023-01-17 06:48:27 +00:00
|
|
|
|
|
|
|
log.setLevel(logging.NOTSET)
|
|
|
|
log.handlers = []
|
|
|
|
log.propagate = False
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2022-10-11 02:50:11 +00:00
|
|
|
pytorch_logger.setLevel(logging.NOTSET)
|
|
|
|
|
|
|
|
|
|
|
|
def disable_common_warnings():
|
|
|
|
warnings.filterwarnings(
|
|
|
|
"ignore",
|
|
|
|
category=UserWarning,
|
|
|
|
message=r"The operator .*?is not currently supported.*",
|
|
|
|
)
|
|
|
|
warnings.filterwarnings(
|
|
|
|
"ignore", category=UserWarning, message=r"The parameter 'pretrained' is.*"
|
|
|
|
)
|
|
|
|
warnings.filterwarnings(
|
|
|
|
"ignore", category=UserWarning, message=r"Arguments other than a weight.*"
|
|
|
|
)
|
|
|
|
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
|
|
|
|
|
|
|
|
|
|
def suppress_annoying_logs_and_warnings():
|
|
|
|
disable_transformers_custom_logging()
|
|
|
|
disable_pytorch_lighting_custom_logging()
|
|
|
|
disable_common_warnings()
|