-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiwarp_dataloader.py
More file actions
877 lines (759 loc) · 38 KB
/
multiwarp_dataloader.py
File metadata and controls
877 lines (759 loc) · 38 KB
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
import torch.utils.data as data
import numpy as np
import random
import torch
import os
from epimodule import load_multiplane_focalstack
from epimodule import load_tiled_epi_vertical, load_tiled_epi_horizontal, load_tiled_epi_full
from epimodule import load_stacked_epi, load_stacked_epi_no_repeats
from epimodule import load_lightfield, load_relative_pose
from epimodule import DEFAULT_PATCH_INTRINSICS
class MetaData:
"""
Storage class for metadata that might be needed during evaluation
"""
def __init__(self, cameras, tgt_name, ref_names, gray, flipped):
"""
Initializes a dictionary of metadata.
:param cameras: List of camera indices
:type cameras: list
:param tgt_name: Filename of target image
:type tgt_name: str
:param ref_names: List of file names of reference images
:type ref_names: list
:param gray: Boolean indicating grayscale or not
:type gray: bool
:param flipped: Boolean indicating if the image is flipped or not
:type flipped: bool
"""
self.metadata = {
"cameras": cameras, # List of camera indices
"tgt_name": tgt_name, # Filename
"ref_names": ref_names, # Filenames
"gray": gray, # Is grayscale
"flipped": flipped, # Is flipped
}
def get_as_dict(self):
"""
Returns the metadata as a dictionary.
:return: Dictionary of metadata
:rtype: dict
"""
return self.metadata
class TrainingData:
"""
Storage class for data that is needed during training.
The training routine expects a dict with the following fields.
This class ensures a consistent access API for different data loading modules.
The __getitem__ method of any dataset class should create one of these, and
return the dictionary obtained from TrainingData.get_as_dict()
"""
def __init__(self, tgt, tgt_formatted, ref, ref_formatted, intrinsics, poses_gt_refs_tgt, metadata):
"""
Initializer
:param tgt: List of tgt frame lightfield sub-apertures for multi-warp photometric loss
:type tgt: tensor
:param tgt_formatted: The encoded target lightfield, to be input to the disp and pose networks
:type tgt_formatted: tensor
:param ref: List of list of ref frame lightfield sub-apertures for multi-warp photometric loss
:type ref: tensor
:param ref_formatted: List of encoded reference lightfields, to be input to the disp and pose networks
:type ref_formatted: list
:param intrinsics: Camera intrinsics matrix K
:type intrinsics: numpy.array
:param poses_gt_refs_tgt: Ground truth poses of the target frame wrt the reference frames - [refs x 4x4 matrix]
:type poses_gt_refs_tgt: tensor
:param metadata: Metadata. Not used in training but only in evaluation.
:type metadata: MetaData
"""
self.training_data = {
"tgt_lf": tgt, # list of tgt frame lightfield sub-apertures for multi-warp photometric loss
"ref_lfs": ref, # list of list of ref frame lightfield sub-apertures for multi-warp photometric loss
"tgt_lf_formatted": tgt_formatted, # The encoded tgt lightfield image
"ref_lfs_formatted": ref_formatted, # The list of encoded ref lightfield images
"poses_gt_refs_tgt": poses_gt_refs_tgt, # Ground truth poses of the ref frames in the tgt frame
"metadata": metadata.get_as_dict(), # Metadata (not used for training but for eval)
"intrinsics": intrinsics, # Intrinsics K
"intrinsics_inv": np.linalg.inv(intrinsics) # Intrinsics^-1 TODO: why pass this?
}
def get_as_dict(self):
"""
Returns the training data as a dictionary
:return: The dictionary of training data
:rtype: dict
"""
return self.training_data
class TrainingDataEpi:
"""
Storage class for data that is needed during training.
The training routine expects a dict with the following fields.
This class ensures a consistent access API for different data loading modules.
The __getitem__ method of any dataset class should create one of these, and
return the dictionary obtained from TrainingData.get_as_dict()
"""
def __init__(self, tgt, tgt_formatted_h, tgt_formatted_v, ref, ref_formatted_h, ref_formatted_v,
tgt_stack, ref_stacks, intrinsics, poses_gt_refs_tgt, metadata):
"""
Initializer
:param tgt: List of tgt frame lightfield sub-apertures for multi-warp photometric loss
:type tgt: tensor
:param tgt_formatted_h: The encoded tgt lightfield image for horizontal sub-apertures
:type tgt_formatted_h: tensor
:param tgt_formatted_v: The encoded tgt lightfield image for vertical sub-apertures
:type tgt_formatted_v: tensor
:param ref: List of list of ref frame lightfield sub-apertures for multi-warp photometric loss
:type ref: tensor
:param ref_formatted_h: List of encoded ref lightfield images of horizontal sub-apertures
:type ref_formatted_h: tensor
:param ref_formatted_v: List of encoded ref lightfield images of vertical sub-apertures
:type ref_formatted_v: tensor
:param tgt_stack: List of tgt lightfield sub-apertures for sending as stack to encoders
:type tgt_stack: tensor
:param ref_stacks: List of list of ref lightfield sub-apertures for sending as stack to encoders
:type ref_stacks: tensor
:param intrinsics: Camera intrinsics matrix K
:type intrinsics: ndarray
:param poses_gt_refs_tgt: Ground truth poses of the target frame wrt the reference frames - [refs x 4x4 matrix]
:type poses_gt_refs_tgt: tensor
:param metadata: Metadata. Not used in training but only in evaluation.
:type metadata: MetaData
"""
self.training_data = {
"tgt_lf": tgt, # list of tgt frame lightfield sub-apertures for multi-warp photometric loss
"ref_lfs": ref, # list of list of ref frame lightfield sub-apertures for multi-warp photometric loss
"tgt_lf_formatted_h": tgt_formatted_h, # The encoded tgt lightfield image for horizontal sub-apertures
"tgt_lf_formatted_v": tgt_formatted_v, # The encoded tgt lightfield image for vertical sub-apertures
"ref_lfs_formatted_h": ref_formatted_h, # List of encoded ref lightfield images of horizontal sub-apertures
"ref_lfs_formatted_v": ref_formatted_v, # List of encoded ref lightfield images of vertical sub-apertures
"tgt_stack": tgt_stack, # list of tgt lightfield sub-apertures for sending as stack to encoders
"ref_stacks": ref_stacks, # list of list of ref lightfield sub-apertures for sending as stack to encoders
"poses_gt_refs_tgt": poses_gt_refs_tgt, # List of ground truth poses of the tgt frame wrt ref frames
"metadata": metadata.get_as_dict(), # Metadata (not used for training but for eval)
"intrinsics": intrinsics, # Intrinsics K
"intrinsics_inv": np.linalg.inv(intrinsics) # Intrinsics^-1 TODO: why pass this?
}
def get_as_dict(self):
"""
Returns the training data as a dictionary
:return: The dictionary of training data
:rtype: dict
"""
return self.training_data
class BaseDataset(data.Dataset):
"""
Base class for loading epi-module data-sets. Takes care of crawling the root directory and storing some common
configuration parameters.
"""
def __init__(self, root, cameras, gray, seed, train, sequence_length, transform, shuffle, sequence):
"""
Initializer
:param root: Path to the folder where the data is present.
This folder should have subfolders seq#, train.txt, val.txt
:type root: str
:param cameras: List of camera indices to use for computing the multi-warp photometric error
:type cameras: list
:param gray: Boolean indicating if the images have to be converted to grayscale
:type gray: bool
:param seed: Seed for random number generator
:type seed: int
:param train: Boolean indicating if the data to be loaded is the training or the validation set. Default is True
:type train: bool
:param sequence_length: Number of images in the sequence. Default 3
:type sequence_length: int
:param transform: Transform that has to be applied to the images. Default is None
:type transform: tensor
:param shuffle: Boolean indicating if the data has to be shuffled. Default is True for training.
:type shuffle: bool
:param sequence: Sequence index that has to be loaded, if required. Default is None to load all data.
:type sequence: int
"""
np.random.seed(seed)
random.seed(seed)
self.samples = None # the data samples
self.cameras = cameras # indices of cameras to use for computing the multi-warp photometric error
self.gray = gray # boolean indicating if the image should be converted to grayscale
self.root = root # root directory where the data is present
self.shuffle = shuffle # boolean indicating if data has to be shuffled
self.transform = transform # transform to apply on the images
# number of images in a sequence (reference images + target image),
# if sequencce_length = 2 then reference image index is target-1
self.sequence_length = sequence_length
scene_list_path = self.root + '/train.txt' if train else self.root + '/val.txt'
# Hardcoding to load image from central camera (8)
if sequence is not None:
# choose the particular scene (called seq# in the folder)
self.scenes = [self.root + "/seq" + str(sequence) + '/8']
else:
# get all the scenes (called seq# in the folder) from the the list of scenes.
# Note: Need to strip newline first
self.scenes = [self.root + "/" + folder[:].rstrip() + '/8' for folder in open(scene_list_path)]
self.crawl_folders()
def crawl_folders(self):
"""
Crawls through the folders that represent a scene and creates a list of images to load.
This is stored in the member variable self.samples and consists of the following
the intrinsics matrix,
target image = image at index i
reference images = list of images at indices i-demi_length to i+demi_length except image at i
If sequence_length == 2 then reference images = image at i-1
"""
sequence_set = []
if self.sequence_length == 2:
demi_length = 1
shifts = [-1]
else:
demi_length = (self.sequence_length - 1) // 2 # floor( (sequence_length-1) /2)
shifts = list(range(-demi_length, demi_length + 1)) # create list [-d, -(d-1), ..-1, 0, 1, ..., d-1, d]
shifts.pop(demi_length) # remove element 0 from the list
for scene in self.scenes:
intrinsics = DEFAULT_PATCH_INTRINSICS
# load image files - replaced the use of module path.files with os.walk
# imgs = sorted(scene.files('*.png'))
imgs = []
for _, _, files in os.walk(scene):
for img_file in files:
if img_file.endswith(".png"):
imgs.append(os.path.join(scene, img_file))
imgs.sort()
if len(imgs) < self.sequence_length:
continue
# form a sequence set. It contains the following
# intrinsics matrix,
# target image = image at index i
# reference images = list of images at indices i-demi_length to i+demi_length except image at i
# If sequence_length == 2 then reference images = image at i-1
for i in range(demi_length, len(imgs) - demi_length):
sample = {'intrinsics': intrinsics, 'tgt': imgs[i], 'ref_imgs': []}
for j in shifts:
sample['ref_imgs'].append(imgs[i + j])
sequence_set.append(sample)
if self.shuffle:
random.shuffle(sequence_set)
self.samples = sequence_set
def __getitem__(self, index):
"""
Returns the element at the desired index from the dataset.
This function has to be overwritten by children classes that inherit this class.
:param index: index of the element of the dataset
:type index: int
:return: Error
:rtype: BaseException
"""
raise NotImplementedError
def __len__(self):
"""
Returns the number of samples in the dataset.
:return: Number of samples in the dataset
:rtype: int
"""
return len(self.samples)
class FocalstackLoader(BaseDataset):
"""
Class for loading lightfield images as a focal stack
"""
def __init__(self, root, cameras, fs_num_cameras, fs_num_planes, gray=False, seed=None, train=True,
sequence_length=3, transform=None, shuffle=True, sequence=None, no_pose=False):
"""
Initializer
:param root: Path to the folder where the data is present.
This folder should have subfolders seq#, train.txt, val.txt
:type root: str
:param cameras: List of camera indices to use for computing the multi-warp photometric error.
:type cameras: list
:param fs_num_cameras: Number of cameras to use to form the focal stack encoding
:type fs_num_cameras: int
:param fs_num_planes: Number of planes in the multi-plane focal stack
:type fs_num_planes: int
:param gray: Boolean indicating if the images have to be converted to grayscale
:type gray: bool
:param seed: Seed for random number generator
:type seed: int
:param train: Boolean indicating if the data to be loaded is the training or the validation set. Default is True
:type train: bool
:param sequence_length: Number of images in the sequence. Default 3
:type sequence_length: int
:param transform: Transform that has to be applied to the images. Default is None
:type transform: tensor
:param shuffle: Boolean indicating if the data has to be shuffled. Default is True for training.
:type shuffle: bool
:param sequence: Sequence index that has to be loaded, if required. Default is None to load all data.
:type sequence: int
"""
super(FocalstackLoader, self).__init__(root, cameras, gray, seed, train, sequence_length,
transform, shuffle, sequence)
self.num_fs_cameras = fs_num_cameras
self.num_planes = fs_num_planes
self.no_pose = no_pose
def __getitem__(self, index):
"""
Returns the element at the desired index from the dataset.
:param index: Index of the element of the dataset
:type index: int
:return: Dictionary of training data
:rtype: dict
"""
# get the data sample at the specified index
sample = self.samples[index]
# load target and reference images from teh chosen cameras. This is used only to compute the multi-warp
# photometric loss. If self.cameras is [8] then single-warp photometric loss is computed.
tgt_lf = load_lightfield(sample['tgt'], self.cameras, self.gray)
ref_lfs = [load_lightfield(ref_img, self.cameras, self.gray) for ref_img in sample['ref_imgs']]
# load target and reference lightfields as a focal stack
tgt_focalstack = load_multiplane_focalstack(sample['tgt'],
num_planes=self.num_planes,
num_cameras=self.num_fs_cameras,
gray=self.gray)
ref_focalstacks = [load_multiplane_focalstack(ref_img,
num_planes=self.num_planes,
num_cameras=self.num_fs_cameras,
gray=self.gray) for ref_img in sample['ref_imgs']]
# load ground truth pose of target frame wrt reference frames - [refs x 4x4 matrix]
poses_gt_refs_tgt = torch.Tensor([load_relative_pose(ref, sample['tgt'], self.no_pose)
for ref in sample['ref_imgs']])
# intrinsics = np.copy(sample['intrinsics'])
# apply transforms if present
if self.transform is not None:
tgt_lf, _ = self.transform(tgt_lf, np.zeros((3, 3)))
ref_lfs = [self.transform(ref, np.zeros((3, 3)))[0] for ref in ref_lfs]
tgt_focalstack, _ = self.transform(tgt_focalstack, np.zeros((3, 3)))
ref_focalstacks = [self.transform(ref, np.zeros((3, 3)))[0] for ref in ref_focalstacks]
# concatenate light fields on colour channel
tgt_lf = torch.cat(tuple(tgt_lf), 0)
ref_lfs = [torch.cat(ref, 0) for ref in ref_lfs]
# concatenate multiple focal plane images on colour channel
tgt_focalstack = torch.cat(tgt_focalstack, 0)
ref_focalstacks = [torch.cat(ref, 0) for ref in ref_focalstacks]
# Create metadata and training data
metadata = MetaData(self.cameras, sample['tgt'], sample['ref_imgs'], self.gray, False)
trainingdata = TrainingData(tgt_lf, tgt_focalstack,
ref_lfs, ref_focalstacks,
sample['intrinsics'], poses_gt_refs_tgt, metadata)
return trainingdata.get_as_dict()
class StackedLFLoader(BaseDataset):
"""
Class for loading lightfield images
"""
def __init__(self, root, cameras, gray=False, seed=None, train=True, sequence_length=3,
transform=None, shuffle=True, sequence=None, cameras_stacked='input',
no_pose=False, is_monocular=False):
"""
Initlializer
:param root: Path to the folder where the data is present.
This folder should have subfolders seq#, train.txt, val.txt
:type root: str
:param cameras: List of camera indices to use for computing the multi-warp photometric error.
:type cameras: list
:param gray: Boolean indicating if the images have to be converted to grayscale
:type gray: bool
:param seed: Seed for random number generator
:type seed: int
:param train: Boolean indicating if the data to be loaded is the training or the validation set. Default is True
:type train: bool
:param sequence_length: Number of images in the sequence. Default 3
:type sequence_length: int
:param transform: Transform that has to be applied to the images. Default is None
:type transform: tensor
:param shuffle: Boolean indicating if the data has to be shuffled. Default is True for training.
:type shuffle: bool
:param sequence: Sequence index that has to be loaded, if required. Default is None to load all data.
:type sequence: int
:param cameras_stacked: Either 'input' or 'full'. If 'input', then the list @cameras is used to generate the
stack. If 'full' then all the 17 cameras are used to generate the stack.
:type cameras_stacked: str
"""
super(StackedLFLoader, self).__init__(root, cameras, gray, seed, train, sequence_length,
transform, shuffle, sequence)
assert cameras_stacked in ['input', 'full']
self.cameras_stacked = cameras_stacked
if is_monocular:
self.cameras_stacked_indices = [8]
else:
self.cameras_stacked_indices = [3, 8, 13, 7, 9]
self.no_pose = no_pose
def __getitem__(self, index):
"""
Returns the element at the desired index from the dataset.
:param index: Index of the element of the dataset
:type index: int
:return: Dictionary of training data
:rtype: dict
"""
# get the data sample at the specified index
sample = self.samples[index]
# load target and reference lightfield images images
# --> list of images that form the target lightfield image
tgt_lf = load_lightfield(sample['tgt'], self.cameras, self.gray)
# --> list of (list of images that form each of the reference lightfield images)
ref_lfs = [load_lightfield(ref_img, self.cameras, self.gray) for ref_img in sample['ref_imgs']]
tgt_stack = None
ref_stacks = None
if self.cameras_stacked == 'full':
# load target and reference lightfields from all the cameras as a stack
tgt_stack = load_stacked_epi_no_repeats(sample['tgt'], same_parallax=False)
ref_stacks = [load_stacked_epi_no_repeats(ref_img, same_parallax=False) for ref_img in sample['ref_imgs']]
elif self.cameras_stacked == 'input':
tgt_stack = load_lightfield(sample['tgt'], self.cameras_stacked_indices, self.gray)
ref_stacks = [load_lightfield(ref_img, self.cameras_stacked_indices, self.gray)
for ref_img in sample['ref_imgs']]
# load ground truth pose of target frame wrt reference frames - [refs x 4x4 matrix]
poses_gt_refs_tgt = torch.Tensor([load_relative_pose(ref, sample['tgt'], self.no_pose)
for ref in sample['ref_imgs']])
# intrinsics = np.copy(sample['intrinsics'])
# apply transforms if present
if self.transform is not None:
tgt_lf, _ = self.transform(tgt_lf, np.zeros((3, 3)))
ref_lfs = [self.transform(ref, np.zeros((3, 3)))[0] for ref in ref_lfs]
tgt_stack, _ = self.transform(tgt_stack, np.zeros((3, 3)))
ref_stacks = [self.transform(ref, np.zeros((3, 3)))[0] for ref in ref_stacks]
# concatenate light fields on colour channel
tgt_lf = torch.cat(tuple(tgt_lf), 0)
ref_lfs = [torch.cat(ref, 0) for ref in ref_lfs]
tgt_stack = torch.cat(tuple(tgt_stack), 0)
ref_stacks = [torch.cat(ref, 0) for ref in ref_stacks]
# Create metadata and training data
metadata = MetaData(self.cameras, sample['tgt'], sample['ref_imgs'], self.gray, False)
trainingdata = TrainingData(tgt_lf, tgt_stack, ref_lfs, ref_stacks, sample['intrinsics'],
poses_gt_refs_tgt, metadata)
# if self.cameras_stacked == 'full':
#
# trainingdata = TrainingData(tgt_lf, tgt_stack, ref_lfs, ref_stacks, sample['intrinsics'], pose, metadata)
# else:
# # the stacks sent as input for the pose and disp nets are the same as the stack used for photometric error
# trainingdata = TrainingData(tgt_lf, tgt_lf, ref_lfs, ref_lfs, sample['intrinsics'], pose, metadata)
return trainingdata.get_as_dict()
class TiledEPILoader(BaseDataset):
"""
Class for loading tiles Epipolar Plane Images (EPI)
"""
def __init__(self, root, cameras, gray=False, seed=None, train=True, sequence_length=3,
transform=None, shuffle=True, sequence=None, cameras_epi='vertical', no_pose=False):
"""
Initlializer
:param root: Path to the folder where the data is present.
This folder should have subfolders seq#, train.txt, val.txt
:type root: str
:param cameras: List of camera indices to use for computing the multi-warp photometric error.
:type cameras: list
:param gray: Boolean indicating if the images have to be converted to grayscale
:type gray: bool
:param seed: Seed for random number generator
:type seed: int
:param train: Boolean indicating if the data to be loaded is the training or the validation set. Default is True
:type train: bool
:param sequence_length: Number of images in the sequence. Default 3
:type sequence_length: int
:param transform: Transform that has to be applied to the images. Default is None
:type transform: tensor
:param shuffle: Boolean indicating if the data has to be shuffled. Default is True for training.
:type shuffle: bool
:param sequence: Sequence index that has to be loaded, if required. Default is None to load all data.
:type sequence: int
:param cameras_epi: String indicating what cameras to use in forming the EPI. 'vertical', 'horizontal', 'full'
:type cameras_epi: str
"""
super(TiledEPILoader, self).__init__(root, cameras, gray, seed, train, sequence_length,
transform, shuffle, sequence)
assert cameras_epi in ["vertical", "horizontal", "full"]
self.cameras_epi = cameras_epi
self.cameras_stacked_indices = [3, 8, 13, 7, 9]
self.no_pose = no_pose
def __getitem__(self, index):
"""
Returns the element at the desired index from the dataset.
:param index: Index of the element of the dataset
:type index: int
:return: Dictionary of training data
:rtype: dict
"""
# get the data sample at the specified index
sample = self.samples[index]
# load target and reference light fields for photometric error computation
tgt_lf = load_lightfield(sample['tgt'], self.cameras, self.gray)
ref_lfs = [load_lightfield(ref_img, self.cameras, self.gray) for ref_img in sample['ref_imgs']]
# load target and reference light fields for sending to encoders
tgt_stack = load_lightfield(sample['tgt'], self.cameras_stacked_indices, self.gray)
ref_stacks = [load_lightfield(ref_img, self.cameras_stacked_indices, self.gray)
for ref_img in sample['ref_imgs']]
# load epi images
tgt_epi_h = tgt_epi_v = tgt_epi = None
ref_epis_h = ref_epis_v = ref_epis = None
if self.cameras_epi == 'vertical':
# the full epi is directly only the vertical epi
tgt_epi = load_tiled_epi_vertical(sample['tgt'])
ref_epis = [load_tiled_epi_vertical(ref_img) for ref_img in sample['ref_imgs']]
elif self.cameras_epi == 'horizontal':
# the full epi is directly only the horizontal epi
tgt_epi = load_tiled_epi_horizontal(sample['tgt'])
ref_epis = [load_tiled_epi_horizontal(ref_img) for ref_img in sample['ref_imgs']]
elif self.cameras_epi == 'full':
# horizontal and vertical epis need to be separately handled
tgt_epi_v, tgt_epi_h = load_tiled_epi_full(sample['tgt'])
# this will be a list of tuples
ref_epis_v = []
ref_epis_h = []
for ref_img in sample['ref_imgs']:
ref_epi_v, ref_epi_h = load_tiled_epi_full(ref_img)
ref_epis_v.append(ref_epi_v)
ref_epis_h.append(ref_epi_h)
# load ground truth pose of each reference frame wrt target frame - [refs x 4x4 matrix]
poses_gt_refs_tgt = torch.Tensor([load_relative_pose(ref, sample['tgt'], self.no_pose)
for ref in sample['ref_imgs']])
# intrinsics = np.copy(sample['intrinsics'])
# apply transforms if present
if self.transform is not None:
# apply transforms on the images photometric error
tgt_lf, _ = self.transform(tgt_lf, np.zeros((3, 3)))
ref_lfs = [self.transform(ref, np.zeros((3, 3)))[0] for ref in ref_lfs]
tgt_stack, _ = self.transform(tgt_stack, np.zeros((3, 3)))
ref_stacks = [self.transform(ref, np.zeros((3, 3)))[0] for ref in ref_stacks]
# apply transforms on the encoded light fields
if self.cameras_epi == 'full':
# apply separately on the horizontal and vertical images
tgt_epi_h, _ = self.transform(tgt_epi_h, np.zeros((3, 3)))
tgt_epi_v, _ = self.transform(tgt_epi_v, np.zeros((3, 3)))
ref_epis_h = [self.transform(ref, np.zeros((3, 3)))[0] for ref in ref_epis_h]
ref_epis_v = [self.transform(ref, np.zeros((3, 3)))[0] for ref in ref_epis_v]
else:
tgt_epi, _ = self.transform(tgt_epi, np.zeros((3, 3)))
ref_epis = [self.transform(ref, np.zeros((3, 3)))[0] for ref in ref_epis]
# concatenate light fields on colour channel
tgt_lf = torch.cat(tuple(tgt_lf), 0)
ref_lfs = [torch.cat(ref, 0) for ref in ref_lfs]
tgt_stack = torch.cat(tuple(tgt_stack), 0)
ref_stacks = [torch.cat(ref, 0) for ref in ref_stacks]
if self.cameras_epi == 'full':
tgt_epi_h = torch.cat(tuple(tgt_epi_h), 0)
tgt_epi_v = torch.cat(tuple(tgt_epi_v), 0)
ref_epis_h = [torch.cat(ref, 0) for ref in ref_epis_h]
ref_epis_v = [torch.cat(ref, 0) for ref in ref_epis_v]
else:
tgt_epi = torch.cat(tuple(tgt_epi), 0)
ref_epis = [torch.cat(ref, 0) for ref in ref_epis]
# Create metadata and training data
metadata = MetaData(self.cameras, sample['tgt'], sample['ref_imgs'], self.gray, False)
if self.cameras_epi == 'full':
trainingdata = TrainingDataEpi(tgt=tgt_lf, tgt_formatted_h=tgt_epi_h, tgt_formatted_v=tgt_epi_v,
ref=ref_lfs, ref_formatted_h=ref_epis_h, ref_formatted_v=ref_epis_v,
tgt_stack=tgt_stack, ref_stacks=ref_stacks,
intrinsics=sample['intrinsics'],
poses_gt_refs_tgt=poses_gt_refs_tgt, metadata=metadata)
else:
trainingdata = TrainingData(tgt=tgt_lf, tgt_formatted=tgt_epi, ref=ref_lfs, ref_formatted=ref_epis,
intrinsics=sample['intrinsics'], poses_gt_refs_tgt=poses_gt_refs_tgt,
metadata=metadata)
return trainingdata.get_as_dict()
def get_epi_loaders(args, train_transform, valid_transform, shuffle=True, no_pose=False):
"""
Returns the dataset objects with epipolar plane images
:param args: user specified arguments
:type args: argparse object
:param train_transform: Transform to apply on the training images
:type train_transform: Tensor
:param valid_transform: Transform to apply on the validation images
:type valid_transform: Tensor
:param shuffle: Boolean indicating if data has to be shuffled
:type shuffle: bool
:param no_pose: Boolean indicating if pose is present or not. Default False
:type no_pose: bool
:return: Training and Validation datasets
:rtype: tuple of objects
"""
train_set = TiledEPILoader(
args.data,
cameras=args.cameras,
gray=True, # TODO: Why is EPI loader always grayscale? Is it to reduce computation?
seed=args.seed,
train=True,
sequence_length=args.sequence_length,
transform=train_transform,
shuffle=shuffle,
cameras_epi=args.cameras_epi,
no_pose=no_pose
)
val_set = TiledEPILoader(
args.data,
cameras=args.cameras,
gray=True, # TODO: Why is EPI loader always grayscale? Is it to reduce computation?
seed=args.seed,
train=False,
sequence_length=args.sequence_length,
transform=valid_transform,
shuffle=False, # shuffle is false for validation
cameras_epi=args.cameras_epi,
no_pose=no_pose
)
return train_set, val_set
def get_validation_epi_loader(args, sequence=None, transform=None, no_pose=False):
"""
Returns the dataset objects with epipolar plane images for validation
:param args:user specified arguments
:type args: argparse object
:param sequence: Index of the sequence to use for validation
:type sequence: int
:param transform: Transform to apply on the images
:type transform: Tensor
:param no_pose: Boolean indicating if pose is present or not. Default False
:type no_pose: bool
:return: Validation dataset
:rtype: object
"""
return TiledEPILoader(
args.data,
cameras=args.cameras,
gray=True,
seed=args.seed,
train=False,
sequence_length=args.sequence_length,
transform=transform,
shuffle=False, # shuffle is false for validation
sequence=sequence,
cameras_epi=args.cameras_epi,
no_pose=no_pose
)
def get_focal_stack_loaders(args, train_transform, valid_transform, shuffle=True, no_pose=False):
"""
Returns the dataset objects with light field focal stacks.
:param args: user specified arguments
:type args: argparse object
:param train_transform: Transform to apply on the training images
:type train_transform: Tensor
:param valid_transform: Transform to apply on the validation images
:type valid_transform: Tensor
:param shuffle: Boolean indicating if data has to be shuffled
:type shuffle: bool
:param no_pose: Boolean indicating if pose is present or not. Default False
:type no_pose: bool
:return: Training and Validation datasets
:rtype: tuple of objects
"""
train_set = FocalstackLoader(
args.data,
cameras=args.cameras,
gray=args.gray,
transform=train_transform,
seed=args.seed,
train=True,
sequence_length=args.sequence_length,
fs_num_cameras=args.num_cameras,
fs_num_planes=args.num_planes,
shuffle=shuffle,
no_pose=no_pose
)
val_set = FocalstackLoader(
args.data,
cameras=args.cameras,
gray=args.gray,
transform=valid_transform,
seed=args.seed,
train=False,
sequence_length=args.sequence_length,
fs_num_cameras=args.num_cameras,
fs_num_planes=args.num_planes,
shuffle=False, # shuffle is false for validation
no_pose=no_pose
)
return train_set, val_set
def get_validation_focal_stack_loader(args, sequence=None, transform=None, no_pose=False):
"""
Returns the dataset objects with images as light field focal stacks for validation
:param args:user specified arguments
:type args: argparse object
:param sequence: Index of the sequence to use for validation
:type sequence: int
:param transform: Transform to apply on the images
:type transform: Tensor
:param no_pose: Boolean indicating if pose is present or not. Default False
:type no_pose: bool
:return: Validation dataset
:rtype: object
"""
val_set = FocalstackLoader(
args.data,
cameras=args.cameras,
gray=args.gray,
transform=transform,
seed=args.seed,
train=False,
sequence_length=args.sequence_length,
fs_num_cameras=args.num_cameras,
fs_num_planes=args.num_planes,
shuffle=False, # shuffle is false for validation
sequence=sequence,
no_pose=no_pose
)
return val_set
def get_stacked_lf_loaders(args, train_transform, valid_transform, shuffle=True, no_pose=False, is_monocular=False):
"""
Returns the dataset objects with light field image stacks.
:param args: user specified arguments
:type args: argparse object
:param train_transform: Transform to apply on the training images
:type train_transform: Tensor
:param valid_transform: Transform to apply on the validation images
:type valid_transform: Tensor
:param shuffle: Boolean indicating if data has to be shuffled
:type shuffle: bool
:param no_pose: Boolean indicating if pose is present or not. Default False
:type no_pose: bool
:param is_monocular: Boolean indicating if it is monocular mode of operation. Default False
:type is_monocular: bool
:return: Training and Validation datasets
:rtype: tuple of objects
"""
train_set = StackedLFLoader(
args.data,
cameras=args.cameras,
gray=args.gray,
seed=args.seed,
train=True,
sequence_length=args.sequence_length,
transform=train_transform,
shuffle=shuffle,
cameras_stacked=args.cameras_stacked,
no_pose=no_pose,
is_monocular=is_monocular
)
val_set = StackedLFLoader(
args.data,
cameras=args.cameras,
gray=args.gray,
seed=args.seed,
train=False,
sequence_length=args.sequence_length,
transform=valid_transform,
shuffle=False, # shuffle is false for validation
cameras_stacked=args.cameras_stacked,
no_pose=no_pose,
is_monocular=is_monocular
)
return train_set, val_set
def get_validation_stacked_lf_loader(args, sequence=None, transform=None, no_pose=False, is_monocular=False):
"""
Returns the dataset objects with images as light field image stacks for validation
:param args:user specified arguments
:type args: argparse object
:param sequence: Index of the sequence to use for validation
:type sequence: int
:param transform: Transform to apply on the images
:type transform: Tensor
:param no_pose: Boolean indicating if pose is present or not. Default False
:type no_pose: bool
:param is_monocular: Boolean indicating if it is monocular mode of operation. Default False
:type is_monocular: bool
:return: Validation dataset
:rtype: object
"""
return StackedLFLoader(
args.data,
cameras=args.cameras,
gray=args.gray,
seed=args.seed,
train=False,
sequence_length=args.sequence_length,
transform=transform,
shuffle=False, # shuffle is false for validation
sequence=sequence,
cameras_stacked=args.cameras_stacked,
no_pose=no_pose,
is_monocular=is_monocular
)