-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
289 lines (226 loc) · 9.85 KB
/
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
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
import argparse
import math
import cv2
import tensorflow as tf
import numpy as np
from collections import namedtuple
#-------------------------------------------------------------------------------
def initialize_uninitialized_variables(sess):
"""
Only initialize the weights that have not yet been initialized by other
means, such as importing a metagraph and a checkpoint. It's useful when
extending an existing model.
"""
uninit_vars = []
uninit_tensors = []
for var in tf.global_variables():
uninit_vars.append(var)
uninit_tensors.append(tf.is_variable_initialized(var))
uninit_bools = sess.run(uninit_tensors)
uninit = zip(uninit_bools, uninit_vars)
uninit = [var for init, var in uninit if not init]
sess.run(tf.variables_initializer(uninit))
#-------------------------------------------------------------------------------
def load_data_source(data_source):
"""
Load a data source given it's name
"""
source_module = __import__('source_'+data_source)
get_source = getattr(source_module, 'get_source')
return get_source()
#-------------------------------------------------------------------------------
def rgb2bgr(tpl):
"""
Convert RGB color tuple to BGR
"""
return (tpl[2], tpl[1], tpl[0])
#-------------------------------------------------------------------------------
Label = namedtuple('Label', ['name', 'color'])
Size = namedtuple('Size', ['w', 'h'])
Point = namedtuple('Point', ['x', 'y'])
Sample = namedtuple('Sample', ['filename', 'boxes', 'imgsize'])
Box = namedtuple('Box', ['label', 'labelid', 'center', 'size'])
Score = namedtuple('Score', ['idx', 'score'])
Overlap = namedtuple('Overlap', ['best', 'good'])
#-------------------------------------------------------------------------------
def str2bool(v):
"""
Convert a string to a boolean
"""
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
#-------------------------------------------------------------------------------
def abs2prop(xmin, xmax, ymin, ymax, imgsize):
"""
Convert the absolute min-max box bound to proportional center-width bounds
"""
width = float(xmax-xmin)
height = float(ymax-ymin)
cx = float(xmin)+width/2
cy = float(ymin)+height/2
width /= imgsize.w
height /= imgsize.h
cx /= imgsize.w
cy /= imgsize.h
return Point(cx, cy), Size(width, height)
#-------------------------------------------------------------------------------
def prop2abs(center, size, imgsize):
"""
Convert proportional center-width bounds to absolute min-max bounds
"""
width2 = size.w*imgsize.w/2
height2 = size.h*imgsize.h/2
cx = center.x*imgsize.w
cy = center.y*imgsize.h
return int(cx-width2), int(cx+width2), int(cy-height2), int(cy+height2)
#-------------------------------------------------------------------------------
def box_is_valid(box):
for x in [box.center.x, box.center.y, box.size.w, box.size.h]:
if math.isnan(x) or math.isinf(x):
return False
return True
#-------------------------------------------------------------------------------
def normalize_box(box):
if not box_is_valid(box):
return box
img_size = Size(1000, 1000)
xmin, xmax, ymin, ymax = prop2abs(box.center, box.size, img_size)
xmin = max(xmin, 0)
xmax = min(xmax, img_size.w-1)
ymin = max(ymin, 0)
ymax = min(ymax, img_size.h-1)
# this happens early in the training when box min and max are outside
# of the image
xmin = min(xmin, xmax)
ymin = min(ymin, ymax)
center, size = abs2prop(xmin, xmax, ymin, ymax, img_size)
return Box(box.label, box.labelid, center, size)
#-------------------------------------------------------------------------------
def draw_box(img, box, color):
img_size = Size(img.shape[1], img.shape[0])
xmin, xmax, ymin, ymax = prop2abs(box.center, box.size, img_size)
img_box = np.copy(img)
cv2.rectangle(img_box, (xmin, ymin), (xmax, ymax), color, 2)
cv2.rectangle(img_box, (xmin-1, ymin), (xmax+1, ymin-20), color, cv2.FILLED)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img_box, box.label, (xmin+5, ymin-5), font, 0.5,
(255, 255, 255), 1, cv2.LINE_AA)
alpha = 0.8
cv2.addWeighted(img_box, alpha, img, 1.-alpha, 0, img)
#-------------------------------------------------------------------------------
class PrecisionSummary:
#---------------------------------------------------------------------------
def __init__(self, session, writer, sample_name, labels, restore=False):
self.session = session
self.writer = writer
self.labels = labels
sess = session
ph_name = sample_name+'_mAP_ph'
sum_name = sample_name+'_mAP'
if restore:
self.mAP_placeholder = sess.graph.get_tensor_by_name(ph_name+':0')
self.mAP_summary_op = sess.graph.get_tensor_by_name(sum_name+':0')
else:
self.mAP_placeholder = tf.placeholder(tf.float32, name=ph_name)
self.mAP_summary_op = tf.summary.scalar(sum_name,
self.mAP_placeholder)
self.placeholders = {}
self.summary_ops = {}
for label in labels:
sum_name = sample_name+'_AP_'+label
ph_name = sample_name+'_AP_ph_'+label
if restore:
placeholder = sess.graph.get_tensor_by_name(ph_name+':0')
summary_op = sess.graph.get_tensor_by_name(sum_name+':0')
else:
placeholder = tf.placeholder(tf.float32, name=ph_name)
summary_op = tf.summary.scalar(sum_name, placeholder)
self.placeholders[label] = placeholder
self.summary_ops[label] = summary_op
#---------------------------------------------------------------------------
def push(self, epoch, mAP, APs):
if not APs:
return
feed = {self.mAP_placeholder: mAP}
tensors = [self.mAP_summary_op]
for label in self.labels:
feed[self.placeholders[label]] = APs[label]
tensors.append(self.summary_ops[label])
summaries = self.session.run(tensors, feed_dict=feed)
for summary in summaries:
self.writer.add_summary(summary, epoch)
#-------------------------------------------------------------------------------
class ImageSummary:
#---------------------------------------------------------------------------
def __init__(self, session, writer, sample_name, colors, restore=False):
self.session = session
self.writer = writer
self.colors = colors
sess = session
sum_name = sample_name+'_img'
ph_name = sample_name+'_img_ph'
if restore:
self.img_placeholder = sess.graph.get_tensor_by_name(ph_name+':0')
self.img_summary_op = sess.graph.get_tensor_by_name(sum_name+':0')
else:
self.img_placeholder = tf.placeholder(tf.float32, name=ph_name,
shape=[None, None, None, 3])
self.img_summary_op = tf.summary.image(sum_name,
self.img_placeholder)
#---------------------------------------------------------------------------
def push(self, epoch, samples):
imgs = np.zeros((3, 512, 512, 3))
for i, sample in enumerate(samples):
img = cv2.resize(sample[0], (512, 512))
for _, box in sample[1]:
draw_box(img, box, self.colors[box.label])
img[img > 255] = 255
img[img < 0] = 0
imgs[i] = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_BGR2RGB)
feed = {self.img_placeholder: imgs}
summary = self.session.run(self.img_summary_op, feed_dict=feed)
self.writer.add_summary(summary, epoch)
#-------------------------------------------------------------------------------
class LossSummary:
#---------------------------------------------------------------------------
def __init__(self, session, writer, sample_name, num_samples,
restore=False):
self.session = session
self.writer = writer
self.num_samples = num_samples
self.loss_names = ['total', 'localization', 'confidence', 'l2']
self.loss_values = {}
self.placeholders = {}
sess = session
summary_ops = []
for loss in self.loss_names:
sum_name = sample_name+'_'+loss+'_loss'
ph_name = sample_name+'_'+loss+'_loss_ph'
if restore:
placeholder = sess.graph.get_tensor_by_name(ph_name+':0')
summary_op = sess.graph.get_tensor_by_name(sum_name+':0')
else:
placeholder = tf.placeholder(tf.float32, name=ph_name)
summary_op = tf.summary.scalar(sum_name, placeholder)
self.loss_values[loss] = float(0)
self.placeholders[loss] = placeholder
summary_ops.append(summary_op)
self.summary_ops = tf.summary.merge(summary_ops)
#---------------------------------------------------------------------------
def add(self, values, num_samples):
for loss in self.loss_names:
self.loss_values[loss] += values[loss]*num_samples
#---------------------------------------------------------------------------
def push(self, epoch):
feed = {}
for loss in self.loss_names:
feed[self.placeholders[loss]] = \
self.loss_values[loss]/self.num_samples
summary = self.session.run(self.summary_ops, feed_dict=feed)
self.writer.add_summary(summary, epoch)
for loss in self.loss_names:
self.loss_values[loss] = float(0)