import tensorflow as tf
import matplotlib. pyplot as plt
import numpy as np
import cv2
import os
import random
import datetime
images_dir= r"captcha/images3/"
images_files= os. listdir( images_dir)
train_files= images_files[ : 8000 ]
test_files= images_files[ 8000 : ]
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.5 )
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) +
( 1e - 3 ) * tf. nn. l2_loss( w1) +
( 1e - 3 ) * tf. nn. l2_loss( w2) +
( 1e - 3 ) * tf. nn. l2_loss( w3) +
( 1e - 3 ) * tf. nn. l2_loss( fw1) +
( 1e - 3 ) * tf. nn. l2_loss( fw2)
)
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) )
saver= tf. train. Saver( )
with tf. Session( ) as sess:
train_acc_list= [ ]
test_acc_list= [ ]
Iter_list= [ ]
sess. run( tf. global_variables_initializer( ) )
for i in range ( 5000 ) :
x_train_batch, y_train_batch= return_batch_image_label( 500 , images_dir, train_files)
x_test_batch, y_test_batch= return_batch_image_label( 100 , images_dir, test_files)
loss_, step_= sess. run( [ loss, train_step] , feed_dict= { x: x_train_batch, y: y_train_batch} )
if ( i+ 1 ) % 100 == 0 :
time= str ( datetime. datetime. now( ) ) . split( " " ) [ - 1 ] . split( "." ) [ 0 ]
acc_train= sess. run( accuracy, feed_dict= { x: x_train_batch, y: y_train_batch} )
acc_test= sess. run( accuracy, feed_dict= { x: x_test_batch, y: y_test_batch} )
train_acc_list. append( acc_train)
test_acc_list. append( acc_test)
Iter_list. append( i+ 1 )
print ( f"Iter{i+1} ,loss:{loss_} ,train_acc:{acc_train} ,test_acc:{acc_test},时间{time}" )
if int ( acc_train) == 1 :
break
saver. save( sess, ".\\Model\\model.ckpt" )
import tensorflow as tf
import matplotlib. pyplot as plt
import numpy as np
import cv2
import os
import random
import datetime
images_dir= r"captcha/images3/"
images_files= os. listdir( images_dir)
train_files= images_files[ : 8000 ]
test_files= images_files[ 8000 : ]
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.5 )
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) +
( 1e - 3 ) * tf. nn. l2_loss( w1) +
( 1e - 3 ) * tf. nn. l2_loss( w2) +
( 1e - 3 ) * tf. nn. l2_loss( w3) +
( 1e - 3 ) * tf. nn. l2_loss( fw1) +
( 1e - 3 ) * tf. nn. l2_loss( fw2)
)
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) )
saver = tf. train. Saver( )
with tf. Session( ) as sess:
saver. restore( sess, ".\\Model\\model.ckpt" )
x_train_batch, y_train_batch= return_batch_image_label( 10 , images_dir, train_files)
predict_= sess. run( predict, feed_dict= { x: x_train_batch} )
predict_= return_text( predict_)
for i in range ( 10 ) :
print ( predict_[ i] )
plt. figure( )
plt. imshow( x_train_batch[ i] )
plt. show( )