forked from brodyh/caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriving_utils.py
160 lines (139 loc) · 5.14 KB
/
driving_utils.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
import numpy as np
import scipy
import cv2
import Image
class Rect():
def __init__(self, xmin, ymin, xmax, ymax):
if xmax < xmin:
xmax = xmin
if ymax < ymin:
ymax = ymin
self.xmin = int(xmin)
self.ymin = int(ymin)
self.xmax = int(xmax)
self.ymax = int(ymax)
self.w = self.xmax - self.xmin + 1
self.h = self.ymax - self.ymin + 1
def area(self):
return self.w * self.h
def jaccard(self, other):
xmin = max(self.xmin, other.xmin)
xmax = min(self.xmax, other.xmax)
ymin = max(self.ymin, other.ymin)
ymax = min(self.ymax, other.ymax)
if ymax >= ymin and xmax >= xmin:
intersect = (xmax - xmin + 1) * (ymax - ymin + 1)
else:
return 0
return float(intersect) / (self.area() + other.area() - intersect)
def __repr__(self):
return '(%d,%d,%d,%d)' % (self.xmin, self.ymin, self.xmax, self.ymax)
def __str__(self):
return '(%d,%d,%d,%d)' % (self.xmin, self.ymin, self.xmax, self.ymax)
def get_gt_bbs(bbs):
assert len(bbs) % 4 == 0
rbbs = []
for i in range(0, len(bbs), 4):
rbbs.append(Rect(*bbs[i:i+4]))
return rbbs
def draw_rects(image, rects, color_channel = 1):
for r in rects:
image[r.ymin:r.ymax+1, r.xmin:r.xmin+2, color_channel] = 1
image[r.ymin:r.ymax+1, r.xmax:r.xmax+2, color_channel] = 1
image[r.ymin:r.ymin+2, r.xmin:r.xmax+1, color_channel] = 1
image[r.ymax:r.ymax+2, r.xmin:r.xmax+1, color_channel] = 1
return image
def get_mask_legacy(feat):
mask = np.empty((60, 80))
for y in range(15):
for x in range(20):
mask[y*4:(y+1)*4, x*4:(x+1)*4] = feat[:, y, x].reshape((4, 4))
return mask
def get_mask(feat):
mask = feat[1, :, :]
display_mask = np.zeros(mask.shape)
display_mask[4:,4:] = mask[:-4,:-4]
return mask, display_mask
def get_image(net, mask, rects, unmatched_gts = [], unmatched_rects = []):
image = net.deprocess('data', net.blobs['data'].data[4])
zoomed_mask = np.empty((480, 640))
zoomed_mask = scipy.ndimage.zoom(mask, 480 / mask.shape[0], order=0)
masked_image = image.transpose((2, 0, 1))
masked_image[0, :, :] += zoomed_mask
masked_image = np.clip(masked_image, 0, 1)
masked_image = masked_image.transpose((1, 2, 0))
boxed_image = np.copy(masked_image)
if len(rects) > 0:
boxed_image = draw_rects(boxed_image, rects)
if len(unmatched_gts) > 0:
boxed_image = draw_rects(boxed_image, unmatched_gts, 2)
if len(unmatched_rects) > 0:
boxed_image = draw_rects(boxed_image, unmatched_rects, 0)
return (boxed_image * 255).astype('uint8')
def dump_image(net, mask, rects, path, unmatched_gts = [], unmatched_rects = []):
image = get_image(net, mask, rects, unmatched_gts, unmatched_rects)
Image.fromarray(image).save(path)
def get_rects_legacy(feat, mask, boundary_offset = 0):
hard_mask = np.round(mask + 0.25)
bb = np.empty((4, 60, 80))
for y in range(15):
for x in range(20):
for c in range(4):
bb[c, y*4:(y+1)*4, x*4:(x+1)*4] = feat[c*16:(c+1)*16, y, x].reshape((4, 4))
for c in range(4):
bb[c, :, :] *= hard_mask
y_offset = np.array([np.arange(16, 480, 32)]).T
y_offset = np.tile(y_offset, (1, 20))
x_offset = np.arange(16, 640, 32)
x_offset = np.tile(x_offset, (15, 1))
y_offset = scipy.ndimage.zoom(y_offset, 4, order=0)
x_offset = scipy.ndimage.zoom(x_offset, 4, order=0)
bb[0, :, :] += x_offset
bb[2, :, :] += x_offset
bb[1, :, :] += y_offset
bb[3, :, :] += y_offset
selected_rects = hard_mask > 0
num_rects = np.sum(selected_rects)
rects = np.empty((num_rects, 4))
for i in range(4):
rects[:, i] = bb[i, selected_rects] + boundary_offset
rects = rects[np.logical_and((rects[:, 2] - rects[:, 0]) > 0, (rects[:, 3] - rects[:, 1]) > 0), :]
rects[:, (2, 3)] -= rects[:, (0, 1)]
rects = np.clip(rects, 0, 640)
rects = [rects[i, :] for i in range(rects.shape[0])]
rects, scores = cv2.groupRectangles(rects, 2, 0.15)
rectangles = []
if len(rects) == 0:
return rectangles
for i in range(rects.shape[0]):
rectangles.append(Rect(rects[i, 0], rects[i, 1], rects[i, 0] + rects[i, 2], rects[i, 1] + rects[i, 3]))
return rectangles
def get_rects(feat, mask, boundary_offset = 0):
hard_mask = np.round(mask + 0.25)
bb = np.copy(feat)
for c in range(4):
bb[c, :, :] *= hard_mask
y_offset = np.array([np.arange(0, 480, 4)]).T
y_offset = np.tile(y_offset, (1, 160))
x_offset = np.arange(0, 640, 4)
x_offset = np.tile(x_offset, (120, 1))
bb[0, :, :] += x_offset
bb[2, :, :] += x_offset
bb[1, :, :] += y_offset
bb[3, :, :] += y_offset
selected_rects = hard_mask > 0
num_rects = np.sum(selected_rects)
rects = np.empty((num_rects, 4))
for i in range(4):
rects[:, i] = bb[i, selected_rects] + boundary_offset
rects = rects[np.logical_and((rects[:, 2] - rects[:, 0]) > 0, (rects[:, 3] - rects[:, 1]) > 0), :]
rects[:, (2, 3)] -= rects[:, (0, 1)]
rects = np.clip(rects, 0, 640)
rects = [rects[i, :] for i in range(rects.shape[0])]
rects, scores = cv2.groupRectangles(rects, 2, 0.15)
rectangles = []
if len(rects) == 0:
return rectangles
for i in range(rects.shape[0]):
rectangles.append(Rect(rects[i, 0], rects[i, 1], rects[i, 0] + rects[i, 2], rects[i, 1] + rects[i, 3]))
return rectangles