File size: 12,716 Bytes
2b534de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# ---------------------------------------------------------     Config  -------------------------------------------------
num_workers :int = 1
DDIM_STEPS = 50
BATCH_SIZE = 1
FIXED_CODE = False
# for vis
SAVE_INTERMEDIATES = True
NUM_grid_in_a_column = 5
# ------------------------------------------------------------------------------------------------------------------------
import sys
import os
from pathlib import Path

cur_dir = os.path.dirname(os.path.abspath(__file__))

from confs import *
import torch
import numpy as np
from omegaconf import OmegaConf
from PIL import Image
from tqdm import tqdm
from einops import rearrange
from torchvision.utils import make_grid
from my_py_lib.image_util import imgs_2_grid_A,img_paths_2_grid_A
from pytorch_lightning import seed_everything
from torch import autocast
from contextlib import nullcontext
import torchvision

from ldm.models.diffusion.ddpm import LatentDiffusion
from ldm.util import instantiate_from_config
from ldm.models.diffusion.ddim import DDIMSampler
from Dataset_custom import Dataset_custom
from MoE import offload_unused_tasks__LD
from ldm.models.diffusion.ddpm import LandmarkExtractor
from my_py_lib.torch_util import cleanup_gpu_memory
from gen_lmk_and_mask import gen_lmk_and_mask









# ------------------------------------------------------------------------------------------------------------------------
DDIM_ETA = 0.0
SCALE = 3.0
PRECISION = "full"  # "full" or "autocast"
H = 512
W = 512
C = 4
F = 8
# ------------------------------------------------------------------------------------------------------------------------


def load_first_stage_from_sd14(model: LatentDiffusion, sd14_path: Path) -> None:
    print(f"Loading first_stage_model from {sd14_path}")
    sd14 = torch.load(str(sd14_path), map_location="cpu")
    if isinstance(sd14, dict) and "state_dict" in sd14:
        sd14_sd = sd14["state_dict"]
    else:
        sd14_sd = sd14

    prefixes = ["first_stage_model.", "model.first_stage_model."]
    fs_sd = {}
    for prefix in prefixes:
        for k, v in sd14_sd.items():
            if k.startswith(prefix):
                fs_sd[k[len(prefix):]] = v
        if fs_sd:
            break

    if not fs_sd:
        raise RuntimeError("Could not find first_stage_model weights in SD v1-4 checkpoint.")

    model.first_stage_model.load_state_dict(fs_sd, strict=True)


def save_sample_by_decode(x, model, base_path, segment_id, intermediate_num):
    x = model.decode_first_stage(x)
    x = torch.clamp((x + 1.0) / 2.0, min=0.0, max=1.0)
    x = x.cpu().permute(0, 2, 3, 1).numpy()
    for i in range(len(x)):
        img = Image.fromarray((x[i] * 255).astype(np.uint8))
        save_path = Path(base_path) / segment_id
        save_path.mkdir(parents=True, exist_ok=True)
        img.save(save_path / f"{intermediate_num}.png")


def get_tensor_clip(normalize=True, toTensor=True):
    transform_list = []
    if toTensor:
        transform_list += [torchvision.transforms.ToTensor()]
    if normalize:
        transform_list += [
            torchvision.transforms.Normalize(
                (0.48145466, 0.4578275, 0.40821073),
                (0.26862954, 0.26130258, 0.27577711),
            )
        ]
    return torchvision.transforms.Compose(transform_list)


def load_model_from_config(ckpt, verbose=1):
    if 1:
        ckpt = Path(ckpt)
        print(f"Loading model from {ckpt}")
        pl_sd = torch.load(str(ckpt), map_location="cpu")
        if isinstance(pl_sd, dict) and "state_dict" in pl_sd:
            sd = pl_sd["state_dict"]
        else:
            sd = pl_sd
    else:
        print("DEBUG_skip_load_ckpt")
    if 1:
        from init_model import get_moe
        model: LatentDiffusion = get_moe()
        model.ptsM_Generator = LandmarkExtractor(include_visualizer=True, img_256_mode=False)
        cleanup_gpu_memory()
    if 1:
        m, u = model.load_state_dict(sd, strict=False)
        if len(m) > 0 and verbose:
            print("missing keys:")
            pretty_print_torch_module_keys(m)
        if len(u) > 0 and verbose:
            print("unexpected keys:")
            pretty_print_torch_module_keys(u)
        load_first_stage_from_sd14(model, SD14_localpath)

    offload_unused_tasks__LD(model, TASK, method="del") # for save cuda mem
    model.cuda()
    model.eval()
    return model




def load_pairs(pair_list, tgt, ref):
    if tgt and ref:
        pairs = [(tgt, ref), ]
    elif pair_list:
        pairs = []
        with open(pair_list, "r") as f:
            for line_num, line in enumerate(f, start=1):
                line = line.strip()
                if not line or line.startswith("#"):
                    continue
                parts = line.split(" ")
                if len(parts) != 2:
                    raise ValueError(f"Invalid pair list line {line_num}: expected white-space-separated tgt/ref. got {parts=}")
                pairs.append((parts[0], parts[1]))
    else:
        raise ValueError("No input pairs provided. Use --tgt/--ref or --pair-list.")
    print(f"{pairs=}")
    return pairs


def un_norm(x):
    return (x + 1.0) / 2.0


def un_norm_clip(x1):
    x = x1 * 1.0
    reduce = False
    if len(x.shape) == 3:
        x = x.unsqueeze(0)
        reduce = True
    x[:, 0, :, :] = x[:, 0, :, :] * 0.26862954 + 0.48145466
    x[:, 1, :, :] = x[:, 1, :, :] * 0.26130258 + 0.4578275
    x[:, 2, :, :] = x[:, 2, :, :] * 0.27577711 + 0.40821073
    if reduce:
        x = x.squeeze(0)
    return x


if __name__ == "__main__":
    pairs = load_pairs(args.pair_list, args.tgt, args.ref)

    out_dir = Path(args.out_dir)
    result_path = out_dir / "results"
    grid_path = out_dir / "grid"
    inter_path = out_dir / "intermediates"
    inter_pred_path = inter_path / "pred_x0"
    inter_noised_path = inter_path / "noised"
    out_dir.mkdir(parents=False, exist_ok=True)
    result_path.mkdir(parents=False, exist_ok=True)
    grid_path.mkdir(parents=False, exist_ok=True)
    inter_path.mkdir(parents=False, exist_ok=True)
    if SAVE_INTERMEDIATES:
        inter_pred_path.mkdir(parents=False, exist_ok=True)
        inter_noised_path.mkdir(parents=False, exist_ok=True)
    paths_tgt = [p[0] for p in pairs]
    paths_ref = [p[1] for p in pairs]
    gen_lmk_and_mask(paths_tgt + paths_ref)

    seed_everything(42)

    model: LatentDiffusion = load_model_from_config(PRETRAIN_CKPT_PATH, )
    device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
    model = model.to(device)
    sampler = DDIMSampler(model)

    dataset = Dataset_custom(
        "test",
        task=TASK,
        paths_tgt=paths_tgt,
        paths_ref=paths_ref,
    )
    dataloader = torch.utils.data.DataLoader(
        dataset,
        batch_size=BATCH_SIZE,
        num_workers=num_workers,
        pin_memory=True,
        shuffle=False,
        drop_last=False,
    )

    start_code = None
    if FIXED_CODE:
        start_code = torch.randn([BATCH_SIZE, C, H // F, W // F], device=device)

    precision_scope = autocast if PRECISION == "autocast" else nullcontext
    grids = []
    grid_stems = []

    with torch.no_grad():
        with precision_scope("cuda"):
            with model.ema_scope():
                for test_batch, prior, test_model_kwargs, out_stem_batch in tqdm(dataloader):
                    model.set_task(test_model_kwargs)
                    bs = test_batch.shape[0]

                    batch_ = {
                        **test_model_kwargs,
                        "GT": torch.zeros_like(test_model_kwargs["inpaint_image"]),
                    }
                    batch_, c = model.get_input_and_conditioning(batch_, device=device)
                    z_inpaint = batch_["z4_inpaint"]
                    z_inpaint_mask = batch_["tgt_mask_64"]
                    z_ref = batch_["z_ref"]
                    z9 = batch_["z9"]

                    uc = None
                    if SCALE != 1.0:
                        uc = model.learnable_vector[TASK].repeat(bs, 1, 1)

                    shape = [C, H // F, W // F]
                    local_start_code = start_code
                    if FIXED_CODE and (local_start_code is None or local_start_code.shape[0] != bs):
                        local_start_code = torch.randn([bs, C, H // F, W // F], device=device)
                    samples_ddim, intermediates = sampler.sample(
                        S=DDIM_STEPS,
                        conditioning=c,
                        batch_size=bs,
                        shape=shape,
                        verbose=False,
                        unconditional_guidance_scale=SCALE,
                        unconditional_conditioning=uc,
                        eta=DDIM_ETA,
                        x_T=local_start_code,
                        log_every_t=100,
                        z_inpaint=z_inpaint,
                        z_inpaint_mask=z_inpaint_mask,
                        z_ref=z_ref,
                        z9=z9,
                    )

                    if SAVE_INTERMEDIATES:
                        intermediate_pred_x0 = intermediates["pred_x0"]
                        intermediate_noised = intermediates["x_inter"]
                        for i in range(len(intermediate_pred_x0)):
                            for j in range(bs):
                                stem = f"{out_stem_batch[j]}"
                                save_sample_by_decode(
                                    intermediate_pred_x0[i][j : j + 1],
                                    model,
                                    inter_pred_path,
                                    stem,
                                    i,
                                )
                                save_sample_by_decode(
                                    intermediate_noised[i][j : j + 1],
                                    model,
                                    inter_noised_path,
                                    stem,
                                    i,
                                )

                    x_samples_ddim = model.decode_first_stage(samples_ddim)
                    x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
                    x_samples_ddim = x_samples_ddim.cpu().permute(0, 2, 3, 1).numpy()

                    x_checked_image_torch = torch.from_numpy(x_samples_ddim).permute(0, 3, 1, 2)
                    for i, x_sample in enumerate(x_checked_image_torch):
                        stem = f"{out_stem_batch[i]}"
                        out_path = result_path / f"{stem}.png"
                        img = Image.fromarray((x_sample.permute(1, 2, 0).numpy() * 255).astype(np.uint8))
                        img.save(out_path)
                        print(f"{out_path=}")

                    for i, x_sample in enumerate(x_checked_image_torch):
                        all_img = []
                        all_img.append(un_norm(test_batch[i]).cpu())
                        if TASK != 2:
                            ref_img = test_model_kwargs["ref_imgs"].squeeze(1)
                            ref_img = torchvision.transforms.Resize([512, 512])(ref_img)
                            ref_img = un_norm_clip(ref_img[i]).cpu()
                        else:
                            ref_img = un_norm(test_model_kwargs["ref512"].squeeze(1)[i]).cpu()
                        all_img.append(ref_img)
                        all_img.append(x_sample)

                        grid = torch.stack(all_img, 0)
                        grid = make_grid(grid)
                        grid = 255.0 * rearrange(grid, "c h w -> h w c").cpu().numpy()
                        img = Image.fromarray(grid.astype(np.uint8))
                        stem = f"{out_stem_batch[i]}"
                        path_save_img = grid_path / f"grid-{stem}.jpg"
                        img.save(path_save_img)
                        print(f"{path_save_img=}")
                        grids.append(img)
                        grid_stems.append(stem)
                        if len(grids) >= NUM_grid_in_a_column:
                                stem_start = grid_stems[0]
                                stem_end = grid_stems[-1]
                                grid_column = imgs_2_grid_A(
                                    grids,
                                    grid_layout='column',
                                    grid_path=os.path.join(grid_path, f"{stem_start}--{stem_end}.jpg"),
                                )
                                grids = []
                                grid_stems = []

                    model.unset_task()

    print(f"Your samples are ready and waiting for you here: {out_dir}")