- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章利用Tensorflow构建和训练自己的CNN来做简单的验证码识别方式由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
Tensorflow是目前最流行的深度学习框架,我们可以用它来搭建自己的卷积神经网络并训练自己的分类器,本文介绍怎样使用Tensorflow构建自己的CNN,怎样训练用于简单的验证码识别的分类器。本文假设你已经安装好了Tensorflow,了解过CNN的一些知识.
下面将分步介绍怎样获得训练数据,怎样使用tensorflow构建卷积神经网络,怎样训练,以及怎样测试训练出来的分类器 。
1. 准备训练样本 。
使用Python的库captcha来生成我们需要的训练样本,代码如下:
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
|
import
sys
import
os
import
shutil
import
random
import
time
#captcha是用于生成验证码图片的库,可以 pip install captcha 来安装它
from
captcha.image
import
ImageCaptcha
#用于生成验证码的字符集
CHAR_SET
=
[
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
]
#字符集的长度
CHAR_SET_LEN
=
10
#验证码的长度,每个验证码由4个数字组成
CAPTCHA_LEN
=
4
#验证码图片的存放路径
CAPTCHA_IMAGE_PATH
=
'E:/Tensorflow/captcha/images/'
#用于模型测试的验证码图片的存放路径,它里面的验证码图片作为测试集
TEST_IMAGE_PATH
=
'E:/Tensorflow/captcha/test/'
#用于模型测试的验证码图片的个数,从生成的验证码图片中取出来放入测试集中
TEST_IMAGE_NUMBER
=
50
#生成验证码图片,4位的十进制数字可以有10000种验证码
def
generate_captcha_image(charSet
=
CHAR_SET, charSetLen
=
CHAR_SET_LEN, captchaImgPath
=
CAPTCHA_IMAGE_PATH):
k
=
0
total
=
1
for
i
in
range
(CAPTCHA_LEN):
total
*
=
charSetLen
for
i
in
range
(charSetLen):
for
j
in
range
(charSetLen):
for
m
in
range
(charSetLen):
for
n
in
range
(charSetLen):
captcha_text
=
charSet[i]
+
charSet[j]
+
charSet[m]
+
charSet[n]
image
=
ImageCaptcha()
image.write(captcha_text, captchaImgPath
+
captcha_text
+
'.jpg'
)
k
+
=
1
sys.stdout.write(
"\rCreating %d/%d"
%
(k, total))
sys.stdout.flush()
#从验证码的图片集中取出一部分作为测试集,这些图片不参加训练,只用于模型的测试
def
prepare_test_set():
fileNameList
=
[]
for
filePath
in
os.listdir(CAPTCHA_IMAGE_PATH):
captcha_name
=
filePath.split(
'/'
)[
-
1
]
fileNameList.append(captcha_name)
random.seed(time.time())
random.shuffle(fileNameList)
for
i
in
range
(TEST_IMAGE_NUMBER):
name
=
fileNameList[i]
shutil.move(CAPTCHA_IMAGE_PATH
+
name, TEST_IMAGE_PATH
+
name)
if
__name__
=
=
'__main__'
:
generate_captcha_image(CHAR_SET, CHAR_SET_LEN, CAPTCHA_IMAGE_PATH)
prepare_test_set()
sys.stdout.write(
"\nFinished"
)
sys.stdout.flush()
|
运行上面的代码,可以生成验证码图片, 。
生成的验证码图片如下图所示:
2. 构建CNN,训练分类器 。
代码如下:
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
|
import
tensorflow as tf
import
numpy as np
from
PIL
import
Image
import
os
import
random
import
time
#验证码图片的存放路径
CAPTCHA_IMAGE_PATH
=
'E:/Tensorflow/captcha/images/'
#验证码图片的宽度
CAPTCHA_IMAGE_WIDHT
=
160
#验证码图片的高度
CAPTCHA_IMAGE_HEIGHT
=
60
CHAR_SET_LEN
=
10
CAPTCHA_LEN
=
4
#60%的验证码图片放入训练集中
TRAIN_IMAGE_PERCENT
=
0.6
#训练集,用于训练的验证码图片的文件名
TRAINING_IMAGE_NAME
=
[]
#验证集,用于模型验证的验证码图片的文件名
VALIDATION_IMAGE_NAME
=
[]
#存放训练好的模型的路径
MODEL_SAVE_PATH
=
'E:/Tensorflow/captcha/models/'
def
get_image_file_name(imgPath
=
CAPTCHA_IMAGE_PATH):
fileName
=
[]
total
=
0
for
filePath
in
os.listdir(imgPath):
captcha_name
=
filePath.split(
'/'
)[
-
1
]
fileName.append(captcha_name)
total
+
=
1
return
fileName, total
#将验证码转换为训练时用的标签向量,维数是 40
#例如,如果验证码是 ‘0296' ,则对应的标签是
# [1 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 1
# 0 0 0 0 0 0 1 0 0 0]
def
name2label(name):
label
=
np.zeros(CAPTCHA_LEN
*
CHAR_SET_LEN)
for
i, c
in
enumerate
(name):
idx
=
i
*
CHAR_SET_LEN
+
ord
(c)
-
ord
(
'0'
)
label[idx]
=
1
return
label
#取得验证码图片的数据以及它的标签
def
get_data_and_label(fileName, filePath
=
CAPTCHA_IMAGE_PATH):
pathName
=
os.path.join(filePath, fileName)
img
=
Image.
open
(pathName)
#转为灰度图
img
=
img.convert(
"L"
)
image_array
=
np.array(img)
image_data
=
image_array.flatten()
/
255
image_label
=
name2label(fileName[
0
:CAPTCHA_LEN])
return
image_data, image_label
#生成一个训练batch
def
get_next_batch(batchSize
=
32
, trainOrTest
=
'train'
, step
=
0
):
batch_data
=
np.zeros([batchSize, CAPTCHA_IMAGE_WIDHT
*
CAPTCHA_IMAGE_HEIGHT])
batch_label
=
np.zeros([batchSize, CAPTCHA_LEN
*
CHAR_SET_LEN])
fileNameList
=
TRAINING_IMAGE_NAME
if
trainOrTest
=
=
'validate'
:
fileNameList
=
VALIDATION_IMAGE_NAME
totalNumber
=
len
(fileNameList)
indexStart
=
step
*
batchSize
for
i
in
range
(batchSize):
index
=
(i
+
indexStart)
%
totalNumber
name
=
fileNameList[index]
img_data, img_label
=
get_data_and_label(name)
batch_data[i, : ]
=
img_data
batch_label[i, : ]
=
img_label
return
batch_data, batch_label
#构建卷积神经网络并训练
def
train_data_with_CNN():
#初始化权值
def
weight_variable(shape, name
=
'weight'
):
init
=
tf.truncated_normal(shape, stddev
=
0.1
)
var
=
tf.Variable(initial_value
=
init, name
=
name)
return
var
#初始化偏置
def
bias_variable(shape, name
=
'bias'
):
init
=
tf.constant(
0.1
, shape
=
shape)
var
=
tf.Variable(init, name
=
name)
return
var
#卷积
def
conv2d(x, W, name
=
'conv2d'
):
return
tf.nn.conv2d(x, W, strides
=
[
1
,
1
,
1
,
1
], padding
=
'SAME'
, name
=
name)
#池化
def
max_pool_2X2(x, name
=
'maxpool'
):
return
tf.nn.max_pool(x, ksize
=
[
1
,
2
,
2
,
1
], strides
=
[
1
,
2
,
2
,
1
], padding
=
'SAME'
, name
=
name)
#输入层
#请注意 X 的 name,在测试model时会用到它
X
=
tf.placeholder(tf.float32, [
None
, CAPTCHA_IMAGE_WIDHT
*
CAPTCHA_IMAGE_HEIGHT], name
=
'data-input'
)
Y
=
tf.placeholder(tf.float32, [
None
, CAPTCHA_LEN
*
CHAR_SET_LEN], name
=
'label-input'
)
x_input
=
tf.reshape(X, [
-
1
, CAPTCHA_IMAGE_HEIGHT, CAPTCHA_IMAGE_WIDHT,
1
], name
=
'x-input'
)
#dropout,防止过拟合
#请注意 keep_prob 的 name,在测试model时会用到它
keep_prob
=
tf.placeholder(tf.float32, name
=
'keep-prob'
)
#第一层卷积
W_conv1
=
weight_variable([
5
,
5
,
1
,
32
],
'W_conv1'
)
B_conv1
=
bias_variable([
32
],
'B_conv1'
)
conv1
=
tf.nn.relu(conv2d(x_input, W_conv1,
'conv1'
)
+
B_conv1)
conv1
=
max_pool_2X2(conv1,
'conv1-pool'
)
conv1
=
tf.nn.dropout(conv1, keep_prob)
#第二层卷积
W_conv2
=
weight_variable([
5
,
5
,
32
,
64
],
'W_conv2'
)
B_conv2
=
bias_variable([
64
],
'B_conv2'
)
conv2
=
tf.nn.relu(conv2d(conv1, W_conv2,
'conv2'
)
+
B_conv2)
conv2
=
max_pool_2X2(conv2,
'conv2-pool'
)
conv2
=
tf.nn.dropout(conv2, keep_prob)
#第三层卷积
W_conv3
=
weight_variable([
5
,
5
,
64
,
64
],
'W_conv3'
)
B_conv3
=
bias_variable([
64
],
'B_conv3'
)
conv3
=
tf.nn.relu(conv2d(conv2, W_conv3,
'conv3'
)
+
B_conv3)
conv3
=
max_pool_2X2(conv3,
'conv3-pool'
)
conv3
=
tf.nn.dropout(conv3, keep_prob)
#全链接层
#每次池化后,图片的宽度和高度均缩小为原来的一半,进过上面的三次池化,宽度和高度均缩小8倍
W_fc1
=
weight_variable([
20
*
8
*
64
,
1024
],
'W_fc1'
)
B_fc1
=
bias_variable([
1024
],
'B_fc1'
)
fc1
=
tf.reshape(conv3, [
-
1
,
20
*
8
*
64
])
fc1
=
tf.nn.relu(tf.add(tf.matmul(fc1, W_fc1), B_fc1))
fc1
=
tf.nn.dropout(fc1, keep_prob)
#输出层
W_fc2
=
weight_variable([
1024
, CAPTCHA_LEN
*
CHAR_SET_LEN],
'W_fc2'
)
B_fc2
=
bias_variable([CAPTCHA_LEN
*
CHAR_SET_LEN],
'B_fc2'
)
output
=
tf.add(tf.matmul(fc1, W_fc2), B_fc2,
'output'
)
loss
=
tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels
=
Y, logits
=
output))
optimizer
=
tf.train.AdamOptimizer(
0.001
).minimize(loss)
predict
=
tf.reshape(output, [
-
1
, CAPTCHA_LEN, CHAR_SET_LEN], name
=
'predict'
)
labels
=
tf.reshape(Y, [
-
1
, CAPTCHA_LEN, CHAR_SET_LEN], name
=
'labels'
)
#预测结果
#请注意 predict_max_idx 的 name,在测试model时会用到它
predict_max_idx
=
tf.argmax(predict, axis
=
2
, name
=
'predict_max_idx'
)
labels_max_idx
=
tf.argmax(labels, axis
=
2
, name
=
'labels_max_idx'
)
predict_correct_vec
=
tf.equal(predict_max_idx, labels_max_idx)
accuracy
=
tf.reduce_mean(tf.cast(predict_correct_vec, tf.float32))
saver
=
tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
steps
=
0
for
epoch
in
range
(
6000
):
train_data, train_label
=
get_next_batch(
64
,
'train'
, steps)
sess.run(optimizer, feed_dict
=
{X : train_data, Y : train_label, keep_prob:
0.75
})
if
steps
%
100
=
=
0
:
test_data, test_label
=
get_next_batch(
100
,
'validate'
, steps)
acc
=
sess.run(accuracy, feed_dict
=
{X : test_data, Y : test_label, keep_prob:
1.0
})
print
(
"steps=%d, accuracy=%f"
%
(steps, acc))
if
acc >
0.99
:
saver.save(sess, MODEL_SAVE_PATH
+
"crack_captcha.model"
, global_step
=
steps)
break
steps
+
=
1
if
__name__
=
=
'__main__'
:
image_filename_list, total
=
get_image_file_name(CAPTCHA_IMAGE_PATH)
random.seed(time.time())
#打乱顺序
random.shuffle(image_filename_list)
trainImageNumber
=
int
(total
*
TRAIN_IMAGE_PERCENT)
#分成测试集
TRAINING_IMAGE_NAME
=
image_filename_list[ : trainImageNumber]
#和验证集
VALIDATION_IMAGE_NAME
=
image_filename_list[trainImageNumber : ]
train_data_with_CNN()
print
(
'Training finished'
)
|
运行上面的代码,开始训练,训练要花些时间,如果没有GPU的话,会慢些, 。
训练完后,输出如下结果,经过4100次的迭代,训练出来的分类器模型在验证集上识别的准确率为99.5% 。
生成的模型文件如下,在模型测试时将用到这些文件 。
3. 测试模型 。
编写代码,对训练出来的模型进行测试 。
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
|
import
tensorflow as tf
import
numpy as np
from
PIL
import
Image
import
os
import
matplotlib.pyplot as plt
CAPTCHA_LEN
=
4
MODEL_SAVE_PATH
=
'E:/Tensorflow/captcha/models/'
TEST_IMAGE_PATH
=
'E:/Tensorflow/captcha/test/'
def
get_image_data_and_name(fileName, filePath
=
TEST_IMAGE_PATH):
pathName
=
os.path.join(filePath, fileName)
img
=
Image.
open
(pathName)
#转为灰度图
img
=
img.convert(
"L"
)
image_array
=
np.array(img)
image_data
=
image_array.flatten()
/
255
image_name
=
fileName[
0
:CAPTCHA_LEN]
return
image_data, image_name
def
digitalStr2Array(digitalStr):
digitalList
=
[]
for
c
in
digitalStr:
digitalList.append(
ord
(c)
-
ord
(
'0'
))
return
np.array(digitalList)
def
model_test():
nameList
=
[]
for
pathName
in
os.listdir(TEST_IMAGE_PATH):
nameList.append(pathName.split(
'/'
)[
-
1
])
totalNumber
=
len
(nameList)
#加载graph
saver
=
tf.train.import_meta_graph(MODEL_SAVE_PATH
+
"crack_captcha.model-4100.meta"
)
graph
=
tf.get_default_graph()
#从graph取得 tensor,他们的name是在构建graph时定义的(查看上面第2步里的代码)
input_holder
=
graph.get_tensor_by_name(
"data-input:0"
)
keep_prob_holder
=
graph.get_tensor_by_name(
"keep-prob:0"
)
predict_max_idx
=
graph.get_tensor_by_name(
"predict_max_idx:0"
)
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint(MODEL_SAVE_PATH))
count
=
0
for
fileName
in
nameList:
img_data, img_name
=
get_image_data_and_name(fileName, TEST_IMAGE_PATH)
predict
=
sess.run(predict_max_idx, feed_dict
=
{input_holder:[img_data], keep_prob_holder :
1.0
})
filePathName
=
TEST_IMAGE_PATH
+
fileName
print
(filePathName)
img
=
Image.
open
(filePathName)
plt.imshow(img)
plt.axis(
'off'
)
plt.show()
predictValue
=
np.squeeze(predict)
rightValue
=
digitalStr2Array(img_name)
if
np.array_equal(predictValue, rightValue):
result
=
'正确'
count
+
=
1
else
:
result
=
'错误'
print
(
'实际值:{}, 预测值:{},测试结果:{}'
.
format
(rightValue, predictValue, result))
print
(
'\n'
)
print
(
'正确率:%.2f%%(%d/%d)'
%
(count
*
100
/
totalNumber, count, totalNumber))
if
__name__
=
=
'__main__'
:
model_test()
|
对模型的测试结果如下,在测试集上识别的准确率为 94% 。
下面是两个识别错误的验证码 。
以上这篇利用Tensorflow构建和训练自己的CNN来做简单的验证码识别方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/maliao1123/article/details/79415828 。
最后此篇关于利用Tensorflow构建和训练自己的CNN来做简单的验证码识别方式的文章就讲到这里了,如果你想了解更多关于利用Tensorflow构建和训练自己的CNN来做简单的验证码识别方式的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我一直在阅读有关汇编函数的内容,但对于是使用进入和退出还是仅使用调用/返回指令来快速执行,我感到很困惑。一种方式快而另一种方式更小吗?例如,在不内联函数的情况下,在汇编中执行此操作的最快(stdcal
我正在处理一个元组列表,如下所示: res = [('stori', 'JJ'), ('man', 'NN'), ('unnatur', 'JJ'), ('feel', 'NN'), ('pig',
最近我一直在做很多网络或 IO 绑定(bind)操作,使用线程有助于加快代码速度。我注意到我一直在一遍又一遍地编写这样的代码: threads = [] for machine, user, data
假设我有一个名为 user_stats 的资源,其中包含用户拥有的帖子、评论、喜欢和关注者的数量。是否有一种 RESTful 方式只询问该统计数据的一部分(即,对于 user_stats/3,请告诉我
我有一个简单的 api,它的工作原理是这样的: 用户创建一个请求 ( POST /requests ) 另一个用户检索所有请求 ( GET /requests ) 然后向请求添加报价 ( POST /
考虑以下 CDK Python 中的示例(对于这个问题,不需要 AWS 知识,这应该对基本上任何构建器模式都有效,我只是在这个示例中使用 CDK,因为我使用这个库遇到了这个问题。): from aws
Scala 中管理对象池的首选方法是什么? 我需要单线程创建和删除大规模对象(不需要同步)。在 C++ 中,我使用了静态对象数组。 在 Scala 中处理它的惯用和有效方法是什么? 最佳答案 我会把它
我有一个带有一些内置方法的类。这是该类的抽象示例: class Foo: def __init__(self): self.a = 0 self.b = 0
返回和检查方法执行的 Pythonic 方式 我目前在 python 代码中使用 golang 编码风格,决定移动 pythonic 方式 例子: import sys from typing imp
我正在开发一个 RESTful API。其中一个 URL 允许调用者通过 id 请求特定人员的记录。 返回该 id 不存在的记录的常规值是什么?服务器是否应该发回一个空对象或者一个 404,或者其他什
我正在使用 pathlib.Path() 检查文件是否存在,并使用 rasterio 将其作为图像打开. filename = pathlib.Path("./my_file-name.tif") 但
我正在寻找一种 Pythonic 方式来从列表和字典创建嵌套字典。以下两个语句产生相同的结果: a = [3, 4] b = {'a': 1, 'b': 2} c = dict(zip(b, a))
我有一个正在操裁剪理设备的脚本。设备有时会发生物理故障,当它发生时,我想重置设备并继续执行脚本。我有这个: while True: do_device_control() device
做组合别名的最pythonic和正确的方法是什么? 这是一个假设的场景: class House: def cleanup(self, arg1, arg2, kwarg1=False):
我正在开发一个小型客户端服务器程序来收集订单。我想以“REST(ful)方式”来做到这一点。 我想做的是: 收集所有订单行(产品和数量)并将完整订单发送到服务器 目前我看到有两种选择: 将每个订单行发
我知道在 Groovy 中您可以使用字符串调用类/对象上的方法。例如: Foo."get"(1) /* or */ String meth = "get" Foo."$meth"(1) 有没有办法
在 ECMAScript6 中,您可以使用扩展运算符来解构这样的对象 const {a, ...rest} = obj; 它将 obj 浅拷贝到 rest,不带属性 a。 有没有一种干净的方法可以在
我有几个函数返回数字或None。我希望我的包装函数返回第一个不是 None 的结果。除了下面的方法之外,还有其他方法吗? def func1(): return None def func2(
假设我想设计一个 REST api 来讨论歌曲、专辑和艺术家(实际上我就是这样做的,就像我之前的 1312414 个人一样)。 歌曲资源始终与其所属专辑相关联。相反,专辑资源与其包含的所有歌曲相关联。
这是我认为必须经常出现的问题,但我一直无法找到一个好的解决方案。假设我有一个函数,它可以作为参数传递一个开放资源(如文件或数据库连接对象),或者需要自己创建一个。如果函数需要自己打开文件,最佳实践通常
我是一名优秀的程序员,十分优秀!