日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tensorflow实例3: 验证码图片的识别训练,每张图片有4个字母

發(fā)布時間:2024/9/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tensorflow实例3: 验证码图片的识别训练,每张图片有4个字母 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

學(xué)習(xí)目標(biāo)
目標(biāo)
說明驗證碼識別的原理
說明全連接層的輸出設(shè)置
說明輸出結(jié)果的損失、準(zhǔn)確率計算
說明驗證碼標(biāo)簽值的數(shù)字轉(zhuǎn)換
應(yīng)用tf.one_hot實現(xiàn)驗證碼目標(biāo)值的one_hot編碼處理
應(yīng)用
應(yīng)用神經(jīng)網(wǎng)絡(luò)識別驗證碼圖片
1、識別效果


2、驗證碼識別實戰(zhàn)

處理原始數(shù)據(jù)
方便特征值、目標(biāo)值讀取訓(xùn)練
設(shè)計網(wǎng)絡(luò)結(jié)構(gòu)
網(wǎng)絡(luò)的輸出處理
訓(xùn)練模型并預(yù)測
原理分析


1、目標(biāo)標(biāo)簽分析


考慮每個位置的可能性?“ABCDEFGHIJKLMNOPQRSTUVWXYZ”

第一個位置:26種可能性

第二個位置:26種可能性

第三個位置:26種可能性

第四個位置:26種可能性

如何比較輸出結(jié)果和真實值的正確性?可以對每個位置進行one_hot編碼

2、網(wǎng)絡(luò)輸出分析
按照這樣的順序,“ABCDEFGHIJKLMNOPQRSTUVWXYZ”

真實值:
第一個位置:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0]
第二個位置:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
第三個位置:[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0]
第四個位置:[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0]
1
2
3
4
5
那么每個驗證碼的目標(biāo)有[4, 26]這樣一個數(shù)組

3、如何衡量損失
我們考慮將目標(biāo)值拼接在一起,形成一個[104]長度的一階張量

真實值:
[0,0,0,0,...0,0,1,0,0][0,0,0,1,...0,0,0,0,0][0,0,0,0,...0,0,0,1,0][1,0,0,0,...0,0,0,0,0]
?? ??? ? ?26 ? ? ? ? ? ? ? ? ? ?26 ? ? ? ? ? ? ? ? ? 26 ? ? ? ? ? ? ? ? ? ? 26

預(yù)測概率值:
[0.001,0.01,,...,0.2,][0.001,0.01,,...,0.2,][0.001,0.01,,...,0.2,][0.02,0.01,,...,0.1,]
?? ??? ? ?26 ? ? ? ? ? ? ? ? ? ?26 ? ? ? ? ? ? ? ? ? 26 ? ? ? ? ? ? ? ? ? ? 26
1
2
3
4
5
6
7
這兩個104的一階張量進行交叉熵?fù)p失計算,得出損失大小。會提高四個位置的概率,使得4組中每組26個目標(biāo)值中為1的位置對應(yīng)的預(yù)測概率值越來越大,在預(yù)測的四組當(dāng)中概率值最大。這樣得出預(yù)測中每組的字母位置。

所有104個概率相加為1

4、準(zhǔn)確率如何計算

預(yù)測值和目標(biāo)值形狀要變?yōu)閇None, 4, 26],即可這樣去比較


在每個驗證碼的第三個維度去進行比較,4個標(biāo)簽的目標(biāo)值位置與預(yù)測概率位置是否相等,4個全相等,這個樣本才預(yù)測正確

維度位置比較:
? ? 0 ? 1 ? 2
[None, 4, 26]

tf.argmax(y_predict, 2)
1
2
3
4
5
3.1 處理原始圖片標(biāo)簽數(shù)據(jù)到TFRecords

3.1.1 驗證碼原始數(shù)據(jù)

3.1.2 處理分析

處理特征值
避免讀取的時候文件名字混亂,自己構(gòu)造的0~5999的驗證碼圖片文件名字列表

def get_captcha_image():
? ? """
? ? 獲取驗證碼圖片數(shù)據(jù)
? ? :param file_list: 路徑+文件名列表
? ? :return: image
? ? """
? ? # 構(gòu)造文件名
? ? filename = []

? ? for i in range(6000):
? ? ? ? string = str(i) + ".jpg"
? ? ? ? filename.append(string)

? ? # 構(gòu)造路徑+文件
? ? file_list = [os.path.join(FLAGS.captcha_dir, file) for file in filename]

? ? # 構(gòu)造文件隊列
? ? file_queue = tf.train.string_input_producer(file_list, shuffle=False)

? ? # 構(gòu)造閱讀器
? ? reader = tf.WholeFileReader()

? ? # 讀取圖片數(shù)據(jù)內(nèi)容
? ? key, value = reader.read(file_queue)

? ? # 解碼圖片數(shù)據(jù)
? ? image = tf.image.decode_jpeg(value)

? ? image.set_shape([20, 80, 3])

? ? # 批處理數(shù)據(jù) [6000, 20, 80, 3]
? ? image_batch = tf.train.batch([image], batch_size=6000, num_threads=1, capacity=6000)

? ? return image_batch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
目標(biāo)值處理
目標(biāo)值怎么處理,我們每個圖片的目標(biāo)值都是一個字符串。那么將其當(dāng)做一個個的字符單獨處理。一張驗證碼的圖片的目標(biāo)值由4個數(shù)字組成。建立這樣的對應(yīng)關(guān)系

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
0,1,2,................,24,25

最終:
"NZPP"----> [[13, 25, 15, 15]]
1
2
3
4
5
然后將所有的目標(biāo)值都變成四個數(shù)字,然后與對應(yīng)的特征值一起存入example當(dāng)中

[[13, 25, 15, 15], [22, 10, 7, 10], [22, 15, 18, 9], [16, 6, 13, 10], [1, 0, 8, 17], [0, 9, 24, 14].....]
1
代碼部分:

讀取label文件

def get_captcha_label():
? ? """
? ? 讀取驗證碼圖片標(biāo)簽數(shù)據(jù)
? ? :return: label
? ? """
? ? file_queue = tf.train.string_input_producer(["../data/Genpics/labels.csv"], shuffle=False)

? ? reader = tf.TextLineReader()

? ? key, value = reader.read(file_queue)

? ? records = [[1], ["None"]]

? ? number, label = tf.decode_csv(value, record_defaults=records)

? ? # [["NZPP"], ["WKHK"], ["ASDY"]]
? ? label_batch = tf.train.batch([label], batch_size=6000, num_threads=1, capacity=6000)

? ? return label_batch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
處理目標(biāo)值

# [b'NZPP' b'WKHK' b'WPSJ' ..., b'FVQJ' b'BQYA' b'BCHR']
label_str = sess.run(label)

print(label_str)

# 處理字符串標(biāo)簽到數(shù)字張量
label_batch = dealwithlabel(label_str)
1
2
3
4
5
6
7
轉(zhuǎn)換對應(yīng)的數(shù)字

def dealwithlabel(label_str):

? ? # 構(gòu)建字符索引 {0:'A', 1:'B'......}
? ? num_letter = dict(enumerate(list(FLAGS.letter)))

? ? # 鍵值對反轉(zhuǎn) {'A':0, 'B':1......}
? ? letter_num = dict(zip(num_letter.values(), num_letter.keys()))

? ? print(letter_num)

? ? # 構(gòu)建標(biāo)簽的列表
? ? array = []

? ? # 給標(biāo)簽數(shù)據(jù)進行處理[[b"NZPP"]......]
? ? for string in label_str:

? ? ? ? letter_list = []# [1,2,3,4]

? ? ? ? # 修改編碼,b'FVQJ'到字符串,并且循環(huán)找到每張驗證碼的字符對應(yīng)的數(shù)字標(biāo)記
? ? ? ? for letter in string.decode('utf-8'):
? ? ? ? ? ? letter_list.append(letter_num[letter])

? ? ? ? array.append(letter_list)

? ? # [[13, 25, 15, 15], [22, 10, 7, 10], [22, 15, 18, 9], [16, 6, 13, 10], [1, 0, 8, 17], [0, 9, 24, 14].....]
? ? print(array)

? ? # 將array轉(zhuǎn)換成tensor類型
? ? label = tf.constant(array)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
特征值、目標(biāo)值一一對應(yīng)構(gòu)造example并寫入文件
同一個圖片的特征值目標(biāo)值由于都是非0維數(shù)組,所以都以bytes存入

def write_to_tfrecords(image_batch, label_batch):
? ? """
? ? 將圖片內(nèi)容和標(biāo)簽寫入到tfrecords文件當(dāng)中
? ? :param image_batch: 特征值
? ? :param label_batch: 標(biāo)簽紙
? ? :return: None
? ? """
? ? # 轉(zhuǎn)換類型
? ? label_batch = tf.cast(label_batch, tf.uint8)

? ? print(label_batch)

? ? # 建立TFRecords 存儲器
? ? writer = tf.python_io.TFRecordWriter(FLAGS.tfrecords_dir)

? ? # 循環(huán)將每一個圖片上的數(shù)據(jù)構(gòu)造example協(xié)議塊,序列化后寫入
? ? for i in range(6000):
? ? ? ? # 取出第i個圖片數(shù)據(jù),轉(zhuǎn)換相應(yīng)類型,圖片的特征值要轉(zhuǎn)換成字符串形式
? ? ? ? image_string = image_batch[i].eval().tostring()

? ? ? ? # 標(biāo)簽值,轉(zhuǎn)換成整型
? ? ? ? label_string = label_batch[i].eval().tostring()

? ? ? ? # 構(gòu)造協(xié)議塊
? ? ? ? example = tf.train.Example(features=tf.train.Features(feature={
? ? ? ? ? ? "image": tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_string])),
? ? ? ? ? ? "label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label_string]))
? ? ? ? }))

? ? ? ? writer.write(example.SerializeToString())

? ? # 關(guān)閉文件
? ? writer.close()

? ? return None
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
3.2 讀取數(shù)據(jù)訓(xùn)練

3.2.1 讀取TFRecords文件數(shù)據(jù)

def read_captcha_tfrecords():
? ? """
? ? 從tfrecords讀取圖片特征值和目標(biāo)值
? ? :return: 特征值、目標(biāo)值
? ? """
? ? # 1、構(gòu)造文件隊列
? ? file_queue = tf.train.string_input_producer([FLAGS.captcha_tfrecords])

? ? # 2、構(gòu)造讀取器去讀取數(shù)據(jù),默認(rèn)一個樣本
? ? reader = tf.TFRecordReader()

? ? key, values = reader.read(file_queue)

? ? # 3、解析example協(xié)議
? ? feature = tf.parse_single_example(values, features={
? ? ? ? "image": tf.FixedLenFeature([], tf.string),
? ? ? ? "label": tf.FixedLenFeature([], tf.string),
? ? })

? ? # 4、對bytes類型的數(shù)據(jù)進行解碼
? ? image = tf.decode_raw(feature['image'], tf.uint8)

? ? label = tf.decode_raw(feature['label'], tf.uint8)

? ? print(image, label)

? ? # 固定每一個數(shù)據(jù)張量的形狀
? ? image_reshape = tf.reshape(image, [FLAGS.height, FLAGS.width, FLAGS.channel])

? ? label_reshape = tf.reshape(label, [FLAGS.label_num])

? ? print(image_reshape, label_reshape)

? ? # 處理數(shù)據(jù)的類型
? ? # 對特征值進行類型修改
? ? image_reshape = tf.cast(image_reshape, tf.float32)

? ? label_reshape = tf.cast(label_reshape, tf.int32)

? ? # 5、進行批處理
? ? # 意味著每批次訓(xùn)練的樣本數(shù)量
? ? image_batch, label_batch = tf.train.batch([image_reshape, label_reshape], batch_size=100, num_threads=1, capacity=100)

? ? print(image_batch, label_batch)

? ? return image_batch, label_batch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
3.2.2 標(biāo)簽數(shù)據(jù)處理成三維

def change_to_onehot(label_batch):
? ? """
? ? 處理圖片的四個目標(biāo)值到ont_hot編碼
? ? :param label_batch: [[13, 25, 15, 15], [22, 10, 7, 10], [22, 15, 18, 9]]
? ? :return: ont_hot
? ? """

? ? # [100, 4]---->[100, 4, 26]
? ? y_true = tf.one_hot(label_batch, depth=FLAGS.depth, on_value=1.0)

? ? return y_true
1
2
3
4
5
6
7
8
9
10
11
3.2.3 全連接層模型建立

每個樣本的目標(biāo)值4個,每個目標(biāo)值26中可能性,全連接層神經(jīng)元個數(shù)4*26個

def captcha_model(image_batch):
? ? """
? ? 定義驗證碼的神經(jīng)網(wǎng)絡(luò)模型,得出模型輸出
? ? :param image_batch: 模型的輸入數(shù)據(jù)
? ? :return: 模型輸出結(jié)果(預(yù)測結(jié)果)
? ? """

? ? # 直接使用一層 ?全連接層的神經(jīng)網(wǎng)絡(luò)進行預(yù)測
? ? # 確定全連接層的模型計算
? ? # 輸入:[100, 20, 80, 3] ? ? ? ? 輸出:[None, 104] ? 104 = 4個目標(biāo)值 * 26中可能性
? ? with tf.variable_scope("captcha_model"):

? ? ? ? # [100, 20 * 80 * 3]*[20*80*3, 104]+[104] = [None, 104]
? ? ? ? # 隨機初始化全連接層的權(quán)重和偏置
? ? ? ? w = weight_variables([20 * 80 * 3, 104])

? ? ? ? b = bias_variables([104])

? ? ? ? # 做出全連接層的形狀改變[100, 20, 80, 3] ----->[100, 20 * 80 * 3]
? ? ? ? image_reshape = tf.reshape(image_batch, [-1, FLAGS.height * FLAGS.width * FLAGS.channel])

? ? ? ? # 進行矩陣運算
? ? ? ? # y_predict ? [None, 104]
? ? ? ? y_predict = tf.matmul(image_reshape, w) + b

? ? return y_predict
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
3.2.4 計算交叉熵?fù)p失

每個圖片的104個預(yù)測概率與104個真實值之間進行交叉熵計算

# 3、softmax運算計算交叉熵?fù)p失
with tf.variable_scope("softmax_crossentropy"):
? ? # y_true:真實值 [100, 4, 26] ?one_hot---->[100, 4 * 26]
? ? # y_predict :全臉層的輸出[100, 104]
? ? # 返回每個樣本的損失組成的列表
? ? loss = tf.reduce_mean(
? ? ? ? tf.nn.softmax_cross_entropy_with_logits(labels=tf.reshape(y_true, [100, FLAGS.label_num * FLAGS.depth]),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? logits=y_predict)
1
2
3
4
5
6
7
8
3.2.5 得出準(zhǔn)確率

形狀:[100, 4, 26]的低三個維度進行比較最大值位置

# 5、得出每次訓(xùn)練的準(zhǔn)確率(通過真實值和預(yù)測值進行位置比較,每個樣本都比較)
with tf.variable_scope("accuracy"):
? ? # 準(zhǔn)確率計算需要三維數(shù)據(jù)對比
? ? # y_true:真實值 [100, 4, 26]
? ? # y_predict :全臉層的輸出[100, 104]--->[100, 4, 26]
? ? equal_list = tf.equal(
? ? tf.argmax(y_true, 2),
? ? tf.argmax(tf.reshape(y_predict, [100, FLAGS.label_num, FLAGS.depth]), 2)
? ? )

? ? accuracy = tf.reduce_mean(tf.cast(tf.reduce_all(equal_list, 1), tf.float32))
1
2
3
4
5
6
7
8
9
10
11
需要用到一個函數(shù)處理equal_list

```python
? ? x = tf.constant([[True, ?True], [False, False]])
? ? tf.reduce_all(x) ? ? # False
? ? tf.reduce_all(x, 0) ?# [False, False]
? ? tf.reduce_all(x, 1) ?# [True, False]
```
1
2
3
4
5
6
3.2.6 封裝連個參數(shù)工具函數(shù)

# 封裝兩個初始化參數(shù)的API,以變量Op定義
def weight_variables(shape):
? ? w = tf.Variable(tf.random_normal(shape=shape, mean=0.0, stddev=1.0))
? ? return w


def bias_variables(shape):
? ? b = tf.Variable(tf.random_normal(shape=shape, mean=0.0, stddev=1.0))
? ? return b
1
2
3
4
5
6
7
8
9
3.3 模型訓(xùn)練

def captcha_reco():
? ? """
? ? 四個目標(biāo)值的驗證碼圖片識別
? ? :return:
? ? """
? ? # 1、從tfrecords讀取圖片特征值和目標(biāo)值
? ? # image_batch [100, 20, 80, 3]
? ? # label_batch [100, 4] ?[[13, 25, 15, 15], [22, 10, 7, 10], [22, 15, 18, 9]]
? ? image_batch, label_batch = read_captcha_tfrecords()

? ? # 2、建立識別驗證碼的神經(jīng)網(wǎng)絡(luò)模型
? ? # y_predict-->[100, 104]
? ? y_predict = captcha_model(image_batch)

? ? # 對目標(biāo)值進行one_hot編碼處理
? ? # y_true是一個三維形狀[100, 4, 26]
? ? y_true = change_to_onehot(label_batch)

? ? # 3、softmax運算計算交叉熵?fù)p失
? ? with tf.variable_scope("softmax_crossentropy"):
? ? ? ? # y_true:真實值 [100, 4, 26] ?one_hot---->[100, 4 * 26]
? ? ? ? # y_predict :全臉層的輸出[100, 104]
? ? ? ? # 返回每個樣本的損失組成的列表
? ? ? ? loss = tf.reduce_mean(
? ? ? ? ? ? tf.nn.softmax_cross_entropy_with_logits(labels=tf.reshape(y_true, [100, FLAGS.label_num * FLAGS.depth]),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? logits=y_predict)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? )
? ? # 4、梯度下降損失優(yōu)化
? ? with tf.variable_scope("optimizer"):
? ? ? ? # 學(xué)習(xí)率
? ? ? ? train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

? ? # 5、得出每次訓(xùn)練的準(zhǔn)確率(通過真實值和預(yù)測值進行位置比較,每個樣本都比較)
? ? with tf.variable_scope("accuracy"):
? ? ? ? # 準(zhǔn)確率計算需要三維數(shù)據(jù)對比
? ? ? ? # y_true:真實值 [100, 4, 26]
? ? ? ? # y_predict :全臉層的輸出[100, 104]--->[100, 4, 26]
? ? ? ? equal_list = tf.equal(
? ? ? ? ? ? tf.argmax(y_true, 2),
? ? ? ? ? ? tf.argmax(tf.reshape(y_predict, [100, FLAGS.label_num, FLAGS.depth]), 2)
? ? ? ? )

? ? ? ? accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))

? ? # 初始化變量的op
? ? init_op = tf.global_variables_initializer()

? ? # 開啟會話運行
? ? with tf.Session() as sess:
? ? ? ? sess.run(init_op)

? ? ? ? # 創(chuàng)建線程去開啟讀取任務(wù)
? ? ? ? coord = tf.train.Coordinator()

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

? ? ? ? # sess.run([image_batch, label_batch])
? ? ? ? # 循環(huán)訓(xùn)練
? ? ? ? for i in range(1000):

? ? ? ? ? ? sess.run(train_op)

? ? ? ? ? ? print("第%d步的驗證碼訓(xùn)練準(zhǔn)確率為:%f" % (i,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?accuracy.eval()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?))

? ? ? ? # 回收線程
? ? ? ? coord.request_stop()

? ? ? ? coord.join(threads)

? ? return None
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
3.3 保存模型預(yù)測

if i % 100 == 0:

?? ?saver.save(sess, "./tmp/model/captcha_model")
1
2
3
完整代碼:
# -*- coding=utf-8 -*-
# tensorboard圖像 終端查看
# tensorboard --logdir="./temp/summary/"

import os
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 這是默認(rèn)的顯示等級,顯示所有信息 ?
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只顯示 warning 和 Error ??
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只顯示 Error

import tensorflow as tf


class CaptchaIdentification(object):
? ? """
? ? 驗證碼的讀取數(shù)據(jù)、網(wǎng)絡(luò)訓(xùn)練
? ? """

? ? def __init__(self):
? ? ? ? # 驗證碼圖片的屬性
? ? ? ? self.height = 20
? ? ? ? self.width = 80
? ? ? ? self.channel = 3
? ? ? ? # 每個驗證碼的目標(biāo)值個數(shù)(4個字符)
? ? ? ? self.label_num = 4
? ? ? ? # 每個目標(biāo)值對應(yīng)的屬性
? ? ? ? self.feature_num = 26

? ? ? ? # 權(quán)重和偏置
? ? ? ? self.weight = []
? ? ? ? self.bias = []

? ? ? ? # 每批次訓(xùn)練樣本個數(shù)
? ? ? ? self.train_batch = 100

? ? @staticmethod ?# 設(shè)置靜態(tài)方法
? ? def weight_variables(shape):
? ? ? ? w = tf.Variable(tf.random_normal(shape=shape, mean=0.0, stddev=0.1))
? ? ? ? return w

? ? @staticmethod ?# 設(shè)置靜態(tài)方法
? ? def bias_variables(shape):
? ? ? ? b = tf.Variable(tf.random_normal(shape=shape, mean=0.0, stddev=0.1))
? ? ? ? return b

? ? def read_tfrecords(self):
? ? ? ? """
? ? ? ? 讀取驗證碼特征值和目標(biāo)值數(shù)據(jù)
? ? ? ? :return:
? ? ? ? """
? ? ? ? # 1、構(gòu)造文件的隊列
? ? ? ? file_queue = tf.train.string_input_producer(["./tfrecords/captcha.tfrecords"])

? ? ? ? # 2、 tf.TFRecordReader 讀取TFRecorders數(shù)據(jù)
? ? ? ? reader = tf.TFRecordReader()

? ? ? ? # 單個樣本數(shù)據(jù)
? ? ? ? key, value = reader.read(file_queue)

? ? ? ? # 3、解析example協(xié)議
? ? ? ? feature = tf.parse_single_example(value, features={
? ? ? ? ? ? "image": tf.FixedLenFeature(shape=[], dtype=tf.string),
? ? ? ? ? ? "label": tf.FixedLenFeature(shape=[], dtype=tf.string),
? ? ? ? })

? ? ? ? # 4、解碼操作、數(shù)據(jù)類型、形狀
? ? ? ? image = tf.decode_raw(bytes=feature["image"], out_type=tf.uint8)
? ? ? ? label = tf.decode_raw(bytes=feature["label"], out_type=tf.uint8)

? ? ? ? # 確定類型和形狀
? ? ? ? # 圖片的形狀 [20, 80, 3]
? ? ? ? # 目標(biāo)值 [4]
? ? ? ? image_reshape = tf.reshape(image, shape=[self.height, self.width, self.channel])
? ? ? ? label_reshape = tf.reshape(label, shape=[self.label_num])

? ? ? ? # 類型轉(zhuǎn)換
? ? ? ? image_type = tf.cast(image_reshape, dtype=tf.float32)
? ? ? ? label_type = tf.cast(label_reshape, dtype=tf.int32)
? ? ? ? # print(image_type, label_type)

? ? ? ? # 5、批處理
? ? ? ? # 提供每批次多少樣本去進行訓(xùn)練
? ? ? ? image_batch, label_batch = tf.train.batch([image_type, label_type],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? batch_size=self.train_batch,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? num_threads=1,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? capacity=self.train_batch)

? ? ? ? print(image_batch, label_batch)
? ? ? ? return image_batch, label_batch

? ? def captcha_model(self, image_batch, label_batch):
? ? ? ? """
? ? ? ? 建立全連接層神經(jīng)網(wǎng)絡(luò)
? ? ? ? :param image_batch: 驗證碼圖片特征值
? ? ? ? :param label_batch: 驗證碼圖片的目標(biāo)值
? ? ? ? :return: 預(yù)測結(jié)果
? ? ? ? """
? ? ? ? # 全連接層
? ? ? ? # [self.train_batch, self.height, self.width, self.channel] --> [self.train_batch, self.height * self.width * self.channel]
? ? ? ? # 即:[100, 20, 80, 3] ?--> [100, 20 * 80 * 3]
? ? ? ? # [self.train_batch, self.height * self.width * self.channel] * [self.height * self.width * self.channel, self.label_num * self.feature_num] + [self.label_num * self.feature_num] = [None, self.label_num * self.feature_num]
? ? ? ? # 即:[100, 20 * 80 * 3] * [20 * 80 * 3, 104] + [104] = [None, 104] ? 104= 4*26
? ? ? ? with tf.variable_scope("captcha_fc_model"):
? ? ? ? ? ? # 初始化權(quán)重和偏置參數(shù)
? ? ? ? ? ? self.weight = self.weight_variables(
? ? ? ? ? ? ? ? shape=[self.height * self.width * self.channel, self.label_num * self.feature_num])
? ? ? ? ? ? self.bias = self.bias_variables(shape=[self.label_num * self.feature_num])

? ? ? ? ? ? # 4維 --> 2維做矩陣運算
? ? ? ? ? ? x_reshape = tf.reshape(tensor=image_batch,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?shape=[self.train_batch, self.height * self.width * self.channel])

? ? ? ? ? ? # 預(yù)測結(jié)果的形狀 [self.train_batch, self.label*self.feature_num]
? ? ? ? ? ? y_predict = tf.matmul(x_reshape, self.weight) + self.bias

? ? ? ? return y_predict, self.weight, self.bias

? ? def turn_to_onehot(self, label_batch):
? ? ? ? """
? ? ? ? 目標(biāo)值轉(zhuǎn)換成one_hot編碼
? ? ? ? :param label_batch: 目標(biāo)值 [None, 4]
? ? ? ? :return:
? ? ? ? """
? ? ? ? with tf.variable_scope("one_hot"):
? ? ? ? ? ? # [None, self.label_num] --> [None, self.label_num, self.feature_num]
? ? ? ? ? ? # 即:[None, 4] --> [None, 4, 26]
? ? ? ? ? ? y_true = tf.one_hot(indices=label_batch,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? depth=self.feature_num,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? on_value=1.0)
? ? ? ? ? ? return y_true

? ? def loss(self, y_true, y_predict):
? ? ? ? """
? ? ? ? 建立驗證碼4個目標(biāo)值
? ? ? ? :param y_true:
? ? ? ? :param y_predict:
? ? ? ? :return:
? ? ? ? """
? ? ? ? with tf.variable_scope("loss"):
? ? ? ? ? ? # 先進行網(wǎng)絡(luò)輸出值的概率計算softmax,再進行交叉熵?fù)p失計算
? ? ? ? ? ? # y_true:[None, 4, 26] -->[None, 104]
? ? ? ? ? ? # y_predict:[None, 104]
? ? ? ? ? ? y_reshape = tf.reshape(tensor=y_true, shape=[self.train_batch, self.label_num * self.feature_num])
? ? ? ? ? ? all_loss = tf.nn.softmax_cross_entropy_with_logits(labels=y_reshape,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?logits=y_predict,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?name="compute_loss")

? ? ? ? ? ? # 求出平均損失
? ? ? ? ? ? loss = tf.reduce_mean(all_loss)

? ? ? ? return loss

? ? def sgd(self, loss):
? ? ? ? """
? ? ? ? 梯度下降優(yōu)化損失
? ? ? ? :param loss:
? ? ? ? :return:
? ? ? ? """
? ? ? ? with tf.variable_scope("sgd"):
? ? ? ? ? ? train_op = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss=loss)

? ? ? ? return train_op

? ? def accuracy(self, y_true, y_predict):
? ? ? ? """
? ? ? ? 就按準(zhǔn)確率
? ? ? ? :param y_true: 真實值
? ? ? ? :param y_predict: 預(yù)測值
? ? ? ? :return: accuracy
? ? ? ? """
? ? ? ? with tf.variable_scope("accuracy"):
? ? ? ? ? ? # y_true: [None, self.label_num, self.feature_num] ?即:[None, 4, 26]
? ? ? ? ? ? # y_predict: [None, self.label_num * self.feature_num] 即:[None, 104]
? ? ? ? ? ? y_predict_reshape = tf.reshape(tensor=y_predict, shape=[self.train_batch, self.label_num, self.feature_num])

? ? ? ? ? ? # 先對最大值的位置去求解
? ? ? ? ? ? t1 = tf.argmax(y_true, 2) ?# 這里 2 是矩陣的層數(shù)減1。 [None, 104]的層數(shù)為2
? ? ? ? ? ? t2 = tf.argmax(y_predict_reshape, 2)
? ? ? ? ? ? equal_list = tf.equal(t1, t2) ?# 返回的是bool值

? ? ? ? ? ? # 需要對每個樣本進行判斷
? ? ? ? ? ? # x = tf.constant([[True, True], [False, False]])
? ? ? ? ? ? # tf.reduce_all(x, 1) ?# [True, False]
? ? ? ? ? ? accuracy = tf.reduce_mean(tf.cast(tf.reduce_all(equal_list, 1), dtype=tf.float32)) ?# 這里 1 是矩陣的層數(shù)減2。 [None, 104]的層數(shù)為2

? ? ? ? return accuracy

? ? def train(self):
? ? ? ? """
? ? ? ? 模型訓(xùn)練邏輯
? ? ? ? :return:
? ? ? ? """
? ? ? ? # 1、通過接口獲取特征值和目標(biāo)值
? ? ? ? # image_batch: [100, 20, 80, 3]
? ? ? ? # label_batch: [100, 4] ?例:[[13, 25, 15, 15], [22, 10, 7, 10], [22, 15, 18, 9], ...]
? ? ? ? image_batch, label_batch = self.read_tfrecords()

? ? ? ? # 2、建立驗證碼識別的模型
? ? ? ? # 全連接層神經(jīng)網(wǎng)絡(luò)
? ? ? ? # y_predict:[self.train_batch, self.label*self.feature_num] 即:[100, 104]
? ? ? ? y_predict, self.weight, self.bias = self.captcha_model(image_batch, label_batch)

? ? ? ? # 轉(zhuǎn)換label_batch 到one_hot編碼
? ? ? ? # y_true:[None, 4, 26]
? ? ? ? y_true = self.turn_to_onehot(label_batch)

? ? ? ? # 3、利用真實值和目標(biāo)值建立損失
? ? ? ? loss = self.loss(y_true, y_predict)

? ? ? ? # 4、對損失進行梯度下降優(yōu)化
? ? ? ? train_op = self.sgd(loss)

? ? ? ? # 5、計算準(zhǔn)確率
? ? ? ? accuracy = self.accuracy(y_true, y_predict)

? ? ? ? # 6、tensorflowboard展示的數(shù)據(jù)
? ? ? ? # 1)收集要在tensorflowboard觀察的張量值
? ? ? ? # 數(shù)值型 ?--> scalar 準(zhǔn)確率, 損失值
? ? ? ? tf.summary.scalar("loss", loss)
? ? ? ? tf.summary.scalar("accuracy", accuracy)

? ? ? ? # 維度高的張量值
? ? ? ? tf.summary.histogram("w", self.weight)
? ? ? ? tf.summary.histogram("b", self.bias)

? ? ? ? # 2)合并變量
? ? ? ? merged = tf.summary.merge_all()

? ? ? ? # 7、創(chuàng)建保存模型的OP
? ? ? ? saver = tf.train.Saver()

? ? ? ? # 會話訓(xùn)練
? ? ? ? with tf.Session() as sess:
? ? ? ? ? ? # 會話初始化
? ? ? ? ? ? sess.run(tf.global_variables_initializer())

? ? ? ? ? ? # 創(chuàng)建tensorboard的events文件
? ? ? ? ? ? filte_writer = tf.summary.FileWriter("./temp/summary/", graph=sess.graph)

? ? ? ? ? ? # 生成線程的管理
? ? ? ? ? ? coord = tf.train.Coordinator()

? ? ? ? ? ? # 指定開啟子線程去讀取數(shù)據(jù)
? ? ? ? ? ? threads = tf.train.start_queue_runners(sess=sess, coord=coord)

? ? ? ? ? ? # 循環(huán)訓(xùn)練打印結(jié)果
? ? ? ? ? ? for i in range(1000):
? ? ? ? ? ? ? ? _, loss_run, accuracy_run, summary = sess.run([train_op, loss, accuracy, merged])

? ? ? ? ? ? ? ? print("第 {:d} 次訓(xùn)練的損失為:{:.6f},準(zhǔn)確率為:{:.6f}".format(i, loss_run, accuracy_run))

? ? ? ? ? ? ? ? # 3) 寫入運行的結(jié)果到文件當(dāng)中
? ? ? ? ? ? ? ? filte_writer.add_summary(summary, i)

? ? ? ? ? ? # 回收線程
? ? ? ? ? ? coord.request_stop()
? ? ? ? ? ? coord.join(threads=threads)

? ? ? ? return None


if __name__ == '__main__':
? ? pic_indentify = CaptchaIdentification()
? ? pic_indentify.train()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
4、拓展
如果驗證碼的標(biāo)簽值不止是大寫字母,比如還包含小寫字母和數(shù)字,該怎么處理?
如果圖片的目標(biāo)值不止4個,可能5,6個,該怎么處理?
注:主要是在網(wǎng)絡(luò)輸出的結(jié)果以及數(shù)據(jù)對應(yīng)數(shù)字進行分析
————————————————
版權(quán)聲明:本文為CSDN博主「Kungs8」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/yanpenggong/article/details/84680149

總結(jié)

以上是生活随笔為你收集整理的Tensorflow实例3: 验证码图片的识别训练,每张图片有4个字母的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日本午夜电影网站 | 美女网站av | yjizz视频网 国产乱人对白 | av电影中文字幕 | 五月婷婷丁香激情 | 日韩99 | 国产良妇出轨视频在线观看 | 99re在线播放 | 国产吃瓜在线 | 成人免费视频网站在线看 | 一区二区成人在线 | 精品国产xxx| 久久久久国产精品一区二区 | 国产美女特级嫩嫩嫩bbb | 免费av电影网站 | 二色av| 视频在线亚洲 | 欧美第五页 | 日本久久综合 | 日本免费网站在线观看 | www.一起操 | 久久综合九色综合欧美狠狠 | 91成人一区| 中文一区二区在线观看 | 久久怡红院 | 日本在线观看一区 | 久福利| 台湾佬美性中文娱乐网 | 成人尤物| 国产美女明星三级做爰 | 天天插天天射 | 欧美成人国产精品高潮 | 成人五区| 超能一家人电影免费喜剧在线观看 | 香蕉视频网页版 | 亚洲成人看片 | 无码国产色欲xxxxx视频 | 午夜免费在线观看 | 99国产免费 | 91人人澡人人爽人人精品 | 一区二区片 | 成人av国产 | 欧美一区2区 | 美女扒开腿免费视频 | 久久888| 日韩欧美中文在线 | 亚洲熟区 | 亚色影库| 依依成人在线 | 久久久久国产精品无码免费看 | 国产区视频在线 | 黄色短视频在线播放 | 久久久网址 | 免费成人毛片 | 一区二区在线观看免费 | 精品久久久久久久久久久国产字幕 | 欧美a久久| 日b免费视频| 亚洲成色网| 福利视频免费 | 日韩影院一区 | 粗大黑人巨茎大战欧美成人免费看 | 四虎影裤 | 亚洲欧洲国产视频 | 国产伦理一区二区三区 | 337p色噜噜| av老司机福利 | 91在线观看喷潮 | 免费二区 | 青青在线免费观看 | 人妻无码中文字幕免费视频蜜桃 | 男男做性免费视频网 | 日韩欧美理论 | 欧美日韩亚洲系列 | 日本成人中文字幕 | 欧美激情在线免费观看 | 亚洲av人人夜夜澡人人 | 国产成人精品网 | 少妇高潮一区二区三区 | 天天操天天操天天操天天操天天操 | 日韩一级色 | 全国男人天堂网 | 久久国产加勒比精品无码 | 福利久久久 | 亚洲午夜电影网 | 日韩高清片 | 色窝在线 | 99热热热| 亚洲香蕉久久 | 国产高清一级 | 欧美一级欧美三级 | 香蕉视频毛片 | 中文字幕色网 | 日韩精品一区二区三区在线观看 | 成人国产精品免费 | 蜜桃91麻豆精品一二三区 | 欧美性受黑人性爽 | 免费黄色三级网站 | av免费网址在线观看 |