偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

教你使用TensorFlow2對識別驗證碼

開發(fā) 前端
驗證碼是根據(jù)隨機字符生成一幅圖片,然后在圖片中加入干擾象素,用戶必須手動填入,防止有人利用機器人自動批量注冊、灌水、發(fā)垃圾廣告等等 。

[[405770]]

 驗證碼是根據(jù)隨機字符生成一幅圖片,然后在圖片中加入干擾象素,用戶必須手動填入,防止有人利用機器人自動批量注冊、灌水、發(fā)垃圾廣告等等 。

數(shù)據(jù)集來源:https://www.kaggle.com/fournierp/captcha-version-2-images

圖片是5個字母的單詞,可以包含數(shù)字。這些圖像應用了噪聲(模糊和一條線)。它們是200 x 50 PNG。我們的任務是嘗試制作光學字符識別算法的模型。

在數(shù)據(jù)集中存在的驗證碼png圖片,對應的標簽就是圖片的名字。

  1. import os 
  2. import numpy as np 
  3. import pandas as pd 
  4. import cv2 
  5. import matplotlib.pyplot as plt 
  6. import seaborn as sns 
  7. # imgaug 圖片數(shù)據(jù)增強 
  8. import imgaug.augmenters as iaa 
  9. import tensorflow as tf 
  10. # Conv2D MaxPooling2D Dropout Flatten Dense BN  GAP 
  11. from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Layer, BatchNormalization, GlobalAveragePooling2D  
  12. from tensorflow.keras.optimizers import Adam 
  13. from tensorflow.keras import Model, Input  
  14. from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau 
  15. # 圖片處理器 
  16. from tensorflow.keras.preprocessing.image import ImageDataGenerator 
  17. import plotly.express as px 
  18. import plotly.graph_objects as go 
  19. import plotly.offline as pyo 
  20. pyo.init_notebook_mode() 

對數(shù)據(jù)進行一個簡單的分析,統(tǒng)計圖像中大約出現(xiàn)了什么樣的符號。

  1. # 數(shù)據(jù)路徑 
  2. DIR = '../input/captcha-version-2-images/samples/samples' 
  3. # 存儲驗證碼的標簽 
  4. captcha_list = [] 
  5. characters = {} 
  6. for captcha in os.listdir(DIR): 
  7.     captcha_list.append(captcha) 
  8.     # 每張驗證碼的captcha_code 
  9.     captcha_code = captcha.split(".")[0] 
  10.     for i in captcha_code: 
  11.         # 遍歷captcha_code  
  12.         characters[i] = characters.get(i, 0) +1 
  13. symbols = list(characters.keys()) 
  14. len_symbols = len(symbols) 
  15. print(f'圖像中只使用了{len_symbols}符號'
  16.  
  17. plt.bar(*zip(*characters.items())) 
  18. plt.title('Frequency of symbols'
  19. plt.show() 

 

如何提取圖像的數(shù)據(jù)建立X,y??

  1. # 如何提取圖像 建立 model  X 的shape  1070 * 50 * 200 * 1  
  2. # y的shape 5 * 1070 * 19 
  3.   
  4. for i, captcha in enumerate(captcha_list): 
  5.     captcha_code = captcha.split('.')[0] 
  6.     # cv2.IMREAD_GRAYSCALE 灰度圖 
  7.     captcha_cv2 = cv2.imread(os.path.join(DIR, captcha),cv2.IMREAD_GRAYSCALE) 
  8.     # 縮放 
  9.     captcha_cv2 = captcha_cv2 / 255.0 
  10.     # print(captcha_cv2.shape) (50, 200)  
  11.     # 將captcha_cv2的(50, 200) 切換成(50, 200, 1) 
  12.     captcha_cv2 = np.reshape(captcha_cv2, img_shape) 
  13.     # (5,19) 
  14.     targs = np.zeros((len_captcha, len_symbols)) 
  15.      
  16.     for a, b in enumerate(captcha_code): 
  17.         targs[a, symbols.index(b)] = 1 
  18.     X[i] = captcha_cv2 
  19.     y[:, i] = targs 
  20.  
  21. print("shape of X:", X.shape) 
  22. print("shape of y:", y.shape) 

輸出如下

  1. print("shape of X:", X.shape) 
  2. print("shape of y:", y.shape) 

通過Numpy中random 隨機選擇數(shù)據(jù),劃分訓練集和測試集

  1. # 生成隨機數(shù) 
  2. from numpy.random import default_rng 
  3.  
  4. rng = default_rng(seed=1) 
  5. test_numbers = rng.choice(1070, size=int(1070*0.3), replace=False
  6. X_test = X[test_numbers] 
  7. X_full = np.delete(X, test_numbers,0) 
  8. y_test = y[:,test_numbers] 
  9. y_full = np.delete(y, test_numbers,1) 
  10.  
  11. val_numbers = rng.choice(int(1070*0.7), size=int(1070*0.3), replace=False
  12.  
  13. X_val = X_full[val_numbers] 
  14. X_train = np.delete(X_full, val_numbers,0) 
  15. y_val = y_full[:,val_numbers] 
  16. y_train = np.delete(y_full, val_numbers,1) 

在此驗證碼數(shù)據(jù)中,容易出現(xiàn)過擬合的現(xiàn)象,你可能會想到添加更多的新數(shù)據(jù)、 添加正則項等, 但這里使用數(shù)據(jù)增強的方法,特別是對于機器視覺的任務,數(shù)據(jù)增強技術(shù)尤為重要。

常用的數(shù)據(jù)增強操作:imgaug庫。imgaug是提供了各種圖像增強操作的python庫 https://github.com/aleju/imgaug。

imgaug幾乎包含了所有主流的數(shù)據(jù)增強的圖像處理操作, 增強方法詳見github

  1. # Sequential(C, R)  尺寸增加了5倍, 
  2. # 選取一系列子增強器C作用于每張圖片的位置,第二個參數(shù)表示是否對每個batch的圖片應用不同順序的Augmenter list     # rotate=(-8, 8)  旋轉(zhuǎn) 
  3. # iaa.CropAndPad  截取(crop)或者填充(pad),填充時,被填充區(qū)域為黑色。 
  4. # px: 想要crop(negative values)的或者pad(positive values)的像素點。 
  5. # (topright, bottom, left
  6. # 當pad_mode=constant的時候選擇填充的值 
  7. aug =iaa.Sequential([iaa.CropAndPad( 
  8.     px=((0, 10), (0, 35), (0, 10), (0, 35)), 
  9.     pad_mode=['edge'], 
  10.     pad_cval=1 
  11. ),iaa.Rotate(rotate=(-8,8))]) 
  12.  
  13. X_aug_train = None 
  14. y_aug_train = y_train 
  15. for i in range(40): 
  16.     X_aug = aug(images = X_train) 
  17.     if X_aug_train is not None: 
  18.         X_aug_train = np.concatenate([X_aug_train, X_aug], axis = 0) 
  19.         y_aug_train = np.concatenate([y_aug_train, y_train], axis = 1) 
  20.     else
  21.         X_aug_train = X_aug 

讓我們看看一些數(shù)據(jù)增強的訓練圖像。

  1. fig, ax = plt.subplots(nrows=2, ncols =5, figsize = (16,16)) 
  2. for i in range(10): 
  3.     index = np.random.randint(X_aug_train.shape[0]) 
  4.     ax[i//5][i%5].imshow(X_aug_train[index],cmap='gray'

 

這次使用函數(shù)式API創(chuàng)建模型,函數(shù)式API是創(chuàng)建模型的另一種方式,它具有更多的靈活性,包括創(chuàng)建更為復雜的模型。

需要定義inputs和outputs

  1. #函數(shù)式API模型創(chuàng)建 
  2. captcha = Input(shape=(50,200,channels)) 
  3. x = Conv2D(32, (5,5),padding='valid',activation='relu')(captcha) 
  4. x = MaxPooling2D((2,2),padding='same')(x) 
  5. x = Conv2D(64, (3,3),padding='same',activation='relu')(x) 
  6. x = MaxPooling2D((2,2),padding='same')(x) 
  7. x = Conv2D(128, (3,3),padding='same',activation='relu')(x) 
  8. maxpool = MaxPooling2D((2,2),padding='same')(x) 
  9. outputs = [] 
  10. for i in range(5): 
  11.     x = Conv2D(256, (3,3),padding='same',activation='relu')(maxpool) 
  12.     x = MaxPooling2D((2,2),padding='same')(x) 
  13.     x = Flatten()(x) 
  14.     x = Dropout(0.5)(x) 
  15.     x = BatchNormalization()(x) 
  16.     x = Dense(64, activation='relu')(x) 
  17.     x = Dropout(0.5)(x) 
  18.     x = BatchNormalization()(x) 
  19.     x = Dense(len_symbols , activation='softmax' , name=f'char_{i+1}')(x) 
  20.     outputs.append(x) 
  21.      
  22. model = Model(inputs = captcha , outputs=outputs) 
  23. # ReduceLROnPlateau更新學習率 
  24. reduce_lr = ReduceLROnPlateau(patience =3, factor = 0.5,verbose = 1) 
  25. model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.0005), metrics=["accuracy"]) 
  26. # EarlyStopping用于提前停止訓練的callbacks。具體地,可以達到當訓練集上的loss不在減小 
  27. earlystopping = EarlyStopping(monitor ="val_loss",   
  28.                              mode ="min", patience = 10, 
  29.                               min_delta = 1e-4, 
  30.                              restore_best_weights = True)  
  31.  
  32. history = model.fit(X_train, [y_train[i] for i in range(5)], batch_size=32, epochs=30, verbose=1, validation_data = (X_val, [y_val[i] for i in range(5)]), callbacks =[earlystopping,reduce_lr]) 

 

下面對model進行一個測試和評估。

  1. score = model.evaluate(X_test,[y_test[0], y_test[1], y_test[2], y_test[3], y_test[4]],verbose=1) 
  2. metrics = ['loss','char_1_loss''char_2_loss''char_3_loss''char_4_loss''char_5_loss''char_1_acc''char_2_acc''char_3_acc''char_4_acc''char_5_acc'
  3.  
  4. for i,j in zip(metrics, score): 
  5.     print(f'{i}: {j}'

具體輸出如下:

  1. 11/11 [==============================] - 0s 11ms/step - loss: 0.7246 - char_1_loss: 0.0682 - char_2_loss: 0.1066 - char_3_loss: 0.2730 - char_4_loss: 0.2636 - char_5_loss: 0.0132 - char_1_accuracy: 0.9844 - char_2_accuracy: 0.9657 - char_3_accuracy: 0.9408 - char_4_accuracy: 0.9626 - char_5_accuracy: 0.9938 
  2. loss: 0.7246273756027222 
  3. char_1_loss: 0.06818050146102905 
  4. char_2_loss: 0.10664034634828568 
  5. char_3_loss: 0.27299806475639343 
  6. char_4_loss: 0.26359987258911133 
  7. char_5_loss: 0.013208594173192978 
  8. char_1_acc: 0.9844236969947815 
  9. char_2_acc: 0.9657320976257324 
  10. char_3_acc: 0.940809965133667 
  11. char_4_acc: 0.9626168012619019 
  12. char_5_acc: 0.9937694668769836 

字母1到字母5的精確值都大于

繪制loss和score

  1. metrics_df = pd.DataFrame(history.history) 
  2.  
  3. columns = [col for col in metrics_df.columns if 'loss' in col and len(col)>8] 
  4.  
  5. fig = px.line(metrics_df, y = columns) 
  6. fig.show() 

  1. plt.figure(figsize=(15,8)) 
  2. plt.plot(history.history['loss']) 
  3. plt.plot(history.history['val_loss']) 
  4. plt.title('model loss'
  5. plt.ylabel('loss'
  6. plt.xlabel('epoch'
  7. plt.legend(['train''val'], loc='upper right',prop={'size': 10}) 
  8. plt.show() 

 

  1. # 預測數(shù)據(jù) 
  2. def predict(captcha): 
  3.     captcha = np.reshape(captcha , (1, 50,200,channels)) 
  4.     result = model.predict(captcha) 
  5.     result = np.reshape(result ,(5,len_symbols)) 
  6.     # 取出最大預測中的輸出 
  7.     label = ''.join([symbols[np.argmax(i)] for i in result]) 
  8.     return label 
  9.      
  10. predict(X_test[2]) 
  11. # 25277 

下面預測所有的數(shù)據(jù)

  1. actual_pred = [] 
  2.  
  3. for i in range(X_test.shape[0]): 
  4.     actual = ''.join([symbols[i] for i in (np.argmax(y_test[:, i],axis=1))]) 
  5.     pred =  predict(X_test[i]) 
  6.     actual_pred.append((actual, pred)) 
  7. print(actal_pred[:10]) 

輸出如下:

  1. [('n4b4m''n4b4m'), ('42nxy''42nxy'), ('25257''25277'), ('cewnm''cewnm'), ('w46ep''w46ep'), ('cdcb3''edcb3'), ('8gf7n''8gf7n'), ('nny5e''nny5e'), ('gm2c2''gm2c2'), ('g7fmc''g7fmc')] 
  1. sameCount = 0 
  2. diffCount = 0 
  3. letterDiff = {i:0 for i in range(5)} 
  4. incorrectness = {i:0 for i in range(1,6)} 
  5. for real, pred in actual_pred: 
  6.     # 預測和輸出相同 
  7.     if real == pred: 
  8.         sameCount += 1 
  9.     else
  10.         # 失敗 
  11.         diffCount += 1 
  12.         # 遍歷 
  13.         incorrectnessPoint = 0 
  14.         for i in range(5): 
  15.             if real[i] != pred[i]: 
  16.                 letterDiff[i] += 1 
  17.                 incorrectnessPoint += 1 
  18.         incorrectness[incorrectnessPoint] += 1 
  19.  
  20.  
  21. x = ['True predicted''False predicted'
  22. y = [sameCount, diffCount] 
  23.  
  24. fig = go.Figure(data=[go.Bar(x = x, y = y)]) 
  25. fig.show() 

在預測數(shù)據(jù)中,一共有287個數(shù)據(jù)預測正確。

在這里,我們可以看到出現(xiàn)錯誤到底是哪一個index。

  1. x1 = ["Character " + str(x) for x in range(1, 6)] 
  2.      
  3. fig = go.Figure(data=[go.Bar(x = x1, y = list(letterDiff.values()))]) 
  4. fig.show() 

  

為了計算每個單詞的錯誤數(shù),繪制相關(guān)的條形圖。

  1. x2 = [str(x) + " incorrect" for x in incorrectness.keys()] 
  2. y2 = list(incorrectness.values()) 
  3.  
  4. fig = go.Figure(data=[go.Bar(x = x2, y = y2)]) 
  5. fig.show() 

 

下面繪制錯誤的驗證碼圖像,并標準正確和錯誤的區(qū)別。

  1. fig, ax = plt.subplots(nrows = 8, ncols=4,figsize = (16,20)) 
  2. count = 0 
  3. for i, (actual , pred) in enumerate(actual_pred): 
  4.     if actual != pred: 
  5.         img = X_test[i] 
  6.         try: 
  7.             ax[count//4][count%4].imshow(img, cmap = 'gray'
  8.             ax[count//4][count%4].title.set_text(pred + ' - ' + actual) 
  9.             count += 1 
  10.         except
  11.             pass 

 

責任編輯:姜華 來源: Python之王
相關(guān)推薦

2021-06-15 06:39:43

深度學習TensorFlowKeras API

2021-06-11 05:37:20

TensorFlow2深度學習Keras API

2013-06-19 10:19:59

2020-12-29 05:33:03

Serverless驗證碼架構(gòu)

2021-06-10 18:24:59

反爬蟲驗證碼爬蟲

2023-10-27 08:53:13

Python驗證碼圖片識別

2024-01-29 08:32:10

Python驗證碼識別

2021-07-22 10:25:07

JS驗證碼前端

2014-04-24 10:09:05

驗證碼C#

2020-11-16 07:28:53

驗證碼

2009-02-09 14:17:36

2009-08-11 14:05:28

JSP驗證碼

2015-09-21 15:31:05

php實現(xiàn)驗證碼

2017-12-21 07:38:19

2021-01-19 10:29:34

短信驗證碼密碼

2022-02-11 07:10:15

驗證碼

2015-03-23 17:58:04

驗證碼倒計時并行

2021-09-09 08:55:50

Python項目驗證碼

2022-05-11 07:41:31

Python驗證碼
點贊
收藏

51CTO技術(shù)棧公眾號