def train(height = CAPTCHA_HEIGHT, width = CAPTCHA_WIDTH, y_size = len(CAPTCHA_LIST) * CAPTCHA_LEN): acc_rate = 0.95 x = placeholder(float32, [None, height * width]) y = placeholder(float32, [None, y_size]) keep_prob = placeholder(float32) y_conv = cnn_graph(x, keep_prob, (height, width)) optimizer = optimize_graph(y, y_conv) accuracy = accuracy_graph(y, y_conv) saver = Saver() sess = Session() sess.run(global_variables_initializer()) step = 0 while 1: batch_x, batch_y = get_next_batch(64) sess.run(optimizer, feed_dict = {x: batch_x, y: batch_y, keep_prob: 0.75}) if step % 100 == 0: batch_x_test, batch_y_test = get_next_batch(100) acc = sess.run(accuracy, feed_dict = {x: batch_x_test, y: batch_y_test, keep_prob: 1.0}) print(datetime.now().strftime('%c'), ' step:', step, ' accuracy:', acc) if acc > acc_rate: if not isdir('./model'): mkdir('./model') print('Saving to model/captcha.model') saver.save(sess, './model/captcha.model', global_step = step) print('Saved to model/captcha.model') acc_rate += 0.005 if acc_rate >= 1: break step += 1 sess.close()
def train(height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH, y_size=len(CAPTCHA_LIST) * CAPTCHA_LEN): """ cnn训练 :param height: 验证码高度 :param width: 验证码宽度 :param y_size: 验证码预备字符列表长度*验证码长度(默认为4) :return: """ # cnn在图像大小是2的倍数时性能最高, 如果图像大小不是2的倍数,可以在图像边缘补无用像素 # 在图像上补2行,下补3行,左补2行,右补2行 # np.pad(image,((2,3),(2,2)), 'constant', constant_values=(255,)) acc_rate = 0.95 # 预设模型准确率标准 # 按照图片大小申请占位符 x = tf.placeholder(tf.float32, [None, height * width]) y = tf.placeholder(tf.float32, [None, y_size]) # 防止过拟合 训练时启用 测试时不启用 神经元使用率 keep_prob = tf.placeholder(tf.float32) # cnn模型 y_conv = cnn_graph(x, keep_prob, (height, width)) # 优化 optimizer = optimize_graph(y, y_conv) # 计算准确率 accuracy = accuracy_graph(y, y_conv) # 启动会话.开始训练 saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) # 初始化 step = 0 # 步数 while 1: batch_x, batch_y = get_next_batch(64) sess.run(optimizer, feed_dict={ x: batch_x, y: batch_y, keep_prob: 0.75 }) # 每训练一百次测试一次 if step % 100 == 0: batch_x_test, batch_y_test = get_next_batch(100) acc = sess.run(accuracy, feed_dict={ x: batch_x_test, y: batch_y_test, keep_prob: 1.0 }) print(datetime.now().strftime('%c'), ' step:', step, ' accuracy:', acc) # 准确率满足要求,保存模型 if acc > acc_rate: model_path = "./model/captcha.model" saver.save(sess, model_path, global_step=step) acc_rate += 0.01 if acc_rate > 0.99: # 准确率达到99%则退出 break step += 1 sess.close()
def train(height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH, y_size=len(CHARSET_LIST) * CAPTCHA_LEN): """ cnn训练 :param height: 验证码高度 :param width: 验证码宽度 :param y_size: 验证码预备字符列表长度*验证码长度(默认为4) :return: """ target_accuracy = PRESET_ACCURACY # 预设模型准确率标准 # cnn 在图像大小是 2 的倍数时性能最高, 如果图像大小不是 2 的倍数,可以在图像边缘补无用像素 # 在图像上补 2 行,下补 3 行,左补 2 行,右补 2 行 # np.pad(image, ((2, 3), (2, 2)), 'constant', constant_values=(255,)) # 按照图片大小申请占位符 x = tf.compat.v1.placeholder(tf.float32, [None, height * width]) y = tf.compat.v1.placeholder(tf.float32, [None, y_size]) # 防止过拟合 训练时启用 测试时不启用 神经元使用率 keep_prob = tf.compat.v1.placeholder(tf.float32) # cnn 模型 y_conv = cnn_graph(x, keep_prob, (height, width)) # 优化 optimizer = optimize_graph(y, y_conv) # 计算准确率 accuracy = accuracy_graph(y, y_conv) # 启动会话,开始训练 saver = tf.compat.v1.train.Saver() sess = tf.compat.v1.Session() sess.run(tf.compat.v1.global_variables_initializer()) # 初始化 step = 0 # 步数 while 1: batch_x, batch_y = get_next_batch(64) sess.run(optimizer, feed_dict={ x: batch_x, y: batch_y, keep_prob: 0.75 }) # 每训练 100 次测试一次 if step % 100 == 0: batch_x_test, batch_y_test = get_next_batch(100) current_accuracy = sess.run(accuracy, feed_dict={ x: batch_x_test, y: batch_y_test, keep_prob: 1.0 }) print(datetime.now().strftime('%c'), ' step:', step, ' accuracy:', current_accuracy) # 准确率满足要求,保存模型 if current_accuracy > target_accuracy: model_path = os.path.join(MODEL_DIR, "captcha-model") saver.save(sess, model_path, global_step=step) target_accuracy += 0.01 if target_accuracy > FINAL_ACCURACY: # 准确率达到 FINAL_ACCURACY 则退出 break step += 1 sess.close()
def train(height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH, y_size=len(CAPTCHA_LIST)*CAPTCHA_LEN): """ cnn训练 :param height: 验证码高度 :param width: 验证码宽度 :param y_size: 验证码预备字符列表长度*验证码长度(默认为4) :return: """ # cnn在图像大小是2的倍数时性能最高, 如果图像大小不是2的倍数,可以在图像边缘补无用像素 # 在图像上补2行,下补3行,左补2行,右补2行 # np.pad(image,((2,3),(2,2)), 'constant', constant_values=(255,)) acc_rate = 0.95 # 预设模型准确率标准 # 按照图片大小申请占位符 x = tf.placeholder(tf.float32, [None, height * width]) y = tf.placeholder(tf.float32, [None, y_size]) # 防止过拟合 训练时启用 测试时不启用 神经元使用率 keep_prob = tf.placeholder(tf.float32) # cnn模型 y_conv = cnn_graph(x, keep_prob, (height, width)) # 优化 optimizer = optimize_graph(y, y_conv) # 计算准确率 accuracy = accuracy_graph(y, y_conv) # 启动会话.开始训练 saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) # 初始化 # 基于当前已有模型进行二次训练 if os.path.exists("model/checkpoint"): model_file = tf.train.latest_checkpoint('model/') saver.restore(sess, model_file) step = 0 # 步数 while 1: batch_x, batch_y = get_next_batch(64) sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, keep_prob: 0.75}) # 每训练一百次测试一次 if step % 100 == 0: batch_x_test, batch_y_test = get_next_batch(100) acc = sess.run(accuracy, feed_dict={x: batch_x_test, y: batch_y_test, keep_prob: 1.0}) print(datetime.now().strftime('%c'), ' step:', step, ' accuracy:', acc) # 准确率满足要求,保存模型 if acc > acc_rate: model_path = "./model/captcha.model" saver.save(sess, model_path, global_step=step) acc_rate += 0.01 if acc_rate > 0.99: # 准确率达到99%则退出 # 训练结束时保存pb模型 constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['output']) with tf.gfile.FastGFile("./model/" + str(step) + 'model.pb', mode='wb') as f: f.write(constant_graph.SerializeToString()) break step += 1 sess.close()
summary = tf.summary.merge_all() saver = tf.train.Saver() # Initializer init = tf.global_variables_initializer() saver_step = 100 with tf.Session() as sess: writer = tf.summary.FileWriter(logdir="./Tensorboard", graph=sess.graph) sess.run(init) for step in range(1, par.num_steps + 1): # Get the next batch batch_x, batch_y = util.get_next_batch(batch_size=par.batch_size, image_size=par.image_size, threshold=par.threshold) batch_y = tf.one_hot(np.reshape(batch_y, [-1]), par.num_classes) _, s = sess.run( [training_op, summary], feed_dict={ X: batch_x, Y: sess.run(batch_y), keep_prob: par.dropout, global_step: step }) writer.add_summary(s, step) if step % par.display_step == 0 or step == 1: loss, acc = sess.run([loss_op, accuracy],