|
import numpy as np |
|
import os.path as osp |
|
import torch_fidelity |
|
from PIL import Image |
|
from tqdm import tqdm |
|
import pickle as pkl |
|
import os, hashlib, pdb |
|
from pathlib import Path |
|
from torch import Tensor |
|
import torch, torchvision |
|
from einops import rearrange |
|
from omegaconf import OmegaConf |
|
import torch.distributed as dist |
|
from typing import List, Optional |
|
from torchvision import transforms |
|
from io import BytesIO as Bytes2Data |
|
from smart_open import open |
|
from .misc import is_main_process, get_rank |
|
import importlib, datetime, requests, time, shutil |
|
from collections import defaultdict, deque, OrderedDict |
|
from dotwiz import DotWiz |
|
|
|
URL_MAP = { |
|
"vgg_lpips": "https://heibox.uni-heidelberg.de/f/607503859c864bc1b30b/?dl=1" |
|
} |
|
|
|
CKPT_MAP = { |
|
"vgg_lpips": "vgg.pth" |
|
} |
|
|
|
MD5_MAP = { |
|
"vgg_lpips": "d507d7349b931f0638a25a48a722f98a" |
|
} |
|
|
|
def disabled_train(self, mode=True): |
|
"""Overwrite model.train with this function to make sure train/eval mode |
|
does not change anymore.""" |
|
return self |
|
|
|
def customized_collate_fn(batch): |
|
|
|
collate_fn = {} |
|
if len(batch) < 2: |
|
for key, value in batch[0].items(): |
|
collate_fn[key] = [value] |
|
else: |
|
|
|
for i, dd in enumerate(batch): |
|
if i < 1: |
|
for key, value in dd.items(): |
|
collate_fn[key] = [value] |
|
else: |
|
for key, value in dd.items(): |
|
collate_fn[key].append(value) |
|
|
|
return collate_fn |
|
|
|
|
|
def trivial_batch_collator(batch): |
|
""" |
|
A batch collator that does nothing. |
|
""" |
|
return batch |
|
|
|
class NestedTensor(object): |
|
def __init__(self, tensors, mask: Optional[Tensor]): |
|
self.tensors = tensors |
|
self.mask = mask |
|
|
|
def to(self, device): |
|
|
|
cast_tensor = self.tensors.to(device) |
|
mask = self.mask |
|
if mask is not None: |
|
assert mask is not None |
|
cast_mask = mask.to(device) |
|
else: |
|
cast_mask = None |
|
return NestedTensor(cast_tensor, cast_mask) |
|
|
|
def decompose(self): |
|
return self.tensors, self.mask |
|
|
|
def __repr__(self): |
|
return str(self.tensors) |
|
|
|
|
|
def _max_by_axis(the_list): |
|
|
|
maxes = the_list[0] |
|
for sublist in the_list[1:]: |
|
for index, item in enumerate(sublist): |
|
maxes[index] = max(maxes[index], item) |
|
return maxes |
|
|
|
|
|
def nested_tensor_from_tensor_list(tensor_list: List[Tensor]): |
|
|
|
if tensor_list[0].ndim == 3: |
|
if torchvision._is_tracing(): |
|
|
|
|
|
return _onnx_nested_tensor_from_tensor_list(tensor_list) |
|
|
|
|
|
max_size = _max_by_axis([list(img.shape) for img in tensor_list]) |
|
|
|
batch_shape = [len(tensor_list)] + max_size |
|
b, c, h, w = batch_shape |
|
dtype = tensor_list[0].dtype |
|
device = tensor_list[0].device |
|
tensor = torch.zeros(batch_shape, dtype=dtype, device=device) |
|
mask = torch.ones((b, h, w), dtype=torch.bool, device=device) |
|
for img, pad_img, m in zip(tensor_list, tensor, mask): |
|
pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img) |
|
m[: img.shape[1], : img.shape[2]] = False |
|
else: |
|
raise ValueError("not supported") |
|
return NestedTensor(tensor, mask) |
|
|
|
|
|
|
|
|
|
@torch.jit.unused |
|
def _onnx_nested_tensor_from_tensor_list(tensor_list: List[Tensor]) -> NestedTensor: |
|
max_size = [] |
|
for i in range(tensor_list[0].dim()): |
|
max_size_i = torch.max( |
|
torch.stack([img.shape[i] for img in tensor_list]).to(torch.float32) |
|
).to(torch.int64) |
|
max_size.append(max_size_i) |
|
max_size = tuple(max_size) |
|
|
|
|
|
|
|
|
|
|
|
padded_imgs = [] |
|
padded_masks = [] |
|
for img in tensor_list: |
|
padding = [(s1 - s2) for s1, s2 in zip(max_size, tuple(img.shape))] |
|
padded_img = torch.nn.functional.pad(img, (0, padding[2], 0, padding[1], 0, padding[0])) |
|
padded_imgs.append(padded_img) |
|
|
|
m = torch.zeros_like(img[0], dtype=torch.int, device=img.device) |
|
padded_mask = torch.nn.functional.pad(m, (0, padding[2], 0, padding[1]), "constant", 1) |
|
padded_masks.append(padded_mask.to(torch.bool)) |
|
|
|
tensor = torch.stack(padded_imgs) |
|
mask = torch.stack(padded_masks) |
|
|
|
return NestedTensor(tensor, mask=mask) |
|
|
|
|
|
def is_dist_avail_and_initialized(): |
|
if not dist.is_available(): |
|
return False |
|
if not dist.is_initialized(): |
|
return False |
|
return True |
|
|
|
|
|
class SmoothedValue(object): |
|
"""Track a series of values and provide access to smoothed values over a |
|
window or the global series average. |
|
""" |
|
|
|
def __init__(self, window_size=20, fmt=None): |
|
if fmt is None: |
|
fmt = "{median:.4f} ({global_avg:.4f})" |
|
self.deque = deque(maxlen=window_size) |
|
self.total = 0.0 |
|
self.count = 0 |
|
self.fmt = fmt |
|
|
|
def update(self, value, n=1): |
|
self.deque.append(value) |
|
self.count += n |
|
self.total += value * n |
|
|
|
def synchronize_between_processes(self): |
|
""" |
|
Warning: does not synchronize the deque! |
|
""" |
|
if not is_dist_avail_and_initialized(): |
|
return |
|
t = torch.tensor([self.count, self.total], dtype=torch.float64, device='cuda') |
|
dist.barrier() |
|
dist.all_reduce(t) |
|
t = t.tolist() |
|
self.count = int(t[0]) |
|
self.total = t[1] |
|
|
|
@property |
|
def median(self): |
|
d = torch.tensor(list(self.deque)) |
|
return d.median().item() |
|
|
|
@property |
|
def avg(self): |
|
d = torch.tensor(list(self.deque), dtype=torch.float32) |
|
return d.mean().item() |
|
|
|
@property |
|
def global_avg(self): |
|
return self.total / self.count |
|
|
|
@property |
|
def max(self): |
|
return max(self.deque) |
|
|
|
@property |
|
def value(self): |
|
return self.deque[-1] |
|
|
|
def __str__(self): |
|
return self.fmt.format( |
|
median=self.median, |
|
avg=self.avg, |
|
global_avg=self.global_avg, |
|
max=self.max, |
|
value=self.value) |
|
|
|
|
|
class MetricLogger(object): |
|
def __init__(self, delimiter="\t"): |
|
self.meters = defaultdict(SmoothedValue) |
|
self.delimiter = delimiter |
|
|
|
def update(self, **kwargs): |
|
for k, v in kwargs.items(): |
|
if v is None: |
|
continue |
|
if isinstance(v, torch.Tensor): |
|
v = v.item() |
|
assert isinstance(v, (float, int)) |
|
self.meters[k].update(v) |
|
|
|
def __getattr__(self, attr): |
|
if attr in self.meters: |
|
return self.meters[attr] |
|
if attr in self.__dict__: |
|
return self.__dict__[attr] |
|
raise AttributeError("'{}' object has no attribute '{}'".format( |
|
type(self).__name__, attr)) |
|
|
|
def __str__(self): |
|
loss_str = [] |
|
for name, meter in self.meters.items(): |
|
loss_str.append( |
|
"{}: {}".format(name, str(meter)) |
|
) |
|
return self.delimiter.join(loss_str) |
|
|
|
def synchronize_between_processes(self): |
|
for meter in self.meters.values(): |
|
meter.synchronize_between_processes() |
|
|
|
def add_meter(self, name, meter): |
|
self.meters[name] = meter |
|
|
|
def log_every(self, iterable, print_freq, header=None): |
|
i = 0 |
|
if not header: |
|
header = '' |
|
start_time = time.time() |
|
end = time.time() |
|
iter_time = SmoothedValue(fmt='{avg:.4f}') |
|
data_time = SmoothedValue(fmt='{avg:.4f}') |
|
space_fmt = ':' + str(len(str(len(iterable)))) + 'd' |
|
log_msg = [ |
|
header, |
|
'[{0' + space_fmt + '}/{1}]', |
|
'eta: {eta}', |
|
'{meters}', |
|
'time: {time}', |
|
'data: {data}' |
|
] |
|
if torch.cuda.is_available(): |
|
log_msg.append('max mem: {memory:.0f}') |
|
log_msg = self.delimiter.join(log_msg) |
|
MB = 1024.0 * 1024.0 |
|
for obj in iterable: |
|
data_time.update(time.time() - end) |
|
yield obj |
|
iter_time.update(time.time() - end) |
|
if i % print_freq == 0 or i == len(iterable) - 1: |
|
eta_seconds = iter_time.global_avg * (len(iterable) - i) |
|
eta_string = str(datetime.timedelta(seconds=int(eta_seconds))) |
|
if torch.cuda.is_available(): |
|
print(log_msg.format( |
|
i, len(iterable), eta=eta_string, |
|
meters=str(self), |
|
time=str(iter_time), data=str(data_time), |
|
memory=torch.cuda.max_memory_allocated() / MB)) |
|
else: |
|
print(log_msg.format( |
|
i, len(iterable), eta=eta_string, |
|
meters=str(self), |
|
time=str(iter_time), data=str(data_time))) |
|
i += 1 |
|
end = time.time() |
|
total_time = time.time() - start_time |
|
total_time_str = str(datetime.timedelta(seconds=int(total_time))) |
|
print('{} Total time: {} ({:.4f} s / it)'.format( |
|
header, total_time_str, total_time / len(iterable))) |
|
|
|
|
|
def all_reduce_mean(x): |
|
world_size = dist.get_world_size() |
|
if world_size > 1: |
|
x_reduce = torch.tensor(x).cuda() |
|
dist.all_reduce(x_reduce) |
|
x_reduce /= world_size |
|
return x_reduce.item() |
|
else: |
|
return x |
|
|
|
|
|
class NativeScaler: |
|
state_dict_key = "amp_scaler" |
|
|
|
def __init__(self): |
|
self._scaler = torch.cuda.amp.GradScaler() |
|
|
|
def __call__(self, loss, optimizer, clip_grad=3., parameters=None, create_graph=False, update_grad=True): |
|
self._scaler.scale(loss).backward(create_graph=create_graph) |
|
if update_grad: |
|
if clip_grad is not None: |
|
assert parameters is not None |
|
self._scaler.unscale_(optimizer) |
|
norm = torch.nn.utils.clip_grad_norm_(parameters, clip_grad) |
|
else: |
|
self._scaler.unscale_(optimizer) |
|
norm = get_grad_norm_(parameters) |
|
self._scaler.step(optimizer) |
|
self._scaler.update() |
|
else: |
|
norm = None |
|
return norm |
|
|
|
def state_dict(self): |
|
return self._scaler.state_dict() |
|
|
|
def load_state_dict(self, state_dict): |
|
self._scaler.load_state_dict(state_dict) |
|
|
|
|
|
def get_grad_norm_(parameters, norm_type: float = 2.0) -> torch.Tensor: |
|
if isinstance(parameters, torch.Tensor): |
|
parameters = [parameters] |
|
parameters = [p for p in parameters if p.grad is not None and p.requires_grad] |
|
norm_type = float(norm_type) |
|
if len(parameters) == 0: |
|
return torch.tensor(0.) |
|
device = parameters[0].grad.device |
|
total_norm = torch.norm(torch.stack([torch.norm(p.grad.detach(), norm_type).to(device) for p in parameters]), |
|
norm_type) |
|
return total_norm |
|
|
|
|
|
def get_obj_from_str(string, reload=False): |
|
module, cls = string.rsplit(".", 1) |
|
if reload: |
|
module_imp = importlib.import_module(module) |
|
importlib.reload(module_imp) |
|
return getattr(importlib.import_module(module, package=None), cls) |
|
|
|
|
|
def instantiate_from_config(config): |
|
if not "target" in config: |
|
raise KeyError("Expected key `target` to instantiate.") |
|
return get_obj_from_str(config["target"])(**config.get("params", dict())) |
|
|
|
|
|
def save_on_master(*args, **kwargs): |
|
if dist.get_rank() == 0: |
|
torch.save(*args, **kwargs) |
|
|
|
|
|
def save_model(args, epoch, model, model_without_ddp, optimizer_g, optimizer_d, loss_scaler): |
|
output_dir = Path(args.output_dir) |
|
epoch_name = str(epoch) |
|
if loss_scaler is not None: |
|
checkpoint_paths = [output_dir / ('checkpoint-%s.pth' % epoch_name)] |
|
for checkpoint_path in checkpoint_paths: |
|
to_save = { |
|
'model': model_without_ddp.state_dict(), |
|
'optimizer_g': optimizer_g.state_dict(), |
|
'optimizer_d': optimizer_d.state_dict(), |
|
'epoch': epoch, |
|
'scaler': loss_scaler.state_dict(), |
|
'args': args, |
|
} |
|
|
|
save_on_master(to_save, checkpoint_path) |
|
else: |
|
client_state = {'epoch': epoch} |
|
model.save_checkpoint(save_dir=args.output_dir, tag="checkpoint-%s" % epoch_name, client_state=client_state) |
|
|
|
|
|
def load_model(args, model_without_ddp, optimizer_g, optimizer_d, loss_scaler): |
|
if args.resume: |
|
if args.resume.startswith('https'): |
|
checkpoint = torch.hub.load_state_dict_from_url( |
|
args.resume, map_location='cpu', check_hash=True) |
|
else: |
|
checkpoint = torch.load(args.resume, map_location='cpu') |
|
model_without_ddp.load_state_dict(checkpoint['model']) |
|
print("Resume checkpoint %s" % args.resume) |
|
if 'optimizer_g' in checkpoint and 'epoch' in checkpoint and not (hasattr(args, 'eval') and args.eval): |
|
optimizer_g.load_state_dict(checkpoint['optimizer_g']) |
|
optimizer_d.load_state_dict(checkpoint['optimizer_d']) |
|
args.start_epoch = checkpoint['epoch'] + 1 |
|
if 'scaler' in checkpoint: |
|
loss_scaler.load_state_dict(checkpoint['scaler']) |
|
print("With optim & sched!") |
|
|
|
|
|
def download(url, local_path, chunk_size=1024): |
|
os.makedirs(os.path.split(local_path)[0], exist_ok=True) |
|
with requests.get(url, stream=True) as r: |
|
total_size = int(r.headers.get("content-length", 0)) |
|
with tqdm(total=total_size, unit="B", unit_scale=True) as pbar: |
|
with open(local_path, "wb") as f: |
|
for data in r.iter_content(chunk_size=chunk_size): |
|
if data: |
|
f.write(data) |
|
pbar.update(chunk_size) |
|
|
|
|
|
def md5_hash(path): |
|
with open(path, "rb") as f: |
|
content = f.read() |
|
return hashlib.md5(content).hexdigest() |
|
|
|
|
|
def get_ckpt_path(name, root, check=False): |
|
assert name in URL_MAP |
|
path = os.path.join(root, CKPT_MAP[name]) |
|
if not os.path.exists(path) or (check and not md5_hash(path) == MD5_MAP[name]): |
|
print("Downloading {} model from {} to {}".format(name, URL_MAP[name], path)) |
|
download(URL_MAP[name], path) |
|
md5 = md5_hash(path) |
|
assert md5 == MD5_MAP[name], md5 |
|
return path |
|
|
|
|
|
class KeyNotFoundError(Exception): |
|
def __init__(self, cause, keys=None, visited=None): |
|
self.cause = cause |
|
self.keys = keys |
|
self.visited = visited |
|
messages = list() |
|
if keys is not None: |
|
messages.append("Key not found: {}".format(keys)) |
|
if visited is not None: |
|
messages.append("Visited: {}".format(visited)) |
|
messages.append("Cause:\n{}".format(cause)) |
|
message = "\n".join(messages) |
|
super().__init__(message) |
|
|
|
|
|
def retrieve( |
|
list_or_dict, key, splitval="/", default=None, expand=True, pass_success=False |
|
): |
|
"""Given a nested list or dict return the desired value at key expanding |
|
callable nodes if necessary and :attr:`expand` is ``True``. The expansion |
|
is done in-place. |
|
|
|
Parameters |
|
---------- |
|
list_or_dict : list or dict |
|
Possibly nested list or dictionary. |
|
key : str |
|
key/to/value, path like string describing all keys necessary to |
|
consider to get to the desired value. List indices can also be |
|
passed here. |
|
splitval : str |
|
String that defines the delimiter between keys of the |
|
different depth levels in `key`. |
|
default : obj |
|
Value returned if :attr:`key` is not found. |
|
expand : bool |
|
Whether to expand callable nodes on the path or not. |
|
|
|
Returns |
|
------- |
|
The desired value or if :attr:`default` is not ``None`` and the |
|
:attr:`key` is not found returns ``default``. |
|
|
|
Raises |
|
------ |
|
Exception if ``key`` not in ``list_or_dict`` and :attr:`default` is |
|
``None``. |
|
""" |
|
|
|
keys = key.split(splitval) |
|
|
|
success = True |
|
try: |
|
visited = [] |
|
parent = None |
|
last_key = None |
|
for key in keys: |
|
if callable(list_or_dict): |
|
if not expand: |
|
raise KeyNotFoundError( |
|
ValueError( |
|
"Trying to get past callable node with expand=False." |
|
), |
|
keys=keys, |
|
visited=visited, |
|
) |
|
list_or_dict = list_or_dict() |
|
parent[last_key] = list_or_dict |
|
|
|
last_key = key |
|
parent = list_or_dict |
|
|
|
try: |
|
if isinstance(list_or_dict, dict): |
|
list_or_dict = list_or_dict[key] |
|
else: |
|
list_or_dict = list_or_dict[int(key)] |
|
except (KeyError, IndexError, ValueError) as e: |
|
raise KeyNotFoundError(e, keys=keys, visited=visited) |
|
|
|
visited += [key] |
|
|
|
if expand and callable(list_or_dict): |
|
list_or_dict = list_or_dict() |
|
parent[last_key] = list_or_dict |
|
except KeyNotFoundError as e: |
|
if default is None: |
|
raise e |
|
else: |
|
list_or_dict = default |
|
success = False |
|
|
|
if not pass_success: |
|
return list_or_dict |
|
else: |
|
return list_or_dict, success |
|
|
|
|
|
if __name__ == "__main__": |
|
config = {"keya": "a", |
|
"keyb": "b", |
|
"keyc": |
|
{"cc1": 1, |
|
"cc2": 2, |
|
} |
|
} |
|
from omegaconf import OmegaConf |
|
|
|
config = OmegaConf.create(config) |
|
print(config) |
|
retrieve(config, "keya") |
|
|
|
def instantiate_from_config(config): |
|
|
|
if not "target" in config: |
|
raise KeyError("Expected key `target` to instantiate.") |
|
return get_obj_from_str(config["target"])(**config.get("params", dict())) |
|
|
|
def get_obj_from_str(string, reload=False): |
|
module, cls = string.rsplit(".", 1) |
|
if reload: |
|
module_imp = importlib.import_module(module) |
|
importlib.reload(module_imp) |
|
return getattr(importlib.import_module(module, package=None), cls) |
|
|