-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainIDCard.py
191 lines (158 loc) · 8.11 KB
/
trainIDCard.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
tf 训练识别身份证数字(18个字符)图片
@author: pengyuanjie
"""
from genIDCard import *
from datetime import datetime
import numpy as np
import tensorflow as tf
#obj = gen_id_card()
#image,text,vec = obj.gen_image()
#图像大小
IMAGE_HEIGHT, IMAGE_WIDTH = 32, 256
MAX_CAPTCHA = 18
#CHAR_SET_LEN = obj.len
CHAR_SET_LEN = 11
# 生成一个训练batch
def get_next_batch(batch_size=128):
obj = gen_id_card()
batch_x = np.zeros([batch_size, IMAGE_HEIGHT*IMAGE_WIDTH])
batch_y = np.zeros([batch_size, MAX_CAPTCHA*CHAR_SET_LEN])
for i in range(batch_size):
image, text, vec = obj.gen_image()
batch_x[i,:] = image.reshape((IMAGE_HEIGHT*IMAGE_WIDTH))
batch_y[i,:] = vec
return batch_x, batch_y
####################################################################
#Batch Normalization? 有空再理解,tflearn or slim都有封装
## http://stackoverflow.com/a/34634291/2267819
def batch_norm(x, beta, gamma, phase_train, scope='bn', decay=0.9, eps=1e-5):
with tf.variable_scope(scope):
#beta = tf.get_variable(name='beta', shape=[n_out], initializer=tf.constant_initializer(0.0), trainable=True)
#gamma = tf.get_variable(name='gamma', shape=[n_out], initializer=tf.random_normal_initializer(1.0, stddev), trainable=True)
batch_mean, batch_var = tf.nn.moments(x, [0, 1, 2], name='moments')
ema = tf.train.ExponentialMovingAverage(decay=decay)
def mean_var_with_update():
ema_apply_op = ema.apply([batch_mean, batch_var])
with tf.control_dependencies([ema_apply_op]):
return tf.identity(batch_mean), tf.identity(batch_var)
mean, var = tf.cond(phase_train, mean_var_with_update, lambda: (ema.average(batch_mean), ema.average(batch_var)))
normed = tf.nn.batch_normalization(x, mean, var, beta, gamma, eps)
return normed
# 定义CNN
def crack_captcha_cnn(input_tensor, train_phase, keep_prob, w_alpha=0.01, b_alpha=0.1):
x = tf.reshape(input_tensor, shape=[-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1])
# 4 conv layer
w_c1 = tf.Variable(w_alpha*tf.random_normal([5, 5, 1, 32]))
b_c1 = tf.Variable(b_alpha*tf.random_normal([32]))
conv1 = tf.nn.bias_add(tf.nn.conv2d(x, w_c1, strides=[1, 1, 1, 1], padding='SAME'), b_c1)
conv1 = batch_norm(conv1, tf.constant(0.0, shape=[32]), tf.random_normal(shape=[32], mean=1.0, stddev=0.02), train_phase, scope='bn_1')
conv1 = tf.nn.relu(conv1)
conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
conv1 = tf.nn.dropout(conv1, keep_prob)
w_c2 = tf.Variable(w_alpha*tf.random_normal([5, 5, 32, 64]))
b_c2 = tf.Variable(b_alpha*tf.random_normal([64]))
conv2 = tf.nn.bias_add(tf.nn.conv2d(conv1, w_c2, strides=[1, 1, 1, 1], padding='SAME'), b_c2)
conv2 = batch_norm(conv2, tf.constant(0.0, shape=[64]), tf.random_normal(shape=[64], mean=1.0, stddev=0.02), train_phase, scope='bn_2')
conv2 = tf.nn.relu(conv2)
conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
conv2 = tf.nn.dropout(conv2, keep_prob)
w_c3 = tf.Variable(w_alpha*tf.random_normal([3, 3, 64, 64]))
b_c3 = tf.Variable(b_alpha*tf.random_normal([64]))
conv3 = tf.nn.bias_add(tf.nn.conv2d(conv2, w_c3, strides=[1, 1, 1, 1], padding='SAME'), b_c3)
conv3 = batch_norm(conv3, tf.constant(0.0, shape=[64]), tf.random_normal(shape=[64], mean=1.0, stddev=0.02), train_phase, scope='bn_3')
conv3 = tf.nn.relu(conv3)
conv3 = tf.nn.max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
conv3 = tf.nn.dropout(conv3, keep_prob)
w_c4 = tf.Variable(w_alpha*tf.random_normal([3, 3, 64, 64]))
b_c4 = tf.Variable(b_alpha*tf.random_normal([64]))
conv4 = tf.nn.bias_add(tf.nn.conv2d(conv3, w_c4, strides=[1, 1, 1, 1], padding='SAME'), b_c4)
conv4 = batch_norm(conv4, tf.constant(0.0, shape=[64]), tf.random_normal(shape=[64], mean=1.0, stddev=0.02), train_phase, scope='bn_4')
conv4 = tf.nn.relu(conv4)
conv4 = tf.nn.max_pool(conv4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
conv4 = tf.nn.dropout(conv4, keep_prob)
# Fully connected layer
w_d = tf.Variable(w_alpha*tf.random_normal([2*16*64, 1024]))
b_d = tf.Variable(b_alpha*tf.random_normal([1024]))
dense = tf.reshape(conv4, [-1, w_d.get_shape().as_list()[0]])
dense = tf.nn.relu(tf.add(tf.matmul(dense, w_d), b_d))
dense = tf.nn.dropout(dense, keep_prob)
w_out = tf.Variable(w_alpha*tf.random_normal([1024, MAX_CAPTCHA*CHAR_SET_LEN]))
b_out = tf.Variable(b_alpha*tf.random_normal([MAX_CAPTCHA*CHAR_SET_LEN]))
out = tf.add(tf.matmul(dense, w_out), b_out)
#out = tf.nn.softmax(out)
return out
#训练
model_dir = './models/'
def train_crack_captcha_cnn():
with tf.Graph().as_default() as g:
X = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT*IMAGE_WIDTH])
Y = tf.placeholder(tf.float32, [None, MAX_CAPTCHA*CHAR_SET_LEN])
keep_prob = tf.placeholder(tf.float32) # dropout
# 是否在训练阶段
train_phase = tf.placeholder(tf.bool)
global_step = tf.train.get_or_create_global_step()
with tf.device('/gpu:0'):
output = crack_captcha_cnn(X, train_phase, keep_prob)
# loss
#loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=Y))
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y))
# 最后一层用来分类的softmax和sigmoid有什么不同?
# optimizer 为了加快训练 learning_rate应该开始大,然后慢慢衰
optimizer = tf.train.AdamOptimizer(learning_rate=0.002).minimize(loss, global_step=global_step)
predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN])
max_idx_p = tf.argmax(predict, 2)
max_idx_l = tf.argmax(tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
correct_pred = tf.equal(max_idx_p, max_idx_l)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
sv = tf.train.Supervisor(logdir=model_dir, init_op=init_op, saver=saver, global_step=global_step)
config = tf.ConfigProto(allow_soft_placement=True,
log_device_placement=False,
gpu_options=tf.GPUOptions(allow_growth=True))
with sv.managed_session(config=config) as sess:
while True:
batch_x, batch_y = get_next_batch(64)
_, loss_, step = sess.run([optimizer, loss, global_step], feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.75, train_phase:True})
print('[%s]' % (datetime.now()), step, loss_)
# 每100 step计算一次准确率
if step % 100 == 0 and step != 0:
batch_x_test, batch_y_test = get_next_batch(100)
label_p, label_l = sess.run([max_idx_p, max_idx_l], feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1., train_phase:False})
acc = sess.run(accuracy, feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1., train_phase:False})
print ("[%s] 第%s步,训练准确率为:%s" % (datetime.now(), step, acc))
#saver.save(sess, model_dir+"crack_capcha_%d.model"%(step), global_step=step)
saver.save(sess, model_dir+"crack_capcha.ckpt", global_step=step)
# 如果准确率大90%,保存模型,完成训练
if acc > 0.9:
if (input ('input True for exit')): break
if __name__ == '__main__':
train_crack_captcha_cnn()
#%%
'''
#小验证
import matplotlib.pyplot as plt
model_dir = './models/'
with tf.device('/gpu:0'):
output = crack_captcha_cnn()
saver = tf.train.Saver()
sv = tf.train.Supervisor(logdir=model_dir, saver=saver)
config = tf.ConfigProto(allow_soft_placement=True)
with sv.managed_session(config=config) as sess:
image, text, vec = gen_id_card().gen_image()
x_val = image.reshape((1, IMAGE_HEIGHT*IMAGE_WIDTH))
#y_val = vec.reshape((1, -1))
#y_predict, loss_ = sess.run([output, loss], feed_dict={X: x_val, Y: y_val, keep_prob: 1, train_phase:False})
y_predict = sess.run(output, feed_dict={X: x_val, keep_prob: 1, train_phase:False})
plt.imshow(image, 'gray')
def vec2num(vec):
vec = np.argmax(vec.reshape((MAX_CAPTCHA, CHAR_SET_LEN)),1)
vec = [str(num) for num in vec]
vec = ''.join(vec)
return vec
#label_truth = vec2num(y_val)
label_predict = vec2num(y_predict)
'''