深度学习------卷积神经网络实现验证码

1. 卷积神经网络基本操作

import tensorflow as tf
import numpy as np
import os
import matplotlib.pyplot as plt
import random

def readimg(file):
    image=plt.imread(file)#读取文件
    image=image/255#归一化
    return image

images=[]
labels=[]
path=r'E:\ana\envs\tf14\day12\data3'
for f in os.listdir(path):
    img=readimg(path+'\\'+f)
    images.append(img)
    ones=np.eye(3)#标签进行独热
    labels.append(ones[int(f[0])])
print(labels)
print(images)
image_data=np.array(images)
label_data=np.array(labels)
print(image_data.shape)
print(label_data.shape)

#洗牌处理
np.random.seed(3)
m=image_data.shape[0]
order=np.random.permutation(m)
imgarr=image_data[order]
y_one_hot=label_data[order]
total=imgarr.shape[0]
print(total)

g=0
def next_batch(size):
    global g
    xb=imgarr[g:g+size]
    yb=y_one_hot[g:g+size]
    g=g+size
    return xb,yb

x=tf.placeholder(tf.float32,shape=[None,32,32,3])
y=tf.placeholder(tf.float32,shape=[None,3])# 独热编码

# 第1层卷积,输入图片数据(?, 32, 32, 3)
W1 = tf.Variable(tf.random_normal([3, 3, 3, 32]))  #卷积核3x3,输入通道3,输出通道32
L1 = tf.nn.conv2d(x, W1, strides=[1, 1, 1, 1], padding='SAME') #卷积输出 (?, 32, 32, 32)
L1 = tf.nn.relu(L1)
L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') #池化输出 (?, 16, 16, 32)

# 第2层卷积,输入图片数据(?, 16, 16, 32)
W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01)) #卷积核3x3,输入通道32,输出通道64
L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME') #卷积输出  (?, 16, 16, 64)
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') #池化输出 (?, 8, 8, 64)

# 变成一维向量
dim = L2.get_shape()[1].value * L2.get_shape()[2].value * L2.get_shape()[3].value
L2_flat = tf.reshape(L2,[-1, dim])

# 全连接  -> 3 outputs
W3 = tf.get_variable("W3", shape=[dim, 3], initializer=tf.contrib.layers.xavier_initializer())
b = tf.Variable(tf.random_normal([3]))
logits = tf.matmul(L2_flat, W3) + b

#代价函数
cost=tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,labels=y)
#梯度下降函数(优化器)
optimiter=tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)

#精确度
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

sess=tf.Session()
sess.run(tf.global_variables_initializer())

print('开始学习')
batch_size=100
train_count=16
train_test=0.9
for epo in range(train_count):
    avg_cost=0
    g=0
    total_batch=int(total*train_test/batch_size)
    for i in range(total_batch):
        batch_x,batch_y=next_batch(batch_size)
        cost_val,_=sess.run([cost,optimiter],feed_dict={x:batch_x,y:batch_y})
        avg_cost=avg_cost+cost_val/total_batch
    acc = sess.run(accuracy, feed_dict={x: imgarr[int(total * train_test):], y: y_one_hot[int(total * train_test):]})
    print(epo,avg_cost,acc)
acc_v=sess.run(accuracy,feed_dict={x:imgarr[int(total*train_test):],y:y_one_hot[int(total*train_test):]})
print(acc_v)
print('结束学习')
# 在测试集中随机抽一个样本进行测试
r=random.randint(0,int(total*train_test)-1)
label=sess.run(tf.argmax(y_one_hot[r:r+1],1))
predict=sess.run(tf.argmax(logits,1),feed_dict={x:imgarr[r:r+1]})
print(label)
print(predict)

2.卷积神经网络实现验证码

# 卷积神经网络验证码版本
import tensorflow as tf
import matplotlib.pyplot as plt
import os
import numpy as np
import random
from PIL import Image
tf.set_random_seed(123)

## 获取数据集
train_num=1000#训练集数量
test_num=100#测试集数量

img_height=60
img_width=160

char_num = 4  # 验证码字符个数
characters =range(10)
print(characters)
labellen = char_num * len(characters)

#标签独热处理
def label2vec(label):
    """
        :param label: 1327
            [0,1,0,0,0,0,0,0,0,0,
            0,0,0,1,0,0,0,0,0,0,
            0,0,1,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,1,0,0]    """
    label_vec=np.zeros(labellen)
    for i,num in enumerate(label):
        xb=i*len(characters)+int(num)
        label_vec[xb]=1
    return label_vec

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 get_all_files(file_path,num):
    image_list=[]
    label_list=[]
    i=0
    for ep in os.listdir(file_path):
        img=Image.open(file_path+'\\'+ep)
        img=convert2gray(np.array(img))
        img=np.array(img)/255
        image_list.append(img.reshape([img_height,img_width,1]))
        label=os.path.splitext(os.path.split(ep)[1])[0]
        label_list.append(label2vec(label))
        i+=1
        if i>=num:
            pass
    return np.array(image_list),np.array(label_list)

train_dir=r'E:\ana\envs\tf14\day12\vcode_data\train'
test_dir=r'E:\ana\envs\tf14\day12\vcode_data\test'
imgarr,y_one_hot=get_all_files(train_dir,train_num)
imgarr_test,y_test=get_all_files(test_dir,test_num)

# 自己实现next_batch函数,每次返回一批数据
g=0
def next_batch(size):
    global g
    xb=imgarr[g:g+size]
    yb=y_one_hot[g:g+size]
    g=g+size
    return xb,yb

#定义占位符
x=tf.placeholder(tf.float32,shape=[None,60,160,1])
y=tf.placeholder(tf.float32,shape=[None,labellen])
keep_pro=tf.placeholder(tf.float32)

#卷积层1
with tf.name_scope('cov1'):
    w1=tf.Variable(tf.random_normal([3,3,1,32]))
    l1=tf.nn.conv2d(x,w1,strides=[1,1,1,1],padding='SAME')
    l1=tf.nn.relu(l1)
    l1=tf.nn.max_pool(l1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
    l1=tf.nn.dropout(l1,keep_pro)
#卷积层2
with tf.name_scope('cov2'):
    w2=tf.Variable(tf.random_normal([3,3,32,64]))
    l2=tf.nn.conv2d(l1,w2,strides=[1,1,1,1],padding='SAME')
    l2=tf.nn.relu(l2)
    l2=tf.nn.max_pool(l2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
    l2=tf.nn.dropout(l2,keep_pro)

dim=l2.get_shape()[1].value*l2.get_shape()[2].value*l2.get_shape()[3].value
l2_flat=tf.reshape(l2,[-1,dim])
#全连接层1
w3=tf.Variable(tf.random_normal([dim,100],stddev=0.01))
b3=tf.Variable(tf.random_normal([100]))
logit1=tf.nn.relu(tf.matmul(l2_flat,w3)+b3)

#全连接层2
w4=tf.Variable(tf.random_normal([100,labellen],stddev=0.01))
b4=tf.Variable(tf.random_normal([labellen]))
logit2=tf.matmul(logit1,w4)+b4

#代价函数
cost=tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=logit2)
#优化器
optimiter=tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

# 测试模型计算准确率
y_true=tf.reshape(y,[-1,4,10])
y_predict=tf.reshape(logit2,[-1,4,10])
y_true=tf.argmax(y_true,2)
y_predict=tf.argmax(y_predict,2)
accurancy=tf.reduce_mean(tf.cast(tf.equal(y_true,y_predict),tf.float32))


#初始化全局变量
sess=tf.Session()
sess.run(tf.global_variables_initializer())

# 迭代训练
batch_size=100
batch_count=15
print('开始学习')
for ep in range(batch_count):
    avg_cost=0
    g=0
    total_batch=int(train_num/batch_size)
    for i in range(total_batch):
        batch_x,batch_y=next_batch(batch_size)
        cost_val,_=sess.run([cost,optimiter],feed_dict={x:batch_x,y:batch_y,keep_pro:0.7})
        avg_cost+=cost_val/total_batch
    print(ep,avg_cost)

acc=sess.run(accurancy,feed_dict={x:imgarr_test,y:y_test,keep_pro:0.7})
print(acc)
print('学习完成')

r = int(random.randint(0, test_num - 1))
print("标签: ", sess.run(tf.argmax(y_test[r:r + 1].reshape(4, 10), 1)))
pre = sess.run([tf.argmax(tf.reshape(logit2, [4, 10]), 1)], feed_dict={x: imgarr_test[r:r + 1],keep_pro:0.7})
print("预测: ", pre)

你可能感兴趣的:(深度学习,cnn,python)