验证码识别rnn训练模型代码

机器性能太弱,不能设置太多参数,梯度下降难

在这里插入图片描述

import numpy as np
import  random
from PIL import Image
import matplotlib.pyplot as plt
import tensorflow as tf
def code_cnn(x,y):
    #conv->relu->max_pool->conv->relu->max_pool->dropout
    # ->conv->relu->max_pool->full_connection->softmax
    with tf.variable_scope('net'):
        x_shape=x.get_shape()
        in_channels=x_shape[3]
        # print(in_channels)
        y_shape=y.get_shape()
        with tf.variable_scope('conv1',initializer=tf.random_normal_initializer(0,0.1),dtype=tf.float32):
            kernel_size_1=1
            w=tf.get_variable('w',shape=[3,3,in_channels,kernel_size_1])
            b=tf.get_variable('b',shape=[kernel_size_1])
            net=tf.nn.conv2d(x,w,strides=[1,1,1,1],padding='SAME')
            net=tf.nn.bias_add(net,b)
        with tf.variable_scope('relu1'):
            net=tf.nn.relu6(net)
        with tf.variable_scope('max_pool1'):
            tf.nn.max_pool(net,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
        with tf.variable_scope('conv2'):
            kernel_size_2 = 1
            w = tf.get_variable('w', shape=[3, 3, kernel_size_1, kernel_size_2])
            b = tf.get_variable('b', shape=[kernel_size_2])
            net = tf.nn.conv2d(net, w, strides=[1, 1, 1, 1], padding='SAME')
            net = tf.nn.bias_add(net, b)
        with tf.variable_scope('relu2'):
            net = tf.nn.relu6(net)
        with tf.variable_scope('max_pool2'):
            tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        with tf.variable_scope('dropout1'):
            tf.nn.dropout(net,keep_prob=0.75)
        with tf.variable_scope('conv3'):
            kernel_size_3 = 64
            w = tf.get_variable('w', shape=[11, 11, kernel_size_2, kernel_size_3])
            b = tf.get_variable('b', shape=[kernel_size_3])
            net = tf.nn.conv2d(net, w, strides=[1, 2, 2, 1], padding='SAME')
            net = tf.nn.bias_add(net, b)
        with tf.variable_scope('relu3'):
            net = tf.nn.relu6(net)
        with tf.variable_scope('max_pool3'):
            tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        with tf.variable_scope('fc1'):
            unit_number_1=124
            net_shape=net.get_shape()
            net_sample_feature_number=net_shape[1]*net_shape[2]*net_shape[3]
            net=tf.reshape(net,shape=[-1,net_sample_feature_number])#-1 所有元素按照每行net_sample_feature_number列组合,即成一行
            w=tf.get_variable('w',shape=[net_sample_feature_number,unit_number_1])
            b=tf.get_variable('b',shape=[unit_number_1])
            net=tf.add(tf.matmul(net,w),b)
        with tf.variable_scope('softmax'):
            unit_number_3= 4
            w = tf.get_variable('w', shape=[unit_number_1,unit_number_3])
            b = tf.get_variable('b', shape=[unit_number_3])
            net = tf.add(tf.matmul(net, w), b)
            act=tf.nn.softmax(net)
    return act
def random_next_batch(batch_size=1,code_size=4):#太多oom
    batch_x=[]
    batch_y=[]
    for i in range(batch_size):
        text,image=generate_code_image(code_size)
        code_number=[]
        for ch in text:
            code_number.append(code_char_2number_dict[ch])
        batch_x.append(image)
        batch_y.append(code_number)
    return np.array(batch_x),np.array(batch_y)
from captcha.image import ImageCaptcha

code_char_set = ['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']


def random_code_text(code_size):
    code_text=[]
    for i in range(code_size):
        c=random.choice(code_char_set)
        code_text.append(c)
    return code_text
code_char_2number_dict=dict(zip(code_char_set,range(len(code_char_set))))
code_number_2_char_dict=dict(zip(range(len(code_char_set)),code_char_set))
# print(code_number_2_char_dict)
def generate_code_image(code_size):
    image=ImageCaptcha()
    code_text=random_code_text(code_size)
    code_text=''.join(code_text)
    captcha=image.generate(code_text)
    # image.write(code_text,'captcha/'+code_text+'.jpg')
    # print(captcha)
    code_image=Image.open(captcha)
    code_image=np.array(code_image)
    return code_text,code_image
def train_code_cnn(mode_path):
    in_image_height=60
    in_image_weight=160
    code_size=4
    x=tf.placeholder(tf.float32,shape=[None,in_image_height,in_image_weight,1],name='x')
    y=tf.placeholder(tf.float32,shape=[None,code_size],name='y')
    network=code_cnn(x,y)
    cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=network,labels=y))
    # cost=tf.reduce_mean(tf.reduce_sum(tf.cast(tf.not_equal(y,network),tf.float32),axis=1))#不支持
    # cost=tf.reduce_mean(tf.reduce_sum(tf.square(y-network),axis=1))
    train=tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)
    accuracy=tf.reduce_mean(
        tf.cast(tf.equal(tf.reduce_sum(tf.cast(tf.equal(y,network),tf.int32),axis=1),4),tf.float32))#4个字母都正确
    saver=tf.train.Saver()
    with tf.Session(config=tf.ConfigProto(device_count={'cpu':0})) as sess:
        sess.run(tf.global_variables_initializer())
        step=0
        while True:
            batch_x,batch_y=random_next_batch(batch_size=64,code_size=code_size)
            batch_x=tf.image.rgb_to_grayscale(batch_x)
            batch_x=tf.image.resize_images(batch_x,size=(in_image_height,in_image_weight)
                                           ,method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
            _,cost_,accuracy_=sess.run([train,cost    ],feed_dict={x:batch_x.eval(),y:batch_y} )
            print('step:{},cost:{},accuracy:{}'.format(step,cost_,accuracy_))
            if step % 10 == 0:
                test_batch_x, test_batch_y = random_next_batch(64, code_size=4)
                test_batch_x = tf.image.rgb_to_grayscale(test_batch_x)
                test_batch_x = tf.image.resize_images(test_batch_x, size=(in_image_height, in_image_weight)
                                                 , method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
                acc = sess.run(accuracy, feed_dict={x: test_batch_x.eval(), y: test_batch_y})
                print('测试集准确率:{}'.format(acc))
                if acc>0.7 and accuracy_>0.7:
                    saver.save(sess,mode_path,global_step=step)
                    break
            step+=1

    pass
if __name__ == '__main__':
    # batch_x,batch_y=random_next_batch(10,4)
    # print(np.array(batch_x).shape)
    # print(batch_y)
    # # text,image=generate_code_image(4)
    # text='aaaa'
    # image=batch_x[0]
    # for ch in text:
    #     print(ch)
    # ax=plt.figure()
    # print(code_char_2number_dict['Z'])
    # ax.text(0.1,0.9,text,ha='center',va='center')
    # plt.imshow(image)
    # plt.show()
    train_code_cnn('./savepath/capcha.model')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BigData-0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值