-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimage.py
359 lines (285 loc) · 10.7 KB
/
image.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
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
#!/usr/bin/python
# encoding: utf-8
import random
import os
import pdb
import numpy as np
from PIL import Image
from PIL import ImageFile
from cfg import cfg
ImageFile.LOAD_TRUNCATED_IMAGES = True
def scale_image_channel(im, c, v):
cs = list(im.split())
cs[c] = cs[c].point(lambda i: i * v)
out = Image.merge(im.mode, tuple(cs))
return out
def distort_image(im, hue, sat, val):
im = im.convert('HSV')
cs = list(im.split())
cs[1] = cs[1].point(lambda i: i * sat)
cs[2] = cs[2].point(lambda i: i * val)
def change_hue(x):
x += hue * 255
if x > 255:
x -= 255
if x < 0:
x += 255
return x
cs[0] = cs[0].point(change_hue)
im = Image.merge(im.mode, tuple(cs))
im = im.convert('RGB')
# constrain_image(im)
return im
def rand_scale(s):
scale = random.uniform(1, s)
if (random.randint(1, 10000) % 2):
return scale
return 1. / scale
def random_distort_image(im, hue, saturation, exposure):
dhue = random.uniform(-hue, hue)
dsat = rand_scale(saturation)
dexp = rand_scale(exposure)
res = distort_image(im, dhue, dsat, dexp)
return res
def data_augmentation(img, shape, jitter, hue, saturation, exposure, flag=True):
oh = img.height
ow = img.width
dw = int(ow * jitter)
dh = int(oh * jitter)
if flag:
pleft = random.randint(-dw, dw)
pright = random.randint(-dw, dw)
ptop = random.randint(-dh, dh)
pbot = random.randint(-dh, dh)
flip = random.randint(1, 10000) % 2
swidth = ow - pleft - pright
sheight = oh - ptop - pbot
sx = float(swidth) / ow
sy = float(sheight) / oh
cropped = img.crop((pleft, ptop, pleft + swidth - 1, ptop + sheight - 1))
dx = (float(pleft) / ow) / sx
dy = (float(ptop) / oh) / sy
sized = cropped.resize(shape)
if flip:
sized = sized.transpose(Image.FLIP_LEFT_RIGHT)
img = random_distort_image(sized, hue, saturation, exposure)
else:
# pleft, pright, ptop, pbot, flip = 0, 0, 0, 0, 0
flip, dx, dy, sx, sy = 0, 0, 0, 1, 1
img = img.resize(shape)
return img, flip, dx, dy, sx, sy
def data_augmentation_detect(img, label, shape, jitter, hue, saturation, exposure, flag=True):
oh = img.height
ow = img.width
label = np.reshape(label, (-1, 5))
if random.random() < 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
label[:, [1, 3]] = ow - label[:, [3, 1]]
if random.random() < 0.5:
max_bbox = np.concatenate([np.min(label[:, 1:3], axis=0), np.max(label[:, 3:5], axis=0)], axis=-1)
max_l_trans = max_bbox[0]
max_u_trans = max_bbox[1]
max_r_trans = ow - max_bbox[2]
max_d_trans = oh - max_bbox[3]
crop_xmin = max(0, int(max_bbox[0] - random.uniform(0, max_l_trans)))
crop_ymin = max(0, int(max_bbox[1] - random.uniform(0, max_u_trans)))
crop_xmax = min(ow, int(max_bbox[2] + random.uniform(0, max_r_trans)))
crop_ymax = min(oh, int(max_bbox[3] + random.uniform(0, max_d_trans)))
img = img.crop((crop_xmin, crop_ymin, crop_xmax, crop_ymax))
label[:, [0, 2]] = label[:, [0, 2]] - crop_xmin
label[:, [1, 3]] = label[:, [1, 3]] - crop_ymin
ow = img.width
oh = img.height
def fill_truth_detection(labpath, w, h, flip, dx, dy, sx, sy):
max_boxes = cfg.max_boxes
label = np.zeros((max_boxes, 5))
if os.path.exists(labpath) and os.path.getsize(labpath):
bs = np.loadtxt(labpath)
if bs is None:
return label
bs = np.reshape(bs, (-1, 5))
cc = 0
for i in range(bs.shape[0]):
# Filter out bboxes not in base classes
imgid = labpath.split('/')[-1].split('.')[0]
clsid = int(bs[i][0])
# if clsid not in cfg.base_ids:
# continue
if clsid in cfg.base_ids:
keepit = True
elif cfg.yolo_joint and imgid in cfg.metaids:
keepit = True
else:
keepit = False
if not keepit:
continue
x1 = bs[i][1] - bs[i][3] / 2
y1 = bs[i][2] - bs[i][4] / 2
x2 = bs[i][1] + bs[i][3] / 2
y2 = bs[i][2] + bs[i][4] / 2
x1 = min(0.999, max(0, x1 * sx - dx))
y1 = min(0.999, max(0, y1 * sy - dy))
x2 = min(0.999, max(0, x2 * sx - dx))
y2 = min(0.999, max(0, y2 * sy - dy))
bs[i][1] = (x1 + x2) / 2
bs[i][2] = (y1 + y2) / 2
bs[i][3] = (x2 - x1)
bs[i][4] = (y2 - y1)
if flip:
bs[i][1] = 0.999 - bs[i][1]
if bs[i][3] < 0.001 or bs[i][4] < 0.001:
continue
label[cc] = bs[i]
cc += 1
if cc >= 50:
break
label = np.reshape(label, (-1))
return label
def fill_truth_detection_meta(labpath, w, h, flip, dx, dy, sx, sy):
max_boxes = cfg.max_boxes
n_cls = len(cfg.base_classes)
label = np.zeros((n_cls, max_boxes, 5))
if os.path.getsize(labpath):
bs = np.loadtxt(labpath)
if bs is None:
return label
bs = np.reshape(bs, (-1, 5))
ccs = [0] * n_cls
for i in range(bs.shape[0]):
# Filter out bboxes not in base classes
clsid = int(bs[i][0])
if clsid not in cfg.base_ids:
continue
x1 = bs[i][1] - bs[i][3] / 2
y1 = bs[i][2] - bs[i][4] / 2
x2 = bs[i][1] + bs[i][3] / 2
y2 = bs[i][2] + bs[i][4] / 2
x1 = min(0.999, max(0, x1 * sx - dx))
y1 = min(0.999, max(0, y1 * sy - dy))
x2 = min(0.999, max(0, x2 * sx - dx))
y2 = min(0.999, max(0, y2 * sy - dy))
bs[i][1] = (x1 + x2) / 2
bs[i][2] = (y1 + y2) / 2
bs[i][3] = (x2 - x1)
bs[i][4] = (y2 - y1)
if flip:
bs[i][1] = 0.999 - bs[i][1]
if bs[i][3] < 0.001 or bs[i][4] < 0.001:
continue
# Copy bbox info for building target
ind = cfg.base_ids.index(clsid)
if ind >= n_cls or ccs[ind] >= cfg.max_boxes:
pdb.set_trace()
label[ind][ccs[ind]] = bs[i]
label[ind][ccs[ind]][0] = ind
ccs[ind] += 1
if sum(ccs) >= 50:
break
label = np.reshape(label, (n_cls, -1))
return label
def fill_truth_detection_metaV2(labpath):
max_boxes = cfg.max_boxes
n_cls = len(cfg.base_classes)
label = np.zeros((n_cls, max_boxes, 5))
if os.path.exists(labpath) and os.path.getsize(labpath):
bs = np.loadtxt(labpath)
if bs is None:
return label
bs = np.reshape(bs, (-1, 5))
ccs = [0] * n_cls
for i in range(bs.shape[0]):
# Filter out bboxes not in base classes
clsid = int(bs[i][0])
if clsid not in cfg.base_ids:
continue
x1 = bs[i][1] - bs[i][3] / 2
y1 = bs[i][2] - bs[i][4] / 2
x2 = bs[i][1] + bs[i][3] / 2
y2 = bs[i][2] + bs[i][4] / 2
bs[i][1] = x1
bs[i][2] = y1
bs[i][3] = x2
bs[i][4] = y2
ind = cfg.base_ids.index(clsid)
if ind >= n_cls or ccs[ind] >= cfg.max_boxes:
pdb.set_trace()
label[ind][ccs[ind]] = bs[i]
label[ind][ccs[ind]][0] = ind
ccs[ind] += 1
if sum(ccs) >= 50:
break
return label
def load_label(labpath, w, h, flip, dx, dy, sx, sy):
label = []
# if os.path.exists(labpath) and os.path.getsize(labpath):
if os.path.getsize(labpath):
bs = np.loadtxt(labpath)
if bs is None:
return label
bs = np.reshape(bs, (-1, 5))
cc = 0
for i in range(bs.shape[0]):
x1 = bs[i][1] - bs[i][3] / 2
y1 = bs[i][2] - bs[i][4] / 2
x2 = bs[i][1] + bs[i][3] / 2
y2 = bs[i][2] + bs[i][4] / 2
x1 = min(0.999, max(0, x1 * sx - dx))
y1 = min(0.999, max(0, y1 * sy - dy))
x2 = min(0.999, max(0, x2 * sx - dx))
y2 = min(0.999, max(0, y2 * sy - dy))
bs[i][1] = (x1 + x2) / 2
bs[i][2] = (y1 + y2) / 2
bs[i][3] = (x2 - x1)
bs[i][4] = (y2 - y1)
if flip:
bs[i][1] = 0.999 - bs[i][1]
if bs[i][3] < 0.001 or bs[i][4] < 0.001:
continue
# label[cc] = bs[i]
label.append(bs[i, 1:])
cc += 1
if cc >= 50:
break
return label
def load_labelV2(labpath):
label = []
if os.path.getsize(labpath):
bs = np.loadtxt(labpath)
if bs is None:
return label
bs = np.reshape(bs, (-1, 5))
cc = 0
for i in range(bs.shape[0]):
x1 = bs[i][1] - bs[i][3] / 2
y1 = bs[i][2] - bs[i][4] / 2
x2 = bs[i][1] + bs[i][3] / 2
y2 = bs[i][2] + bs[i][4] / 2
bs[i][1] = x1
bs[i][2] = y1
bs[i][3] = x2
bs[i][4] = y2
label.append(bs[i, 1:])
cc += 1
if cc >= 50:
break
return label
def load_data_detection(imgpath, labpath, shape, jitter, hue, saturation, exposure, data_aug=True):
# labpath = imgpath.replace('images', 'labels').replace('JPEGImages', 'labels').replace('.jpg', '.txt').replace('.png','.txt')
# labpath = imgpath.replace('images', 'labels_1c/aeroplane').replace('JPEGImages', 'labels_1c/aeroplane').replace('.jpg', '.txt').replace('.png','.txt')
## data augmentation
img = Image.open(imgpath).convert('RGB')
img, flip, dx, dy, sx, sy = data_augmentation(img, shape, jitter, hue, saturation, exposure, flag=data_aug)
if cfg.metayolo:
label = fill_truth_detection_meta(labpath, img.width, img.height, flip, dx, dy, 1. / sx, 1. / sy)
else:
label = fill_truth_detection(labpath, img.width, img.height, flip, dx, dy, 1. / sx, 1. / sy)
return img, label
def load_data_detectionV2(imgpath, labpath, shape, jitter, hue, saturation, exposure, data_aug=True):
img = Image.open(imgpath).convert('RGB')
label = fill_truth_detection_metaV2(labpath)
def load_data_with_label(imgpath, labpath, shape, jitter, hue, saturation, exposure, data_aug=True):
## data augmentation
img = Image.open(imgpath).convert('RGB')
img, flip, dx, dy, sx, sy = data_augmentation(img, shape, jitter, hue, saturation, exposure, flag=data_aug)
label = load_label(labpath, img.width, img.height, flip, dx, dy, 1. / sx, 1. / sy)
return img, label