代码拉取完成,页面将自动刷新
the 1st tensorflow project.
Solve captcha using TensorFlow
生成测试数据, 1000 组, 纯数字
$ python gen_captcha.py -n 1000 -d
训练
$ time python simple_softmax.py
data loaded
train images: 10000. test images: 2000
label_size: 10, image_size: 6000
...
step = 9100, accuracy = 91.10%
step = 9200, accuracy = 91.40%
step = 9300, accuracy = 92.00%
step = 9400, accuracy = 91.40%
step = 9500, accuracy = 91.35%
step = 9600, accuracy = 90.80%
step = 9700, accuracy = 91.60%
step = 9800, accuracy = 91.65%
step = 9900, accuracy = 90.50%
testing accuracy = 91.05%
real2m46.478s
user2m29.704s
sys0m17.828s
基本的原理:
tensorflow 执行时,写 log 文件, tensorboard 解析 log 并做数据可视化。
定义 graph 的时候, 用 tf.summary 定义需要写入日志的变量值和格式。
代码:softmax_with_log.py
$ python softmax_with_log.py
在另外 1 个 terminal 中执行
$ tensorboard --logdir=log
浏览器中打开 http://127.0.0.1:6006/
$ time python conv_captcha.py
data loaded
train images: 10000. test images: 2000
label_size: 10, image_size: 6000
...
step 9100, training accuracy = 100.00%, testing accuracy = 98.90%
step 9200, training accuracy = 100.00%, testing accuracy = 98.80%
step 9300, training accuracy = 100.00%, testing accuracy = 98.90%
step 9400, training accuracy = 100.00%, testing accuracy = 98.80%
step 9500, training accuracy = 100.00%, testing accuracy = 98.90%
step 9600, training accuracy = 100.00%, testing accuracy = 98.60%
step 9700, training accuracy = 100.00%, testing accuracy = 98.65%
step 9800, training accuracy = 100.00%, testing accuracy = 98.95%
step 9900, training accuracy = 100.00%, testing accuracy = 98.95%
testing accuracy = 99.15%
real 4m44.143s
user 3m40.896s
sys 0m33.492s
每一个 dataset 分 train 和 test 2 个目录存放图片数据。 根目录下的 meta.json 存放参数信息。
meta.json 的例子
{
"num_per_image": 1,
"n_train": 1,
"label_choices": "0123456789",
"n_test": 1,
"width": 60,
"height": 100,
"label_size": 10
}
图片的例子
使用 python 的 captcha package 生成测试数据
查看用法说明
$ python gen_captcha.py -h
usage: gen_captcha.py [-h] [-n N] [-t T] [-d] [-l] [-u] [--npi NPI]
optional arguments:
-h, --help show this help message and exit
-n N number of captchas permutations
-t T ratio of test / train.
-d, --digit use digits in labels.
-l, --lower use lowercase characters in labels.
-u, --upper use uppercase characters in labels.
--npi NPI number of characters per image.
例如,生成包含数字 + 小写字母的验证码,每张图片包含 2 个字符, 10 组训练数据,另外生成 10% 的测试数据
$ python gen_captcha.py -dl --npi 2 -n 10 -t 0.1
36 choices: 0123456789abcdefghijklmnopqrstuvwxyz
generating 10 groups of captchas in images/char-2-groups-10/train
generating 1 groups of captchas in images/char-2-groups-10/test
write meta info in images/char-2-groups-10/meta.json
用时约 1 min。生成的图片数量如下
$ ls images/char-2-groups-10/train | wc -l
12600
$ ls images/char-2-groups-10/test/ | wc -l
1260
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。