File size: 7,676 Bytes
a1d8bef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import numpy as np
import webdataset as wds
import pytorch_lightning as pl
import torch
from torch.utils.data import Dataset
from torch.utils.data.distributed import DistributedSampler
from PIL import Image
from pathlib import Path
from tqdm import tqdm
from src.utils.train_util import instantiate_from_config


class DataModuleFromConfig(pl.LightningDataModule):
    def __init__(
        self, 
        batch_size=8, 
        num_workers=4, 
        train=None, 
        validation=None, 
        test=None, 
        **kwargs,
    ):
        super().__init__()

        self.batch_size = batch_size
        self.num_workers = num_workers

        self.dataset_configs = dict()
        if train is not None:
            self.dataset_configs['train'] = train
        if validation is not None:
            self.dataset_configs['validation'] = validation
        if test is not None:
            self.dataset_configs['test'] = test
    
    def setup(self, stage):

        if stage in ['fit']:
            self.datasets = dict((k, instantiate_from_config(self.dataset_configs[k])) for k in self.dataset_configs)
        else:
            raise NotImplementedError

    def train_dataloader(self):

        # sampler = DistributedSampler(self.datasets['train'])
        return wds.WebLoader(self.datasets['train'], batch_size=self.batch_size, num_workers=self.num_workers, shuffle=True)

    def val_dataloader(self):

        # sampler = DistributedSampler(self.datasets['validation'])
        return wds.WebLoader(self.datasets['validation'], batch_size=4, num_workers=self.num_workers, shuffle=False)

    def test_dataloader(self):

        return wds.WebLoader(self.datasets['validation'], batch_size=self.batch_size, num_workers=self.num_workers, shuffle=False)



class RefinementData(Dataset):
    lights_to_caption = {
        0 : "Morning light",
        1 : "Midday light, clear sky",
        2 : "Afternoon light, cloudy sky",
    }
    def __init__(self,
                 root_dir='refinement_dataset/',
                 gt_subpath='gt',
                 pred_subpath='shap_e',
                 validation=False,
                 overfit=False,
                 caption_path=None,
                 split_path=None,
                 single_view=False,
                 single_light=False,
                 ) -> None:
        self.root_dir = Path(root_dir)
        self.gt_subpath = gt_subpath
        self.pred_subpath = pred_subpath
        self.single_view = single_view
        self.single_light = single_light
        if caption_path is not None:
            caption_path = self.root_dir / caption_path
            with open(caption_path) as f:
                self.captions_dict = json.load(f)
        split_json = self.root_dir / split_path
        with open(split_json) as f:
            split_dict = json.load(f)
        # print(split_dict.keys
        # exit(0)
        if validation:
            uuids = split_dict['val']
        else:
            uuids = split_dict['train']
        self.paths = [self.root_dir / uuid for uuid in uuids]
        
        print('============= length of dataset %d =============' % len(self.paths))
        
    def __len__(self):
        return len(self.paths)
    
    def load_im(self, path, color):
        pil_img = Image.open(path)

        image = np.asarray(pil_img, dtype=np.float32) / 255.
        if image.shape[2] == 4:
            alpha = image[:, :, 3:]
            image = image[:, :, :3] * alpha + color * (1 - alpha)
        else:
            alpha = np.ones_like(image[:, :, :1])
        image = torch.from_numpy(image).permute(2, 0, 1).contiguous().float()
        alpha = torch.from_numpy(alpha).permute(2, 0, 1).contiguous().float()
        return image, alpha
    
    def __getitem__(self, index):
        if os.path.exists(self.paths[index] / 'lights.json'):
            num_lights = 3
        else:
            num_lights = len(os.listdir(self.paths[index] / self.gt_subpath))
        if self.single_view:
            view_index = np.random.randint(0, 6)
        if self.single_light:
            light_index = 0
        else:
            light_index = np.random.randint(0, num_lights)
        # print("light index", light_index)
        # exit(0)
        uuid = self.paths[index].name
        caption = self.captions_dict[uuid]
        # if "lights.json" in os.listdir(self.paths[index]) and num_lights == 3: # according to additions to the dataset
            # caption += " " + self.lights_to_caption[light_index]
        image_path_gt = os.path.join(self.paths[index],'gt',str(light_index), "latent.pt")
        image_path_pred = os.path.join(self.paths[index],'shap_e', "latent.pt")
        '''background color, default: white'''
        try:
            imgs_gt = torch.load(image_path_gt,map_location='cpu').squeeze()
            imgs_pred = torch.load(image_path_pred,map_location='cpu').squeeze()
        except Exception as e:
            print("Error loading latent tensors, gt path %s, pred path %s" % (image_path_gt, image_path_pred))
            raise e
        if self.single_view:
            row = view_index // 2
            col = view_index % 2
            imgs_gt = imgs_gt[:, row*40:(row+1)*40, col*40:(col+1)*40]
            imgs_pred = imgs_pred[:, row*40:(row+1)*40, col*40:(col+1)*40]
        # imgs_gt = imgs_gt
        data = {
            'refined_imgs': imgs_gt,        # (6, 3, H, W)
            'unrefined_imgs': imgs_pred,        # (6, 3, H, W)
            'caption': caption,
            'index': index
        }
        return data

    
class ObjaverseData(Dataset):
    def __init__(self,
        root_dir='objaverse/',
        meta_fname='valid_paths.json',
        image_dir='rendering_zero123plus',
        validation=False,
    ):
        self.root_dir = Path(root_dir)
        self.image_dir = image_dir

        with open(os.path.join(root_dir, meta_fname)) as f:
            lvis_dict = json.load(f)
        paths = []
        for k in lvis_dict.keys():
            paths.extend(lvis_dict[k])
        self.paths = paths
            
        total_objects = len(self.paths)
        if validation:
            self.paths = self.paths[-16:] # used last 16 as validation
        else:
            self.paths = self.paths[:-16]
      
        print('============= length of dataset %d =============' % len(self.paths))

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

    def load_im(self, path, color):
        pil_img = Image.open(path)

        image = np.asarray(pil_img, dtype=np.float32) / 255.
        alpha = image[:, :, 3:]
        image = image[:, :, :3] * alpha + color * (1 - alpha)

        image = torch.from_numpy(image).permute(2, 0, 1).contiguous().float()
        alpha = torch.from_numpy(alpha).permute(2, 0, 1).contiguous().float()
        return image, alpha
    
    def __getitem__(self, index):
        while True:
            image_path = os.path.join(self.root_dir, self.image_dir, self.paths[index])

            '''background color, default: white'''
            bkg_color = [1., 1., 1.]

            img_list = []
            try:
                for idx in range(7):
                    img, alpha = self.load_im(os.path.join(image_path, '%03d.png' % idx), bkg_color)
                    img_list.append(img)

            except Exception as e:
                print(e)
                index = np.random.randint(0, len(self.paths))
                continue

            break
        
        imgs = torch.stack(img_list, dim=0).float()

        data = {
            'cond_imgs': imgs[0],           # (3, H, W)
            'target_imgs': imgs[1:],        # (6, 3, H, W)
        }
        return data