-
Notifications
You must be signed in to change notification settings - Fork 1
/
bts_dataloader.py
224 lines (180 loc) · 8.61 KB
/
bts_dataloader.py
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
import os
import random
import cv2
import numpy as np
from PIL import Image
import torch
from torch.utils.data import Dataset, DataLoader
import torch.utils.data.distributed
from torchvision import transforms
from distributed_sampler_no_evenly_divisible import *
def _is_pil_image(img):
return isinstance(img, Image.Image)
def _is_numpy_image(img):
return isinstance(img, np.ndarray) and (img.ndim in {2, 3})
def preprocessing_transforms(mode):
return transforms.Compose([
ToTensor(mode=mode)
])
class BtsDataLoader(object):
def __init__(self, args, mode):
if mode == 'train':
self.training_samples = DataLoadPreprocess(args, mode, transform=preprocessing_transforms(mode))
if args.distributed:
self.train_sampler = torch.utils.data.distributed.DistributedSampler(self.training_samples)
else:
self.train_sampler = None
self.data = DataLoader(self.training_samples, args.batch_size,
shuffle=(self.train_sampler is None),
num_workers=args.num_threads,
pin_memory=True,
sampler=self.train_sampler)
elif mode == 'online_eval':
self.testing_samples = DataLoadPreprocess(args, mode, transform=preprocessing_transforms(mode))
if args.distributed:
# self.eval_sampler = torch.utils.data.distributed.DistributedSampler(self.testing_samples, shuffle=False)
self.eval_sampler = DistributedSamplerNoEvenlyDivisible(self.testing_samples, shuffle=False)
else:
self.eval_sampler = None
self.data = DataLoader(self.testing_samples, 1,
shuffle=False,
num_workers=1,
pin_memory=True,
sampler=self.eval_sampler)
elif mode == 'test':
self.testing_samples = DataLoadPreprocess(args, mode, transform=preprocessing_transforms(mode))
self.data = DataLoader(self.testing_samples, 1, shuffle=False, num_workers=1)
else:
print('mode should be one of \'train, test, online_eval\'. Got {}'.format(mode))
class DataLoadPreprocess(Dataset):
def __init__(self, args, mode, transform=None, is_for_online_eval=False):
self.args = args
if mode == 'online_eval':
with open(args.filenames_file_eval, 'r') as f:
self.filenames = f.readlines()
else:
with open(args.filenames_file, 'r') as f:
self.filenames = f.readlines()
self.mode = mode
self.transform = transform
self.to_tensor = ToTensor
self.is_for_online_eval = is_for_online_eval
def __getitem__(self, idx):
sample_path = self.filenames[idx]
focal = float(sample_path.split()[2])
if self.mode == 'train':
if self.args.dataset == 'argo' and self.args.use_right is True and random.random() > 0.5:
image_path = os.path.join(self.args.data_path, "./" + sample_path.split()[3])
depth_path = os.path.join(self.args.gt_path, "./" + sample_path.split()[4])
else:
image_path = os.path.join(self.args.data_path, "./" + sample_path.split()[0])
depth_path = os.path.join(self.args.gt_path, "./" + sample_path.split()[1])
image = Image.open(image_path)
depth_gt = Image.open(depth_path)
if self.args.do_random_rotate is True:
random_angle = (random.random() - 0.5) * 2 * self.args.degree
image = self.rotate_image(image, random_angle)
depth_gt = self.rotate_image(depth_gt, random_angle, flag=Image.NEAREST)
image = np.asarray(image, dtype=np.float32) / 255.0
depth_gt = np.asarray(depth_gt, dtype=np.float32)
depth_gt = np.expand_dims(depth_gt, axis=2)
depth_gt = depth_gt / 256.0
image, depth_gt = self.random_crop(image, depth_gt, self.args.input_height, self.args.input_width)
image, depth_gt = self.train_preprocess(image, depth_gt)
sample = {'image': image, 'depth': depth_gt, 'focal': focal}
else:
data_path = self.args.data_path
image_path = os.path.join(data_path, "./" + sample_path.split()[0])
image = cv2.imread(image_path).astype(np.float32)/255.0
height, width, _ = image.shape
adjusted_height = lambda height: 32*(math.ceil(height/32)) if height%32 !=0 else height
adjusted_width = lambda height: 32*(math.ceil(width/32)) if width%32 !=0 else width
image = cv2.resize(image, (adjusted_width(width), adjusted_height(height)), interpolation = cv2.INTER_LANCZOS4)
sample = {'image': image, 'focal': focal}
if self.transform:
sample = self.transform(sample)
return sample
def rotate_image(self, image, angle, flag=Image.BILINEAR):
result = image.rotate(angle, resample=flag)
return result
def random_crop(self, img, depth, height, width):
assert img.shape[0] >= height
assert img.shape[1] >= width
assert img.shape[0] == depth.shape[0]
assert img.shape[1] == depth.shape[1]
x = random.randint(0, img.shape[1] - width)
y = random.randint(0, img.shape[0] - height)
img = img[y:y + height, x:x + width, :]
depth = depth[y:y + height, x:x + width, :]
return img, depth
def train_preprocess(self, image, depth_gt):
# Random flipping
do_flip = random.random()
if do_flip > 0.5:
image = (image[:, ::-1, :]).copy()
depth_gt = (depth_gt[:, ::-1, :]).copy()
# Random gamma, brightness, color augmentation
do_augment = random.random()
if do_augment > 0.5:
image = self.augment_image(image)
return image, depth_gt
def augment_image(self, image):
# gamma augmentation
gamma = random.uniform(0.9, 1.1)
image_aug = image ** gamma
# brightness augmentation
brightness = random.uniform(0.9, 1.1)
image_aug = image_aug * brightness
# color augmentation
colors = np.random.uniform(0.9, 1.1, size=3)
white = np.ones((image.shape[0], image.shape[1]))
color_image = np.stack([white * colors[i] for i in range(3)], axis=2)
image_aug *= color_image
image_aug = np.clip(image_aug, 0, 1)
return image_aug
def __len__(self):
return len(self.filenames)
class ToTensor(object):
def __init__(self, mode):
self.mode = mode
self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
def __call__(self, sample):
image, focal = sample['image'], sample['focal']
image = self.to_tensor(image)
image = self.normalize(image)
if self.mode == 'test':
return {'image': image, 'focal': focal}
depth = sample['depth']
if self.mode == 'train':
depth = self.to_tensor(depth)
return {'image': image, 'depth': depth, 'focal': focal}
else:
has_valid_depth = sample['has_valid_depth']
return {'image': image, 'depth': depth, 'focal': focal, 'has_valid_depth': has_valid_depth}
def to_tensor(self, pic):
if not (_is_pil_image(pic) or _is_numpy_image(pic)):
raise TypeError(
'pic should be PIL Image or ndarray. Got {}'.format(type(pic)))
if isinstance(pic, np.ndarray):
img = torch.from_numpy(pic.transpose((2, 0, 1)))
return img
# handle PIL Image
if pic.mode == 'I':
img = torch.from_numpy(np.array(pic, np.int32, copy=False))
elif pic.mode == 'I;16':
img = torch.from_numpy(np.array(pic, np.int16, copy=False))
else:
img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))
# PIL image mode: 1, L, P, I, F, RGB, YCbCr, RGBA, CMYK
if pic.mode == 'YCbCr':
nchannel = 3
elif pic.mode == 'I;16':
nchannel = 1
else:
nchannel = len(pic.mode)
img = img.view(pic.size[1], pic.size[0], nchannel)
img = img.transpose(0, 1).transpose(0, 2).contiguous()
if isinstance(img, torch.ByteTensor):
return img.float()
else:
return img