-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdata_manager.py
290 lines (216 loc) · 7.42 KB
/
data_manager.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
279
280
281
282
283
284
285
286
287
288
289
290
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from PIL import Image
import cv2
import numpy as np
import random
IMAGE_CAPACITY = 12288
OP_AND = 0
OP_IN_COMMON = 1
OP_IGNORE = 2
class DataManager(object):
def __init__(self):
pass
def _load_image(self, index, hsv=True):
file_name = "data/image{}.png".format(index)
if hsv:
# [HSV]
img = cv2.imread(file_name) # opencv returns [BGR] image
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Convert to HSV
scale = np.array([1.0/180.0, 1.0/255.0, 1.0/255.0],
dtype=np.float32) # Scale to fit 0.0 ~ 1.0
return hsv * scale
else:
# [RGB]
img = np.array(Image.open(file_name), dtype=np.float32)
return img * (1.0/255.0)
def prepare(self):
print("start filling image pool")
self.images = []
for i in range(IMAGE_CAPACITY):
image = self._load_image(i)
self.images.append(image)
print("finish filling image pool")
self._prepare_indices()
def _prepare_indices(self):
self.image_indices = list(range(IMAGE_CAPACITY))
random.shuffle(self.image_indices)
self.used_image_index = 0
def _get_masked_image(self, image):
masked_image = image.copy()
shape = masked_image.shape
h = shape[0]
w = shape[1]
h0 = np.random.randint(0,h)
h1 = np.random.randint(0,h)
w0 = np.random.randint(0,w)
w1 = np.random.randint(0,w)
if h0 <= h1:
hmin = h0
hmax = h1+1
else:
hmin = h1
hmax = h0+1
if w0 <= w1:
wmin = w0
wmax = w1+1
else:
wmin = w1
wmax = w0+1
masked_image[hmin:hmax,wmin:wmax,:] = 0.0
return masked_image
def _randint_excepting(self, high, excep):
list = []
for r in range(high):
if r != excep:
list.append(r)
random.shuffle(list)
return list[0]
def _choose_indices(self, high, size):
indices = list(range(high))
random.shuffle(indices)
ret = indices[:size]
# result is not sorted
return ret
def _choose_op_triplet(self, op_type):
param_sizes = [8, 8, 8, 3]
param0 = [-1, -1, -1, -1] # input0
param1 = [-1, -1, -1, -1] # input1
param_out = [-1, -1, -1, -1] # output
if op_type == OP_AND:
taret_type0 = np.random.randint(4)
taret_type1 = self._randint_excepting(4, taret_type0)
param0[taret_type0] = np.random.randint(0, param_sizes[taret_type0])
param1[taret_type1] = np.random.randint(0, param_sizes[taret_type1])
param_out[taret_type0] = param0[taret_type0]
param_out[taret_type1] = param1[taret_type1]
elif op_type == OP_IN_COMMON:
target_size0 = np.random.randint(1, 5) # 1,2,3,4
target_types0 = self._choose_indices(4, target_size0) # not sorted
common_target_type = target_types0[0]
target_size1 = np.random.randint(1, 5) # 1,2,3,4
target_types1 = self._choose_indices(4, target_size1) # not sorted
if common_target_type not in target_types1:
target_types1.append(common_target_type)
for i in range(4):
if i in target_types0:
param0[i] = np.random.randint(param_sizes[i])
if i in target_types1:
if i == common_target_type:
param1[i] = param0[i]
param_out[i] = param0[i]
elif param0[i] != -1:
param1[i] = self._randint_excepting(param_sizes[i], param0[i])
else:
param1[i] = np.random.randint(param_sizes[i])
elif op_type == OP_IGNORE:
target_size0 = np.random.randint(2, 5) # 1,2,3,4
target_types0 = self._choose_indices(4, target_size0) # not sorted
ignore_target_type = target_types0[0]
for i in range(4):
if i in target_types0:
param0[i] = np.random.randint(param_sizes[i])
if i == ignore_target_type:
param1[i] = param0[i]
else:
param_out[i] = param0[i]
return param0, param1, param_out
def get_op_training_batch(self, batch_size):
ys0 = []
ys1 = []
xs = []
ys = []
hs = []
for i in range(batch_size):
op_type = np.random.randint(0,3)
param0, param1, param_out = self._choose_op_triplet(op_type)
y0 = self.get_labels(param0[0], param0[1], param0[2], param0[3])
y1 = self.get_labels(param1[0], param1[1], param1[2], param1[3])
y = self.get_labels(param_out[0], param_out[1], param_out[2], param_out[3])
x = self.get_image(param_out[0], param_out[1], param_out[2], param_out[3])
ys0.append(y0)
ys1.append(y1)
ys.append(y)
xs.append(x)
hs.append(op_type)
return ys0, ys1, ys, xs, hs
def get_labels(self, obj_color=-1, wall_color=-1, floor_color=-1, obj_id=-1):
""" Get labels (float array with 51 values of 0.0 or 1.0) by specifing each elements.
If element is -1, it means that element (color or object type) is not specified.
"""
labels = np.zeros(3+16*3, dtype=np.float32)
if obj_color >= 0:
labels[obj_color] = 1.0
if wall_color >= 0:
labels[16 + wall_color] = 1.0
if floor_color >= 0:
labels[32 + floor_color] = 1.0
if obj_id >= 0:
labels[48 + obj_id] = 1.0
return labels
def _index_to_labels(self, index):
obj_color = index % 16
index = index // 16
wall_color = index % 16
index = index // 16
floor_color = index % 16
index = index // 16
obj_id = index % 3
return self.get_labels(obj_color, wall_color, floor_color, obj_id)
def choose_labels(self, y):
""" retrieve label element from img2sym output. """
label_indices = []
for i,v in enumerate(y):
if random.random() <= v:
label_indices.append(i)
obj_color = []
wall_color = []
floor_color = []
obj_id = []
for index in label_indices:
if index < 16:
obj_color.append(index)
elif index < 32:
wall_color.append(index-16)
elif index < 48:
floor_color.append(index-32)
else:
obj_id.append(index-48)
return (obj_color, wall_color, floor_color, obj_id)
def next_batch(self, batch_size, use_labels=False):
batch = []
if use_labels:
labels_batch = []
for i in range(batch_size):
index = self.image_indices[self.used_image_index]
image = self.images[index]
batch.append(image)
if use_labels:
labels = self._index_to_labels(index)
labels_batch.append(labels)
self.used_image_index += 1
if self.used_image_index >= IMAGE_CAPACITY:
self._prepare_indices()
if use_labels:
return (batch, labels_batch)
else:
return batch
def next_masked_batch(self, batch_size):
batch_org = []
batch_masked = []
for i in range(batch_size):
index = self.image_indices[self.used_image_index]
image = self.images[index]
batch_org.append(image)
masked_image = self._get_masked_image(image)
batch_masked.append(masked_image)
self.used_image_index += 1
if self.used_image_index >= IMAGE_CAPACITY:
self._prepare_indices()
return batch_masked, batch_org
def get_image(self, obj_color, wall_color, floor_color, obj_id):
index = obj_color + wall_color * 16 + floor_color * 16 * 16 + obj_id * 16 * 16 * 16
image = self.images[index]
return image