-
Notifications
You must be signed in to change notification settings - Fork 352
/
data.py
198 lines (145 loc) · 7.68 KB
/
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
import numpy as np
from utils import DepthNorm
from io import BytesIO
from PIL import Image
from zipfile import ZipFile
from tensorflow.keras.utils import Sequence
from augment import BasicPolicy
def extract_zip(input_zip):
input_zip=ZipFile(input_zip)
return {name: input_zip.read(name) for name in input_zip.namelist()}
def nyu_resize(img, resolution=480, padding=6):
from skimage.transform import resize
return resize(img, (resolution, int(resolution*4/3)), preserve_range=True, mode='reflect', anti_aliasing=True )
def get_nyu_data(batch_size, nyu_data_zipfile='CSVdata.zip'):
data = extract_zip(nyu_data_zipfile)
nyu2_train = list((row.split(',') for row in (data['data/trainData.csv']).decode("utf-8").split('\n') if len(row) > 0))
nyu2_test = list((row.split(',') for row in (data['data/valData.csv']).decode("utf-8").split('\n') if len(row) > 0))
shape_rgb = (batch_size, 480, 640, 3)
shape_depth = (batch_size, 240, 320, 1)
# Helpful for testing...
if False:
nyu2_train = nyu2_train[:10]
nyu2_test = nyu2_test[:10]
return data, nyu2_train, nyu2_test, shape_rgb, shape_depth
def get_nyu_train_test_data(batch_size):
data, nyu2_train, nyu2_test, shape_rgb, shape_depth = get_nyu_data(batch_size)
train_generator = NYU_BasicAugmentRGBSequence(data, nyu2_train, batch_size=batch_size, shape_rgb=shape_rgb, shape_depth=shape_depth)
test_generator = NYU_BasicRGBSequence(data, nyu2_test, batch_size=batch_size, shape_rgb=shape_rgb, shape_depth=shape_depth)
return train_generator, test_generator
class NYU_BasicAugmentRGBSequence(Sequence):
def __init__(self, data, dataset, batch_size, shape_rgb, shape_depth, is_flip=False, is_addnoise=False, is_erase=False):
self.data = data
self.dataset = dataset
self.policy = BasicPolicy( color_change_ratio=0.50, mirror_ratio=0.50, flip_ratio=0.0 if not is_flip else 0.2,
add_noise_peak=0 if not is_addnoise else 20, erase_ratio=-1.0 if not is_erase else 0.5)
self.batch_size = batch_size
self.shape_rgb = shape_rgb
self.shape_depth = shape_depth
self.maxDepth = 1000.0
from sklearn.utils import shuffle
self.dataset = shuffle(self.dataset, random_state=0)
self.N = len(self.dataset)
def __len__(self):
return int(np.ceil(self.N / float(self.batch_size)))
def __getitem__(self, idx, is_apply_policy=True):
batch_x, batch_y = np.zeros( self.shape_rgb ), np.zeros( self.shape_depth )
# Augmentation of RGB images
for i in range(batch_x.shape[0]):
index = min((idx * self.batch_size) + i, self.N-1)
sample = self.dataset[index]
x = np.clip(np.asarray(Image.open( "../"+sample[0] )).reshape(480,640,3)/255,0,1)
y = np.clip(np.asarray(Image.open( "../"+sample[1] )).reshape(480,640,1)/255*self.maxDepth,0,self.maxDepth)
y = DepthNorm(y, maxDepth=self.maxDepth)
batch_x[i] = nyu_resize(x, 480)
batch_y[i] = nyu_resize(y, 240)
if is_apply_policy: batch_x[i], batch_y[i] = self.policy(batch_x[i], batch_y[i])
# DEBUG:
#self.policy.debug_img(batch_x[i], np.clip(DepthNorm(batch_y[i])/maxDepth,0,1), idx, i)
#exit()
return batch_x, batch_y
class NYU_BasicRGBSequence(Sequence):
def __init__(self, data, dataset, batch_size,shape_rgb, shape_depth):
self.data = data
self.dataset = dataset
self.batch_size = batch_size
self.N = len(self.dataset)
self.shape_rgb = shape_rgb
self.shape_depth = shape_depth
self.maxDepth = 1000.0
def __len__(self):
return int(np.ceil(self.N / float(self.batch_size)))
def __getitem__(self, idx):
batch_x, batch_y = np.zeros( self.shape_rgb ), np.zeros( self.shape_depth )
for i in range(self.batch_size):
index = min((idx * self.batch_size) + i, self.N-1)
sample = self.dataset[index]
x = np.clip(np.asarray(Image.open( "../"+sample[0])).reshape(480,640,3)/255,0,1)
y = np.asarray(Image.open( "../"+sample[1]), dtype=np.float32).reshape(480,640,1).copy().astype(float) / 10.0
y = DepthNorm(y, maxDepth=self.maxDepth)
batch_x[i] = nyu_resize(x, 480)
batch_y[i] = nyu_resize(y, 240)
# DEBUG:
#self.policy.debug_img(batch_x[i], np.clip(DepthNorm(batch_y[i])/maxDepth,0,1), idx, i)
#exit()
return batch_x, batch_y
#================
# Unreal dataset
#================
import cv2
from skimage.transform import resize
def get_unreal_data(batch_size, unreal_data_file='unreal_data.h5'):
shape_rgb = (batch_size, 480, 640, 3)
shape_depth = (batch_size, 240, 320, 1)
# Open data file
import h5py
data = h5py.File(unreal_data_file, 'r')
# Shuffle
from sklearn.utils import shuffle
keys = shuffle(list(data['x'].keys()), random_state=0)
# Split some validation
unreal_train = keys[:len(keys)-100]
unreal_test = keys[len(keys)-100:]
# Helpful for testing...
if False:
unreal_train = unreal_train[:10]
unreal_test = unreal_test[:10]
return data, unreal_train, unreal_test, shape_rgb, shape_depth
def get_unreal_train_test_data(batch_size):
data, unreal_train, unreal_test, shape_rgb, shape_depth = get_unreal_data(batch_size)
train_generator = Unreal_BasicAugmentRGBSequence(data, unreal_train, batch_size=batch_size, shape_rgb=shape_rgb, shape_depth=shape_depth)
test_generator = Unreal_BasicAugmentRGBSequence(data, unreal_test, batch_size=batch_size, shape_rgb=shape_rgb, shape_depth=shape_depth, is_skip_policy=True)
return train_generator, test_generator
class Unreal_BasicAugmentRGBSequence(Sequence):
def __init__(self, data, dataset, batch_size, shape_rgb, shape_depth, is_flip=False, is_addnoise=False, is_erase=False, is_skip_policy=False):
self.data = data
self.dataset = dataset
self.policy = BasicPolicy( color_change_ratio=0.50, mirror_ratio=0.50, flip_ratio=0.0 if not is_flip else 0.2,
add_noise_peak=0 if not is_addnoise else 20, erase_ratio=-1.0 if not is_erase else 0.5)
self.batch_size = batch_size
self.shape_rgb = shape_rgb
self.shape_depth = shape_depth
self.maxDepth = 1000.0
self.N = len(self.dataset)
self.is_skip_policy = is_skip_policy
def __len__(self):
return int(np.ceil(self.N / float(self.batch_size)))
def __getitem__(self, idx, is_apply_policy=True):
batch_x, batch_y = np.zeros( self.shape_rgb ), np.zeros( self.shape_depth )
# Useful for validation
if self.is_skip_policy: is_apply_policy=False
# Augmentation of RGB images
for i in range(batch_x.shape[0]):
index = min((idx * self.batch_size) + i, self.N-1)
sample = self.dataset[index]
rgb_sample = cv2.imdecode(np.asarray(self.data['x/{}'.format(sample)]), 1)
depth_sample = self.data['y/{}'.format(sample)]
depth_sample = resize(depth_sample, (self.shape_depth[1], self.shape_depth[2]), preserve_range=True, mode='reflect', anti_aliasing=True )
x = np.clip(rgb_sample/255, 0, 1)
y = np.clip(depth_sample, 10, self.maxDepth)
y = DepthNorm(y, maxDepth=self.maxDepth)
batch_x[i] = x
batch_y[i] = y
if is_apply_policy: batch_x[i], batch_y[i] = self.policy(batch_x[i], batch_y[i])
#self.policy.debug_img(batch_x[i], np.clip(DepthNorm(batch_y[i],self.maxDepth)/self.maxDepth,0,1), index, i)
return batch_x, batch_y