def batch_hack_captcha(): """ 批量生成验证码,然后再批量进行识别 :return: """ # 定义预测计算图 output = crack_captcha_cnn() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) saver = tf.train.Saver() with tf.Session() as sess: # saver = tf.train.import_meta_graph(save_model + ".meta") saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() task_cnt = 1000 right_cnt = 0 for i in range(task_cnt): text, image = wrap_gen_captcha_text_and_image() image = convert2gray(image) image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) if text == predict_text: right_cnt += 1 else: print("标记: {} 预测: {}".format(text, predict_text)) pass # print("标记: {} 预测: {}".format(text, predict_text)) print('task:', task_cnt, ' cost time:', (time.time() - stime), 's') print('right/total-----', right_cnt, '/', task_cnt)
def __init__(self): self.output = crack_captcha_cnn() self.predict = tf.argmax( tf.reshape(self.output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) self.saver = tf.train.Saver() self.sess = tf.Session() self.saver.restore(self.sess, tf.train.latest_checkpoint(model_path))
def batch_hack_captcha_save(): """ 批量生成验证码,然后再批量进行识别 :return: """ # 定义预测计算图 output = crack_captcha_cnn() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) saver = tf.train.Saver() with tf.Session() as sess: # saver = tf.train.import_meta_graph(save_model + ".meta") saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() task_cnt = 1000 right_cnt = 0 rootdir = join(capt.cfg.workspace, 'test') file_names = [] for parent, dirnames, filenames in os.walk( rootdir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 file_names = filenames for file_name in filenames: print(file_name) file_name, image = get_captcha_text_and_image2(rootdir, file_name) image = convert2gray(image) image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) file_name2 = '__' + predict_text + '__' + file_name os.rename(join(rootdir, file_name), join(rootdir, file_name2))
def predict_captcha(image): image = Image.open(image) image = np.array(image) image = convert2gray(image) image = image.flatten() / 255 output = crack_captcha_cnn() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) saver = tf.train.Saver() with tf.Session() as sess: # restore only once saver.restore(sess, tf.train.latest_checkpoint(model_path)) text = hack_function(sess, predict, image) return text
def batch_hack_captcha(): """ 批量生成验证码,然后再批量进行识别 :return: """ # 定义预测计算图 output = crack_captcha_cnn() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) saver = tf.train.Saver() with tf.Session() as sess: # saver = tf.train.import_meta_graph(save_model + ".meta") saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() task_cnt = 1000 right_cnt = 0 rootdir = join(capt.cfg.workspace, 'test') file_names = [] for parent, dirnames, filenames in os.walk( rootdir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 file_names = filenames for i in range(task_cnt): text, image = get_captcha_text_and_image(rootdir, file_names) image = convert2gray(image) image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) if text == predict_text: right_cnt += 1 else: print("标记: {} 预测: {}".format(text, predict_text)) pass # print("标记: {} 预测: {}".format(text, predict_text)) print('task:', task_cnt, ' cost time:', (time.time() - stime), 's') print('right/total-----', right_cnt, '/', task_cnt)
def batch_hack_captcha(): """ 批量生成验证码,然后再批量进行识别 :return: """ # 定义预测计算图 output = crack_captcha_cnn() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) saver = tf.train.Saver() with tf.Session() as sess: # saver = tf.train.import_meta_graph(save_model + ".meta") # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/state_ops.md#latest_checkpoint saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() task_cnt = 1000 right_cnt = 0 for i in range(task_cnt): text, image = wrap_gen_captcha_text_and_image() text = text.upper() image = convert2gray(image) image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) if text == predict_text: right_cnt += 1 print("正确预测:标记: {} 预测: {}".format(text, predict_text)) else: print("错误预测:标记: {} 预测: {}".format(text, predict_text)) pass # print("标记: {} 预测: {}".format(text, predict_text)) print('task:', task_cnt, ' cost time:', (time.time() - stime), 's') print('right/total-----', right_cnt, '/', task_cnt) pass
def train_crack_captcha_cnn(): """ train model :return: """ output = crack_captcha_cnn() predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) # 36行,4列 label = tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) max_idx_p = tf.argmax(predict, 2) # shape:batch_size,4,nb_cls max_idx_l = tf.argmax(label, 2) correct_pred = tf.equal(max_idx_p, max_idx_l) with tf.name_scope('my_monitor'): loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=predict, labels=label)) # loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y)) tf.summary.scalar('my_loss', loss) #What is the difference between softmax and sigmoid #We use optimizer to accelerate the speed of training, and then decrease slowly optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) with tf.name_scope('my_monitor'): accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) tf.summary.scalar('my_accuracy', accuracy) saver = tf.train.Saver() # save the procedures of training sess = tf.InteractiveSession(config=tf.ConfigProto( log_device_placement=False)) sess.run(tf.global_variables_initializer()) merged = tf.summary.merge_all() writer = tf.summary.FileWriter(tb_log_path, sess.graph) step = 0 while True: batch_x, batch_y = get_next_batch(64) # 64 _, loss_ = sess.run([optimizer, loss], feed_dict={ X: batch_x, Y: batch_y, keep_prob: 0.95 }) print(step, 'loss:\t', loss_) step += 1 # save the result once every 2000 times if step % 2000 == 0: saver.save(sess, save_model, global_step=step) # Calculate the accuracy on the train set if step % 50 != 0: continue # use the new data generated to calculate the accuracy, we calculated once every 50 steps batch_x_test, batch_y_test = get_next_batch( 256) # use the new data to test acc = sess.run(accuracy, feed_dict={ X: batch_x_test, Y: batch_y_test, keep_prob: 1. }) print(step, 'acc---------------------------------\t', acc) # end this operation when if acc > 0.98: break # invocate the tensor board summary = sess.run(merged, feed_dict={ X: batch_x_test, Y: batch_y_test, keep_prob: 1. }) writer.add_summary(summary, step)
def train_crack_captcha_cnn(): output = crack_captcha_cnn() predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) # 36行,4列 label = tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) max_idx_p = tf.argmax(predict, 2) # shape:batch_size,4,nb_cls max_idx_l = tf.argmax(label, 2) correct_pred = tf.equal(max_idx_p, max_idx_l) with tf.name_scope('my_monitor'): loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=predict, labels=label)) # loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y)) tf.summary.scalar('my_loss', loss) optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) with tf.name_scope('my_monitor'): accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) tf.summary.scalar('my_accuracy', accuracy) saver = tf.train.Saver() # 将训练过程进行保存 sess = tf.InteractiveSession(config=tf.ConfigProto( log_device_placement=False)) sess.run(tf.global_variables_initializer()) merged = tf.summary.merge_all() writer = tf.summary.FileWriter(tb_log_path, sess.graph) step = 0 while True: batch_x, batch_y = get_next_batch(64) # 64 _, loss_ = sess.run([optimizer, loss], feed_dict={ X: batch_x, Y: batch_y, keep_prob: 0.95 }) print(step, 'loss:\t', loss_) step += 1 # 每2000步保存一次实验结果 if step % 2000 == 0: saver.save(sess, save_model, global_step=step) # 在测试集上计算精度 if step % 50 != 0: continue # 每50 step计算一次准确率,使用新生成的数据 batch_x_test, batch_y_test = get_next_batch(256) # 新生成的数据集个来做测试 acc = sess.run(accuracy, feed_dict={ X: batch_x_test, Y: batch_y_test, keep_prob: 1. }) print(step, 'acc---------------------------------\t', acc) # 终止条件 if acc > 0.98: break # 启用监控 tensor board summary = sess.run(merged, feed_dict={ X: batch_x_test, Y: batch_y_test, keep_prob: 1. }) writer.add_summary(summary, step)
def train_crack_captcha_cnn(): """ 训练模型 :return: """ output = crack_captcha_cnn() # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/array_ops.md#reshape predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) # 36行,4列 label = tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/math_ops.md#argmax max_idx_p = tf.argmax(predict, 2) # shape:batch_size,4,nb_cls max_idx_l = tf.argmax(label, 2) correct_pred = tf.equal(max_idx_p, max_idx_l) # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/framework.md#Graph.name_scope with tf.name_scope('my_monitor'): # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/math_ops.md#reduce_mean # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/nn.md#softmax_cross_entropy_with_logits loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=predict, labels=label)) # loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y)) # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#scalar_summary tf.summary.scalar('my_loss', loss) # 最后一层用来分类的softmax和sigmoid有什么不同? # optimizer 为了加快训练 learning_rate应该开始大,然后慢慢衰 # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#AdamOptimizer optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) with tf.name_scope('my_monitor'): accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) tf.summary.scalar('my_accuracy', accuracy) # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/state_ops.md#Saver saver = tf.train.Saver() # 将训练过程进行保存 # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/client.md#InteractiveSession ''' sess = tf.InteractiveSession( config=tf.ConfigProto( log_device_placement=False ) ) ''' sess = tf.Session() sess.run(tf.global_variables_initializer()) # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#merge_all_summaries merged = tf.summary.merge_all() # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/train.md#SummaryWriter writer = tf.summary.FileWriter(tb_log_path, sess.graph) step = 0 while True: batch_x, batch_y = get_next_batch(64) # 64 #x_, conv1_, conv2_, conv3_, dense_, output_, predict_, label_ = sess.run([x, conv1, conv2, conv3, dense, output, predict, label], feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.9}) _, loss_ = sess.run([optimizer, loss], feed_dict={ X: batch_x, Y: batch_y, keep_prob: 0.9 }) print(step, 'loss:\t', loss_) step += 1 # 每2000步保存一次实验结果 if step % 1000 == 0: saver.save(sess, save_model, global_step=step) # 在测试集上计算精度 if step % 50 != 0: continue # 每50 step计算一次准确率,使用新生成的数据 batch_x_test, batch_y_test = get_next_batch(256) # 新生成的数据集个来做测试 acc = sess.run(accuracy, feed_dict={ X: batch_x_test, Y: batch_y_test, keep_prob: 1. }) print(step, 'acc---------------------------------\t', acc) # 终止条件 if acc > 0.98: break # 启用监控 tensor board summary = sess.run(merged, feed_dict={ X: batch_x_test, Y: batch_y_test, keep_prob: 1. }) writer.add_summary(summary, step)