File size: 16,936 Bytes
4c954ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
try:
    from pcache_fileio import fileio
except:
    pass

import os
import os.path as osp
import glob
import math
import copy
from skimage.io import imread
from skimage import color
import PIL
from PIL import Image
import numpy as np
import h5py
import cv2
import pickle
import torch
import torch.utils.data.dataloader as torch_loader
from torch.utils.data import Dataset
from torchvision import transforms
from pathlib import Path 
import json 

from ..config.project_config import Config as cfg
from .transforms import photometric_transforms as photoaug
from .transforms import homographic_transforms as homoaug
from .transforms.utils import random_scaling
from .synthetic_util import get_line_heatmap
from ..misc.train_utils import parse_h5_data
from ..misc.geometry_utils import warp_points, mask_points
from tqdm import tqdm

def images_collate_fn(batch):
    """ Customized collate_fn for wireframe dataset. """
    batch_keys = ["image", "junction_map", "valid_mask", "heatmap",
                  "heatmap_pos", "heatmap_neg", "homography",
                  "line_points", "line_indices"]
    list_keys = ["junctions", "line_map", "line_map_pos",
                 "line_map_neg", "file_key","fname","image_origin","uuid"]

    outputs = {}
    for data_key in batch[0].keys():
        batch_match = sum([_ == data_key for _ in batch_keys])
        list_match = sum([_ == data_key for _ in list_keys])
        # print(batch_match, list_match)
        if batch_match > 0 and list_match == 0:
            outputs[data_key] = torch_loader.default_collate(
                [b[data_key] for b in batch])
        elif batch_match == 0 and list_match > 0:
            outputs[data_key] = [b[data_key] for b in batch]
        elif batch_match == 0 and list_match == 0:
            continue
        else:
            raise ValueError(
        "[Error] A key matches batch keys and list keys simultaneously.")

    return outputs

class ImageCollections(Dataset):
    def __init__(self, mode, config, homoadp=False,homoadp_resume=False):
        super(ImageCollections, self).__init__()
        if config is None:
            self.config = self.get_default_config()
        else:
            self.config = config
        h5path = config.get('gt_source_train',None)
        self.json_list = None
        self.homoadp = homoadp
        self.homoadp_resume = homoadp_resume

        if self.config['img_reg_exp'] == 'all':
            self.config['img_reg_exp'] = []
            for i in range(998):
                self.config['img_reg_exp'].append(f'sa_{i:06d}/images/*.jpg')

        if h5path is not None and h5path.endswith('.h5'):
            self.h5path = osp.join(cfg.EXPORT_ROOT,'export_datasets',h5path)
            with h5py.File(self.h5path,'r') as f:
                self.filenames = [k.decode('UTF-8') for k in f['filenames']]
            self.filenames = [osp.join(cfg.EXPORT_ROOT,f) for f in self.filenames]
        elif h5path is not None and h5path.endswith('.jsons'):
            self.use_json = True
            self.h5path = osp.join(cfg.EXPORT_ROOT,'export_datasets',h5path)
            #json_list = glob.glob(self.h5path+'/*.json')
            json_list = []
            for exp in tqdm(self.config['img_reg_exp']):
                _json_regexp = Path(exp).with_suffix('.json')
                _jsons = glob.glob(osp.join(self.h5path,str(_json_regexp)))
                json_list.extend(_jsons)
            
            if cfg.DATASET_ROOT.startswith('pcache'):
                
                if osp.isfile(Path(h5path).with_suffix('.pcache')) and (self.homoadp_resume or not self.homoadp):
                    with open(Path(h5path).with_suffix('.pcache'),'r') as _f:
                        filenames = _f.readlines()
                    filenames = [ x.rstrip('\n') for x in filenames ]
                else:
                    self.folder_regexp = []
                    filenames = []
                    print('Loading from pcache......')
                    for exp in tqdm(self.config['img_reg_exp']):
                        _path = Path(osp.join(self.config['dataset_root'][0],exp))
                        _p = osp.join(cfg.DATASET_ROOT,str(_path.parent))

                        _e = _path.suffix
                        _list = [osp.basename(_) for _ in os.listdir(_p) if _.endswith(_e)]
                        _list = [osp.join(_p,_) for _ in _list]
                        filenames.extend(_list)
                    
                    with open(Path(h5path).with_suffix('.pcache'),'w') as _f:
                        _f.writelines('\n'.join(filenames))
            else:
                self.folder_regexp = [osp.join(cfg.DATASET_ROOT,self.config['dataset_root'][0],exp) for exp in self.config['img_reg_exp']]
                filenames = sum([glob.glob(exp) for exp in self.folder_regexp],[])
                filenames = [Path(f) for f in filenames]
            
            self.dataset_root = osp.join(cfg.DATASET_ROOT,self.config['dataset_root'][0])
            filedict = {str(Path(osp.relpath(f,self.dataset_root)).with_suffix('')): f for f in filenames}
            
            jsondict = {str(Path(osp.relpath(j,h5path)).with_suffix('')): j for j in json_list}
            self.filenames = []
            self.json_list = []
            
            if self.homoadp:
                for k in filedict.keys():
                    if k in jsondict and self.homoadp_resume:
                        continue
                    else:
                        self.filenames.append(str(filedict[k]))
                self.h5path = None
                self.use_json = False
                print(f"Found {len(json_list)} json files from the folder")
                print(f"Total images are reduced from {len(filenames)} to {len(self.filenames)}")
            else:
                for k in filedict.keys():
                    if k in jsondict:
                        self.filenames.append(str(filedict[k]))
                        self.json_list.append(str(jsondict[k]))
        else:
            self.folder_regexp = [osp.join(cfg.DATASET_ROOT,self.config['dataset_root'][0],exp) for exp in self.config['img_reg_exp']]
            self.filenames = sum([glob.glob(exp) for exp in self.folder_regexp],[])
            self.h5path = None
        
        self.default_config = self.get_default_config()

        self.dataset_name = self.config['alias']

        self.size = self.config['preprocessing']['resize']
        
        print("Found %d images in %s" % (len(self),self.config['dataset_root']))
        
        self.num_pad = int(math.ceil(math.log10(len(self))))+1 if len(self)>0 else 0
        
    def __len__(self):
        return len(self.filenames)

    def get_padded_filename(self, num_pad, idx):
        file_len = len("%d" % (idx))
        filename = "0" * (num_pad - file_len) + "%d" % (idx)
        return filename

    def train_preprocessing(self, data, numpy=False):
        """ Train preprocessing for the dataset. """
        image = data['image']
        junctions = data.get('junctions',None)
        image_size = image.shape[:2]
        if not(list(image_size) == self.config['preprocessing']['resize']):
            size_old = list(image.shape)[:2]

            image = cv2.resize(image, tuple(self.config['preprocessing']['resize'][::-1]), interpolation=cv2.INTER_LINEAR)

            scales = (image.shape[0] / size_old[0], image.shape[1] / size_old[1])
            
            if junctions is not None:
                junctions *= torch.tensor(scales).reshape(1, 2)
        
        if self.config['augmentation']['photometric']['enable']:
            photo_trans_list = self.get_photo_transform()
            ### Apply photometric transforms
            np.random.shuffle(photo_trans_list)
            image_transform = transforms.Compose(photo_trans_list + [photoaug.normalize_image()])
        else:
            image_transform = photoaug.normalize_image()

        image = image_transform(image)

        if self.config['augmentation']['homographic']['enable']:
            homo_trans = self.get_homo_transform()
            outputs = homo_trans(image, junctions, data['line_map'])
            junctions = outputs["junctions"]
            image = outputs["warped_image"]
            line_map = outputs['line_map']
            data['line_map'] = torch.tensor(line_map)
            data['valid_mask'] = outputs['valid_mask']

        data['image'] = torch.from_numpy(image)[None]
        if junctions is not None:
            data['junctions'] = torch.from_numpy(junctions).float()
        
        
        return data

    def get_homo_transform(self):
        """ Get homographic transforms (according to the config). """
        # Get homographic transforms for image
        homo_config = self.config["augmentation"]["homographic"]["params"]
        if not self.config["augmentation"]["homographic"]["enable"]:
            raise ValueError(
        "[Error] Homographic augmentation is not enabled.")

        # Parse the homographic transforms
        image_shape = self.config["preprocessing"]["resize"]

        # Compute the min_label_len from config
        try:
            min_label_tmp = self.config["generation"]["min_label_len"]
        except:
            min_label_tmp = None
        
        # float label len => fraction
        if isinstance(min_label_tmp, float): # Skip if not provided
            min_label_len = min_label_tmp * min(image_shape)
        # int label len => length in pixel
        elif isinstance(min_label_tmp, int):
            scale_ratio = (self.config["preprocessing"]["resize"]
                           / self.config["generation"]["image_size"][0])
            min_label_len = (self.config["generation"]["min_label_len"]
                             * scale_ratio)
        # if none => no restriction
        else:
            min_label_len = 0
        
        # Initialize the transform
        homographic_trans = homoaug.homography_transform(
            image_shape, homo_config, 0, min_label_len)

        return homographic_trans

    def get_photo_transform(self):
        """ Get list of photometric transforms (according to the config). """
        # Get the photometric transform config
        photo_config = self.config["augmentation"]["photometric"]
        if not photo_config["enable"]:
            raise ValueError(
        "[Error] Photometric augmentation is not enabled.")
        
        # Parse photometric transforms
        trans_lst = self.parse_transforms(photo_config["primitives"],
                                          photoaug.available_augmentations)
        trans_config_lst = [photo_config["params"].get(p, {})
                            for p in trans_lst]

        # List of photometric augmentation
        photometric_trans_lst = [
            getattr(photoaug, trans)(**conf) \
            for (trans, conf) in zip(trans_lst, trans_config_lst)
        ]

        return photometric_trans_lst

    def parse_transforms(self, names, all_transforms):
        """ Parse the transform. """
        trans = all_transforms if (names == 'all') \
            else (names if isinstance(names, list) else [names])
        assert set(trans) <= set(all_transforms)
        return trans

    def check_files(self):
        h5path = self.config.get('gt_source_train',None)
        valid_filenames = []
        for filename in self.filenames:
            try:
                image_origin = np.array(PIL.Image.open(filename))
                valid_filenames.append(filename)
            except IOError:
                print(f"Unable to load image from path: {filename}")

        new_pcache_path = Path(h5path).with_name(f"{Path(h5path).stem}_filtered.pcache")
        with open(new_pcache_path, 'w') as _f:
            for filename in valid_filenames:
                _f.write(f"{filename}\n")

    def check_health(self):
        is_healthy = True
        image_fail_list = []
        json_fail_list = []
        for idx in tqdm(range(len(self))):
            #try:
            #    image_origin = np.array(PIL.Image.open(self.filenames[idx]))
            #except:
            #    is_healthy = False
            #    print(f'The image {self.filenames[idx]} is broken.')
            #    image_fail_list.append(self.filenames[idx])

            if self.h5path is not None and self.json_list is not None:
                try:
                    with open(self.json_list[idx],'r') as f:
                        data = json.load(f)
                except:
                    is_healthy = False
                    print(f'The image {self.filenames[idx]} is broken.')
                    json_fail_list.append(self.json_list[idx])
        return {
            'images': image_fail_list,
            'jsons': json_fail_list,
            'status': is_healthy
        }

    def __getitem__(self, idx):
        fname = osp.basename(self.filenames[idx])
        #image_origin = cv2.imread(self.filenames[idx])
        try:
            image_origin = np.array(PIL.Image.open(self.filenames[idx]))
        except:
            image_origin = np.array(PIL.Image.open('hawp/ssl/config/exports/sa1b/00030043_0.png')) # deal with the failed case

        if self.config['gray_scale']:
            image = cv2.cvtColor(image_origin, cv2.COLOR_BGR2GRAY)
        else:
            image = cv2.cvtColor(image_origin, cv2.COLOR_BGR2RGB)
        
        # image = np.array(image,dtype=np.float32)/255.0
        
        data = {
            'fname': self.filenames[idx],
            'image': image,
            # 'image': torch.from_numpy(image)[None],
            # 'valid_mask': torch.ones(self.size,dtype=torch.float32)[None],
            'image_origin': image_origin,
        }
        data['uuid'] = osp.relpath(self.filenames[idx],self.dataset_root)
        
        if self.h5path is not None and self.json_list is None:
            with h5py.File(self.h5path,'r') as f:
                gt_key = self.get_padded_filename(self.num_pad,idx)
                exported_label = parse_h5_data(f[gt_key])
            junctions = torch.tensor(exported_label['junctions']).float()
            edges = torch.tensor(exported_label['edges']).long()
            lines = junctions[edges]
            junctions_valid = torch.zeros(len(junctions),dtype=torch.bool)
            junctions_valid[edges.unique()] = 1
            junctions_idx = -torch.ones(len(junctions),dtype=torch.long)
            junctions_idx[junctions_valid] = torch.arange(junctions_valid.sum())
            edges_remapped = junctions_idx[edges]
            junctions = junctions[junctions_valid]
            lines_remapped = junctions[edges_remapped]
            line_map = torch.zeros(junctions.shape[0],junctions.shape[0],dtype=torch.float32)
            if len(edges_remapped) > 0:
                line_map[edges_remapped[:,0],edges_remapped[:,1]] = 1
                line_map[edges_remapped[:,1],edges_remapped[:,0]] = 1

            data['line_map'] = line_map
            data['junctions'] = junctions[:,[1,0]]
        elif self.h5path is not None and self.json_list is not None:
            with open(self.json_list[idx],'r') as f:
                json_data = json.load(f)
            junctions = torch.tensor(json_data['junctions']).float()
            if junctions.shape[0] == 0:
                junctions = torch.zeros((1,2)).float()
            edges = torch.tensor(json_data['edges']).long()
            lines = junctions[edges]
            junctions_valid = torch.zeros(len(junctions),dtype=torch.bool)
            junctions_valid[edges.unique()] = 1
            junctions_idx = -torch.ones(len(junctions),dtype=torch.long)
            junctions_idx[junctions_valid] = torch.arange(junctions_valid.sum())
            edges_remapped = junctions_idx[edges]
            junctions = junctions[junctions_valid]
            lines_remapped = junctions[edges_remapped]
            line_map = torch.zeros(junctions.shape[0],junctions.shape[0],dtype=torch.float32)
            if len(edges_remapped) > 0:
                line_map[edges_remapped[:,0],edges_remapped[:,1]] = 1
                line_map[edges_remapped[:,1],edges_remapped[:,0]] = 1

            data['line_map'] = line_map
            data['junctions'] = junctions[:,[1,0]]
        else:
            data['valid_mask'] = torch.ones(self.size,dtype=torch.float32)[None]
            
        return self.train_preprocessing(data)
        return data # TODO: remove this line
        
    def get_default_config(self):
        return {
            "dataset_name": "images",
            "add_augmentation_to_all_splits": False,
            "preprocessing": {
                "resize": [512,512],
                "blur_size": 11,
            },
            "augmentation": {
                "photometric": {
                    "enable": False
                },
                "homographic": {
                    "enable": False
                }
            }
        }