-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
308 lines (261 loc) · 9.08 KB
/
common.py
File metadata and controls
308 lines (261 loc) · 9.08 KB
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
# -*- coding: utf-8 -*-
# File: common.py
import numpy as np
import cv2
from tensorpack.dataflow import RNGDataFlow
from tensorpack.dataflow.imgaug import ImageAugmentor, ResizeTransform,Transform
from tensorpack.dataflow.imgaug.transform import WarpAffineTransform
class DataFromListOfDict(RNGDataFlow):
def __init__(self, lst, keys, shuffle=False):
self._lst = lst
self._keys = keys
self._shuffle = shuffle
self._size = len(lst)
def __len__(self):
return self._size
def __iter__(self):
if self._shuffle:
self.rng.shuffle(self._lst)
for dic in self._lst:
dp = [dic[k] for k in self._keys]
yield dp
class CustomResize(ImageAugmentor):
"""
Try resizing the shortest edge to a certain number
while avoiding the longest edge to exceed max_size.
"""
def __init__(self, short_edge_length, max_size, interp=cv2.INTER_LINEAR):
"""
Args:
short_edge_length ([int, int]): a [min, max] interval from which to sample the
shortest edge length.
max_size (int): maximum allowed longest edge length.
"""
super(CustomResize, self).__init__()
if isinstance(short_edge_length, int):
short_edge_length = (short_edge_length, short_edge_length)
self._init(locals())
def get_transform(self, img):
h, w = img.shape[:2]
size = self.rng.randint(
self.short_edge_length[0], self.short_edge_length[1] + 1)
scale = size * 1.0 / min(h, w)
if h < w:
newh, neww = size, scale * w
else:
newh, neww = scale * h, size
if max(newh, neww) > self.max_size:
scale = self.max_size * 1.0 / max(newh, neww)
newh = newh * scale
neww = neww * scale
neww = int(neww + 0.5)
newh = int(newh + 0.5)
return ResizeTransform(h, w, newh, neww, self.interp)
class Random_Resize(ImageAugmentor):
''' 图片的尺寸应能整除一些数字
'''
def __init__(self, short_edge_length, max_size,ratios=(3/4.,4/3.),divs=32, interp=cv2.INTER_LINEAR):
"""
Args:
short_edge_length ([int, int]): a [min, max] interval from which to sample the
shortest edge length.
max_size (int): maximum allowed longest edge length.
"""
super(Random_Resize, self).__init__()
self.divs = divs
if isinstance(short_edge_length, int):
short_edge_length = (short_edge_length, short_edge_length)
self._init(locals())
def get_transform(self, img):
h, w = img.shape[:2]
size = self.rng.randint(
self.short_edge_length[0], self.short_edge_length[1] + 1)
scale = size * 1.0 / min(h, w)
rt = self.rng.rand()*(self.ratios[1]-self.ratios[0])+self.ratios[0]
rt = np.sqrt(rt)
rt = np.sqrt(rt)
# rt=1
if h < w:
newh, neww = size, scale * w
else:
newh, neww = scale * h, size
if max(newh, neww) > self.max_size:
scale = self.max_size * 1.0 / max(newh, neww)
newh = newh * scale
neww = neww * scale
neww = int(neww/rt + 0.5)
newh = int(newh*rt + 0.5)
neww = ((neww+self.divs-1)//self.divs)*self.divs
newh = ((newh+self.divs-1)//self.divs)*self.divs
return ResizeTransform(h, w, newh, neww, self.interp)
class CusRotation(ImageAugmentor):
""" Random rotate the image w.r.t a random center
原始版本会切除图像,避免这种情况进行resize
"""
def __init__(self, max_deg, center_range=(0, 1),
interp=cv2.INTER_LINEAR,
border=cv2.BORDER_REPLICATE, step_deg=None, border_value=0):
assert step_deg is None or (max_deg == 180 and max_deg % step_deg == 0)
super(CusRotation, self).__init__()
self._init(locals())
def get_transform(self, img):
center = img.shape[1::-1] * self._rand_range(
self.center_range[0], self.center_range[1], (2,))
deg = self._rand_range(-self.max_deg, self.max_deg)
if self.step_deg:
deg = deg // self.step_deg * self.step_deg
# print(deg)
matrix = cv2.getRotationMatrix2D(tuple(center - 0.5), deg, 1)
width, height = img.shape[1::-1]
cos = np.abs(matrix[0,0])
sin = np.abs(matrix[0,1])
new_W = int((height * sin) + (width * cos))
new_H = int((height * cos) + (width * sin))
matrix[0,2] += (new_W/2) - width/2
matrix[1,2] += ((new_H/2)) - height/2
return WarpAffineTransform(
matrix, (new_W,new_H), interp=self.interp,
borderMode=self.border, borderValue=self.border_value)
class Resize(ImageAugmentor):
''' 图片的尺寸应能整除一些数字
'''
def __init__(self, min_length, max_length,keep_ratio=False,divs=32, interp=cv2.INTER_LINEAR):
"""
Args:
short_edge_length ([int, int]): a [min, max] interval from which to sample the
shortest edge length.
max_size (int): maximum allowed longest edge length.
"""
super(Resize, self).__init__()
self.divs = divs
self.min_length = min_length
self.max_length = max_length
self.keep_ratio = keep_ratio
self._init(locals())
def get_transform(self, img):
h, w = img.shape[:2]
if not self.keep_ratio:
if h < w:
newh, neww = self.min_length, self.max_length
else:
newh, neww = self.max_length, self.min_length
else:
sacle = self.max_length/max(h,w)
# sacle = self.min_length/min(h,w)
newh,neww = h*sacle,w*sacle
neww = int(neww)
newh = int(newh)
neww = ((neww+self.divs-1)//self.divs)*self.divs
newh = ((newh+self.divs-1)//self.divs)*self.divs
return ResizeTransform(h, w, newh, neww, self.interp)
class OrderTransform(Transform):
"""
调整polygon点的顺序
"""
def __init__(self):
self._init(locals())
def apply_image(self, img):
return img
def apply_coords(self, coords):
# 总共分两步:
# 确保点的顺序为顺时针
# 确定起点
return coords
class Sort_Order(ImageAugmentor):
"""
确保polygon使用合适的顺序
"""
def __init__(self, horiz=False, vert=False, prob=0.5):
"""
Args:
horiz (bool): use horizontal flip.
vert (bool): use vertical flip.
prob (float): probability of flip.
"""
super(Sort_Order, self).__init__()
self._init(locals())
def get_transform(self, img):
return NoOpTransform()
def box_to_point4(boxes):
"""
Convert boxes to its corner points.
Args:
boxes: nx4
Returns:
(nx4)x2
"""
b = boxes[:, [0, 1, 2, 3, 0, 3, 2, 1]]
b = b.reshape((-1, 2))
return b
def point4_to_box(points):
"""
Args:
points: (nx4)x2
Returns:
nx4 boxes (x1y1x2y2)
"""
p = points.reshape((-1, 4, 2))
minxy = p.min(axis=1) # nx2
maxxy = p.max(axis=1) # nx2
return np.concatenate((minxy, maxxy), axis=1)
def polygons_to_mask(polys, height, width):
"""
Convert polygons to binary masks.
Args:
polys: a list of nx2 float array. Each array contains many (x, y) coordinates.
Returns:
a binary matrix of (height, width)
"""
polys = [p.flatten().tolist() for p in polys]
assert len(polys) > 0, "Polygons are empty!"
import pycocotools.mask as cocomask
rles = cocomask.frPyObjects(polys, height, width)
rle = cocomask.merge(rles)
return cocomask.decode(rle)
def clip_boxes(boxes, shape):
"""
Args:
boxes: (...)x4, float
shape: h, w
"""
orig_shape = boxes.shape
boxes = boxes.reshape([-1, 4])
h, w = shape
boxes[:, [0, 1]] = np.maximum(boxes[:, [0, 1]], 0)
boxes[:, 2] = np.minimum(boxes[:, 2], w)
boxes[:, 3] = np.minimum(boxes[:, 3], h)
return boxes.reshape(orig_shape)
def filter_boxes_inside_shape(boxes, shape):
"""
Args:
boxes: (nx4), float
shape: (h, w)
Returns:
indices: (k, )
selection: (kx4)
"""
assert boxes.ndim == 2, boxes.shape
assert len(shape) == 2, shape
h, w = shape
indices = np.where(
(boxes[:, 0] >= 0) &
(boxes[:, 1] >= 0) &
(boxes[:, 2] <= w) &
(boxes[:, 3] <= h))[0]
return indices, boxes[indices, :]
try:
import pycocotools.mask as cocomask
# Much faster than utils/np_box_ops
def np_iou(A, B):
def to_xywh(box):
box = box.copy()
box[:, 2] -= box[:, 0]
box[:, 3] -= box[:, 1]
return box
ret = cocomask.iou(
to_xywh(A), to_xywh(B),
np.zeros((len(B),), dtype=np.bool))
# can accelerate even more, if using float32
return ret.astype('float32')
except ImportError:
from utils.np_box_ops import iou as np_iou # noqa