实验使用tensorflow实现卷积神经网络对验证码进行识别,在识别的过程中遇到了优化速度较慢的问题,将调参的心得记录如下。
- batch_size大小不要小于输出值得个数
- 学习率大的情况下会出现损失忽大忽小的情况,学习率小的情况下容易出现训练速度过慢的问题,可以人工尝试多个不同的学习率观察损失的变化,看是否能优化,也可以使用动态调整的方法。
- 使用ELU代替RELU
- 使用Batch Normalization可以提高训练的精确度和速度,并且可以替换到Droupout
在下面的代码中包含在已有训练模型的基础上训练模型和测试数据等功能。其中测试效果图和代码如下。
#训练代码
import tensorflow as tf
from captcha.image import ImageCaptcha
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import random
import os
number=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
#alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
#ALPHABET = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
def random_captcha_text(char_set=number,captcha_size=4):
captcha_text=[]
for i in range(captcha_size):
c=random.choice(char_set)
captcha_text.append(c)
return captcha_text
def gen_captcha_text_image():
image=ImageCaptcha()
captcha_text=random_captcha_text()
captcha_text=''.join(captcha_text)
captcha=image.generate(captcha_text)
captcha_image=Image.open(captcha)
captcha_image=np.array(captcha_image)
return captcha_text,captcha_image
def convert2gray(img):
if len(img.shape)>2:
r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
else:
return img
def text2vec(text):
text_len = len(text)
if text_len > max_captcha:
raise ValueError('验证码最长4个字符')
vector = np.zeros(max_captcha * char_set_len)
def char2pos(c):
if c == '_':
k = 62
return k
k = ord(c) - 48
if k > 9:
k = ord(c) - 55
if k > 35:
k = ord(c) - 61
if k > 61:
raise ValueError('No Map')
return k
for i, c in enumerate(text):
idx = i * char_set_len + char2pos(c)
vector[idx] = 1
return vector
def get_next_batch(batch_size=128):
batch_x=np.zeros([batch_size,image_height*image_width])
batch_y=np.zeros([batch_size,max_captcha*char_set_len])
def wrap_gen_captcha_text_and_image():
while True:
text, image = gen_captcha_text_image()
if image.shape == (60, 160, 3):
return text, image
for i in range(batch_size):
text, image = wrap_gen_captcha_text_and_image()
image = convert2gray(image)
batch_x[i, :] = image.flatten() / 255
batch_y[i, :] = text2vec(text)
return batch_x, batch_y
def cnn_structure(w_alpha=0.01, b_alpha=0.1):
x = tf.reshape(X, shape=[-1, image_height, image_width, 1])
wc1=tf.get_variable(name='wc1',shape=[3,3,1,32],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
#wc1 = tf.Variable(w_alpha * tf.random_normal([3, 3, 1, 32]))
bc1 = tf.Variable(b_alpha * tf.random_normal([32]))
conv1 = tf.nn.bias_add(tf.nn.conv2d(x, wc1, strides=[1, 1, 1, 1], padding='SAME'), bc1)
conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#批标准化
batch_mean, batch_var = tf.nn.moments(conv1, [0, 1, 2], keep_dims=True)
shift = tf.Variable(tf.zeros([32]))
scale = tf.Variable(tf.ones([32]))
epsilon = 1e-3
conv1 = tf.nn.batch_normalization(conv1, batch_mean, batch_var, shift, scale, epsilon)
#放在最大池化之后的relu
conv1 = tf.nn.elu(conv1)
conv1 = tf.nn.dropout(conv1, keep_prob)
wc2=tf.get_variable(name='wc2',shape=[3,3,32,64],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
# wc2 = tf.Variable(w_alpha * tf.random_normal([3, 3, 32, 64]))
bc2 = tf.Variable(b_alpha * tf.random_normal([64]))
conv2 = tf.nn.bias_add(tf.nn.conv2d(conv1, wc2, strides=[1, 1, 1, 1], padding='SAME'), bc2)
conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#批标准化
batch_mean, batch_var = tf.nn.moments(conv2, [0, 1, 2], keep_dims=True)
shift = tf.Variable(tf.zeros([64]))
scale = tf.Variable(tf.ones([64]))
epsilon = 1e-3
conv2 = tf.nn.batch_normalization(conv2, batch_mean, batch_var, shift, scale, epsilon)
conv2 = tf.nn.elu(conv2)
conv2 = tf.nn.dropout(conv2, keep_prob)
wc3=tf.get_variable(name='wc3',shape=[3,3,64,128],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
#wc3 = tf.Variable(w_alpha * tf.random_normal([3, 3, 64, 128]))
bc3 = tf.Variable(b_alpha * tf.random_normal([128]))
conv3 = tf.nn.bias_add(tf.nn.conv2d(conv2, wc3, strides=[1, 1, 1, 1], padding='SAME'), bc3)
conv3 = tf.nn.max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#批标准化 通过相当于一个图
batch_mean, batch_var = tf.nn.moments(conv3, [0, 1, 2], keep_dims=True)
shift = tf.Variable(tf.zeros([128]))
scale = tf.Variable(tf.ones([128]))
epsilon = 1e-3
conv3 = tf.nn.batch_normalization(conv3, batch_mean, batch_var, shift, scale, epsilon)
conv3 = tf.nn.elu(conv3)
conv3 = tf.nn.dropout(conv3, keep_prob)
wd1=tf.get_variable(name='wd1',shape=[8*20*128,1024],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
#wd1 = tf.Variable(w_alpha * tf.random_normal([7*20*128,1024]))
bd1 = tf.Variable(b_alpha * tf.random_normal([1024]))
dense = tf.reshape(conv3, [-1, wd1.get_shape().as_list()[0]])
dense = tf.nn.elu(tf.add(tf.matmul(dense, wd1), bd1))
dense = tf.nn.dropout(dense, keep_prob)
wout=tf.get_variable('name',shape=[1024,max_captcha * char_set_len],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
#wout = tf.Variable(w_alpha * tf.random_normal([1024, max_captcha * char_set_len]))
bout = tf.Variable(b_alpha * tf.random_normal([max_captcha * char_set_len]))
out = tf.add(tf.matmul(dense, wout), bout)
return out
def train_cnn():
output=cnn_structure()
cost=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output,labels=Y))
#尝试降低学习率,从原来模型的基础上继续训练
optimizer=tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
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))
saver=tf.train.Saver()
modelRestore = True
path = "./model"
with tf.Session() as sess:
#先初始化变量
init = tf.global_variables_initializer()
sess.run(init)
# 重新加载模型
if modelRestore and os.path.isfile(os.path.join(path, 'checkpoint')):#判断是否要加载并且是否存在训练好的模型
ckpt = tf.train.get_checkpoint_state(path) # 读取最后一个模型的路径
print( ckpt.model_checkpoint_path)
saver.restore(sess, ckpt.model_checkpoint_path) #加载模型
# init = tf.global_variables_initializer()
# sess.run(init)
step = 0
while True:
batch_x, batch_y = get_next_batch(1024)
_, cost_= sess.run([optimizer, cost], feed_dict={X: batch_x, Y: batch_y, keep_prob: 1})
print(step, cost_)
if step % 10 == 0:
batch_x_test, batch_y_test = get_next_batch(100)
acc = sess.run(accuracy, feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.})
print(step, acc)
if acc > 0.97:
saver.save(sess, "./model/crack_capcha.model", global_step=step)
break
step += 1
def crack_captcha(captcha_image):
output = cnn_structure()
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, "./model/crack_capcha.model-710")
predict = tf.argmax(tf.reshape(output, [-1, max_captcha, char_set_len]), 2)
text_list = sess.run(predict, feed_dict={X: [captcha_image], keep_prob: 1.})
text = text_list[0].tolist()
return text
if __name__=='__main__':
text,image=gen_captcha_text_image()
print("验证码大小:",image.shape)#(60,160,3)
image_height=60
image_width=160
max_captcha=len(text)
print("验证码文本最长字符数",max_captcha)
char_set=number
char_set_len=len(char_set)
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)
train_cnn()
#测试代码
import tensorflow as tf
from captcha.image import ImageCaptcha
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import random
model_path = './model'
image_height = 60
image_width = 160
number=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
char_set = number
char_set_len = len(char_set)
def random_captcha_text(char_set=number,captcha_size=4):
captcha_text=[]
for i in range(captcha_size):
c=random.choice(char_set)
captcha_text.append(c)
return captcha_text
def gen_captcha_text_image():
image=ImageCaptcha()
captcha_text=random_captcha_text()
captcha_text=''.join(captcha_text)
captcha=image.generate(captcha_text)
captcha_image=Image.open(captcha)
captcha_image=np.array(captcha_image)
return captcha_text,captcha_image
def convert2gray(img):
if len(img.shape)>2:
r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
else:
return img
def text2vec(text):
text_len = len(text)
if text_len > max_captcha:
raise ValueError('验证码最长4个字符')
vector = np.zeros(max_captcha * char_set_len)
def char2pos(c):
if c == '_':
k = 62
return k
k = ord(c) - 48
if k > 9:
k = ord(c) - 55
if k > 35:
k = ord(c) - 61
if k > 61:
raise ValueError('No Map')
return k
for i, c in enumerate(text):
idx = i * char_set_len + char2pos(c)
vector[idx] = 1
return vector
text, image = gen_captcha_text_image()
print("原始数据:",text)
f = plt.figure()
ax = f.add_subplot(111)
ax.text(0.1, 0.9, text, ha='center', va='center', transform=ax.transAxes)
plt.imshow(image)
# plt.show()
max_captcha = len(text)
image = convert2gray(image)
#获取训练的图片数据
image = image.flatten() / 255
y_label = np.array(range(620)).reshape(1,620)
saver = tf.train.import_meta_graph(model_path + '/crack_capcha.model-1390.meta')# 加载图结构
gragh = tf.get_default_graph()# 获取当前图,为了后续训练时恢复变量
# tensor_name_list = [tensor.name for tensor in gragh.as_graph_def().node]# 得到当前图中所有变量的名称
x = gragh.get_tensor_by_name('Placeholder:0')# 获取输入变量(占位符,由于保存时未定义名称,tf自动赋名称“Placeholder”)
keep_prob = gragh.get_tensor_by_name('Placeholder_2:0')# 获取dropout的保留参数
pred = gragh.get_tensor_by_name('Add_1:0')# 获取网络输出值
predict = tf.argmax(tf.reshape(pred, [-1, max_captcha, char_set_len]), 2)
model_path = "./model"
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint(model_path))# 加载变量值
print('finish loading model!')
text = sess.run(predict, feed_dict = {x:[image], keep_prob:1})
text = text[0].tolist()
text = [number[index] for index in text]
print("预测数据:",text)