File size: 4,825 Bytes
6d314be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import pickle
import random
import shutil
import typing as tp

import numpy as np
import torch
import torchvision.transforms as T
import wandb
from PIL import Image
from joblib import Parallel, delayed
from torch.utils.data import DataLoader, TensorDataset
from torchmetrics.image.fid import FrechetInceptionDistance
from tqdm.auto import tqdm

from models.Encoders import ClipModel


def image_grid(imgs, rows, cols):
    assert len(imgs) == rows * cols

    w, h = imgs[0].size
    grid = Image.new('RGB', size=(cols * w, rows * h))

    for i, img in enumerate(imgs):
        grid.paste(img, box=(i % cols * w, i // cols * h))
    return grid


class WandbLogger:
    def __init__(self, name='base-name', project='HairFast'):
        self.name = name
        self.project = project

    def start_logging(self):
        wandb.login(key=os.environ['WANDB_KEY'].strip(), relogin=True)
        wandb.init(
            project=self.project,
            name=self.name
        )
        self.wandb = wandb
        self.run_dir = self.wandb.run.dir
        self.train_step = 0

    def log(self, scalar_name: str, scalar: tp.Any):
        self.wandb.log({scalar_name: scalar}, step=self.train_step, commit=False)

    def log_scalars(self, scalars: dict):
        self.wandb.log(scalars, step=self.train_step, commit=False)

    def next_step(self):
        self.train_step += 1

    def save(self, file_path, save_online=True):
        file = os.path.basename(file_path)
        new_path = os.path.join(self.run_dir, file)
        shutil.copy2(file_path, new_path)
        if save_online:
            self.wandb.save(new_path)

    def __del__(self):
        self.wandb.finish()


def toggle_grad(model, flag=True):
    for p in model.parameters():
        p.requires_grad = flag


class _LegacyUnpickler(pickle.Unpickler):
    def find_class(self, module, name):
        if module == 'dnnlib.tflib.network' and name == 'Network':
            return _TFNetworkStub
        module = module.replace('torch_utils', 'models.stylegan2.torch_utils')
        module = module.replace('dnnlib', 'models.stylegan2.dnnlib')
        return super().find_class(module, name)


def seed_everything(seed: int = 1729) -> None:
    random.seed(seed)
    os.environ["PYTHONHASHSEED"] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True


def load_images_to_torch(paths, imgs=None, use_tqdm=True):
    transform = T.PILToTensor()
    tensor = []
    for path in paths:
        if imgs is None:
            pbar = sorted(os.listdir(path))
        else:
            pbar = imgs

        if use_tqdm:
            pbar = tqdm(pbar)

        for img_name in pbar:
            if '.jpg' in img_name or '.png' in img_name:
                img_path = os.path.join(path, img_name)
                img = Image.open(img_path).resize((299, 299), resample=Image.LANCZOS)
                tensor.append(transform(img))
    try:
        return torch.stack(tensor)
    except:
        print(paths, imgs)
        return torch.tensor([], dtype=torch.uint8)


def parallel_load_images(paths, imgs):
    assert imgs is not None
    if not isinstance(paths, list):
        paths = [paths]

    list_torch_images = Parallel(n_jobs=-1)(delayed(load_images_to_torch)(
        paths, [i], use_tqdm=False
    ) for i in tqdm(imgs))
    return torch.cat(list_torch_images)


def get_fid_calc(instance='fid.pkl', dataset_path='', device=torch.device('cuda')):
    if os.path.isfile(instance):
        with open(instance, 'rb') as f:
            fid = pickle.load(f)
    else:
        fid = FrechetInceptionDistance(feature=ClipModel(), reset_real_features=False, normalize=True)
        fid.to(device).eval()

        imgs_file = []
        for file in os.listdir(dataset_path):
            if 'flip' not in file and os.path.splitext(file)[1] in ['.png', '.jpg']:
                imgs_file.append(file)

        tensor_images = parallel_load_images([dataset_path], imgs_file).float().div(255)
        real_dataloader = DataLoader(TensorDataset(tensor_images), batch_size=128)
        with torch.inference_mode():
            for batch in tqdm(real_dataloader):
                batch = batch[0].to(device)
                fid.update(batch, real=True)

        with open(instance, 'wb') as f:
            pickle.dump(fid.cpu(), f)
    fid.to(device).eval()

    @torch.inference_mode()
    def compute_fid_datasets(images):
        nonlocal fid, device
        fid.reset()

        fake_dataloader = DataLoader(TensorDataset(images), batch_size=128)
        for batch in tqdm(fake_dataloader):
            batch = batch[0].to(device)
            fid.update(batch, real=False)

        return fid.compute()

    return compute_fid_datasets