import tensorflow as tf
import numpy as np
import cv2
import os
import random
images_dir=r"captcha/images2/"
images_files=os.listdir(images_dir)
def one_hot_encode(text):
zeros=np.zeros([40])
for i,j in enumerate(text):
k=ord(j)
if k<48 or k>57:
raise ValueError("字符不匹配")
k=k-48
zeros[i*10+k]=1
return zeros
def return_a_couple_image_label(images_dir,images_files):
file=random.choice(images_files)
image=os.path.join(images_dir,file)
image=cv2.imread(image,cv2.IMREAD_GRAYSCALE)
image=np.reshape(image,[24,72])
result=np.zeros([24,72],dtype=np.float32)
cv2.normalize(image,result,0,1,cv2.NORM_MINMAX,dtype=cv2.CV_32F)
label1=file[0:4]
label=one_hot_encode(label1)
return result,label
def return_batch_image_label(batch_size,images_dir,images_files):
images_list=[]
labels_list=[]
for i in range(batch_size):
image,label=return_a_couple_image_label(images_dir,images_files)
images_list.append(image)
labels_list.append(label)
return np.array(images_list),np.array(labels_list)
def return_text(one_hot_label_list):
total_labels_list=[]
for i in one_hot_label_list:
label_text=""
one_hot_label=np.reshape(i,[4,10])
label_list=list(np.argmax(one_hot_label,1))
for item in label_list:
label_text=label_text+str(item)
total_labels_list.append(label_text)
return total_labels_list
x=tf.placeholder(dtype=tf.float32,shape=[None,24,72])
y=tf.placeholder(dtype=tf.float32,shape=[None,40])
X=tf.reshape(x,[-1,24,72,1])
w1=tf.Variable(tf.random_normal(shape=[3,3,1,32],stddev=0.1))
b1=tf.Variable(tf.random_normal(shape=[32],stddev=0.1))
conv1=tf.nn.conv2d(input=X,filter=w1,strides=[1,1,1,1],padding="SAME")
conv1=tf.nn.relu(conv1)
conv1=tf.nn.max_pool(value=conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
w2=tf.Variable(tf.random_normal(shape=[3,3,32,64],stddev=0.1))
b2=tf.Variable(tf.random_normal(shape=[64],stddev=0.1))
conv2=tf.nn.conv2d(input=conv1,filter=w2,strides=[1,1,1,1],padding="SAME")
conv2=tf.nn.relu(conv2)
conv2=tf.nn.max_pool(value=conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
w3=tf.Variable(tf.random_normal(shape=[3,3,64,64],stddev=0.1))
b3=tf.Variable(tf.random_normal(shape=[64],stddev=0.1))
conv3=tf.nn.conv2d(input=conv2,filter=w3,strides=[1,1,1,1],padding="SAME")
conv3=tf.nn.relu(conv3)
conv3=tf.nn.max_pool(value=conv3,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
conv3=tf.reshape(conv3,[-1,3*9*64])
fw1=tf.Variable(tf.random_normal(shape=[3*9*64,1024]))
fb1=tf.Variable(tf.random_normal(shape=[1024]))
f1=tf.matmul(conv3,fw1)+fb1
f1=tf.nn.dropout(f1,keep_prob=0.9)
fw2=tf.Variable(tf.random_normal(shape=[1024,40]))
fb2=tf.Variable(tf.random_normal(shape=[40]))
f2=tf.matmul(f1,fw2)+fb2
loss=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y,logits=f2))
train_step=tf.train.AdamOptimizer(1e-3).minimize(loss)
Y=tf.reshape(y,[-1,4,10])
predict=tf.reshape(f2,[-1,4,10])
accuracy=tf.equal(tf.argmax(Y,2),tf.argmax(predict,2))
accuracy=tf.reduce_mean(tf.cast(accuracy,tf.float32))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(2000):
x_batch,y_batch=return_batch_image_label(100,images_dir,images_files)
loss_,step_=sess.run([loss,train_step],feed_dict={x:x_batch,y:y_batch})
if(i+1)%100==0:
acc=sess.run(accuracy,feed_dict={x:x_batch,y:y_batch})
print(f"Iter{i+1} loss:{loss_} acc:{acc}")
if int(acc)==1:
break
|