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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import json
import os
import random
from dataclasses import dataclass, field

import cv2
import numpy as np
import pytorch_lightning as pl
import torch
import torch.nn.functional as F
from PIL import Image
from torch.utils.data import DataLoader, Dataset

from ..utils.config import parse_structured
from ..utils.geometry import (
    get_plucker_embeds_from_cameras,
    get_plucker_embeds_from_cameras_ortho,
    get_position_map_from_depth,
    get_position_map_from_depth_ortho,
)
from ..utils.typing import *

os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"


def _parse_scene_list_single(scene_list_path: str, root_data_dir: str):
    all_scenes = []
    if scene_list_path.endswith(".json"):
        with open(scene_list_path) as f:
            for p in json.loads(f.read()):
                if "/" in p:
                    all_scenes.append(os.path.join(root_data_dir, p))
                else:
                    all_scenes.append(os.path.join(root_data_dir, p[:2], p))
    elif scene_list_path.endswith(".txt"):
        with open(scene_list_path) as f:
            for p in f.readlines():
                p = p.strip()
                if "/" in p:
                    all_scenes.append(os.path.join(root_data_dir, p))
                else:
                    all_scenes.append(os.path.join(root_data_dir, p[:2], p))
    else:
        raise NotImplementedError

    return all_scenes


def _parse_scene_list(
    scene_list_path: Union[str, List[str]], root_data_dir: Union[str, List[str]]
):
    all_scenes = []
    if isinstance(scene_list_path, str):
        scene_list_path = [scene_list_path]
    if isinstance(root_data_dir, str):
        root_data_dir = [root_data_dir]
    for scene_list_path_, root_data_dir_ in zip(scene_list_path, root_data_dir):
        all_scenes += _parse_scene_list_single(scene_list_path_, root_data_dir_)
    return all_scenes


def _parse_reference_scene_list(reference_scenes: List[str], all_scenes: List[str]):
    all_ids = set(scene.split("/")[-1] for scene in all_scenes)
    ref_ids = set(scene.split("/")[-1] for scene in reference_scenes)
    common_ids = ref_ids.intersection(all_ids)
    all_scenes = [scene for scene in all_scenes if scene.split("/")[-1] in common_ids]
    all_ids = {scene.split("/")[-1]: idx for idx, scene in enumerate(all_scenes)}

    ref_scenes = [
        scene for scene in reference_scenes if scene.split("/")[-1] in all_ids
    ]
    sorted_ref_scenes = sorted(ref_scenes, key=lambda x: all_ids[x.split("/")[-1]])
    scene2ref = {
        scene: ref_scene for scene, ref_scene in zip(all_scenes, sorted_ref_scenes)
    }

    return all_scenes, scene2ref


@dataclass
class MultiviewDataModuleConfig:
    root_dir: Any = ""
    scene_list: Any = ""
    image_suffix: str = "webp"
    background_color: Union[str, float] = "gray"
    image_names: List[str] = field(default_factory=lambda: [])
    image_modality: str = "render"
    num_views: int = 1
    random_view_list: Optional[List[List[int]]] = None

    prompt_db_path: Optional[str] = None
    return_prompt: bool = False
    use_empty_prompt: bool = False
    prompt_prefix: Optional[Any] = None
    return_one_prompt: bool = True

    projection_type: str = "ORTHO"

    # source conditions
    source_image_modality: Any = "position"
    use_camera_space_normal: bool = False
    position_offset: float = 0.5
    position_scale: float = 1.0
    plucker_offset: float = 1.0
    plucker_scale: float = 2.0

    # reference image
    reference_root_dir: Optional[Any] = None
    reference_scene_list: Optional[Any] = None
    reference_image_modality: str = "render"
    reference_image_names: List[str] = field(default_factory=lambda: [])
    reference_augment_resolutions: Optional[List[int]] = None
    reference_mask_aug: bool = False

    repeat: int = 1  # for debugging purpose

    train_indices: Optional[Tuple[Any, Any]] = None
    val_indices: Optional[Tuple[Any, Any]] = None
    test_indices: Optional[Tuple[Any, Any]] = None

    height: int = 768
    width: int = 768

    batch_size: int = 1
    eval_batch_size: int = 1

    num_workers: int = 16


class MultiviewDataset(Dataset):
    def __init__(self, cfg: Any, split: str = "train") -> None:
        super().__init__()
        assert split in ["train", "val", "test"]
        self.cfg: MultiviewDataModuleConfig = cfg
        self.all_scenes = _parse_scene_list(self.cfg.scene_list, self.cfg.root_dir)

        if (
            self.cfg.reference_root_dir is not None
            and self.cfg.reference_scene_list is not None
        ):
            reference_scenes = _parse_scene_list(
                self.cfg.reference_scene_list, self.cfg.reference_root_dir
            )
            self.all_scenes, self.reference_scenes = _parse_reference_scene_list(
                reference_scenes, self.all_scenes
            )
        else:
            self.reference_scenes = None

        self.split = split
        if self.split == "train" and self.cfg.train_indices is not None:
            self.all_scenes = self.all_scenes[
                self.cfg.train_indices[0] : self.cfg.train_indices[1]
            ]
            self.all_scenes = self.all_scenes * self.cfg.repeat
        elif self.split == "val" and self.cfg.val_indices is not None:
            self.all_scenes = self.all_scenes[
                self.cfg.val_indices[0] : self.cfg.val_indices[1]
            ]
        elif self.split == "test" and self.cfg.test_indices is not None:
            self.all_scenes = self.all_scenes[
                self.cfg.test_indices[0] : self.cfg.test_indices[1]
            ]

        if self.cfg.prompt_db_path is not None:
            self.prompt_db = json.load(open(self.cfg.prompt_db_path))
        else:
            self.prompt_db = None

    def __len__(self):
        return len(self.all_scenes)

    def get_bg_color(self, bg_color):
        if bg_color == "white":
            bg_color = np.array([1.0, 1.0, 1.0], dtype=np.float32)
        elif bg_color == "black":
            bg_color = np.array([0.0, 0.0, 0.0], dtype=np.float32)
        elif bg_color == "gray":
            bg_color = np.array([0.5, 0.5, 0.5], dtype=np.float32)
        elif bg_color == "random":
            bg_color = np.random.rand(3)
        elif bg_color == "random_gray":
            bg_color = random.uniform(0.3, 0.7)
            bg_color = np.array([bg_color] * 3, dtype=np.float32)
        elif isinstance(bg_color, float):
            bg_color = np.array([bg_color] * 3, dtype=np.float32)
        elif isinstance(bg_color, list) or isinstance(bg_color, tuple):
            bg_color = np.array(bg_color, dtype=np.float32)
        else:
            raise NotImplementedError
        return bg_color

    def load_image(
        self,
        image: Union[str, Image.Image],
        height: int,
        width: int,
        background_color: torch.Tensor,
        rescale: bool = False,
        mask_aug: bool = False,
    ):
        if isinstance(image, str):
            image = Image.open(image)

        image = image.resize((width, height))
        image = torch.from_numpy(np.array(image)).float() / 255.0

        if mask_aug:
            alpha = image[:, :, 3]  # Extract alpha channel
            h, w = alpha.shape
            y_indices, x_indices = torch.where(alpha > 0.5)
            if len(y_indices) > 0 and len(x_indices) > 0:
                idx = torch.randint(len(y_indices), (1,)).item()
                y_center = y_indices[idx].item()
                x_center = x_indices[idx].item()
                mask_h = random.randint(h // 8, h // 4)
                mask_w = random.randint(w // 8, w // 4)

                y1 = max(0, y_center - mask_h // 2)
                y2 = min(h, y_center + mask_h // 2)
                x1 = max(0, x_center - mask_w // 2)
                x2 = min(w, x_center + mask_w // 2)

                alpha[y1:y2, x1:x2] = 0.0
                image[:, :, 3] = alpha

        image = image[:, :, :3] * image[:, :, 3:4] + background_color * (
            1 - image[:, :, 3:4]
        )
        if rescale:
            image = image * 2.0 - 1.0
        return image

    def load_normal_image(
        self,
        path,
        height,
        width,
        background_color,
        camera_space: bool = False,
        c2w: Optional[torch.FloatTensor] = None,
    ):
        image = Image.open(path).resize((width, height), resample=Image.NEAREST)
        image = torch.from_numpy(np.array(image)).float() / 255.0
        alpha = image[:, :, 3:4]
        image = image[:, :, :3]
        if camera_space:
            w2c = torch.linalg.inv(c2w)[:3, :3]
            image = (
                F.normalize(((image * 2 - 1)[:, :, None, :] * w2c).sum(-1), dim=-1)
                * 0.5
                + 0.5
            )
        image = image * alpha + background_color * (1 - alpha)
        return image

    def load_depth(self, path, height, width):
        depth = cv2.imread(path, cv2.IMREAD_UNCHANGED)
        depth = cv2.resize(depth, (width, height), interpolation=cv2.INTER_NEAREST)
        depth = torch.from_numpy(depth[..., 0:1]).float()
        mask = torch.ones_like(depth)
        mask[depth > 1000.0] = 0.0  # depth = 65535 is the invalid value
        depth[~(mask > 0.5)] = 0.0
        return depth, mask

    def retrieve_prompt(self, scene_dir):
        assert self.prompt_db is not None
        source_id = os.path.basename(scene_dir)
        return self.prompt_db.get(source_id, "")

    def __getitem__(self, index):
        background_color = torch.as_tensor(self.get_bg_color(self.cfg.background_color))
        scene_dir = self.all_scenes[index]

        with open(os.path.join(scene_dir, "meta.json")) as f:
            meta = json.load(f)
        name2loc = {loc["index"]: loc for loc in meta["locations"]}

        # target multi-view images
        image_paths = [
            os.path.join(
                scene_dir, f"{self.cfg.image_modality}_{f}.{self.cfg.image_suffix}"
            )
            for f in self.cfg.image_names
        ]
        images = [
            self.load_image(
                p,
                height=self.cfg.height,
                width=self.cfg.width,
                background_color=background_color,
            )
            for p in image_paths
        ]
        images = torch.stack(images, dim=0).permute(0, 3, 1, 2)

        # camera
        c2w = [
            torch.as_tensor(name2loc[name]["transform_matrix"])
            for name in self.cfg.image_names
        ]
        c2w = torch.stack(c2w, dim=0)

        if self.cfg.projection_type == "PERSP":
            camera_angle_x = (
                meta.get("camera_angle_x", None)
                or meta["locations"][0]["camera_angle_x"]
            )
            focal_length = 0.5 * self.cfg.width / np.tan(0.5 * camera_angle_x)
            intrinsics = (
                torch.as_tensor(
                    [
                        [focal_length, 0.0, 0.5 * self.cfg.width],
                        [0.0, focal_length, 0.5 * self.cfg.height],
                        [0.0, 0.0, 1.0],
                    ]
                )
                .unsqueeze(0)
                .float()
                .repeat(len(self.cfg.image_names), 1, 1)
            )
        elif self.cfg.projection_type == "ORTHO":
            ortho_scale = (
                meta.get("ortho_scale", None) or meta["locations"][0]["ortho_scale"]
            )

        # source conditions
        source_image_modality = self.cfg.source_image_modality
        if isinstance(source_image_modality, str):
            source_image_modality = [source_image_modality]
        source_images = []
        for modality in source_image_modality:
            if modality == "position":
                depth_masks = [
                    self.load_depth(
                        os.path.join(scene_dir, f"depth_{f}.exr"),
                        self.cfg.height,
                        self.cfg.width,
                    )
                    for f in self.cfg.image_names
                ]
                depths = torch.stack([d for d, _ in depth_masks])
                masks = torch.stack([m for _, m in depth_masks])
                c2w_ = c2w.clone()
                c2w_[:, :, 1:3] *= -1

                if self.cfg.projection_type == "PERSP":
                    position_maps = get_position_map_from_depth(
                        depths,
                        masks,
                        intrinsics,
                        c2w_,
                        image_wh=(self.cfg.width, self.cfg.height),
                    )
                elif self.cfg.projection_type == "ORTHO":
                    position_maps = get_position_map_from_depth_ortho(
                        depths,
                        masks,
                        c2w_,
                        ortho_scale,
                        image_wh=(self.cfg.width, self.cfg.height),
                    )
                position_maps = (
                    (position_maps + self.cfg.position_offset) / self.cfg.position_scale
                ).clamp(0.0, 1.0)
                source_images.append(position_maps)
            elif modality == "normal":
                normal_maps = [
                    self.load_normal_image(
                        os.path.join(
                            scene_dir, f"{modality}_{f}.{self.cfg.image_suffix}"
                        ),
                        height=self.cfg.height,
                        width=self.cfg.width,
                        background_color=background_color,
                        camera_space=self.cfg.use_camera_space_normal,
                        c2w=c,
                    )
                    for c, f in zip(c2w, self.cfg.image_names)
                ]
                source_images.append(torch.stack(normal_maps, dim=0))
            elif modality == "plucker":
                if self.cfg.projection_type == "ORTHO":
                    plucker_embed = get_plucker_embeds_from_cameras_ortho(
                        c2w, [ortho_scale] * len(c2w), self.cfg.width
                    )
                elif self.cfg.projection_type == "PERSP":
                    plucker_embed = get_plucker_embeds_from_cameras(
                        c2w, [camera_angle_x] * len(c2w), self.cfg.width
                    )
                else:
                    raise NotImplementedError
                plucker_embed = plucker_embed.permute(0, 2, 3, 1)
                plucker_embed = (
                    (plucker_embed + self.cfg.plucker_offset) / self.cfg.plucker_scale
                ).clamp(0.0, 1.0)
                source_images.append(plucker_embed)
            else:
                raise NotImplementedError
        source_images = torch.cat(source_images, dim=-1).permute(0, 3, 1, 2)
        rv = {"rgb": images, "c2w": c2w, "source_rgb": source_images}

        num_images = len(self.cfg.image_names)
        # prompt
        if self.cfg.return_prompt:
            if self.cfg.use_empty_prompt:
                prompt = ""
            else:
                prompt = self.retrieve_prompt(scene_dir)
            prompts = [prompt] * num_images

            if self.cfg.prompt_prefix is not None:
                prompt_prefix = self.cfg.prompt_prefix
                if isinstance(prompt_prefix, str):
                    prompt_prefix = [prompt_prefix] * num_images

                for i, prompt in enumerate(prompts):
                    prompts[i] = f"{prompt_prefix[i]} {prompt}"

            if self.cfg.return_one_prompt:
                rv.update({"prompts": prompts[0]})
            else:
                rv.update({"prompts": prompts})

        # reference image
        if self.reference_scenes is not None:
            reference_scene_dir = self.reference_scenes[scene_dir]
            reference_image_paths = [
                os.path.join(
                    reference_scene_dir,
                    f"{self.cfg.reference_image_modality}_{f}.{self.cfg.image_suffix}",
                )
                for f in self.cfg.reference_image_names
            ]
            reference_image_path = random.choice(reference_image_paths)

            if self.cfg.reference_augment_resolutions is None:
                reference_image = self.load_image(
                    reference_image_path,
                    height=self.cfg.height,
                    width=self.cfg.width,
                    background_color=background_color,
                    mask_aug=self.cfg.reference_mask_aug,
                ).permute(2, 0, 1)
                rv.update({"reference_rgb": reference_image})
            else:
                random_resolution = random.choice(
                    self.cfg.reference_augment_resolutions
                )
                reference_image_ = Image.open(reference_image_path).resize(
                    (random_resolution, random_resolution)
                )
                reference_image = self.load_image(
                    reference_image_,
                    height=self.cfg.height,
                    width=self.cfg.width,
                    background_color=background_color,
                    mask_aug=self.cfg.reference_mask_aug,
                ).permute(2, 0, 1)
                rv.update({"reference_rgb": reference_image})

        return rv

    def collate(self, batch):
        batch = torch.utils.data.default_collate(batch)
        pack = lambda t: t.view(-1, *t.shape[2:])

        if self.cfg.random_view_list is not None:
            indices = random.choice(self.cfg.random_view_list)
        else:
            indices = list(range(self.cfg.num_views))
        num_views = len(indices)

        for k in batch.keys():
            if k in ["rgb", "source_rgb", "c2w"]:
                batch[k] = batch[k][:, indices]
                batch[k] = pack(batch[k])
        for k in ["prompts"]:
            if not self.cfg.return_one_prompt:
                batch[k] = [item for pair in zip(*batch[k]) for item in pair]

        batch.update(
            {
                "num_views": num_views,
                # For SDXL
                "original_size": (self.cfg.height, self.cfg.width),
                "target_size": (self.cfg.height, self.cfg.width),
                "crops_coords_top_left": (0, 0),
            }
        )
        return batch


class MultiviewDataModule(pl.LightningDataModule):
    cfg: MultiviewDataModuleConfig

    def __init__(self, cfg: Optional[Union[dict, DictConfig]] = None) -> None:
        super().__init__()
        self.cfg = parse_structured(MultiviewDataModuleConfig, cfg)

    def setup(self, stage=None) -> None:
        if stage in [None, "fit"]:
            self.train_dataset = MultiviewDataset(self.cfg, "train")
        if stage in [None, "fit", "validate"]:
            self.val_dataset = MultiviewDataset(self.cfg, "val")
        if stage in [None, "test", "predict"]:
            self.test_dataset = MultiviewDataset(self.cfg, "test")

    def prepare_data(self):
        pass

    def train_dataloader(self) -> DataLoader:
        return DataLoader(
            self.train_dataset,
            batch_size=self.cfg.batch_size,
            num_workers=self.cfg.num_workers,
            shuffle=True,
            collate_fn=self.train_dataset.collate,
        )

    def val_dataloader(self) -> DataLoader:
        return DataLoader(
            self.val_dataset,
            batch_size=self.cfg.eval_batch_size,
            num_workers=self.cfg.num_workers,
            shuffle=False,
            collate_fn=self.val_dataset.collate,
        )

    def test_dataloader(self) -> DataLoader:
        return DataLoader(
            self.test_dataset,
            batch_size=self.cfg.eval_batch_size,
            num_workers=self.cfg.num_workers,
            shuffle=False,
            collate_fn=self.test_dataset.collate,
        )

    def predict_dataloader(self) -> DataLoader:
        return self.test_dataloader()