基於TensorFlow的簡單驗證碼識別


  TensorFlow 可以用來實現驗證碼識別的過程,這里識別的驗證碼是圖形驗證碼,首先用標注好的數據來訓練一個模型,然后再用模型來實現這個驗證碼的識別。

生成驗證碼

  首先生成驗證碼,這里使用 Python 的 captcha 庫來生成即可,這個庫默認是沒有安裝的,所以需要先安裝這個庫,另外還需要安裝 pillow 庫,使用 pip3 即可:

pip3 install captcha pillow

  安裝好之后,就可以用如下代碼來生成一個簡單的圖形驗證碼了:

from captcha.image import ImageCaptcha
from PIL import Image
 
text='1234'
image=ImageCaptcha()
captcha=image.generate(text)
captcha_image=Image.open(captcha)
captcha_image.show()

  運行之后便會彈出一張圖片,結果如下:

  

預處理

  首先定義好了要生成的驗證碼文本內容,這就相當於已經有了 label ,然后再用它來生成驗證碼,就可以得到輸入數據 x 了。

  在這里首先定義好我們的輸入詞表,由於大小寫字母加數字的詞表比較龐大,用含有大小寫字母和數字的驗證碼,一個驗證碼四個字符,那么一共可能的組合是 (26 + 26 + 10) ^ 4 = 14776336 種組合,這個數量訓練起來有點大,所以這里精簡一下,只使用純數字的驗證碼來訓練,這樣其組合個數就變為 10 ^ 4 = 10000 種,顯然少了很多。

  定義一個詞表和其長度變量:

#這里 VOCAB 就是詞表的內容,即0到9這10個數字,驗證碼的字符個數即 CAPTCHA_LENGTH 是4,詞表長度是 VOCAB 的長度,即10。
VOCAB=['0','1','2','3','4','5','6','7','8','9']
CAPTCHA_LENGTH=4
VOCAB_LENGTH=len(VOCAB)

 

  接下來定義一個生成驗證碼數據的方法,這里將返回的數據轉為了 Numpy 形式的數組:

from PIL import Image
from captcha.image import ImageCaptcha
import numpy as np
 
def generate_captcha(captcha_text):
    """
    get captcha text and np array
    :param captcha_text: source text
    :return: captcha image and array
    """
    image = ImageCaptcha()
    captcha = image.generate(captcha_text)
    captcha_image = Image.open(captcha)
    captcha_array = np.array(captcha_image)
    return captcha_array

 

  這樣調用此方法,我們就可以得到一個 Numpy 數組了,這個其實是把驗證碼轉化成了每個像素的 RGB,我們調用一下這個方法試試:

captcha = generate_captcha('1234')
print(captcha, captcha.shape)
"""
[[[239 244 244]
  [239 244 244]
  [239 244 244]
  ..., 
  ..., 
  [239 244 244]
  [239 244 244]
  [239 244 244]]] 
(60, 160, 3)
"""

 

  可以看到它的 shape 是 (60, 160, 3),這其實代表驗證碼圖片的高度是 60,寬度是 160,是 60 x 160 像素的驗證碼,每個像素都有 RGB 值,所以最后一維即為像素的 RGB 值。

  接下來需要定義 label,由於需要使用深度學習模型進行訓練,所以這里的 label 數據最好使用 One-Hot 編碼,即如果驗證碼文本是 1234,那么應該詞表索引位置置 1,總共的長度是 40,用程序實現一下 One-Hot 編碼和文本的互相轉換:

def text2vec(text):
    """
    text to one-hot vector
    :param text: source text
    :return: np array
    """
    if len(text) > CAPTCHA_LENGTH:
        return False
    vector = np.zeros(CAPTCHA_LENGTH * VOCAB_LENGTH)
    for i, c in enumerate(text):
        index = i * VOCAB_LENGTH + VOCAB.index(c)
        vector[index] = 1
    return vector
 
 
def vec2text(vector):
    """
    vector to captcha text
    :param vector: np array
    :return: text
    """
    if not isinstance(vector, np.ndarray):
        vector = np.asarray(vector)
    vector = np.reshape(vector, [CAPTCHA_LENGTH, -1])
    text = ''
    for item in vector:
        text += VOCAB[np.argmax(item)]
    return text

 

  上面的 text2vec() 方法就是將真實文本轉化為 One-Hot 編碼,vec2text() 方法就是將 One-Hot 編碼轉回真實文本。

  例如這里調用一下這兩個方法,我們將 1234 文本轉換為 One-Hot 編碼,然后在將其轉回來:

vector = text2vec('1234')
text = vec2text(vector)
print(vector, text)
"""
[ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.
  0.  0.  0.  0.]
1234
"""

 

  接下來構造一批數據,x 數據就是驗證碼的 Numpy 數組,y 數據就是驗證碼的文本的 One-Hot 編碼,生成內容如下:

import random
from os.path import join, exists
import pickle
import numpy as np
from os import makedirs
 
DATA_LENGTH = 10000
DATA_PATH = 'data'
 
def get_random_text():
    text = ''
    for i in range(CAPTCHA_LENGTH):
        text += random.choice(VOCAB)
    return text
 
def generate_data():
    print('Generating Data...')
    data_x, data_y = [], []
 
    # generate data x and y
    for i in range(DATA_LENGTH):
        text = get_random_text()
        # get captcha array
        captcha_array = generate_captcha(text)
        # get vector
        vector = text2vec(text)
        data_x.append(captcha_array)
        data_y.append(vector)
 
    # write data to pickle
    if not exists(DATA_PATH):
        makedirs(DATA_PATH)
 
    x = np.asarray(data_x, np.float32)
    y = np.asarray(data_y, np.float32)
    with open(join(DATA_PATH, 'data.pkl'), 'wb') as f:
        pickle.dump(x, f)
        pickle.dump(y, f)
#這里定義了一個 get_random_text() 方法,可以隨機生成驗證碼文本,然后接下來再利用這個隨機生成的文本來產生對應的 x、y 數據,
#再將數據寫入到 pickle 文件里,這樣就完成了預處理的操作。

 

構建模型

  有了數據之后,就可以構建模型了,這里還是利用 train_test_split() 方法將數據分為三部分,訓練集、開發集、驗證集:

with open('data.pkl', 'rb') as f:
    data_x = pickle.load(f)
    data_y = pickle.load(f)
    return standardize(data_x), data_y
 
train_x, test_x, train_y, test_y = train_test_split(data_x, data_y, test_size=0.4, random_state=40)
dev_x, test_x, dev_y, test_y, = train_test_split(test_x, test_y, test_size=0.5, random_state=40)

 

  接下來使用三個數據集構建三個 Dataset 對象:

# train and dev dataset
train_dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y)).shuffle(10000)
train_dataset = train_dataset.batch(FLAGS.train_batch_size)
 
dev_dataset = tf.data.Dataset.from_tensor_slices((dev_x, dev_y))
dev_dataset = dev_dataset.batch(FLAGS.dev_batch_size)
 
test_dataset = tf.data.Dataset.from_tensor_slices((test_x, test_y))
test_dataset = test_dataset.batch(FLAGS.test_batch_size)

 

  然后初始化一個迭代器,並綁定到這個數據集上:

# a reinitializable iterator
iterator = tf.data.Iterator.from_structure(train_dataset.output_types, train_dataset.output_shapes)
train_initializer = iterator.make_initializer(train_dataset)
dev_initializer = iterator.make_initializer(dev_dataset)
test_initializer = iterator.make_initializer(test_dataset)

 

  接下來就是關鍵的部分了,在這里使用三層卷積和兩層全連接網絡進行構造。為了簡化寫法,直接使用 TensorFlow 的 layers 模塊:

# input Layer
with tf.variable_scope('inputs'):
    # x.shape = [-1, 60, 160, 3]
    x, y_label = iterator.get_next()
keep_prob = tf.placeholder(tf.float32, [])
y = tf.cast(x, tf.float32)
# 3 CNN layers
for _ in range(3):
    y = tf.layers.conv2d(y, filters=32, kernel_size=3, padding='same', activation=tf.nn.relu)
    y = tf.layers.max_pooling2d(y, pool_size=2, strides=2, padding='same')
    # y = tf.layers.dropout(y, rate=keep_prob)
 
# 2 dense layers
y = tf.layers.flatten(y)
y = tf.layers.dense(y, 1024, activation=tf.nn.relu)
y = tf.layers.dropout(y, rate=keep_prob)
y = tf.layers.dense(y, VOCAB_LENGTH)
#這里卷積核大小為 3,padding 使用 SAME 模式,激活函數使用 relu。

 

  經過全連接網絡變換之后,y 的 shape 就變成了 [batch_size, n_classes], label 是 CAPTCHA_LENGTH 個 One-Hot 向量拼合而成的,在這里我們想使用交叉熵來計算,但是交叉熵計算的時候,label 參數向量最后一維各個元素之和必須為 1,不然計算梯度的時候會出現問題。詳情參見 TensorFlow 的官方文檔:https://www.tensorflow.org/api_docs/python/tf/nn/softmax_cross_entropy_with_logits

NOTE: While the classes are mutually exclusive, their probabilities need not be. All that is required is that each row of labels is a valid probability distribution. If they are not, the computation of the gradient will be incorrect.

  但是現在的 label 參數是 CAPTCHA_LENGTH 個 One-Hot 向量拼合而成,所以這里各個元素之和為 CAPTCHA_LENGTH,所以需要重新 reshape 一下,確保最后一維各個元素之和為 1:

y_reshape = tf.reshape(y, [-1, VOCAB_LENGTH])
y_label_reshape = tf.reshape(y_label, [-1, VOCAB_LENGTH])
#這樣就可以確保最后一維是 VOCAB_LENGTH 長度,而它就是一個 One-Hot 向量,所以各元素之和必定為 1。

 

  然后 Loss 和 Accuracy 就好計算了:

# loss
cross_entropy = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=y_reshape, labels=y_label_reshape))
# accuracy
max_index_predict = tf.argmax(y_reshape, axis=-1)
max_index_label = tf.argmax(y_label_reshape, axis=-1)
correct_predict = tf.equal(max_index_predict, max_index_label)
accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32))

再接下來執行訓練即可:

# train
train_op = tf.train.RMSPropOptimizer(FLAGS.learning_rate).minimize(cross_entropy, global_step=global_step)
for epoch in range(FLAGS.epoch_num):
    tf.train.global_step(sess, global_step_tensor=global_step)
    # train
    sess.run(train_initializer)
    for step in range(int(train_steps)):
        loss, acc, gstep, _ = sess.run([cross_entropy, accuracy, global_step, train_op],
                                       feed_dict={keep_prob: FLAGS.keep_prob})
        # print log
        if step % FLAGS.steps_per_print == 0:
            print('Global Step', gstep, 'Step', step, 'Train Loss', loss, 'Accuracy', acc)
 
    if epoch % FLAGS.epochs_per_dev == 0:
        # dev
        sess.run(dev_initializer)
        for step in range(int(dev_steps)):
            if step % FLAGS.steps_per_print == 0:
                print('Dev Accuracy', sess.run(accuracy, feed_dict={keep_prob: 1}), 'Step', step)
#首先初始化 train_initializer,將 iterator 綁定到 Train Dataset 上,然后執行 train_op,獲得 loss、acc、gstep 等結果並輸出。

 

訓練

  運行訓練過程,結果類似如下:驗證集准確率 95% 以上。

...
Dev Accuracy 0.9580078 Step 0
Dev Accuracy 0.9472656 Step 2
Dev Accuracy 0.9501953 Step 4
Dev Accuracy 0.9658203 Step 6
Global Step 3243 Step 0 Train Loss 1.1920928e-06 Accuracy 1.0
Global Step 3245 Step 2 Train Loss 1.5497207e-06 Accuracy 1.0
Global Step 3247 Step 4 Train Loss 1.1920928e-06 Accuracy 1.0
Global Step 3249 Step 6 Train Loss 1.7881392e-06 Accuracy 1.0
...

 

測試

  訓練過程可以每隔幾個 Epoch 保存一下模型:

# save model
if epoch % FLAGS.epochs_per_save == 0:
    saver.save(sess, FLAGS.checkpoint_dir, global_step=gstep)

 

  當然也可以取驗證集上准確率最高的模型進行保存。

  驗證時可以重新 Reload 一下模型,然后進行驗證:

# load model
ckpt = tf.train.get_checkpoint_state('ckpt')
if ckpt:
    saver.restore(sess, ckpt.model_checkpoint_path)
    print('Restore from', ckpt.model_checkpoint_path)
    sess.run(test_initializer)
    for step in range(int(test_steps)):
        if step % FLAGS.steps_per_print == 0:
            print('Test Accuracy', sess.run(accuracy, feed_dict={keep_prob: 1}), 'Step', step)
else:
    print('No Model Found')

  如果要進行新的 Inference 的話,可以替換下 test_x 即可。

結語

  文章內容參考自靜覓 » TensorFlow驗證碼識別

  代碼見:https://github.com/AIDeepLearning/CrackCaptcha

 

        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM