自制正方软件系统验证码的识别程序(3/4)

util.py

这个文件里主要提供了5个函数,提供给package.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
import Image,os,ImageFilter
import numpy as np
table1=[]
table2=[]
threshold1=18
for i in range(256):
if i<threshold1:
table1.append(0)
else:
table1.append(1)
threshold2=240
for i in range(256):
if i<threshold2:
table2.append(0)
else:
table2.append(1)

def blur1(im):
im=im.convert('L')
im=im.point(table1,'1')
return im
def blur2(im):
im=im.convert('L')
im=im.filter(ImageFilter.GaussianBlur(1.5))
im=im.point(table2,'1')
return im

def seed_fill(im):
arr=np.array(im.convert('L')).astype('bool').astype('int')
height,width=arr.shape
arr=arr.tolist()

output=[]
output.append(np.ones(width+2,).astype('int').tolist())
for i in range(height):
tmp=[]
tmp.append(1)
for j in range(width):
tmp.append(arr[i][j])
tmp.append(1)
output.append(tmp)
output.append(np.ones(width+2,).astype('int').tolist())
arr=output
height+=2
width+=2
def dfs(i,j):
if(arr[i][j]==0):
return 0
arr[i][j]=0
if(i>0):
dfs(i-1,j)
if(i<height-1):
dfs(i+1,j)
if(j>0):
dfs(i,j-1)
if(j<width-1):
dfs(i,j+1)
return 1
cnt=0
for i in range(height):
for j in range(width):
cnt+=dfs(i,j)
return cnt

def count_border(im):
arr=np.array(im.convert('L')).astype('bool').astype('int')
height,width=arr.shape
cnt=0
for i in range(height):
for j in range(width):
if arr[i][j]==1:
continue
if i==0 or i==height-1 or j==0 or j==width-1 :
cnt+=1
continue
if arr[i-1][j]==1 or arr[i+1][j]==1 or arr[i][j-1]==1 or arr[i][j+1]==1:
cnt+=1
return cnt

def count_fill(im):
arr=np.array(im.convert('L')).astype('bool').astype('int')
height,width=arr.shape
cnt=0
for i in range(height):
for j in range(width):
if arr[i][j]==0:
cnt+=1
return cnt
  • blur1函数实现的是对图片先进行灰度化,再进行二值化,结合table1数组的使用,将亮度大于某一较低阈值的统统设为纯白,否则为纯黑。这样就只保留了字符的骨架,生成的图片十分便于特征值的测量与计算,而且极大消除了噪声的影响。
  • blur2函数实现的是对图片的模糊化再二值化,为的是将图片变“粗”一点,便于后续的学习(这也是后来偶然发现的提高学习准确性的方法)。注意模糊参数的调节。
  • seed_fill函数实现种子填充,即返回空白联通块的个数,比如8返回3,0返回2等等。当然,首先得在外围加一圈虚拟的白圈防止边界的阻隔。
  • count_border和count_fill函数实现的是边界黑点的计数,以及黑点数目的计数。这只是我开脑洞想到的特征,好像也有点用的。

logistic_sgd.py

这是官网上的学习的核心算法,我只是稍微修改了下参数还有predict函数,有些实现细节还没有彻底搞懂(话说theano还真不怎么好理解)。

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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
"""
This tutorial introduces logistic regression using Theano and stochastic
gradient descent.

Logistic regression is a probabilistic, linear classifier. It is parametrized
by a weight matrix :math:`W` and a bias vector :math:`b`. Classification is
done by projecting data points onto a set of hyperplanes, the distance to
which is used to determine a class membership probability.

Mathematically, this can be written as:

.. math::
P(Y=i|x, W,b) &= softmax_i(W x + b) \\
&= \frac {e^{W_i x + b_i}} {\sum_j e^{W_j x + b_j}}

The output of the model or prediction is then done by taking the argmax of
the vector whose i'th element is P(Y=i|x).

.. math::

y_{pred} = argmax_i P(Y=i|x,W,b)

This tutorial presents a stochastic gradient descent optimization method
suitable for large datasets.

References:

- textbooks: "Pattern Recognition and Machine Learning" -
Christopher M. Bishop, section 4.3.2

"""
__docformat__ = 'restructedtext en'

import cPickle
import matplotlib.pyplot as plt
import gzip
import os
import sys
import timeit
import numpy
import numpy as np

import theano
import theano.tensor as T

class LogisticRegression(object):
"""Multi-class Logistic Regression Class

The logistic regression is fully described by a weight matrix :math:`W`
and bias vector :math:`b`. Classification is done by projecting data
points onto a set of hyperplanes, the distance to which is used to
determine a class membership probability.
"""

def __init__(self, input, n_in, n_out):
""" Initialize the parameters of the logistic regression

:type input: theano.tensor.TensorType
:param input: symbolic variable that describes the input of the
architecture (one minibatch)

:type n_in: int
:param n_in: number of input units, the dimension of the space in
which the datapoints lie

:type n_out: int
:param n_out: number of output units, the dimension of the space in
which the labels lie

"""
# start-snippet-1
# initialize with 0 the weights W as a matrix of shape (n_in, n_out)
self.W = theano.shared(
value=numpy.zeros(
(n_in, n_out),
dtype=theano.config.floatX
),
name='W',
borrow=True
)
# initialize the biases b as a vector of n_out 0s
self.b = theano.shared(
value=numpy.zeros(
(n_out,),
dtype=theano.config.floatX
),
name='b',
borrow=True
)

# symbolic expression for computing the matrix of class-membership
# probabilities
# Where:
# W is a matrix where column-k represent the separation hyperplane for
# class-k
# x is a matrix where row-j represents input training sample-j
# b is a vector where element-k represent the free parameter of
# hyperplane-k
self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)

# symbolic description of how to compute prediction as class whose
# probability is maximal
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
# end-snippet-1

# parameters of the model
self.params = [self.W, self.b]

# keep track of model input
self.input = input

def negative_log_likelihood(self, y):
"""Return the mean of the negative log-likelihood of the prediction
of this model under a given target distribution.

.. math::

\frac{1}{|\mathcal{D}|} \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) =
\frac{1}{|\mathcal{D}|} \sum_{i=0}^{|\mathcal{D}|}
\log(P(Y=y^{(i)}|x^{(i)}, W,b)) \\
\ell (\theta=\{W,b\}, \mathcal{D})

:type y: theano.tensor.TensorType
:param y: corresponds to a vector that gives for each example the
correct label

Note: we use the mean instead of the sum so that
the learning rate is less dependent on the batch size
"""
# start-snippet-2
# y.shape[0] is (symbolically) the number of rows in y, i.e.,
# number of examples (call it n) in the minibatch
# T.arange(y.shape[0]) is a symbolic vector which will contain
# [0,1,2,... n-1] T.log(self.p_y_given_x) is a matrix of
# Log-Probabilities (call it LP) with one row per example and
# one column per class LP[T.arange(y.shape[0]),y] is a vector
# v containing [LP[0,y[0]], LP[1,y[1]], LP[2,y[2]], ...,
# LP[n-1,y[n-1]]] and T.mean(LP[T.arange(y.shape[0]),y]) is
# the mean (across minibatch examples) of the elements in v,
# i.e., the mean log-likelihood across the minibatch.
return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y])
# end-snippet-2

def errors(self, y):
"""Return a float representing the number of errors in the minibatch
over the total number of examples of the minibatch ; zero one
loss over the size of the minibatch

:type y: theano.tensor.TensorType
:param y: corresponds to a vector that gives for each example the
correct label
"""

# check if y has same dimension of y_pred
if y.ndim != self.y_pred.ndim:
raise TypeError(
'y should have the same shape as self.y_pred',
('y', y.type, 'y_pred', self.y_pred.type)
)
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.mean(T.neq(self.y_pred, y))
else:
raise NotImplementedError()

def load_data(dataset):
''' Loads the dataset

:type dataset: string
:param dataset: the path to the dataset (here MNIST)
'''

#############
# LOAD DATA #
#############
''''
# Download the MNIST dataset if it is not present
data_dir, data_file = os.path.split(dataset)
if data_dir == "" and not os.path.isfile(dataset):
# Check if dataset is in the data directory.
new_path = os.path.join(
os.path.split(__file__)[0],
"..",
"data",
dataset
)
if os.path.isfile(new_path) or data_file == 'mnist.pkl.gz':
dataset = new_path

if (not os.path.isfile(dataset)) and data_file == 'mnist.pkl.gz':
import urllib
origin = (
'http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz'
)
print 'Downloading data from %s' % origin
urllib.urlretrieve(origin, dataset)
'''
print '... loading data'

# Load the dataset
f = gzip.open(dataset, 'rb')
train_set, valid_set, test_set = cPickle.load(f)
f.close()
#train_set, valid_set, test_set format: tuple(input, target)
#input is an numpy.ndarray of 2 dimensions (a matrix)
#witch row's correspond to an example. target is a
#numpy.ndarray of 1 dimensions (vector)) that have the same length as
#the number of rows in the input. It should give the target
#target to the example with the same index in the input.

def shared_dataset(data_xy, borrow=True):
""" Function that loads the dataset into shared variables

The reason we store our dataset in shared variables is to allow
Theano to copy it into the GPU memory (when code is run on GPU).
Since copying data into the GPU is slow, copying a minibatch everytime
is needed (the default behaviour if the data is not in a shared
variable) would lead to a large decrease in performance.
"""
data_x, data_y = data_xy
shared_x = theano.shared(numpy.asarray(data_x,
dtype=theano.config.floatX),
borrow=borrow)
shared_y = theano.shared(numpy.asarray(data_y,
dtype=theano.config.floatX),
borrow=borrow)
# When storing data on the GPU it has to be stored as floats
# therefore we will store the labels as ``floatX`` as well
# (``shared_y`` does exactly that). But during our computations
# we need them as ints (we use labels as index, and if they are
# floats it doesn't make sense) therefore instead of returning
# ``shared_y`` we will have to cast it to int. This little hack
# lets ous get around this issue
return shared_x, T.cast(shared_y, 'int32')

test_set_x, test_set_y = shared_dataset(test_set)
valid_set_x, valid_set_y = shared_dataset(valid_set)
train_set_x, train_set_y = shared_dataset(train_set)

rval = [(train_set_x, train_set_y), (valid_set_x, valid_set_y),
(test_set_x, test_set_y)]
return rval

def sgd_optimization_mnist(learning_rate=0.1, n_epochs=10000,
dataset='vericode.pkl.gz',
batch_size=100):
"""
Demonstrate stochastic gradient descent optimization of a log-linear
model

This is demonstrated on MNIST.

:type learning_rate: float
:param learning_rate: learning rate used (factor for the stochastic
gradient)

:type n_epochs: int
:param n_epochs: maximal number of epochs to run the optimizer

:type dataset: string
:param dataset: the path of the MNIST dataset file from
http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz

"""
datasets = load_data(dataset)

train_set_x, train_set_y = datasets[0]
valid_set_x, valid_set_y = datasets[1]
test_set_x, test_set_y = datasets[2]

# compute number of minibatches for training, validation and testing
n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

######################
# BUILD ACTUAL MODEL #
######################
print '... building the model'

# allocate symbolic variables for the data
index = T.lscalar() # index to a [mini]batch

# generate symbolic variables for input (x and y represent a
# minibatch)
x = T.matrix('x') # data, presented as rasterized images
y = T.ivector('y') # labels, presented as 1D vector of [int] labels

# construct the logistic regression class
classifier = LogisticRegression(input=x, n_in=12 * 18+3, n_out=36)

# the cost we minimize during training is the negative log likelihood of
# the model in symbolic format
cost = classifier.negative_log_likelihood(y)

# compiling a Theano function that computes the mistakes that are made by
# the model on a minibatch
test_model = theano.function(
inputs=[index],
outputs=classifier.errors(y),
givens={
x: test_set_x[index * batch_size: (index + 1) * batch_size],
y: test_set_y[index * batch_size: (index + 1) * batch_size]
}
)

validate_model = theano.function(
inputs=[index],
outputs=classifier.errors(y),
givens={
x: valid_set_x[index * batch_size: (index + 1) * batch_size],
y: valid_set_y[index * batch_size: (index + 1) * batch_size]
}
)

# compute the gradient of cost with respect to theta = (W,b)
g_W = T.grad(cost=cost, wrt=classifier.W)
g_b = T.grad(cost=cost, wrt=classifier.b)

# start-snippet-3
# specify how to update the parameters of the model as a list of
# (variable, update expression) pairs.
updates = [(classifier.W, classifier.W - learning_rate * g_W),
(classifier.b, classifier.b - learning_rate * g_b)]

# compiling a Theano function `train_model` that returns the cost, but in
# the same time updates the parameter of the model based on the rules
# defined in `updates`
train_model = theano.function(
inputs=[index],
outputs=cost,
updates=updates,
givens={
x: train_set_x[index * batch_size: (index + 1) * batch_size],
y: train_set_y[index * batch_size: (index + 1) * batch_size]
}
)
# end-snippet-3

###############
# TRAIN MODEL #
###############
print '... training the model'
# early-stopping parameters
patience = 5000 # look as this many examples regardless
patience_increase = 2 # wait this much longer when a new best is
# found
improvement_threshold = 0.995 # a relative improvement of this much is
# considered significant
validation_frequency = min(n_train_batches, patience / 2)
# go through this many
# minibatche before checking the network
# on the validation set; in this case we
# check every epoch

best_validation_loss = numpy.inf
test_score = 0.
start_time = timeit.default_timer()

done_looping = False
epoch = 0
while (epoch < n_epochs) and (not done_looping):
epoch = epoch + 1
for minibatch_index in xrange(n_train_batches):
minibatch_avg_cost = train_model(minibatch_index)
# iteration number
iter = (epoch - 1) * n_train_batches + minibatch_index

if (iter + 1) % validation_frequency == 0:
# compute zero-one loss on validation set
validation_losses = [validate_model(i)
for i in xrange(n_valid_batches)]
this_validation_loss = numpy.mean(validation_losses)

print(
'epoch %i, minibatch %i/%i, validation error %f %%' %
(
epoch,
minibatch_index + 1,
n_train_batches,
this_validation_loss * 100.
)
)

# if we got the best validation score until now
if this_validation_loss < best_validation_loss:
#improve patience if loss improvement is good enough
if this_validation_loss < best_validation_loss * \
improvement_threshold:
patience = max(patience, iter * patience_increase)

best_validation_loss = this_validation_loss
# test it on the test set

test_losses = [test_model(i)
for i in xrange(n_test_batches)]
test_score = numpy.mean(test_losses)

print(
(
' epoch %i, minibatch %i/%i, test error of'
' best model %f %%'
) %
(
epoch,
minibatch_index + 1,
n_train_batches,
test_score * 100.
)
)

# save the best model
with open('best_model.pkl', 'w') as f:
cPickle.dump(classifier, f)

if patience <= iter:
done_looping = True
break

end_time = timeit.default_timer()
print(
(
'Optimization complete with best validation score of %f %%,'
'with test performance %f %%'
)
% (best_validation_loss * 100., test_score * 100.)
)
print 'The code run for %d epochs, with %f epochs/sec' % (
epoch, 1\. * epoch / (end_time - start_time))
print >> sys.stderr, ('The code for file ' +
os.path.split(__file__)[1] +
' ran for %.1fs' % ((end_time - start_time)))

def predict():
"""
An example of how to load a trained model and use it
to predict labels.
"""

# load the saved model
classifier = cPickle.load(open('best_model.pkl'))

# compile a predictor function
predict_model = theano.function(
inputs=[classifier.input],
outputs=classifier.y_pred)

# We can test it on some examples from test test
dataset='vericode.pkl.gz'
datasets = cPickle.load(gzip.open(dataset))
test_set_x, test_set_y = datasets[2]
predicted_values = predict_model(test_set_x[:10])
test_set_y=test_set_y[:10].astype('int')
dict='012345678abcdefghijklmnpqrstuvwxy'
pred=[]
ans=[]
for i in range(len(predicted_values)):
pred.append(dict[predicted_values[i]])
ans.append(dict[test_set_y[i]])
print ("Predicted values for the first 10 examples in test set:")
print pred
print ("Correct values for the first 10 examples in test set:")
print ans

if __name__ == '__main__':
sgd_optimization_mnist()
predict()

主要需要关注的就是 sgd_optimization_mnist 函数的传参,比如学习因子$\alpha$还有分组的大小。迭代的次数反倒不用管他,调到最高也成。

train.py

训练接口,很简单0.0,只是为了方便操作而已。

1
2
3
4
5
import os
os.system('python split.py')
os.system('python util.py')
os.system('python package.py')
os.system('python logistic_sgd.py')

运行之前所有的程序,然后直接训练。split.py会生成名叫number/的文件加,package.py会生成名叫vericode.pkl.gz的数据集,而logistic_sgd.py会直接进行训练,并把最好的结果输出到** best_model.pkl** 中加以保存,并在命令行中打印类似下面的信息:

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
... spliting
... packaging
... loading data
... building the model
... training the model
epoch 1, minibatch 16/16, validation error 71.200000 %
epoch 1, minibatch 16/16, test error of best model 70.800000 %
epoch 2, minibatch 16/16, validation error 56.800000 %
epoch 2, minibatch 16/16, test error of best model 57.600000 %
epoch 3, minibatch 16/16, validation error 49.600000 %
epoch 3, minibatch 16/16, test error of best model 50.200000 %
epoch 4, minibatch 16/16, validation error 44.200000 %
epoch 4, minibatch 16/16, test error of best model 43.000000 %
epoch 5, minibatch 16/16, validation error 38.800000 %
epoch 5, minibatch 16/16, test error of best model 40.000000 %
epoch 6, minibatch 16/16, validation error 36.400000 %
......
epoch 678, minibatch 16/16, validation error 10.000000 %
epoch 679, minibatch 16/16, validation error 10.000000 %
epoch 680, minibatch 16/16, validation error 10.200000 %
epoch 681, minibatch 16/16, validation error 10.200000 %
Optimization complete with best validation score of 9.600000 %,with test performance 11.400000 %
The code run for 682 epochs, with 60.733475 epochs/sec
Predicted values for the first 10 examples in test set:
['l', 'j', 'r', '0', 'q', '3', '6', '3', '7', 'p']
Correct values for the first 10 examples in test set:
['l', 'j', 'r', '0', 'q', '3', '6', '3', '7', 'p']

细致的显示了训练集的训练结果。

check.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
import cPickle,os,Image,ImageFilter,theano,sys,time
import numpy as np
from logistic_sgd import LogisticRegression
from util import *

def recognize(pic='test'):
im=Image.open(pic)

#split
image=[]
image.append(im.crop((5,2,17,20)))
image.append(im.crop((17,2,29,20)))
image.append(im.crop((29,2,41,20)))
image.append(im.crop((41,2,53,20)))

#package
arr=[]
for img in image:
img=blur1(img)
cnt1=seed_fill(img)
cnt2=count_fill(img)
cnt2=(cnt2-50)/50
cnt3=count_border(img)
cnt3=(cnt3-50)/50
img=blur2(img)
array=np.array(img)
array=array.reshape(12*18).astype('int').astype('bool').astype('float32')
array=array.tolist()
array.append(cnt1)
array.append(cnt2)
array.append(cnt3)
array=np.array(array).astype('float32')
arr.append(array)

arr=np.array(arr)

#predict
classifier = cPickle.load(open('best_model.pkl'))
predict_model = theano.function(inputs=[classifier.input],outputs=classifier.y_pred)
predicted_values = predict_model(arr)
dict='012345678abcdefghijklmnpqrstuvwxy'
pred=[]
for i in range(len(predicted_values)):
pred.append(dict[predicted_values[i]])
return pred[0]+pred[1]+pred[2]+pred[3]

def recur_recognize(path):
if not path[-1]=='/':
path.join('/')
files=os.listdir(path)
cost=time.time()
for i in files:
#print '.',
#sys.stdout.flush()
old_name=path+str(i)
name=recognize(old_name)
new_name=path+str(name)
os.rename(old_name,new_name)

return time.time()-cost,len(files)

if __name__ == '__main__':
if len(sys.argv) <2:
print 'Please enter the file'
else:
if os.path.isdir(sys.argv[1]):
cost,size= recur_recognize(sys.argv[1])
print '\nRecognized '+str(size)+' pictures , in '+str(cost)+' s\n'
else:
print recognize(sys.argv[1])

这里实现了对单个文件的识别以及文件夹下所有文件的批量识别。可以如下使用:

$python check.py test.png 以及$python check.py png/