import tensorflow as tf
import numpy as np
import os
from PIL import Image
import matplotlib.pyplot as plt
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
#设置随机种子
tf.set_random_seed(777)
#设置路径
train_path = r"D:\vcode_data\train"
test_path = r"D:\vcode_data\test"
#图片转为灰度
def convert2grey(img):
if len(img.shape)>2:
r,g,b, = img[:,:,0],img[:,:,1],img[:,:,2]
grey = (r+g+b)/3
return grey
else:
return img
#定义读取图片数据方法
def get_file(path,num):
x_data = []
y_data = []
count = 0
for item in os.listdir(path):
#将读取的图片路径放入x_path 中
img_path = path+"\\"+item
img= plt.imread(img_path)
x_data.append(convert2grey(img).reshape(60,160,1))
#生成标签数据
y_one_hot = np.eye(10)[list(map(int,item[:4]))].flatten()
y_data.append(y_one_hot)
# 每读取一张图片,计数+1
count += 1
#当读取图片数量达到num,就停止
if count == num:
break
return np.array(x_data),np.array(y_data)
#分别获取训练集和测试集
train_x,train_y = get_file(train_path,100) #1000张作为训练集
test_x,test_y = get_file(test_path,100) #100张作为测试集
#获取图片维度
height = train_x.shape[1]
width = train_x.shape[2]
#定义next_batch 方法
g_b = 0
def next_batch(size):
global g_b
x = train_x[g_b:g_b+size]
y = train_y[g_b:g_b+size]
g_b += size
return x,y
#定义占位符
x = tf.placeholder(tf.float32,shape=[None,height,width,1])
y = tf.placeholder(tf.float32,shape=[None,40])
#卷积层1
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")
#卷积层2
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")
#全连接层
dim = L2.get_shape()[1].value*L2.get_shape()[2].value*L2.get_shape()[3].value
L2_flat = tf.reshape(L2,shape=[-1,dim])
w3 = tf.Variable(tf.random_normal([dim,40]))
b = tf.Variable(tf.random_normal([40]))
logits = tf.matmul(L2_flat,w3)+b
#定义代价
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits,labels=y))
#优化器
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
#定义精度
y_pred = tf.argmax(tf.reshape(logits,shape=[-1,4,10]),axis=2)
y_true = tf.argmax(tf.reshape(y,shape=[-1,4,10]),axis=2)
correct = tf.equal(y_pred,y_true)
accuracy = tf.reduce_mean(tf.cast(correct,tf.float32))
#定义会话
sess = tf.Session()
sess.run(tf.global_variables_initializer())
#开始迭代
training_epoch = 10
batch_size = 100
for epoch in range(training_epoch):
avg_cost = 0
total_batch = int(1000/batch_size)
for i in range(total_batch):
batch_x,batch_y = next_batch(batch_size)
c,_ = sess.run([loss,optimizer],feed_dict={x:train_x,y:train_y})
avg_cost += c/total_batch
#精度
acc = sess.run(accuracy,feed_dict={x:train_x,y:train_y})
print("epoch:",epoch+1,"acc:",acc)
#打印测试集精度
print("测试集精度是:",sess.run(accuracy,feed_dict={x:test_x,y:test_y}))
#随机测试一个样本
np.random.seed(100)
r = np.random.randint(0,len(test_x)-1)
print("实际值是:",sess.run(tf.argmax(test_y[r].reshape(4,10),1)))
print("预测值是:",sess.run(tf.argmax(tf.reshape(logits,shape=[4,10]),1),feed_dict={x:test_x[r:r+1]}))
CNN用于识别验证码程序
最新推荐文章于 2023-08-23 11:03:10 发布