-
Notifications
You must be signed in to change notification settings - Fork 44
/
RedNet_data.py
278 lines (231 loc) · 11.6 KB
/
RedNet_data.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
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
import numpy as np
import scipy.io
import imageio
import h5py
import os
from torch.utils.data import Dataset
import matplotlib
import matplotlib.colors
import skimage.transform
import random
import torchvision
import torch
from RedNet_train import image_h, image_w
img_dir_train_file = './data/img_dir_train.txt'
depth_dir_train_file = './data/depth_dir_train.txt'
label_dir_train_file = './data/label_train.txt'
img_dir_test_file = './data/img_dir_test.txt'
depth_dir_test_file = './data/depth_dir_test.txt'
label_dir_test_file = './data/label_test.txt'
class SUNRGBD(Dataset):
def __init__(self, transform=None, phase_train=True, data_dir=None):
self.phase_train = phase_train
self.transform = transform
try:
with open(img_dir_train_file, 'r') as f:
self.img_dir_train = f.read().splitlines()
with open(depth_dir_train_file, 'r') as f:
self.depth_dir_train = f.read().splitlines()
with open(label_dir_train_file, 'r') as f:
self.label_dir_train = f.read().splitlines()
with open(img_dir_test_file, 'r') as f:
self.img_dir_test = f.read().splitlines()
with open(depth_dir_test_file, 'r') as f:
self.depth_dir_test = f.read().splitlines()
with open(label_dir_test_file, 'r') as f:
self.label_dir_test = f.read().splitlines()
except:
if data_dir is None:
data_dir = '/path/to/SUNRGB-D'
SUNRGBDMeta_dir = os.path.join(data_dir, 'SUNRGBDtoolbox/Metadata/SUNRGBDMeta.mat')
allsplit_dir = os.path.join(data_dir, 'SUNRGBDtoolbox/traintestSUNRGBD/allsplit.mat')
SUNRGBD2Dseg_dir = os.path.join(data_dir, 'SUNRGBDtoolbox/Metadata/SUNRGBD2Dseg.mat')
self.img_dir_train = []
self.depth_dir_train = []
self.label_dir_train = []
self.img_dir_test = []
self.depth_dir_test = []
self.label_dir_test = []
self.SUNRGBD2Dseg = h5py.File(SUNRGBD2Dseg_dir, mode='r', libver='latest')
SUNRGBDMeta = scipy.io.loadmat(SUNRGBDMeta_dir, squeeze_me=True,
struct_as_record=False)['SUNRGBDMeta']
split = scipy.io.loadmat(allsplit_dir, squeeze_me=True, struct_as_record=False)
split_train = split['alltrain']
seglabel = self.SUNRGBD2Dseg['SUNRGBD2Dseg']['seglabel']
for i, meta in enumerate(SUNRGBDMeta):
meta_dir = '/'.join(meta.rgbpath.split('/')[:-2])
real_dir = meta_dir.replace('/n/fs/sun3d/data', data_dir)
depth_bfx_path = os.path.join(real_dir, 'depth_bfx/' + meta.depthname)
rgb_path = os.path.join(real_dir, 'image/' + meta.rgbname)
label_path = os.path.join(real_dir, 'label/label.npy')
if not os.path.exists(label_path):
os.makedirs(os.path.join(real_dir, 'label'), exist_ok=True)
label = np.array(self.SUNRGBD2Dseg[seglabel.value[i][0]].value.transpose(1, 0))
np.save(label_path, label)
if meta_dir in split_train:
self.img_dir_train = np.append(self.img_dir_train, rgb_path)
self.depth_dir_train = np.append(self.depth_dir_train, depth_bfx_path)
self.label_dir_train = np.append(self.label_dir_train, label_path)
else:
self.img_dir_test = np.append(self.img_dir_test, rgb_path)
self.depth_dir_test = np.append(self.depth_dir_test, depth_bfx_path)
self.label_dir_test = np.append(self.label_dir_test, label_path)
local_file_dir = '/'.join(img_dir_train_file.split('/')[:-1])
if not os.path.exists(local_file_dir):
os.mkdir(local_file_dir)
with open(img_dir_train_file, 'w') as f:
f.write('\n'.join(self.img_dir_train))
with open(depth_dir_train_file, 'w') as f:
f.write('\n'.join(self.depth_dir_train))
with open(label_dir_train_file, 'w') as f:
f.write('\n'.join(self.label_dir_train))
with open(img_dir_test_file, 'w') as f:
f.write('\n'.join(self.img_dir_test))
with open(depth_dir_test_file, 'w') as f:
f.write('\n'.join(self.depth_dir_test))
with open(label_dir_test_file, 'w') as f:
f.write('\n'.join(self.label_dir_test))
def __len__(self):
if self.phase_train:
return len(self.img_dir_train)
else:
return len(self.img_dir_test)
def __getitem__(self, idx):
if self.phase_train:
img_dir = self.img_dir_train
depth_dir = self.depth_dir_train
label_dir = self.label_dir_train
else:
img_dir = self.img_dir_test
depth_dir = self.depth_dir_test
label_dir = self.label_dir_test
label = np.load(label_dir[idx])
depth = imageio.imread(depth_dir[idx])
image = imageio.imread(img_dir[idx])
sample = {'image': image, 'depth': depth, 'label': label}
if self.transform:
sample = self.transform(sample)
return sample
class RandomHSV(object):
"""
Args:
h_range (float tuple): random ratio of the hue channel,
new_h range from h_range[0]*old_h to h_range[1]*old_h.
s_range (float tuple): random ratio of the saturation channel,
new_s range from s_range[0]*old_s to s_range[1]*old_s.
v_range (int tuple): random bias of the value channel,
new_v range from old_v-v_range to old_v+v_range.
Notice:
h range: 0-1
s range: 0-1
v range: 0-255
"""
def __init__(self, h_range, s_range, v_range):
assert isinstance(h_range, (list, tuple)) and \
isinstance(s_range, (list, tuple)) and \
isinstance(v_range, (list, tuple))
self.h_range = h_range
self.s_range = s_range
self.v_range = v_range
def __call__(self, sample):
img = sample['image']
img_hsv = matplotlib.colors.rgb_to_hsv(img)
img_h, img_s, img_v = img_hsv[:, :, 0], img_hsv[:, :, 1], img_hsv[:, :, 2]
h_random = np.random.uniform(min(self.h_range), max(self.h_range))
s_random = np.random.uniform(min(self.s_range), max(self.s_range))
v_random = np.random.uniform(-min(self.v_range), max(self.v_range))
img_h = np.clip(img_h * h_random, 0, 1)
img_s = np.clip(img_s * s_random, 0, 1)
img_v = np.clip(img_v + v_random, 0, 255)
img_hsv = np.stack([img_h, img_s, img_v], axis=2)
img_new = matplotlib.colors.hsv_to_rgb(img_hsv)
return {'image': img_new, 'depth': sample['depth'], 'label': sample['label']}
class scaleNorm(object):
def __call__(self, sample):
image, depth, label = sample['image'], sample['depth'], sample['label']
# Bi-linear
image = skimage.transform.resize(image, (image_h, image_w), order=1,
mode='reflect', preserve_range=True)
# Nearest-neighbor
depth = skimage.transform.resize(depth, (image_h, image_w), order=0,
mode='reflect', preserve_range=True)
label = skimage.transform.resize(label, (image_h, image_w), order=0,
mode='reflect', preserve_range=True)
return {'image': image, 'depth': depth, 'label': label}
class RandomScale(object):
def __init__(self, scale):
self.scale_low = min(scale)
self.scale_high = max(scale)
def __call__(self, sample):
image, depth, label = sample['image'], sample['depth'], sample['label']
target_scale = random.uniform(self.scale_low, self.scale_high)
# (H, W, C)
target_height = int(round(target_scale * image.shape[0]))
target_width = int(round(target_scale * image.shape[1]))
# Bi-linear
image = skimage.transform.resize(image, (target_height, target_width),
order=1, mode='reflect', preserve_range=True)
# Nearest-neighbor
depth = skimage.transform.resize(depth, (target_height, target_width),
order=0, mode='reflect', preserve_range=True)
label = skimage.transform.resize(label, (target_height, target_width),
order=0, mode='reflect', preserve_range=True)
return {'image': image, 'depth': depth, 'label': label}
class RandomCrop(object):
def __init__(self, th, tw):
self.th = th
self.tw = tw
def __call__(self, sample):
image, depth, label = sample['image'], sample['depth'], sample['label']
h = image.shape[0]
w = image.shape[1]
i = random.randint(0, h - self.th)
j = random.randint(0, w - self.tw)
return {'image': image[i:i + image_h, j:j + image_w, :],
'depth': depth[i:i + image_h, j:j + image_w],
'label': label[i:i + image_h, j:j + image_w]}
class RandomFlip(object):
def __call__(self, sample):
image, depth, label = sample['image'], sample['depth'], sample['label']
if random.random() > 0.5:
image = np.fliplr(image).copy()
depth = np.fliplr(depth).copy()
label = np.fliplr(label).copy()
return {'image': image, 'depth': depth, 'label': label}
# Transforms on torch.*Tensor
class Normalize(object):
def __call__(self, sample):
image, depth = sample['image'], sample['depth']
image = image / 255
image = torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])(image)
depth = torchvision.transforms.Normalize(mean=[19050],
std=[9650])(depth)
sample['image'] = image
sample['depth'] = depth
return sample
class ToTensor(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample):
image, depth, label = sample['image'], sample['depth'], sample['label']
# Generate different label scales
label2 = skimage.transform.resize(label, (label.shape[0] // 2, label.shape[1] // 2),
order=0, mode='reflect', preserve_range=True)
label3 = skimage.transform.resize(label, (label.shape[0] // 4, label.shape[1] // 4),
order=0, mode='reflect', preserve_range=True)
label4 = skimage.transform.resize(label, (label.shape[0] // 8, label.shape[1] // 8),
order=0, mode='reflect', preserve_range=True)
label5 = skimage.transform.resize(label, (label.shape[0] // 16, label.shape[1] // 16),
order=0, mode='reflect', preserve_range=True)
# swap color axis because
# numpy image: H x W x C
# torch image: C X H X W
image = image.transpose((2, 0, 1))
depth = np.expand_dims(depth, 0).astype(np.float)
return {'image': torch.from_numpy(image).float(),
'depth': torch.from_numpy(depth).float(),
'label': torch.from_numpy(label).float(),
'label2': torch.from_numpy(label2).float(),
'label3': torch.from_numpy(label3).float(),
'label4': torch.from_numpy(label4).float(),
'label5': torch.from_numpy(label5).float()}