Exemplo n.º 1
0
 def run(self):
     self.signal.emit(QtGui.QListWidget, 'listWidget', '训练已开始。')
     step = 1
     while True:
         if not self.status:
             self.signal.emit(QtGui.QListWidget, 'listWidget', '训练已停止。')
             return
         if self.is_paused:
             if not self.pause_flag:
                 self.signal.emit(QtGui.QListWidget, 'listWidget', '训练已暂停。')
                 self.pause_flag = True
             time.sleep(0.1)
             continue
         batch_x, batch_y = get_next_batch(32)
         _, loss_ = self.sess.run([self.optimizer, self.loss], {
             X: batch_x,
             Y: batch_y,
             keep_prob: 0.98
         })
         # print(step, 'loss:\t', loss_)
         self.signal.emit(QtGui.QListWidget, 'listWidget',
                          '%s loss:\t %s' % (step, loss_))
         if 0 == step % self.rate:
             self.saver.save(self.sess, save_model, step)
         if 0 == step % 100:
             batch_x_test, batch_y_test = get_next_batch(100)
             acc = self.sess.run(self.accuracy, {
                 X: batch_x_test,
                 Y: batch_y_test,
                 keep_prob: 1.
             })
             # print(step, 'acc---------------------------------\t', acc)
             self.signal.emit(
                 QtGui.QListWidget, 'listWidget',
                 '%d acc---------------------------------\t%s' %
                 (step, acc))
         if '   ∞' != self.count and int(self.count) == step:
             return
         step += 1
def train_crack_captcha_cnn():
    output = crack_captcha_cnn()
    loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y))
         # 最后一层用来分类的softmax和sigmoid有什么不同?
	 # optimizer 为了加快训练 learning_rate应该开始大,然后慢慢衰
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

    predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN])
    max_idx_p = tf.argmax(predict, 2)
    max_idx_l = tf.argmax(tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
    correct_pred = tf.equal(max_idx_p, max_idx_l)
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


    print("Configuring TensorBoard and Saver...")
    # 配置 Tensorboard,重新训练时,请将tensorboard文件夹删除,不然图会覆盖
    tensorboard_dir = 'tensorboard/sougou_cnn'
    if not os.path.exists(tensorboard_dir):
        os.makedirs(tensorboard_dir)
    tf.summary.scalar("loss", loss)
    tf.summary.scalar("accuracy", accuracy)
    merged_summary = tf.summary.merge_all()
    writer = tf.summary.FileWriter(tensorboard_dir)
    # 配置 Saver
    saver = tf.train.Saver()
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    start_time = time.time()
    best_acc_val = 0.0           # 最佳验证集准确率
    last_improved = 0            # 记录上一次提升批次
    #require_improvement = 1600   # 如果超过xx轮未提升,提前结束训练

    with tf.Session() as sess:
        writer.add_graph(sess.graph)
        sess.run(tf.global_variables_initializer())
        step = 0
        while True:
            batch_x, batch_y = get_next_batch(64)
            _, loss_ = sess.run([optimizer, loss], feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.5})
            
            # 每100 step计算一次准确率
            if step % 100 == 0:
                batch_x_test, batch_y_test = get_next_batch(100)
                s, acc, losss = sess.run([merged_summary, accuracy, loss], feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.})
                # 将训练结果写入tensorboard scalar
                writer.add_summary(s, step)
                
                if acc > best_acc_val:
                    # 保存最好结果
                    best_acc_val = acc
                    last_improved = step
                    saver.save(sess=sess, save_path=save_dir)
                    improved_str = '*'
                else:
                    improved_str = ''

                time_dif = get_time_dif(start_time)
                msg = 'Step: {0:>6}, Train Loss: {1:>6.2}, Val Loss: {2:>6.2}, Train Acc: {3:>7.2%}, Time: {4} {5}'
                print(msg.format(step, loss_, losss, acc, time_dif, improved_str))
                
                """
                if total_batch - last_improved > require_improvement:
                    # 验证集正确率长期不提升,提前结束训练
                    print("No optimization for a long time, auto-stopping...")
                break  # 跳出循环
            """
                
            step += 1
Exemplo n.º 3
0
def train_crack_captcha_cnn():
    """
    训练模型
    :return:
    """
    output = crack_captcha_cnn()
    with tf.name_scope('Monitor'):
        # loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=Y))
        loss = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y))
    tf.summary.scalar('Loss', loss)
    # optimizer 为了加快训练 learning_rate应该开始大,然后慢慢衰
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

    predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN])

    max_idx_p = tf.argmax(predict, 2)
    max_idx_l = tf.argmax(tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
    correct_pred = tf.equal(max_idx_p, max_idx_l)

    with tf.name_scope('Monitor'):
        accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
    tf.summary.scalar('Accuracy', accuracy)

    saver = tf.train.Saver(max_to_keep=0)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        merged = tf.summary.merge_all()
        writer = tf.summary.FileWriter(tb_log_path, sess.graph)

        step = 0
        cnt = 0
        while True:
            if cnt == 125:
                saver.save(sess, save_model, global_step=step)
                break
            batch_x, batch_y = get_next_batch(64, rand[cnt])
            for j in range(100):
                _, lossSize = sess.run([optimizer, loss],
                                       feed_dict={
                                           X: batch_x,
                                           Y: batch_y,
                                           keep_prob: 0.8
                                       })
                if step % 5 == 0:
                    print("cnt: " + str(cnt) + "\tstep: " + str(step) +
                          "\tloss: " + str(lossSize))
                    batch_x_test, batch_y_test = batch_x, batch_y
                    acc = sess.run(accuracy,
                                   feed_dict={
                                       X: batch_x_test,
                                       Y: batch_y_test,
                                       keep_prob: 1.
                                   })
                    print("Accuracy: " + str(acc))
                if step % 2000 == 0:
                    saver.save(sess, save_model, global_step=step)
                if step % 100 == 0:
                    summary = sess.run(merged,
                                       feed_dict={
                                           X: batch_x_test,
                                           Y: batch_y_test,
                                           keep_prob: 1.
                                       })
                    writer.add_summary(summary, step)
                step += 1
            cnt += 1
Exemplo n.º 4
0
def train_crack_captcha_cnn():
    """
    训练模型
    :return:
    """
    global_step = tf.Variable(0, trainable=False)
    output = crack_captcha_cnn()
    predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN])  
    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 为了加快训练 learning_rate应该开始大,然后慢慢衰
    optimizer = tf.train.AdamOptimizer(learning_rate=0.0003).minimize(loss,global_step=global_step)

    with tf.name_scope('my_monitor'):
        accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
    tf.summary.scalar('my_accuracy', accuracy)


    with tf.device('/gpu:0'):
        sess = tf.Session(config=tf.ConfigProto(log_device_placement=False))

    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.global_variables(),max_to_keep=2)  # 将训练过程进行保存
    try:
        saver.restore(sess, tf.train.latest_checkpoint(model_path))
    except Exception as e:
        print (e)
    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter(tb_log_path, sess.graph)

    step = 0
    time_ep_start=time.time()
    max_acc_on_tax_pic=0
    max_acc_step=0
    while True:
        batch_x, batch_y = get_next_batch(64)  # 64
        _, loss_ ,step= sess.run([optimizer, loss,global_step], feed_dict={X: batch_x, Y: batch_y, keep_prob: 1.0})

        if step%10==0:
            print(step, 'loss: ', loss_)


        # 每100步保存一次实验结果
        if step % 100 == 0:
            saver.save(sess, save_model, global_step=step)
        else:
            continue
        # 生成测试集计算精度
        ep_time=time.time()-time_ep_start
        time_ep_start=time.time()
        acc_train_pic=test_hack_captcha_training_data(sess,output)
        print('EP spend:%0.2fs\n'%ep_time,
            'acc_on_train_pic: %0.2f%%'%(acc_train_pic*100)
            )

        # 终止条件
        if acc_train_pic>0.98:
            break
Exemplo n.º 5
0
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)
    # 最后一层用来分类的softmax和sigmoid有什么不同?

    # optimizer 为了加快训练 learning_rate应该开始大,然后慢慢衰
    optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).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(100)  # 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 % 1000 == 0:
            saver.save(sess, save_model, global_step=step)

        # 在测试集上计算精度
        if step % 50 != 0:
            continue

        # 每50 step计算一次准确率,使用新生成的数据
        batch_x_test, batch_y_test = get_test_batch(300)  # 新生成的数据集个来做测试
        acc = sess.run(accuracy, feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.0})
        print(step, 'acc---------------------------------\t', acc)

        # 终止条件
        if acc > 0.95:
            break

        # 启用监控 tensor board
        summary = sess.run(merged, feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.0})
        writer.add_summary(summary, step)