Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CNN网站验证码训练后重复预测异常 #16

Open
xiefg opened this issue Nov 2, 2017 · 8 comments
Open

CNN网站验证码训练后重复预测异常 #16

xiefg opened this issue Nov 2, 2017 · 8 comments

Comments

@xiefg
Copy link

xiefg commented Nov 2, 2017

使用tensorflow_cnn进行验证码训练并预测,图片预测成功率很高,但是图片验证码样本是根据目标网站自己用PHP模拟生成,几乎接近一致,训练后使用模型进行目标网站的登录,登录逻辑为了防止验证码失败,会有重试,login是个循环,失败了掉login,login里会读取验证码图片调用crack_captcha,当第一次验证准确时直接登录成功,当第一次验证失败,重新调用登录去再读取验证码图片进行预测时报异常,然后N次循环都报异常,而且每一次重试,会增加异常的数量,异常如下:
alert('验证码错误');
9006
2017-11-02 10:22:14.369586: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_11 not found in checkpoint
2017-11-02 10:22:14.369586: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_10 not found in checkpoint
2017-11-02 10:22:14.369586: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_12 not found in checkpoint
2017-11-02 10:22:14.371381: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_14 not found in checkpoint
2017-11-02 10:22:14.371404: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_15 not found in checkpoint
2017-11-02 10:22:14.371410: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_13 not found in checkpoint
2017-11-02 10:22:14.372782: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_17 not found in checkpoint
2017-11-02 10:22:14.372955: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_16 not found in checkpoint
2017-11-02 10:22:14.375048: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_18 not found in checkpoint
2017-11-02 10:22:14.375149: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_19 not found in checkpoint
2393
2017-11-02 10:22:15.583424: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_10 not found in checkpoint
2017-11-02 10:22:15.584296: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_11 not found in checkpoint
2017-11-02 10:22:15.584677: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_13 not found in checkpoint
2017-11-02 10:22:15.584754: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_12 not found in checkpoint
2017-11-02 10:22:15.585934: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_14 not found in checkpoint
2017-11-02 10:22:15.586106: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_16 not found in checkpoint
2017-11-02 10:22:15.586465: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_15 not found in checkpoint
2017-11-02 10:22:15.587122: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_17 not found in checkpoint
2017-11-02 10:22:15.587133: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_18 not found in checkpoint
2017-11-02 10:22:15.588150: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_19 not found in checkpoint
2017-11-02 10:22:15.588196: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_20 not found in checkpoint
2017-11-02 10:22:15.589135: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_21 not found in checkpoint
2017-11-02 10:22:15.589325: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_22 not found in checkpoint
2017-11-02 10:22:15.591827: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_23 not found in checkpoint

@xiefg
Copy link
Author

xiefg commented Nov 2, 2017

重新写个测试更明显点

def test():
    captcha = Captcha()
    text, image = captcha.gen()
    image = convert2gray(image)
    image = image.flatten() / 255
    predict_text = captcha.crack_captcha(image)
    print("正确: {}  预测: {}".format(text, predict_text))
    time.sleep(1)
    test()

2017-11-02 14:34:54.950657: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-11-02 14:34:54.950681: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
正确: 51170 预测: 51170
2017-11-02 14:34:56.887373: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_11 not found in checkpoint
2017-11-02 14:34:56.887503: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_10 not found in checkpoint
2017-11-02 14:34:56.889094: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_13 not found in checkpoint
2017-11-02 14:34:56.889150: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_12 not found in checkpoint
2017-11-02 14:34:56.890027: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_14 not found in checkpoint
2017-11-02 14:34:56.891680: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_15 not found in checkpoint
2017-11-02 14:34:56.891723: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_16 not found in checkpoint
2017-11-02 14:34:56.891904: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_19 not found in checkpoint
2017-11-02 14:34:56.894900: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_17 not found in checkpoint
2017-11-02 14:34:56.894900: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key Variable_18 not found in checkpoint
Traceback (most recent call last):
File "captcha.py", line 252, in
test()
File "captcha.py", line 248, in test
test()
File "captcha.py", line 244, in test
predict_text = captcha.crack_captcha(image) #导入模型识别
File "captcha.py", line 217, in crack_captcha
saver.restore(sess, tf.train.latest_checkpoint('.'))
File "/Library/Python/2.7/site-packages/tensorflow/python/training/saver.py", line 1457, in restore
{self.saver_def.filename_tensor_name: save_path})
File "/Library/Python/2.7/site-packages/tensorflow/python/client/session.py", line 778, in run
run_metadata_ptr)
File "/Library/Python/2.7/site-packages/tensorflow/python/client/session.py", line 982, in _run
feed_dict_string, options, run_metadata)
File "/Library/Python/2.7/site-packages/tensorflow/python/client/session.py", line 1032, in _do_run
target_list, options, run_metadata)
File "/Library/Python/2.7/site-packages/tensorflow/python/client/session.py", line 1052, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError

@xiefg
Copy link
Author

xiefg commented Nov 2, 2017

问题好像是:重试时不要重复定义CNN,即crack_captcha_cnn只执行一次复用即可解决

@1513
Copy link

1513 commented Nov 28, 2017

你解决了吗?好像问题不在这

@1513
Copy link

1513 commented Nov 28, 2017

我每次都是创建一个新的对象去处理,肯定是执行了一次

@xiefg
Copy link
Author

xiefg commented Dec 4, 2017

@1513 crack_captcha_cnn只执行一次,如果在对象里就要处理成单例模式

@1513
Copy link

1513 commented Dec 4, 2017

@xiefg 我已经解决了需要重置tf.reset_default_graph(),每次识别前需要进行这个操作,感谢您的回复,另外,要不要设置成单例,多线程下要不要同步处理,不清楚,我试过python多线程跑,好像没有问题

@SimonWang00
Copy link

@ 1513 ,重置tf.reset_default_graph()代码会报错,你的重置放哪了?

@ResolveWang
Copy link

@1513 请问一下tf.reset_default_graph()这个函数放哪里啊,放预测代码中会报错

目前我的解决方案如 @xiefg 所说的,直接使用的单例

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@xiefg @ResolveWang @1513 @SimonWang00 and others