1. 程式人生 > >神經網路驗證碼識別

神經網路驗證碼識別

根據(2)博主的規劃,筆者已經建立結構如下(文章結尾附上完整程式碼:主要來源(1)的up主):

下面我們一個個說明:

(1)datasets

這是資料集資料夾,因為我們要實現驗證碼,所以首先要生成驗證碼圖片,在datasets目錄下有gen_image.py用於生成驗證碼圖片。這裡可以通過下載或者爬蟲獲取各種資料集,筆者採用下面方法

需要安裝captcha(這是一個生成驗證碼圖片的庫)

 pip install captcha

如果報錯no module named setuptools可以參考

然後執行產生圖片的指令碼(gen_image.bat)

python C:/Users/asus-/Desktop/captcha_demo/datasets/gen_image.py ^
--output_dir C:/Users/asus-/Desktop/captcha_demo/datasets/images/ ^
--Captcha_size 4 ^
--image_num 1000 ^
pause

--output_dir就是輸出圖片的儲存路徑

--Captcha_size就是識別碼圖片上面字元的個數

--image_num就是產生圖片的數量,但是有可能少於這個數,因為有可能產生重複的隨機數,會覆蓋前面的

關於gen_image.py為:

import tensorflow as tf
from captcha.image import ImageCaptcha  
import random
import sys

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('output_dir', '/ ', 'This is the saved directory of the picture')
tf.app.flags.DEFINE_integer('Captcha_size', 3, 'This is the number of characters of captcha')
tf.app.flags.DEFINE_integer('image_num', 1000, 'This is the number of pictures generated ,but less than  image_num')


#驗證碼內容
Captcha_content = ['0','1','2','3','4','5','6','7','8','9']

# 生成字元
def random_captcha_text():
    captcha_text = []
    for i in range(FLAGS.Captcha_size):
        ch = random.choice(Captcha_content)
        captcha_text.append(ch)
    return captcha_text
 
# 生成字元對應的驗證碼
def gen_captcha_text_and_image():
    image = ImageCaptcha()
    captcha_text = random_captcha_text()
    captcha_text = ''.join(captcha_text)
    captcha = image.generate(captcha_text)
    image.write(captcha_text, FLAGS.output_dir + captcha_text + '.jpg')  


def main(unuse_args):
    for i in range(FLAGS.image_num ):
        gen_captcha_text_and_image()
        sys.stdout.write('\r>> Creating image %d/%d' % (i+1, FLAGS.image_num))
        sys.stdout.flush()
    sys.stdout.write('\n')
    sys.stdout.flush()
    print("Finish!!!!!!!!!!!")
                        
if __name__ == '__main__':
    tf.app.run()

執行後為:

接著轉化圖片為tfrecord格式,tfrecord資料檔案是一種將影象資料和標籤統一儲存的二進位制檔案,能更好的利用記憶體,在tensorflow中快速的複製,移動,讀取,儲存等.

同樣這裡寫了一個簡單的指令碼:

python C:/Users/asus-/Desktop/captcha_demo/datasets/gen_tfrecord.py ^
--dataset_dir C:/Users/asus-/Desktop/captcha_demo/datasets/images/ ^
--output_dir C:/Users/asus-/Desktop/captcha_demo/datasets/ ^
--test_num 10 ^
--random_seed 0 ^
pause

從上到下依次是資料集位置,tfrecord生成位置,測試集個數,隨機種子(用於打亂資料集)

import tensorflow as tf
import os
import random
import math
import sys
from PIL import Image
import numpy as np

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('dataset_dir', '/ ', 'This is the source directory of the picture')
tf.app.flags.DEFINE_string('output_dir', '/ ', 'This is the saved directory of the picture')
tf.app.flags.DEFINE_integer('test_num', 20, 'This is the number of test of captcha')
tf.app.flags.DEFINE_integer('random_seed', 0, 'This is the random_seed')

#判斷tfrecord檔案是否存在
def dataset_exists(dataset_dir):
    for split_name in ['train', 'test']:
        output_filename = os.path.join(dataset_dir,split_name + '.tfrecords')
        if not tf.gfile.Exists(output_filename):
            return False
    return True

#獲取所有驗證碼圖片
def get_filenames_and_classes(dataset_dir):
    photo_filenames = []
    for filename in os.listdir(dataset_dir):
        #獲取檔案路徑
        path = os.path.join(dataset_dir, filename)
        photo_filenames.append(path)
    return photo_filenames

def int64_feature(values):
    if not isinstance(values, (tuple, list)):
        values = [values]
    return tf.train.Feature(int64_list=tf.train.Int64List(value=values))

def bytes_feature(values):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))

def image_to_tfexample(image_data, label0, label1, label2, label3):
    #Abstract base class for protocol messages.
    return tf.train.Example(features=tf.train.Features(feature={
      'image': bytes_feature(image_data),
      'label0': int64_feature(label0),
      'label1': int64_feature(label1),
      'label2': int64_feature(label2),
      'label3': int64_feature(label3),
    }))

#把資料轉為TFRecord格式
def convert_dataset(split_name, filenames, dataset_dir):
    assert split_name in ['train', 'test']

    with tf.Session() as sess:
        #定義tfrecord檔案的路徑+名字
        output_filename = os.path.join(FLAGS.output_dir,split_name + '.tfrecords')
        with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer:
            for i,filename in enumerate(filenames):
                try:
                    sys.stdout.write('\r>> Converting image %d/%d' % (i+1, len(filenames)))
                    sys.stdout.flush()

                    #讀取圖片
                    image_data = Image.open(filename)  
                    #根據模型的結構resize
                    image_data = image_data.resize((224, 224))
                    #灰度化
                    image_data = np.array(image_data.convert('L'))
                    #將圖片轉化為bytes
                    image_data = image_data.tobytes()              

                    #獲取label
                    labels = filename.split('/')[-1][0:4]
                    num_labels = []
                    for j in range(4):
                        num_labels.append(int(labels[j]))
                                            
                    #生成protocol資料型別
                    example = image_to_tfexample(image_data, num_labels[0], num_labels[1], num_labels[2], num_labels[3])
                    tfrecord_writer.write(example.SerializeToString())
                    
                except IOError as e:
                    print('Could not read:',filename)
                    print('Error:',e)
    sys.stdout.write('\n')
    sys.stdout.flush()

	
def main(unuse_args):
    
    if dataset_exists(FLAGS.output_dir):
        print('tfcecord file has been existed!!')
        
    else:
    #獲得所有圖片
        photo_filenames = get_filenames_and_classes(FLAGS.dataset_dir)
    #把資料切分為訓練集和測試集,並打亂
        random.seed(FLAGS.random_seed)
        random.shuffle(photo_filenames)
        training_filenames = photo_filenames[FLAGS.test_num:]
        testing_filenames = photo_filenames[:FLAGS.test_num]

    #資料轉換
        convert_dataset('train', training_filenames,FLAGS.dataset_dir)
        convert_dataset('test', testing_filenames, FLAGS.dataset_dir)
        print('Finish!!!!!!!!!!!!!!!!!')
	
if __name__ == '__main__':
    tf.app.run()

執行後:

(2)訓練

執行指令碼(train.bat):

python C:/Users/asus-/Desktop/captcha_demo/train.py ^
--tfrecord_dir C:/Users/asus-/Desktop/captcha_demo/datasets/train.tfrecords ^
--model_dir C:/Users/asus-/Desktop/captcha_demo/model/Alexnet ^
--batch_size 15 ^
--train_num 10 ^
--print_loss_accuracy_interval 5 ^
--learning_rate 0.005 ^
pause

這裡說一個數據集的讀入過程:

def read_and_decode(filename):
    # 根據檔名生成一個佇列
    filename_queue = tf.train.string_input_producer([filename])
    reader = tf.TFRecordReader()
    # 返回檔名和檔案
    _, serialized_example = reader.read(filename_queue)   
    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'image' : tf.FixedLenFeature([], tf.string),
                                           'label0': tf.FixedLenFeature([], tf.int64),
                                           'label1': tf.FixedLenFeature([], tf.int64),
                                           'label2': tf.FixedLenFeature([], tf.int64),
                                           'label3': tf.FixedLenFeature([], tf.int64),
                                       })
    # 獲取圖片資料
    image = tf.decode_raw(features['image'], tf.uint8)
    # tf.train.shuffle_batch必須確定shape
    image = tf.reshape(image, [224, 224])
    # 圖片預處理
    image = tf.cast(image, tf.float32) / 255.0
    image = tf.subtract(image, 0.5)
    image = tf.multiply(image, 2.0)
    # 獲取label
    label0 = tf.cast(features['label0'], tf.int32)
    label1 = tf.cast(features['label1'], tf.int32)
    label2 = tf.cast(features['label2'], tf.int32)
    label3 = tf.cast(features['label3'], tf.int32)

    return image, label0, label1, label2, label3

正如上面所說我們這裡的reader對應的是即tfrecord格式的reader

reader = tf.TFRecordReader()

(1)資料中up主運行了大概6000次,因為筆者電腦配置較低又是cpu,但是為了後續視覺化設計的神經網路,這裡我就暫且先執行10次,使之產生model

接下來我們通過tensorboard寫了個小demo來直觀的看一下設計的多工alexnet網路

import tensorflow as tf

with tf.Session() as sess:
    
    my_saver = tf.train.import_meta_graph('C:/Users/asus-/Desktop/captcha_demo/model/Alexnet.meta')
    my_saver.restore(sess,tf.train.latest_checkpoint('C:/Users/asus-/Desktop/captcha_demo/model/'))
    graph = tf.get_default_graph()
    writer_test=tf.summary.FileWriter('C:/Users/asus-/Desktop/logs/',sess.graph)
         

可以看到前5層convolutional,後邊3層full-connected,最後一層的full-connected採取的是全連線層,對應這個採用多工的類子中最後一層對應四個連線層

關於更多的alexent網路,可以查文件

最後貼一下train.py:

import os
import tensorflow as tf 
from nets import nets_factory
import numpy as np
import image_reader as ir

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('tfrecord_dir', '/ ', 'This is the tfrecord directory of the picture')
tf.app.flags.DEFINE_string('model_dir', '/ ', 'This is the saved model directory of the net')
tf.app.flags.DEFINE_integer('batch_size', 10, 'This is the batch size')
tf.app.flags.DEFINE_integer('train_num', 1000, 'This is the number of train')
tf.app.flags.DEFINE_integer('print_loss_accuracy_interval', 10, 'This is the interval of printing')
tf.app.flags.DEFINE_float('learning_rate', 0.001, 'This is the rate of learning')

# 不同字元數量
CHAR_SET_LEN = 10

# placeholder
x = tf.placeholder(tf.float32, [None, 224, 224])  
y0 = tf.placeholder(tf.float32, [None]) 
y1 = tf.placeholder(tf.float32, [None]) 
y2 = tf.placeholder(tf.float32, [None]) 
y3 = tf.placeholder(tf.float32, [None])

image, label0, label1, label2, label3 = ir.read_and_decode(FLAGS.tfrecord_dir)

#使用shuffle_batch可以隨機打亂
image_batch, label_batch0, label_batch1, label_batch2, label_batch3 = tf.train.shuffle_batch(
        [image, label0, label1, label2, label3], batch_size =FLAGS.batch_size,
        capacity = 50000, min_after_dequeue=10000, num_threads=1)

#定義網路結構
train_network_fn = nets_factory.get_network_fn(
    'alexnet_v2',
    num_classes=CHAR_SET_LEN,
    weight_decay=0.0005,
    is_training=True)

# inputs: a tensor of size [batch_size, height, width, channels]
X = tf.reshape(x, [FLAGS.batch_size, 224, 224, 1])
# 資料輸入網路得到輸出值
logits0,logits1,logits2,logits3,end_points = train_network_fn(X)
    
# 把標籤轉成one_hot的形式
one_hot_labels0 = tf.one_hot(indices=tf.cast(y0, tf.int32), depth=CHAR_SET_LEN)
one_hot_labels1 = tf.one_hot(indices=tf.cast(y1, tf.int32), depth=CHAR_SET_LEN)
one_hot_labels2 = tf.one_hot(indices=tf.cast(y2, tf.int32), depth=CHAR_SET_LEN)
one_hot_labels3 = tf.one_hot(indices=tf.cast(y3, tf.int32), depth=CHAR_SET_LEN)
    
  
loss0 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits0,labels=one_hot_labels0)) 
loss1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits1,labels=one_hot_labels1)) 
loss2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits2,labels=one_hot_labels2)) 
loss3 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits3,labels=one_hot_labels3)) 
  
total_loss = (loss0+loss1+loss2+loss3)/4.0
optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate).minimize(total_loss) 
    
# 計算準確率
correct_prediction0 = tf.equal(tf.argmax(one_hot_labels0,1),tf.argmax(logits0,1))
accuracy0 = tf.reduce_mean(tf.cast(correct_prediction0,tf.float32))
    
correct_prediction1 = tf.equal(tf.argmax(one_hot_labels1,1),tf.argmax(logits1,1))
accuracy1 = tf.reduce_mean(tf.cast(correct_prediction1,tf.float32))
    
correct_prediction2 = tf.equal(tf.argmax(one_hot_labels2,1),tf.argmax(logits2,1))
accuracy2 = tf.reduce_mean(tf.cast(correct_prediction2,tf.float32))
    
correct_prediction3 = tf.equal(tf.argmax(one_hot_labels3,1),tf.argmax(logits3,1))
accuracy3 = tf.reduce_mean(tf.cast(correct_prediction3,tf.float32)) 
    
# 用於儲存模型
saver = tf.train.Saver()

def main(unuse_args):
    
    with tf.Session() as sess:
     
        # 初始化
        sess.run(tf.global_variables_initializer())
        
        # 建立一個協調器,管理執行緒
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        for i in range(FLAGS.train_num):
            b_image, b_label0, b_label1 ,b_label2 ,b_label3 = sess.run([image_batch, label_batch0, label_batch1, label_batch2, label_batch3])
            sess.run(optimizer, feed_dict={x: b_image, y0:b_label0, y1: b_label1, y2: b_label2, y3: b_label3})  

            if i % FLAGS.print_loss_accuracy_interval == 0:
                
                acc0,acc1,acc2,acc3,TotalLoss = sess.run([accuracy0,accuracy1,accuracy2,accuracy3,total_loss],feed_dict={x: b_image,
                                                                                                                    y0: b_label0,
                                                                                                                    y1: b_label1,
                                                                                                                    y2: b_label2,
                                                                                                                    y3: b_label3})      
                print ("times:%d  Loss:%.3f  Accuracy:%.2f,%.2f,%.2f,%.2f" % (i,TotalLoss,acc0,acc1,acc2,acc3))
                 
                
        saver.save(sess, FLAGS.model_dir)                      
        # 通知其他執行緒關閉
        coord.request_stop()
        coord.join(threads)

if __name__ == '__main__':
    tf.app.run()





(3)測試

同樣執行一個指令碼:

python C:/Users/asus-/Desktop/captcha_demo/evaluate.py ^
--tfrecord_dir C:\Users\asus-\Desktop\captcha_demo\datasets\test.tfrecords ^
--test_size 10 ^
pause

關於evaluate.py 則為:

import tensorflow as tf 
import image_reader as ir
from nets import nets_factory



FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('tfrecord_dir', '/ ', 'This is the tfrecord directory of the picture')
tf.app.flags.DEFINE_integer('test_size', 10, 'This is the batch size')



# 不同字元數量
CHAR_SET_LEN = 10

BATCH_SIZE=1

# placeholder
x = tf.placeholder(tf.float32, [None, 224, 224])  
y0 = tf.placeholder(tf.float32, [None]) 
y1 = tf.placeholder(tf.float32, [None]) 
y2 = tf.placeholder(tf.float32, [None]) 
y3 = tf.placeholder(tf.float32, [None])

image, label0, label1, label2, label3 = ir.read_and_decode(FLAGS.tfrecord_dir)

#使用shuffle_batch可以隨機打亂
image_batch, label_batch0, label_batch1, label_batch2, label_batch3 = tf.train.shuffle_batch(
        [image, label0, label1, label2, label3], batch_size =BATCH_SIZE,
        capacity = 50000, min_after_dequeue=10000, num_threads=1)



#定義網路結構
train_network_fn = nets_factory.get_network_fn(
    'alexnet_v2',
    num_classes=CHAR_SET_LEN,
    weight_decay=0.0005,
    is_training=False)

# inputs: a tensor of size [batch_size, height, width, channels]
X = tf.reshape(x, [BATCH_SIZE, 224, 224, 1])
# 資料輸入網路得到輸出值
logits0,logits1,logits2,logits3,end_points = train_network_fn(X)

# 預測值
predict0 = tf.reshape(logits0, [-1, CHAR_SET_LEN])  
predict0 = tf.argmax(predict0, 1)  

predict1 = tf.reshape(logits1, [-1, CHAR_SET_LEN])  
predict1 = tf.argmax(predict1, 1)  

predict2 = tf.reshape(logits2, [-1, CHAR_SET_LEN])  
predict2 = tf.argmax(predict2, 1)  

predict3 = tf.reshape(logits3, [-1, CHAR_SET_LEN])  
predict3 = tf.argmax(predict3, 1) 
    
# 把標籤轉成one_hot的形式
one_hot_labels0 = tf.one_hot(indices=tf.cast(y0, tf.int32), depth=CHAR_SET_LEN)
one_hot_labels1 = tf.one_hot(indices=tf.cast(y1, tf.int32), depth=CHAR_SET_LEN)
one_hot_labels2 = tf.one_hot(indices=tf.cast(y2, tf.int32), depth=CHAR_SET_LEN)
one_hot_labels3 = tf.one_hot(indices=tf.cast(y3, tf.int32), depth=CHAR_SET_LEN)
    
  
loss0 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits0,labels=one_hot_labels0)) 
loss1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits1,labels=one_hot_labels1)) 
loss2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits2,labels=one_hot_labels2)) 
loss3 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits3,labels=one_hot_labels3)) 
  
total_loss = (loss0+loss1+loss2+loss3)/4.0
train_step = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(total_loss) 
    
# 計算準確率
correct_prediction0 = tf.equal(tf.argmax(one_hot_labels0,1),tf.argmax(logits0,1))
accuracy0 = tf.reduce_mean(tf.cast(correct_prediction0,tf.float32))
    
correct_prediction1 = tf.equal(tf.argmax(one_hot_labels1,1),tf.argmax(logits1,1))
accuracy1 = tf.reduce_mean(tf.cast(correct_prediction1,tf.float32))
    
correct_prediction2 = tf.equal(tf.argmax(one_hot_labels2,1),tf.argmax(logits2,1))
accuracy2 = tf.reduce_mean(tf.cast(correct_prediction2,tf.float32))
    
correct_prediction3 = tf.equal(tf.argmax(one_hot_labels3,1),tf.argmax(logits3,1))
accuracy3 = tf.reduce_mean(tf.cast(correct_prediction3,tf.float32)) 
    



def main(unuse_args):
    
    with tf.Session() as sess:
         
        sess.run(tf.global_variables_initializer())
        
        #載入模型
        my_saver = tf.train.import_meta_graph('C:/Users/asus-/Desktop/captcha_demo/model/Alexnet.meta')
        my_saver.restore(sess,tf.train.latest_checkpoint('C:/Users/asus-/Desktop/captcha_demo/model/'))

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        for i in range(FLAGS.test_size):
            b_image, b_label0, b_label1 ,b_label2 ,b_label3 = sess.run([image_batch, label_batch0, label_batch1, label_batch2, label_batch3])
            print('%d times label:%d,%d,%d,%d' % ((i+1) ,b_label0, b_label1 ,b_label2 ,b_label3))
            sess.run(train_step, feed_dict={x: b_image, y0:b_label0, y1: b_label1, y2: b_label2, y3: b_label3}) 
            label0,label1,label2,label3 = sess.run([predict0,predict1,predict2,predict3], feed_dict={x: b_image})
            print('predict:',label0,label1,label2,label3)
            
        coord.request_stop()
        coord.join(threads)



if __name__ == '__main__':
    tf.app.run()

注意筆者在測試的時候

--tfrecord_dir C:\Users\asus-\Desktop\captcha_demo\datasets\test.tfrecords ^

資料夾路徑如果由\改為/即為:

--tfrecord_dir C:/Users/asus-/Desktop/captcha_demo/datasets/test.tfrecords ^

那麼結果會報錯,意思就是說沒有讀取到資料,呵呵目前還不明覺厲!!

最後附上全部程式碼: