8000 initial · coderlv/CNN_keras@05165ca · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 05165ca

Browse files
author
YuJun
committed
initial
1 parent 7d6bb45 commit 05165ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+6521
-1
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# CNN_keras
2-
卷积神经网络 | Keras框架 | 验证码识别
2+
3+
Convolutional Neural Network | Keras | CAPTCHA
4+
5+
Tip:
6+
For detail please email me.
7+
8+
* Copyright
9+
* OpenSource now.
10+
* Do not for commercial.(If you must do, please contact me.)

core_single/model/model_1.hdf5

11.7 MB
Binary file not shown.

core_single/model/model_2.hdf5

2.82 MB
Binary file not shown.

core_single/predict_1.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
File name: predict_1.py
6+
Function Des: ...
7+
~~~~~~~~~~
8+
9+
author: Skyduy <cuteuy@gmail.com> <http://skyduy.me>
10+
11+
"""
12+
import urllib
13+
import numpy as np
14+
from utils import asc_chr
15+
from PIL import Image
16+
from keras.models import Graph
17+
from keras.layers.core import Dense, Flatten, Dropout
18+
from keras.layers.convolutional import Convolution2D, MaxPooling2D
19+
20+
print '... loading data'
21+
22+
23+
def get_data():
24+
pic_name = 'tmp.jpg'
25+
urllib.urlretrieve('http://passport2.chaoxing.com/img/code', 'tmp/%s' % pic_name)
26+
results = {}
27+
raw_img = Image.open('tmp/tmp.jpg')
28+
img = raw_img.convert('1')
29+
x_size, y_size = img.size
30+
for x in xrange(x_size):
31+
results.setdefault(x, 0)
32+
for y in xrange(y_size):
33+
pixel = img.getpixel((x, y))
34+
if pixel == 0:
35+
results[x] += 1
36+
begin = end = -1
37+
for i in range(150):
38+
if results[i] > 0:
39+
begin = i
40+
break
41+
threshold_list = []
42+
for i in range(131, 134):
43+
threshold_list.append(results[i])
44+
threshold = max(threshold_list)
45+
i = 150
46+
stop = False
47+
while not stop:
48+
i -= 1
49+
if i == -1:
50+
begin, end = 10, 135
51+
break
52+
v = results[i]
53+
if v >= max((3.5, 2 * threshold)):
54+
stop = True
55+
tmp_value = v
56+
while True:
57+
i += 1
58+
if results[i] <= tmp_value:
59+
if results[i] <= threshold:
60+
end = i
61+
break
62+
else:
63+
tmp_value = results[i]
64+
else:
65+
end = i - 1
66+
centers = [((2 * i + 1) * end + (9 - 2 * i) * begin) / 10 for i in range(5)]
67+
data = np.empty((5, 3, 40, 40), dtype="float32")
68+
for index, center in enumerate(centers):
69+
a = center - 19
70+
b = center + 21
71+
img = raw_img.crop((a, 0, b, y_size))
72+
arr = np.asarray(img, dtype="float32") / 255.0
73+
data[index, :, :, :] = np.rollaxis(arr, 2)
74+
return data
75+
76+
X = get_data()
77+
78+
79+
print '... create model'
80+
81+
graph = Graph()
82+
graph.add_input(name='input', input_shape=(3, 40, 40))
83+
graph.add_node(Convolution2D(32, 9, 9, activation='relu'), name='conv1', input='input')
84+
graph.add_node(Convolution2D(32, 9, 9, activation='relu'), name='conv2', input='conv1')
85+
graph.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool', input='conv2')
86+
graph.add_node(Dropout(0.25), name='drop', input='pool')
87+
graph.add_node(Flatten(), name='flatten', input='drop')
88+
graph.add_node(Dense(640, activation='relu'), name='ip', input='flatten')
89+
graph.add_node(Dropout(0.5), name='drop_out', input='ip')
90+
graph.add_node(Dense(19, activation='softmax'), name='result', input='drop_out')
91+
92+
graph.add_output(name='out', input='result')
93+
94+
print '... compiling'
95+
graph.compile(
96+
optimizer='adadelta',
97+
loss={
98+
'out': 'categorical_crossentropy',
99+
}
100+
)
101+
102+
graph.load_weights('model/model_1.hdf5')
103+
print '... predicting'
104+
result = graph.predict({'input': X})
105+
out = [np.argmax(result['out'][0]), np.argmax(result['out'][1]),
106+
np.argmax(result['out'][2]), np.argmax(result['out'][3]),
107+
np.argmax(result['out'][4])]
108+
109+
print out
110+
111+
r = [asc_chr(i) for i in out]
112+
113+
print r

core_single/predict_2.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
File name: predict_2.py
6+
Function Des: ...
7+
~~~~~~~~~~
8+
9+
author: Skyduy <cuteuy@gmail.com> <http://skyduy.me>
10+
11+
"""
12+
import urllib
13+
import numpy as np
14+
from utils import asc_chr
15+
from PIL import Image
16+
from keras.models import Graph
17+
from keras.layers.core import Dense, Flatten, Dropout
18+
from keras.layers.convolutional import Convolution2D, MaxPooling2D
19+
20+
print '... loading data'
21+
22+
23+
def get_data():
24+
pic_name = 'tmp.jpg'
25+
urllib.urlretrieve('http://passport2.chaoxing.com/img/code', 'tmp/%s' % pic_name)
26+
results = {}
27+
raw_img = Image.open('tmp/tmp.jpg')
28+
img = raw_img.convert('1')
29+
x_size, y_size = img.size
30+
for x in xrange(x_size):
31+
results.setdefault(x, 0)
32+
for y in xrange(y_size):
33+
pixel = img.getpixel((x, y))
34+
if pixel == 0:
35+
results[x] += 1
36+
begin = end = -1
37+
for i in range(150):
38+
if results[i] > 0:
39+
begin = i
40+
break
41+
threshold_list = []
42+
for i in range(131, 134):
43+
threshold_list.append(results[i])
44+
threshold = max(threshold_list)
45+
i = 150
46+
stop = False
47+
while not stop:
48+
i -= 1
49+
if i == -1:
50+
begin, end = 10, 135
51+
break
52+
v = results[i]
53+
if v >= max((3.5, 2 * threshold)):
54+
stop = True
55+
tmp_value = v
56+
while True:
57+
i += 1
58+
if results[i] <= tmp_value:
59+
if results[i] <= threshold:
60+
end = i
61+
break
62+
else:
63+
tmp_value = results[i]
64+
else:
65+
end = i - 1
66+
centers = [((2 * i + 1) * end + (9 - 2 * i) * begin) / 10 for i in range(5)]
67+
data = np.empty((5, 3, 40, 40), dtype="float32")
68+
for index, center in enumerate(centers):
69+
a = center - 19
70+
b = center + 21
71+
img = raw_img.crop((a, 0, b, y_size))
72+
arr = np.asarray(img, dtype="float32") / 255.0
73+
data[index, :, :, :] = np.rollaxis(arr, 2)
74+
return data
75+
76+
X = get_data()
77+
78+
79+
print '... create model'
80+
81+
graph = Graph()
82+
graph.add_input(name='input', input_shape=(3, 40, 40))
83+
graph.add_node(Convolution2D(22, 5, 5, activation='relu'), name='conv1', input='input')
84+
graph.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool1', input='conv1')
85+
graph.add_node(Convolution2D(44, 3, 3, activation='relu'), name='conv2', input='pool1')
86+
graph.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool2', input='conv2')
87+
graph.add_node(Dropout(0.25), name='drop', input='pool2')
88+
graph.add_node(Flatten(), name='flatten', input='drop')
89+
graph.add_node(Dense(256, activation='relu'), name='ip', input='flatten')
90+
graph.add_node(Dropout(0.5), name='drop_out', input='ip')
91+
graph.add_node(Dense(19, activation='softmax'), name='result', input='drop_out')
92+
93+
graph.add_output(name='out', input='result')
94+
95+
print '... compiling'
96+
graph.compile(
97+
optimizer='adadelta',
98+
loss={
99+
'out': 'categorical_crossentropy',
100+
}
101+
)
102+
103+
graph.load_weights('model/model_2.hdf5')
104+
print '... predicting'
105+
result = graph.predict({'input': X})
106+
out = [np.argmax(result['out'][0]), np.argmax(result['out'][1]),
107+
np.argmax(result['out'][2]), np.argmax(result['out'][3]),
108+
np.argmax(result['out'][4])]
109+
110+
print out
111+
112+
r = [asc_chr(i) for i in out]
113+
114+
print r

core_single/tmp/1.jpg

1.62 MB
Loading

core_single/tmp/2.jpg

4.3 MB
Loading

core_single/tmp/tmp.jpg

2.67 KB
Loading

core_single/train_with_acc_1.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
File name: train_with_acc_1.py
6+
Function Des: ...
7+
8+
~~~~~~~~~~
9+
10+
author: Skyduy <cuteuy@gmail.com> <http://skyduy.me>
11+
12+
"""
13+
from numpy import argmax, array
14+
from sklearn.cross_validation import train_test_split
15+
from keras.callbacks import Callback, ModelCheckpoint
16+
from keras.models import Graph
17+
from keras.utils import np_utils
18+
from keras.layers.core import Dense, Flatten, Dropout
19+
from keras.layers.convolutional import Convolution2D, MaxPooling2D
20+
21+
from utils import load_single_data
22+
23+
print '... loading data'
24+
X, Y_all = load_single_data()
25+
26+
X_train, X_test, y_train, y_test = train_test_split(X, Y_all, test_size=0.1, random_state=0)
27+
28+
29+
y_train = np_utils.to_categorical(y_train, 19)
30+
31+
32+
graph = Graph()
33+
graph.add_input(name='input', input_shape=(3, 40, 40))
34+
graph.add_node(Convolution2D(32, 9, 9, activation='relu'), name='conv1', input='input')
35+
graph.add_node(Convolution2D(32, 9, 9, activation='relu'), name='conv2', input='conv1')
36+
graph.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool', input='conv2')
37+
graph.add_node(Dropout(0.25), name='drop', input='pool')
38+
graph.add_node(Flatten(), name='flatten', input='drop')
39+
graph.add_node(Dense(640, activation='relu'), name='ip', input='flatten')
40+
graph.add_node(Dropout(0.5), name='drop_out', input='ip')
41+
graph.add_node(Dense(19, activation='softmax'), name='result', input='drop_out')
42+
43+
graph.add_output(name='out', input='result')
44+
45+
print '... compiling'
46+
graph.compile(
47+
optimizer='adadelta',
48+
loss={
49+
'out': 'categorical_crossentropy',
50+
}
51+
)
52+
print '... training'
53+
54+
55+
class ValidateAcc(Callback):
56+
def on_epoch_end(self, epoch, logs={}):
57+
print '\n————————————————————————————————————'
58+
graph.load_weights('tmp/weights.%02d.hdf5' % epoch)
59+
r = graph.predict({'input': X_test}, verbose=0)
60+
y_predict = array([argmax(i) for i in r['out']])
61+
length = len(y_predict) * 1.0
62+
acc = sum(y_predict == y_test) / length
63+
print 'Single picture test accuracy: %2.2f%%' % (acc * 100)
64+
print 'Theoretical accuracy: %2.2f%% ~ %2.2f%%' % ((5*acc-4)*100, pow(acc, 5)*100)
65+
print '————————————————————————————————————'
66+
67+
check_point = ModelCheckpoint(filepath="tmp/weights.{epoch:02d}.hdf5")
68+
back = ValidateAcc()
69+
print 'Begin train on %d samples... test on %d samples...' % (len(y_train), len(y_test))
70+
graph.fit(
71+
{'input': X_train, 'out': y_train},
72+
batch_size=128, nb_epoch=100, callbacks=[check_point, back]
73+
)
74+
print '... saving'
75+
graph.save_weights('model/model_1.hdf5')

core_single/train_with_acc_2.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
File name: train_with_acc_2.py
6+
Function Des: 效果跟1差不多,但速度比1快。
7+
~~~~~~~~~~
8+
9+
author: Skyduy <cuteuy@gmail.com> <http://skyduy.me>
10+
11+
"""
12+
from numpy import argmax, array
13+
from sklearn.cross_validation import train_test_split
14+
from keras.callbacks import Callback, ModelCheckpoint
15+
from keras.models import Graph
16+
from keras.utils import np_utils
17+
from keras.layers.core import Dense, Flatten, Dropout
18+
from keras.layers.convolutional import Convolution2D, MaxPooling2D
19+
20+
from utils import load_single_data
21+
22+
print '... loading data'
23+
X, Y_all = load_single_data()
24+
25+
X_train, X_test, y_train, y_test = train_test_split(X, Y_all, test_size=0.1, random_state=0)
26+
27+
28+
y_train = np_utils.to_categorical(y_train, 19)
29+
30+
graph = Graph()
31+
graph.add_input(name='input', input_shape=(3, 40, 40))
32+
graph.add_node(Convolution2D(22, 5, 5, activation='relu'), name='conv1', input='input')
33+
graph.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool1', input='conv1')
34+
graph.add_node(Convolution2D(44, 3, 3, activation='relu'), name='conv2', input='pool1')
35+
graph.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool2', input='conv2')
36+
graph.add_node(Dropout(0.25), name='drop', input='pool2')
37+
graph.add_node(Flatten(), name='flatten', input='drop')
38+
graph.add_node(Dense(256, activation='relu'), name='ip', input='flatten')
39+
graph.add_node(Dropout(0.5), name='drop_out', input='ip')
40+
graph.add_node(Dense(19, activation='softmax'), name='result', input='drop_out')
41+
42+
graph.add_output(name='out', input='result')
43+
44+
print '... compiling'
45+
graph.compile(
46+
optimizer='adadelta',
47+
loss={
48+
'out': 'categorical_crossentropy',
49+
}
50+
)
51+
print '... training'
52+
53+
54+
class ValidateAcc(Callback):
55+
def on_epoch_end(self, epoch, logs={}):
56+
print '\n————————————————————————————————————'
57+
graph.load_weights('tmp/weights.%02d.hdf5' % epoch)
58+
r = graph.predict({'input': X_test}, verbose=0)
59+
y_predict = array([argmax(i) for i in r['out']])
60+
length = len(y_predict) * 1.0
61+
acc = sum(y_predict == y_test) / length
62+
print 'Single picture test accuracy: %2.2f%%' % (acc * 100)
63+
print 'Theoretical accuracy: %2.2f%% ~ %2.2f%%' % ((5*acc-4)*100, pow(acc, 5)*100)
64+
print '————————————————————————————————————'
65+
66+
check_point = ModelCheckpoint(filepath="tmp/weights.{epoch:02d}.hdf5")
67+
back = ValidateAcc()
68+
print 'Begin train on %d samples... test on %d samples...' % (len(y_train), len(y_test))
69+
graph.fit(
70+
{'input': X_train, 'out': y_train},
71+
batch_size=128, nb_epoch=100, callbacks=[check_point, back]
72+
)
73+
print '... saving'
74+
graph.save_weights('model/model_2.hdf5')

0 commit comments

Comments
 (0)
0