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

本文介绍了一种使用卷积神经网络(CNN)进行图像验证码识别的方法。通过搭建包含卷积层、池化层及全连接层的神经网络模型,并采用TensorFlow实现训练过程。实验中使用的验证码图像大小为60x160像素,由数字和大小写字母组成。

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

在这里插入图片描述

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')
内容概要:本文介绍了一个关于超声谐波成像中幅度调制聚焦超声所引起全场位移和应变的分析模型,并提供了基于Matlab的代码实现。该模型旨在精确模拟和分析在超声谐波成像过程中,由于幅度调制聚焦超声作用于生物组织时产生的力学效应,包括全场的位移与应变分布,从而为医学成像和治疗提供理论支持和技术超声谐波成像中幅度调制聚焦超声引起的全场位移和应变的分析模型(Matlab代码实现)手段。文中详细阐述了模型构建的物理基础、数学推导过程以及Matlab仿真流程,具有较强的理论深度与工程应用价值。; 适合人群:具备一定声学、生物医学工程或力学背景,熟悉Matlab编程,从事医学成像、超声技术或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于超声弹性成像中的力学建模与仿真分析;②支持高强度聚焦超声(HIFU)治疗中的组织响应预测;③作为教学案例帮助理解超声与组织相互作用的物理机制;④为相关科研项目提供可复用的Matlab代码框架。; 阅读建议:建议读者结合超声物理和连续介质力学基础知识进行学习,重点关注模型假设、偏微分方程的数值求解方法及Matlab实现细节,建议动手运行并修改代码以加深理解,同时可拓展应用于其他超声成像或治疗场景的仿真研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BigData-0

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

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

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

打赏作者

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

抵扣说明:

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

余额充值