forked from matthewearl/deep-anpr
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdetect_simple.py
More file actions
executable file
·185 lines (143 loc) · 6.03 KB
/
detect_simple.py
File metadata and controls
executable file
·185 lines (143 loc) · 6.03 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
#!/usr/bin/env python
#
# Copyright (c) 2016 Matthew Earl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
Routines for training the network.
"""
__all__ = (
'detect_simple',
)
import glob
import sys
import cv2
import numpy
import tensorflow as tf
import common
import model
import re
def code_to_vec(p, code):
def char_to_vec(c):
y = numpy.zeros((len(common.CHARS),))
y[common.CHARS.index(c)] = 1.0
return y
c = numpy.vstack([char_to_vec(c) for c in code])
return numpy.concatenate([[1. if p else 0], c.flatten()])
def read_data(img_glob):
for fname in sorted(glob.glob(img_glob)):
code = fname.split("/")[1][9:16]
# discard images with less than 7 chars in license plate
if re.findall(r"\*", code) == []:
im = cv2.imread(fname)[:, :, 0].astype(numpy.float32) / 255.
p = fname.split("/")[1][17] == '1'
yield im, code_to_vec(p, code)
def unzip(b):
xs, ys = zip(*b)
xs = numpy.array(xs)
ys = numpy.array(ys)
return xs, ys
def batch(it, batch_size):
out = []
for x in it:
out.append(x)
if len(out) == batch_size:
yield out
out = []
if out:
yield out
def get_loss(y, y_):
# Calculate the loss from digits being incorrect. Don't count loss from
# digits that are in non-present plates.
digits_loss = tf.nn.softmax_cross_entropy_with_logits(
logits=tf.reshape(y[:, 1:],
[-1, len(common.CHARS)]),
labels=tf.reshape(y_[:, 1:],
[-1, len(common.CHARS)]))
digits_loss = tf.reshape(digits_loss, [-1, 7])
digits_loss = tf.reduce_sum(digits_loss, 1)
digits_loss *= (y_[:, 0] != 0)
digits_loss = tf.reduce_sum(digits_loss)
# Calculate the loss from presence indicator being wrong.
presence_loss = tf.nn.sigmoid_cross_entropy_with_logits(
logits=y[:, :1], labels=y_[:, :1])
presence_loss = 7 * tf.reduce_sum(presence_loss)
return digits_loss, presence_loss, digits_loss + presence_loss
def detect(initial_weights=None):
"""
Test the network.
The function operates interactively: Progress is reported on stdout, and
training ceases upon `KeyboardInterrupt` at which point the learned weights
are saved to `weights.npz`, and also returned.
:param initial_weights:
(Optional.) Weights to initialize the network with.
"""
x, y, params = model.get_training_model()
y_ = tf.placeholder(tf.float32, [None, 7 * len(common.CHARS) + 1])
digits_loss, presence_loss, loss = get_loss(y, y_)
best = tf.argmax(tf.reshape(y[:, 1:], [-1, 7, len(common.CHARS)]), 2)
correct = tf.argmax(tf.reshape(y_[:, 1:], [-1, 7, len(common.CHARS)]), 2)
assert len(params) == len(initial_weights)
assign_ops = [w.assign(v) for w, v in zip(params, initial_weights)]
#init = tf.initialize_all_variables() # deprecated
init = tf.global_variables_initializer()
def vec_to_plate(v):
return "".join(common.CHARS[i] for i in v)
def do_report():
r = sess.run([best,
correct,
tf.greater(y[:, 0], 0),
y_[:, 0],
digits_loss,
presence_loss,
loss],
feed_dict={x: test_xs, y_: test_ys})
num_correct = numpy.sum(
numpy.logical_or(
numpy.all(r[0] == r[1], axis=1),
numpy.logical_and(r[2] < 0.5,
r[3] < 0.5)))
num = numpy.equal(r[0], r[1])
num_correct_chars = numpy.sum(num)
r_short = (r[0][:190], r[1][:190], r[2][:190], r[3][:190])
for b, c, pb, pc in zip(*r_short):
print ("{} {} <-> {} {}".format(vec_to_plate(c), pc,
vec_to_plate(b), float(pb)))
print ("Num plates: {} corrChars: {:2.02f}% corrLPs: {:2.02f}% loss: {} (digits: {}, presence: {})".format(
len(r[0]),
100. * num_correct_chars / (len(r[0]) * 7),
100. * num_correct / (len(r[0])),
r[6],
r[4],
r[5]))
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
sess.run(init)
sess.run(assign_ops)
test_xs, test_ys = unzip(list(read_data("pickering_test/*.png"))[:])
do_report()
if __name__ == "__main__":
if len(sys.argv) > 1:
f = numpy.load(sys.argv[1])
initial_weights = [f[n] for n in sorted(f.files,
key=lambda s: int(s[4:]))]
else:
print("[ERROR] Usage example: python detect_simple.py weights.npz")
quit()
detect(initial_weights=initial_weights)