Ejemplo n.º 1
0
def train_running():
    with tf.name_scope('input'):

        train_batch, train_label_batch, _ = input_data.get_batch(train_txt, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
        val_batch, val_label_batch, _ = input_data.get_batch(val_txt, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)

    x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
    y_ = tf.placeholder(tf.int32, shape=[BATCH_SIZE])

    model = models.model(x, N_CLASSES)
    model.AlexNet()
    logits = model.fc3

    loss = tools.loss(logits, y_)
    acc = tools.accuracy(logits, y_)
    train_op = tools.optimize(loss, LEARNING_RATE)

    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        summary_op = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
        val_writer = tf.summary.FileWriter(logs_val_dir, sess.graph)

        try:
            for step in np.arange(MAX_STEP):
                if coord.should_stop():
                    break

                tra_images, tra_labels = sess.run([train_batch, train_label_batch])
                _, tra_loss, tra_acc = sess.run([train_op, loss, acc],
                                                feed_dict={x: tra_images, y_: tra_labels})

                if step % 50 == 0:
                    print('Step %d, train loss = %.4f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc))
                    summary_str = sess.run(summary_op, feed_dict={x: tra_images, y_: tra_labels})
                    train_writer.add_summary(summary_str, step)
                #
                if step % 200 == 0 or (step + 1) == MAX_STEP:
                    val_images, val_labels = sess.run([val_batch, val_label_batch])
                    val_loss, val_acc = sess.run([loss, acc],
                                                 feed_dict={x: val_images, y_: val_labels})

                    print('**  Step %d, val loss = %.4f, val accuracy = %.2f%%  **' % (step, val_loss, val_acc))
                    summary_str = sess.run(summary_op, feed_dict={x: val_images, y_: val_labels})
                    val_writer.add_summary(summary_str, step)
                    #
                if step % 2000 == 0 or (step + 1) == MAX_STEP:
                    checkpoint_path = os.path.join(model_dir, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)

        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()
        coord.join(threads)
Ejemplo n.º 2
0
def run_training():

    # you need to change the directories to yours.
    s_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+YuanTu'
    T_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+X_mid'
    logs_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+'
    s_train, s_train_label = input_data.get_files(s_train_dir)
    s_train_batch, s_train_label_batch = input_data.get_batch(
        s_train, s_train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    T_train, T_train_label = input_data.get_files(T_train_dir)

    T_train_batch, T_train_label_batch = input_data.get_batch(
        T_train, T_train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)

    train_logits = model.inference(s_train_batch, T_train_batch, BATCH_SIZE,
                                   N_CLASSES)
    train_loss = model.losses(train_logits, s_train_label_batch)
    train_op = model.trainning(train_loss, learning_rate)
    train__acc = model.evaluation(train_logits, s_train_label_batch)

    summary_op = tf.summary.merge_all()  #汇总操作
    sess = tf.Session()  #定义sess
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)  #
    saver = tf.train.Saver()  #保存操作

    sess.run(tf.global_variables_initializer())  #初始化所有变量
    coord = tf.train.Coordinator()  #设置多线程协调器
    threads = tf.train.start_queue_runners(
        sess=sess, coord=coord)  #开始Queue Runners(队列运行器)

    #开始训练过程
    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %
                      (step, tra_loss, tra_acc * 100.0))
                #运行汇总操作,写入汇总
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 800 == 0 or (step + 1) == MAX_STEP:
                #保存当前模型和权重到 logs_train_dir,global_step为当前迭代次数
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 3
0
def train_running():

    with tf.variable_scope('input'):
        train_batch, train_label_batch, _ = input_data.get_batch(train_txt, model.config.img_w, model.config.img_h, BATCH_SIZE, CAPACITY)
        val_batch, val_label_batch, _ = input_data.get_batch(val_txt, model.config.img_w, model.config.img_h, BATCH_SIZE, CAPACITY)    
        
    with tf.Session() as sess:
        
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        summary_op = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
        val_writer = tf.summary.FileWriter(logs_val_dir, sess.graph)
        
        try:
            for step in np.arange(MAX_STEP):
                if coord.should_stop():
                    break

                tra_images,tra_labels = sess.run([train_batch, train_label_batch])
                tra_loss, tra_acc,  _ = sess.run([model.loss, model.acc, model.optim],
                                                 feed_dict={model.input_x: tra_images, model.input_y: tra_labels,
                                                            model.keep_prob: 0.5, model.batch_size: BATCH_SIZE})

                if step % 50 == 0:
                    print('Step %d, train loss = %.4f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc))
                    summary_str = sess.run(summary_op, 
                                           feed_dict={model.input_x: tra_images, model.input_y: tra_labels, 
                                                      model.keep_prob: 0.5, model.batch_size: BATCH_SIZE})
                    train_writer.add_summary(summary_str, step)
                #
                if step % 200 == 0 or (step + 1) == MAX_STEP:
                    val_images, val_labels = sess.run([val_batch, val_label_batch])
                    val_loss, val_acc = sess.run([model.loss, model.acc],
                                                 feed_dict={model.input_x: val_images, model.input_y: val_labels, 
                                                            model.keep_prob: 1.0, model.batch_size: BATCH_SIZE})

                    print('**  Step %d, val loss = %.4f, val accuracy = %.2f%%  **' % (step, val_loss, val_acc))
                    summary_str = sess.run(summary_op,
                                           feed_dict={model.input_x: val_images, model.input_y: val_labels, 
                                                      model.keep_prob: 1.0, model.batch_size: BATCH_SIZE})
                    val_writer.add_summary(summary_str, step)
                    #
                if step % 2000 == 0 or (step + 1) == MAX_STEP:
                    checkpoint_path = os.path.join(model_dir, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)

        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()
        coord.join(threads)
Ejemplo n.º 4
0
def evaluate_running():

    with tf.Graph().as_default():
        data_dir = './data/KTH_RGB/'
        model_dir = './model/KTH_RGB6000/'
        train_image, train_label, val_image, val_label, n_test = input_data.get_files(
            data_dir, RATIO, ret_val_num=True)
        train_batch, train_label_batch = input_data.get_batch(
            train_image, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
        val_batch, val_label_batch = input_data.get_batch(
            val_image, val_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
        #
        logits = models.AlexNet(val_batch, N_CLASSES)
        top_k_op = tf.nn.in_top_k(logits, val_label_batch, 1)

        saver = tf.train.Saver(tf.global_variables())

        with tf.Session() as sess:

            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(model_dir)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('Loading success, global_step is %s' % global_step)
            else:
                print('No checkpoint file found')
                return

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            try:

                num_iter = int(math.ceil(n_test / BATCH_SIZE))
                true_count = 0
                total_sample_count = num_iter * BATCH_SIZE
                step = 0

                while step < num_iter and not coord.should_stop():
                    val_images_, val_labels_ = sess.run(
                        [val_batch, val_label_batch])
                    predictions = sess.run([top_k_op])
                    true_count += np.sum(predictions)
                    step += 1
                    precision = true_count / total_sample_count
                print('precision = %.3f' % precision)
            except Exception as e:
                coord.request_stop(e)
            finally:
                coord.request_stop()
            coord.join(threads)
Ejemplo n.º 5
0
def run_training():
    traindir = 'data/train/'
    logs_train_dir = 'logs/train/'

    train_image, train_label = input_data.get_files(traindir)
    train_batch, train_label_batch = input_data.get_batch(
        train_image, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)

    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.training(train_loss, learning_rate)
    train_acc = model.evaluation(train_logits, train_label_batch)

    sess = tf.Session()
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in range(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])

            if step % 20 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()
    coord.join(threads)
    sess.close()
Ejemplo n.º 6
0
def run_training(N_CLASSES, IMG_W, IMG_H, BATCH_SIZE, MAX_STEP, CAPACITY,
                 model1_data, learning_rate, total):
    train, train_label, randomList = input_data.get_files(model1_data, total)
    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    return train_logits, train_batch, train_label_batch, randomList
Ejemplo n.º 7
0
def conv_feature_extract(txtfile):

    with tf.Graph().as_default():

        batch, label_batch, n = input_data.get_batch(txtfile, IMG_W, IMG_H,
                                                     BATCH_SIZE, CAPACITY)

        x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])

        model = models.model(x, N_CLASSES)
        model.AlexNet()  #### notice
        feature = model.conv1

        saver = tf.train.Saver(tf.global_variables())

        with tf.Session() as sess:

            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(model_dir)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('Loading success, global_step is %s' % global_step)
            else:
                print('No checkpoint file found')
                return

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            try:

                num_iter = int(math.ceil(n / BATCH_SIZE))
                step = 0

                while step < num_iter and not coord.should_stop():

                    images, labels = sess.run([batch, label_batch])

                    feature_map = sess.run([feature], feed_dict={x: images})
                    feature_map = np.array(feature_map)
                    feature_map = np.squeeze(feature_map)

                    if step == 0:
                        tools.print_all_variables()

                    print("batch %d-0: feature map------------------------" %
                          step)
                    tools.plot_feature_map(feature_map)

                    step = step + 1

            except Exception as e:
                coord.request_stop(e)
            finally:
                coord.request_stop()
            coord.join(threads)
Ejemplo n.º 8
0
def run_training():
    # 数据集
    train_dir = r'D:\Python\mnist\test_my\big_0_1_4/'  # My dir--20170727-csq
    # logs_train_dir 存放训练模型的过程的数据,在tensorboard 中查看
    logs_train_dir = r'D:\PyCharm_code\Ai\Tensorflow_mooc_note\6\MinstNew\logs\train/'

    # 获取图片和标签集
    train, train_label = input_data.get_files(train_dir)
    # 生成批次
    train_batch, train_label_batch = input_data.get_batch(train,
                                                          train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE,
                                                          CAPACITY)
    # 进入模型
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    # 获取 loss
    train_loss = model.losses(train_logits, train_label_batch)
    # 训练
    train_op = model.trainning(train_loss, learning_rate)
    # 获取准确率
    train__acc = model.evaluation(train_logits, train_label_batch)
    # 合并 summary
    summary_op = tf.summary.merge_all()
    sess = tf.Session()
    # 保存summary
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            ckpt = tf.train.get_checkpoint_state(logs_train_dir)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 2000 == 0 or (step + 1) == MAX_STEP:
                # 每隔2000步保存一下模型,模型保存在 checkpoint_path 中
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()
    coord.join(threads)
    sess.close()
Ejemplo n.º 9
0
def evaluate_running():

    with tf.Graph().as_default():

        val_batch, val_label_batch, n_test = input_data.get_batch(
            val_txt, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
        x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
        y_ = tf.placeholder(tf.int32, shape=[BATCH_SIZE])

        model = models.model(x, N_CLASSES)
        model.AlexNet()  #### notice
        logits = model.fc3
        top_k_op = tf.nn.in_top_k(logits, y_, 1)

        saver = tf.train.Saver(tf.global_variables())

        with tf.Session() as sess:

            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(model_dir)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('Loading success, global_step is %s' % global_step)
            else:
                print('No checkpoint file found')
                return

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            try:

                num_iter = int(math.ceil(n_test / BATCH_SIZE))
                true_count = 0
                total_sample_count = num_iter * BATCH_SIZE
                step = 0

                while step < num_iter and not coord.should_stop():
                    val_images, val_labels = sess.run(
                        [val_batch, val_label_batch])
                    predictions = sess.run([top_k_op],
                                           feed_dict={
                                               x: val_images,
                                               y_: val_labels
                                           })
                    true_count += np.sum(predictions)
                    step += 1
                precision = true_count / total_sample_count * 100.0
                print('precision = %.2f%%' % precision)
            except Exception as e:
                coord.request_stop(e)
            finally:
                coord.request_stop()
            coord.join(threads)
Ejemplo n.º 10
0
def run_evaluating():

    eval_data, eval_label = input_data.get_files(FLAGS.eval_dir)
    eval_batch, eval_label_batch = input_data.get_batch(
        eval_data, eval_label, FLAGS.height, FLAGS.width, FLAGS.batch_size,
        FLAGS.capacity)

    keep_prob = tf.placeholder(tf.float32)

    hypothesis, cross_entropy, eval_step = model.make_network(
        eval_batch, eval_label_batch, keep_prob)

    cost_sum = tf.summary.scalar("cost_eval", cross_entropy)

    eval_accuracy = tf.nn.in_top_k(hypothesis, eval_label_batch, 1)
    eval_acc = model.evaluation(hypothesis, eval_label_batch)

    saver = tf.train.Saver()

    print('Start Evaluation......')
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        total_sample_count = FLAGS.eval_steps * FLAGS.batch_size
        true_count = 0

        writer = tf.summary.FileWriter(FLAGS.log_dir)
        writer.add_graph(sess.graph)  # Show the graph

        merge_sum = tf.summary.merge_all()

        saver.restore(sess, './CNN_Homework/logs/model.ckpt-36000')

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        for step in np.arange(FLAGS.eval_steps + 1):
            _, summary, eval_loss = sess.run(
                [eval_step, merge_sum, cross_entropy],
                feed_dict={keep_prob: 1.0})
            predictions, accuracy = sess.run([eval_accuracy, eval_acc],
                                             feed_dict={keep_prob: 1.0})
            writer.add_summary(summary, global_step=step)

            true_count = true_count + np.sum(predictions)

            if step % 10 == 0:
                print('step : %d, loss : %f, eval_accuracy : %f' %
                      (step, eval_loss, accuracy * 100))

        coord.request_stop()
        coord.join(threads)

        print('precision : %f' % (true_count / total_sample_count))

        sess.close()
Ejemplo n.º 11
0
def run_training():
    '''

    '''
    '''
    tf.train.Coordinator和tf.train.start_queue_runners貌似都要在sess.run之前使用,不然会无法运行
    try:这些语法貌似是模板,直接使用就好
    '''

    # you need to change the directories to yours.
    train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/'
    logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/'

    train, train_label = input_data.get_files(train_dir)

    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.trainning(train_loss, learning_rate)
    train__acc = model.evaluation(train_logits, train_label_batch)

    summary_op = tf.summary.merge_all()
    sess = tf.Session()
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()  #和下面的queue_runners配合使用,发生错误可以正确关闭线程
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %
                      (step, tra_loss, tra_acc * 100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 2000 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        # When done, ask the threads to stop.
        coord.request_stop()

    # Wait for threads to finish.
    coord.join(threads)
    sess.close()
Ejemplo n.º 12
0
def run_training():
    # you need to change the directories to yours.
    train_dir = 'D:/tensorflow/practicePlus/ResNet/train/'
    # val_dir = 'D:/tensorflow/practicePlus/cats_vs_dogs/test'
    logs_train_dir = 'D:/tensorflow/practicePlus/ResNet/save/'

    train, train_label = input_data.get_files(train_dir)
    # val, val_label = input_data.get_files(val_dir)
    train_batch, train_label_batch = input_data.get_batch(train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    # val_batch, val_label_batch = input_data.get_batch(val, val_label,IMG_W,IMG_H,BATCH_SIZE,CAPACITY)

    # train
    train_logits = model_resnet50.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model_resnet50.losses(train_logits, train_label_batch)
    train_op = model_resnet50.trainning(train_loss, learning_rate)
    train_acc = model_resnet50.evaluation(train_logits, train_label_batch)

    # validation
    # test_logits = model.inference(val_batch,BATCH_SIZE,N_CLASSES)
    # test_loss = model.losses(test_logits, val_label_batch)
    # test_acc = model.evaluation(test_logits, val_label_batch)

    summary_op = tf.summary.merge_all()

    sess = tf.Session()
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    # batch trainning
    try:
        # one step one batch
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])

            # print loss and acc each 10 step, record log and write at same time
            if step % 10 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)
            # save modle each 500 steps
            if ((step == 500) or ((step + 1) == MAX_STEP)):
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')

    finally:
        coord.request_stop()
def run_training():
    # dataset
    train_dir = '/Users/xcliang/PycharmProjects/cats_vs_dogs/data/train/'  # My dir--20170727-csq
    # logs_train_dir  store the data of the process of training model, view in tensorbpard
    logs_train_dir = '/Users/xcliang/PycharmProjects/cats_vs_dogs/data/saveNet'

    # Get images and tag sets
    train, train_label = input_data.get_files(train_dir)
    # Generate batch
    train_batch, train_label_batch = input_data.get_batch(train,
                                                          train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE,
                                                          CAPACITY)
    # Entering the model
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    # Get loss
    train_loss = model.losses(train_logits, train_label_batch)
    # train
    train_op = model.trainning(train_loss, learning_rate)
    # Get accuracy
    train__acc = model.evaluation(train_logits, train_label_batch)
    # merge summary
    summary_op = tf.summary.merge_all()
    sess = tf.Session()
    # save summary
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 2000 == 0 or (step + 1) == MAX_STEP:
                # Save the model every 2000 steps and save the model in checkpoint_path
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()
    coord.join(threads)
    sess.close()
Ejemplo n.º 14
0
def run_training():
    starttime = datetime.datetime.now()
    # you need to change the directories to yours.
    train_dir = 'D:\\Thesis\\sample_patch\\6110_4_0\\*\\*.tif'
    logs_train_dir = 'D:\\Thesis\\classify\\6110_4_0\\logs\\train'

    train_image, train_label= input_data.new_getfiles(train_dir)
    train_image = input_data.read_images(train_image,R_Size)

    train_batch, train_label_batch = input_data.get_batch(train_image,
                                                          train_label,
                                                          BATCH_SIZE,
                                                          CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.trainning(train_loss, learning_rate)
    train__acc = model.evaluation(train_logits, train_label_batch)

    summary_op = tf.summary.merge_all()
    # sess = tf.Session()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config) # allowing dynamic memory growth as follows
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    test = coord.should_stop()
    try:
        for step in np.arange(MAX_STEP):
            if test:
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 1 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' % (step, tra_loss, tra_acc * 100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)


            if step % 100 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()

    # coord.join(threads)
    sess.close()
    endtime=datetime.datetime.now()
    print(endtime-starttime).seconds
Ejemplo n.º 15
0
def evaluate():
    with tf.Graph().as_default():
        
#        log_dir = 'C://Users//kevin//Documents//tensorflow//VGG//logsvgg//train//'
        data_dir = '/home/viplab/Desktop/Syneh/HW01/data/'
        train_log_dir = '/home/viplab/Desktop/Syneh/HW01/code/train_log'
        val_log_dir = '/home/viplab/Desktop/Syneh/HW01/code/train_log/val'
        n_test = 12776
                
        val_image_list, val_label_list = input_data.read_files(data_dir=data_dir,
                                                 is_train=False)
        val_image_batch, val_label_batch = input_data.get_batch( val_image_list,
                                                                 val_label_list,
                                                                 IMG_W,
                                                                 IMG_H,
                                                                 BATCH_SIZE,
                                                                 CAPACITY)

        logits = VGG.VGG16N(val_image_batch, N_CLASSES, IS_PRETRAIN)
        correct = tools.num_correct_prediction(logits, val_label_batch)
        saver = tf.train.Saver(tf.global_variables())
        
        with tf.Session() as sess:
            
            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(train_log_dir)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('Loading success, global_step is %s' % global_step)
            else:
                print('No checkpoint file found')
                return
        
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess = sess, coord = coord)
            
            try:
                print('\nEvaluating......')
                num_step = int(math.floor(n_test / BATCH_SIZE))
                num_sample = num_step*BATCH_SIZE
                step = 0
                total_correct = 0
                while step < num_step and not coord.should_stop():
                    batch_correct = sess.run(correct)
                    total_correct += np.sum(batch_correct)
                    step += 1
                print('Total testing samples: %d' %num_sample)
                print('Total correct predictions: %d' %total_correct)
                print('Average accuracy: %.2f%%' %(100*total_correct/num_sample))
            except Exception as e:
                coord.request_stop(e)
            finally:
                coord.request_stop()
                coord.join(threads)
Ejemplo n.º 16
0
def run_training():

    #train set path
    train_dir = '/raidHDD/experimentData/Dev/Knife/hackthon/upup7/'
    #output model path
    logs_train_dir = '/raidHDD/experimentData/Dev/Knife/hackthon/modelX'
    if removeLogFIle:
        if os.path.exists(logs_train_dir):
            for logFile in os.listdir(logs_train_dir):
                os.remove("{0}/{1}".format(logs_train_dir, logFile))
            print("Delete Log file success...")
    train, train_label = input_data.get_files(train_dir)

    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.trainning(train_loss, learning_rate)
    train__acc = model.evaluation(train_logits, train_label_batch)

    summary_op = tf.summary.merge_all()
    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.8  #0.8 =GPU_memory usage
    sess = tf.Session(config=config)
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %
                      (step, tra_loss, tra_acc * 100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)
                train_writer.flush()
            #only save end model
            if (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 17
0
def run_training():
    train_dir = "/home/sxy/PycharmProjects/defect2/data/train"
    logs_train_dir = "/home/sxy/PycharmProjects/defect2/logs/train-4"
    tf.reset_default_graph()
    train, train_label = input_data.get_files(train_dir)
    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.trainning(train_loss, learning_rate)
    train_acc = model.evaluation(train_logits, train_label_batch)
    #测试准确率
    # test_dir="/home/sxy/PycharmProjects/defect2/data/test"
    # test,test_label=input_data.get_files(test_dir)
    # test_batch,test_label_batch=input_data.get_batch(test,
    #                                                       test_label,
    #                                                       IMG_W,
    #                                                       IMG_H,
    #                                                       BATCH_SIZE,
    #                                                       CAPACITY)
    # test_logits = model.inference(test_batch, BATCH_SIZE, N_CLASSES)
    # train_loss = model.losses(test_logits, test_label_batch)
    # train_op = model.trainning(train_loss, learning_rate)
    # train_acc = model.evaluation(test_logits, test_label_batch)

    summary_op = tf.merge_all_summaries()
    sess = tf.Session()
    train_writer = tf.train.SummaryWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.initialize_all_variables())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])

            if step % 50 == 0:
                print("Step %d, train loss = %.2f, train accuracy = %.2f%%" %
                      (step, tra_loss, tra_acc))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)
            if step % 2000 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, "model.ckpt")
                saver.save(sess, checkpoint_path, global_step=step)
    except tf.errors.OutOfRangeError:
        print("Done training -- epoch limit reached.")
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 18
0
def run_training():

    # you need to change the directories to yours.
    train_dir = './data/train/train/'
    logs_train_dir = './logs/'

    train, train_label = input_data.get_files(train_dir)

    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)

    with tf.name_scope("training"):
        train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
        train_loss = model.losses(train_logits, train_label_batch)
        train_op = model.trainning(train_loss, learning_rate)
        train__acc = model.evaluation(train_logits, train_label_batch)

        summary_op = tf.summary.merge_all()

        sess = tf.Session()
        train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
        saver = tf.train.Saver()

        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        try:
            for step in np.arange(MAX_STEP):
                if coord.should_stop():
                    break
                _, tra_loss, tra_acc = sess.run(
                    [train_op, train_loss, train__acc])

                if step % 20 == 0:
                    print(
                        'Step %d, train loss = %.2f, train accuracy = %.2f%%' %
                        (step, tra_loss, tra_acc * 100.0))
                    summary_str = sess.run(summary_op)
                    train_writer.add_summary(summary_str, step)

                if step % 2000 == 0 or (step + 1) == MAX_STEP:
                    checkpoint_path = os.path.join(logs_train_dir,
                                                   'model.ckpt')
                    saver.save(sess, checkpoint_path,
                               global_step=step)  #保存模型和模型参数到logs_train_dir文件夹

        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')

        finally:
            coord.request_stop()
        coord.join(threads)
        sess.close()
Ejemplo n.º 19
0
def run_training():

    # 调用input_data文件的get_files()函数获得image_list, label_list
    train, train_label = input_data.get_files(train_dir)
    # 获得image_batch, label_batch
    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    # 进行前向训练,获得回归值
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    # 计算获得损失值loss
    train_loss = model.losses(train_logits, train_label_batch)
    # 对损失值进行优化
    train_op = model.trainning(train_loss, learning_rate)
    # 根据计算得到的损失值,计算出分类准确率
    train__acc = model.evaluation(train_logits, train_label_batch)
    # 将图形、训练过程合并在一起
    summary_op = tf.summary.merge_all()
    # 新建会话
    sess = tf.Session()
    # 将训练日志写入到logs_train_dir的文件夹内
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    # 保存变量
    saver = tf.train.Saver()
    # 执行训练过程,初始化变量
    sess.run(tf.global_variables_initializer())
    # 创建一个线程协调器,用来管理之后在Session中启动的所有线程
    coord = tf.train.Coordinator()
    # 启动入队的线程,一般情况下,系统有多少个核,就会启动多少个入队线程(入队具体使用多少个线程在tf.train.batch中定义);
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            # 使用 coord.should_stop()来查询是否应该终止所有线程,当文件队列(queue)中的所有文件都已经读取出列的时候,
            # 会抛出一个 OutofRangeError 的异常,这时候就应该停止Sesson中的所有线程了;
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])
            # 每50步打印一次损失值和准确率
            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %
                      (step, tra_loss, tra_acc * 100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)
            # 每2000步保存一次训练得到的模型
            if step % 2000 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
    # 如果读取到文件队列末尾会抛出此异常
    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()  # 使用coord.request_stop()来发出终止所有线程的命令

    coord.join(threads)  # coord.join(threads)把线程加入主线程,等待threads结束
Ejemplo n.º 20
0
def evaluate_all_image():
    start_time = time.time()
    '''
    Test all image against the saved models and parameters.
    Return global accuracy of test_image_set
    ##############################################
    ##Notice that test image must has label to compare the prediction and real
    ##############################################
    '''
    # you need to change the directories to yours.
    test_dir = '/Users/sherry/Documents/Study/CS 6220/HW01/HW01_20190901/01_cats_vs_dogs/data/outlier_test/'
    N_CLASSES = 2
    print('-------------------------')
    test, test_label = input_data.get_files(test_dir)
    BATCH_SIZE = len(test)
    print('There are %d test images totally..' % BATCH_SIZE)
    print('-------------------------')
    test_batch, test_label_batch = input_data.get_batch(
        test, test_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)

    logits = model.inference(test_batch, BATCH_SIZE, N_CLASSES)
    testloss = model.losses(logits, test_label_batch)
    testacc = model.evaluation(logits, test_label_batch)

    logs_train_dir = '/Users/sherry/Documents/Study/CS 6220/HW01/HW01_20190901/01_cats_vs_dogs/data/logs_dataset1/train/'
    saver = tf.train.Saver()

    with tf.Session() as sess:
        print("Reading checkpoints...")
        ckpt = tf.train.get_checkpoint_state(logs_train_dir)
        if ckpt and ckpt.model_checkpoint_path:
            global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
            saver.restore(sess, ckpt.model_checkpoint_path)
            print('Loading success, global_step is %s' % global_step)
        else:
            print('No checkpoint file found')
        print('-------------------------')
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        test_loss, test_acc = sess.run([testloss, testacc])
        print('The model\'s loss is %.2f' % test_loss)
        correct = int(BATCH_SIZE * test_acc)
        print('Correct : %d' % correct)
        print('Wrong : %d' % (BATCH_SIZE - correct))
        print('The accuracy in test images are %.2f%%' % (test_acc * 100.0))
        print(
            "------------------------ Testing time is: %s seconds -----------------------"
            % (time.time() - start_time))
    coord.request_stop()
    coord.join(threads)
    sess.close()
    print("--- Testing time is: %s seconds ---" % (time.time() - start_time))
Ejemplo n.º 21
0
def test():
    test_dir = '/home/saverio_sun/project/chinese_rec_data/test'
    keep_prob = 1.0
    images, labels = input_data.read_files(test_dir)
    num_test = len(labels)
    max_step = int(num_test / BATCH_SIZE)
    with tf.Graph().as_default():

        test_batch, test_label = input_data.get_batch(images, labels, IMG_W,
                                                      IMG_H, BATCH_SIZE,
                                                      CAPACTIY)
        test_logits = network_model.inference(test_batch, BATCH_SIZE, N_CLASS,
                                              keep_prob)
        test_acc = network_model.evaluation(logits=test_logits,
                                            labels=test_label)

        init = tf.global_variables_initializer()
        saver = tf.train.Saver()
        with tf.Session() as sess:
            sess.run(init)
            print 'reading checkpoint...'
            ckpt = tf.train.get_checkpoint_state(log_dir)
            if ckpt and ckpt.model_checkpoint_path:
                global_stop = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print 'load success'
            else:
                print 'No checkpoint'

            print ':::the total number image is {0},step {1}:::'.format(
                num_test, max_step)

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            acc = 0.0
            try:
                for step in range(max_step):
                    if coord.should_stop():
                        break
                    tra_acc = sess.run(test_acc)
                    print 'the step {0} accuracy is {1}'.format(step, tra_acc)
                    acc += tra_acc
            except tf.errors.OutOfRangeError:
                print 'done'
            finally:
                coord.request_stop()
            result_accuracy = acc / max_step
            print '{0} images , accuracy is {1}'.format(
                num_test, result_accuracy)
            coord.join(threads)
            sess.close()
Ejemplo n.º 22
0
def run_training():
    train_dir = './train/' # 加载数据训练
    logs_train_dir = './save_model/' # 储存训练好的位置

    train, train_label = input_data.get_files(train_dir)

    train_batch, train_label_batch = input_data.get_batch(train,
                                                          train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE,
                                                          CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES,True) # forward pass
    train_loss = model.losses(train_logits, train_label_batch) # 设置损失函数
    train_op = model.trainning(train_loss,learning_rate=) # training
    train__acc = model.evaluation(train_logits, train_label_batch) # 验证正确率

    summary_op = tf.summary.merge_all() # 定义合并变量操作,一次性生成所有摘要数据
    sess = tf.Session() # 初始化会话
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) # TensorBoard的记录
    saver = tf.train.Saver() # 储存模型

    sess.run(tf.global_variables_initializer()) # 所有变量初始化
    coord = tf.train.Coordinator() # 多线程
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    csvfile = open('csv.csv', 'w', newline='')
    writer = csv.writer(csvfile)
    writer.writerow(['name','label'])

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 2000 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 23
0
def train():
    image, label = decode_from_tfrecords('data/train.tfrecords')
    batch_image, batch_label = get_batch(image, label, 96, 227)  #生成测试的样例batch

    #网络连接,训练所有
    net = network()
    inf = net.inference(batch_image)
    loss = net.softmax_entropy_loss(inf, batch_label)
    opti = net.optimize(loss)

    #验证集所用
    test_image, test_label = decode_from_tfrecords('data/val.tfrecords',
                                                   num_epoch=None)
    test_images, test_labels = get_test_batch(test_image,
                                              test_label,
                                              batch_size=96,
                                              crop_size=227)

    test_inf = net.inference(test_images)
    correct_prediction = tf.equal(tf.cast(tf.argmax(test_inf, 1), tf.int32),
                                  test_labels)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    init = tf.global_variables_initializer()

    with tf.Session() as session:
        session.run(init)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        max_iter = 150

        if os.path.exists(os.path.join("model", 'model.ckpt')) is True:
            tf.train.Saver(max_to_keep=None).restore(
                session, os.path.join("model", 'model.ckpt'))
        for l in range(max_iter):

            loss_np, _, label_np, image_np = session.run(
                [loss, opti, batch_label, batch_image])
            #		image_np = session.run([batch_image])
            print image_np.shape

            if l % 5 == 0:
                print "Iteration: " + str(iter) + "; Train loss: ", loss_np
            if l % 20 == 0:
                accuracy_np = session.run([accuracy])
                print '***************test accruacy:', accuracy_np, '*******************'

        tf.train.Saver(max_to_keep=None).save(
            session, os.path.join('model', 'model.ckpt'))

        coord.request_stop()  #queue需要关闭,否则报错
        coord.join(threads)
Ejemplo n.º 24
0
def test():

	global_step=tf.Variable(0,trainable=False)
	image, label=decode_from_tfrecords('data/train.tfrecords')
	batch_image, batch_label=get_batch(image,label,96,227)
	
	net=network()
	inf=net.inference(batch_image)
	loss=net.softmax_entropy_loss(inf,batch_label)
	learning_rate=tf.train.exponential_decay(
		0.001, #初始学习率
		global_step,#用于衰减计算
		10, #decay_step,每10次,lr衰减一次
		0.95, #decay_rate
		staircase=True
	)
#	opti=net.optimize(loss,lr=0.00001)
	opti=tf.train.MomentumOptimizer(learning_rate,0.9).minimize(loss,global_step=global_step)
		
	#验证
	test_image,test_label=decode_from_tfrecords('data/val.tfrecords')
	test_images,test_labels=get_test_batch(test_image,test_label,batch_size=500,crop_size=227)
	test_inf=net.inference(test_images)
	correct_prediction=tf.equal(tf.cast(tf.argmax(test_inf,1),tf.int32),test_labels)
	accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))


	
	init=tf.global_variables_initializer()

	with tf.Session() as session:
		session.run(init)
		coord=tf.train.Coordinator()
		threads=tf.train.start_queue_runners(coord=coord)
		
		if os.path.exists(os.path.join("model",'model.ckpt')) is True:
			tf.train.Saver(max_to_keep=None).restore(session,os.path.join("model",'model.ckpt'))

		for l in range(200):
			_,loss_np = session.run([opti,loss])
			print "Iteration:"+str(l)+";Train loss: ",loss_np
				
			if l%5 == 0:
				accuracy_np=session.run(accuracy)
				print "Accuracy: ", accuracy_np

		
		tf.train.Saver(max_to_keep=None).save(session,os.path.join('model','model.ckpt'))
		coord.request_stop()
		coord.join(threads)
Ejemplo n.º 25
0
def run_training():
    train_dir = "./data/TRAIN/"
    logs_train_dir = "./logs/"

    train, train_label = input_data.get_files(train_dir)

    train_batch, train_label_batch = input_data.get_batch(train,train_label,IMG_W,IMG_H,BATCH_SIZE,CAPACITY)


    train_logits = model.inference(train_batch,BATCH_SIZE,N_CLASSES)
    train_loss = model.losses(train_logits,train_label_batch)
    train_op = model.trainning(train_loss, learning_rate)
    train_acc = model.evaluation(train_logits, train_label_batch)


    summary_op = tf.summary.merge_all()
    sess = tf.Session()

    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc= sess.run([train_op, train_loss, train_acc])

            if step % 100 == 0:
                print('Step:', step, 'train loss:', tra_loss, 'train accuracy:', tra_acc)
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if tra_acc > 0.95 and step>6000:

                checkpoint_path = os.path.join(logs_train_dir, "model")
                saver.save(sess, checkpoint_path, global_step=step)
                print("train success!")
                print('Step:', step, 'train loss:', tra_loss, 'train accuracy:', tra_acc)
                coord.request_stop()
    except tf.errors.OutOfRangeError:
        print("Done training -- epoch limit reached.")
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 26
0
def run_training():
    DIR_PRE = os.getcwd() + '/'
    train_dir = DIR_PRE + 'data/train/'
    logs_train_dir = DIR_PRE + 'logs/train/'
    os.makedirs(train_dir, exist_ok=True)
    os.makedirs(logs_train_dir, exist_ok=True)

    # 获取所有图片文件列表和对应的标签列表
    train, train_label = input_data.get_files(train_dir)

    #
    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.trainning(train_loss, learning_rate)
    train__acc = model.evaluation(train_logits, train_label_batch)

    summary_op = tf.summary.merge_all()
    sess = tf.Session()
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %
                      (step, tra_loss, tra_acc * 100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 2000 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 27
0
def run_training():
    
    # you need to change the directories to yours.
    train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/'
    logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/'
    
    train, train_label = input_data.get_files(train_dir)
    
    train_batch, train_label_batch = input_data.get_batch(train,
                                                          train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE, 
                                                          CAPACITY)      
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)        
    train_op = model.trainning(train_loss, learning_rate)
    train__acc = model.evaluation(train_logits, train_label_batch)
       
    summary_op = tf.summary.merge_all()
    sess = tf.Session()
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()
    
    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    
    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                    break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])
               
            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)
            
            if step % 2000 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
                
    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()
        
    coord.join(threads)
    sess.close()
Ejemplo n.º 28
0
def run_training():

    # you need to change the directories to yours.
    # train_dir = '/home/kevin/tensorflow/hams_vs_hots/data/train/'
    train_dir = 'D:/workspace/uploadPicJudge3Class/train/'
    # logs_train_dir = '/home/kevin/tensorflow/hams_vs_hots/logs/train/'
    logs_train_dir = 'D:/workspace/uploadPicJudge3Class/logs/'

    train, train_label = input_data.get_files(train_dir)

    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.trainning(train_loss, learning_rate)
    train__acc = model.evaluation(train_logits, train_label_batch)

    summary_op = tf.summary.merge_all()
    sess = tf.Session()
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 10 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %
                      (step, tra_loss, tra_acc * 100.0) + '  ' +
                      datetime.datetime.now().strftime('%Y-%m-%d %H_%M_%S'))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 100 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 29
0
def run_training():
    train_dir = '/home/saverio_sun/project/chinese_rec_data/train'
    keep_prob = 0.6
    images, labels = input_data.read_files(train_dir)
    train_batch, train_label = input_data.get_batch(images, labels, IMG_W,
                                                    IMG_H, BATCH_SIZE,
                                                    CAPACTIY)
    train_logits = network_model.inference(train_batch, BATCH_SIZE, N_CLASS,
                                           keep_prob)
    train_loss = network_model.losses(train_logits, train_label)
    train_op = network_model.training(train_loss, learning_rate)
    train_acc = network_model.evaluation(logits=train_logits,
                                         labels=train_label)

    summary_op = tf.summary.merge_all()
    init = tf.global_variables_initializer()
    sess = tf.InteractiveSession()
    train_writer = tf.summary.FileWriter('./log/', sess.graph)
    saver = tf.train.Saver()
    sess.run(init)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    start_step = 0
    if isrestore:
        ckpt = tf.train.latest_checkpoint(log_dir)
        if ckpt:
            saver.restore(sess, ckpt)
            print 'restore from the checkpoint {0}'.format(ckpt)
            start_step += int(ckpt.split('-')[-1])
    print '::: Training Start:::'
    try:
        for step in range(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])
            if step % 100 == 0:
                print 'Step %d,train loss=%.2f, train accuracy=%.2f' % (
                    step, tra_loss, tra_acc)
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, global_step=step)
            if step % 10000 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(log_dir, 'model.ckpt')
                saver.save(sess, save_path=checkpoint_path, global_step=step)
    except tf.errors.OutOfRangeError:
        print 'done'
    finally:
        coord.request_stop()
    coord.join(threads)
    sess.close()
Ejemplo n.º 30
0
def run_training():
    train_dir = 'C://Users/Sizhe/Desktop/CatsvsDogs/data/train/'
    logs_train_dir = 'C://Users/Sizhe/Desktop/CatsvsDogs/data/logs/train/'

    train, train_label = input_data.get_files(train_dir)
    train_batch, train_label_batch = input_data.get_batch(train,
                                               train_label,
                                               image_width,
                                               image_height,
                                               batch_size,
                                               capacity)
    train_logits = model.inference(train_batch, batch_size, n_class)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.training(train_loss, learning_rate)
    train_acc = model.evaluation(train_logits, train_label_batch)

    summary_op = tf.summary.merge_all()
    sess = tf.Session()
    train_writer = tf.summary.FileWriter(logs_train_dir,sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess = sess, coord = coord)

    try:
        for step in np.arange(max_step):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])

### step%50 when training            
            if step%50 == 0:
                print ('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.00))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step%2000 == 0 or step == max_step-1:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step= step)

    except tf.errors.OutOfRangeError:
        print ('Training finished -- epoch limit reached')
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 31
0
def run_training():
    print 'let us begin....'
    train_dir = '../data/train/'
    logs_train_dir = './train/'

    train, train_label = input_data.get_files(train_dir)
    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_H, IMG_W, BATCH_SIZE, CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.trainning(train_loss, lr)
    train_acc = model.evaluation(train_logits, train_label_batch)

    summary_op = tf.summary.merge_all()  #????
    sess = tf.Session()

    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                print 'coord stop!'
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])

            if step % 50 == 0:
                print 'Step: %d, train_loss = %.2f, train_accuracy = %.2f\n' % (
                    step, tra_loss, tra_acc * 100.0)
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 2500 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 32
0
def training():

    train, train_label, val, val_label = input_data.get_files(train_dir, RATIO)
    train_batch, train_label_batch = input_data.get_batch(train,
                                                  train_label,
                                                  IMG_W,
                                                  IMG_H,
                                                  BATCH_SIZE,
                                                  CAPACITY)
    val_batch, val_label_batch = input_data.get_batch(val,
                                                  val_label,
                                                  IMG_W,
                                                  IMG_H,
                                                  BATCH_SIZE,
                                                  CAPACITY)

    logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    loss = model.losses(logits, train_label_batch)
    train_op = model.trainning(loss, learning_rate)
    acc = model.evaluation(logits, train_label_batch)

    x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
    y_ = tf.placeholder(tf.int16, shape=[BATCH_SIZE])


    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess= sess, coord=coord)

        summary_op = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(train_logs_dir, sess.graph)
        val_writer = tf.summary.FileWriter(val_logs_dir, sess.graph)

        try:
            for step in np.arange(MAX_STEP):
                if coord.should_stop():
                        break
                tra_images,tra_labels = sess.run([train_batch, train_label_batch])
                _, tra_loss, tra_acc = sess.run([train_op, loss, acc],
                                                feed_dict={x:tra_images, y_:tra_labels})
                if step % 50 == 0:
                    print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0))
                    summary_str = sess.run(summary_op)
                    train_writer.add_summary(summary_str, step)

                if step % 200 == 0 or (step + 1) == MAX_STEP:
                    val_images, val_labels = sess.run([val_batch, val_label_batch])
                    val_loss, val_acc = sess.run([loss, acc],
                                                 feed_dict={x:val_images, y_:val_labels})
                    print('**  Step %d, val loss = %.2f, val accuracy = %.2f%%  **' %(step, val_loss, val_acc*100.0))
                    summary_str = sess.run(summary_op)
                    val_writer.add_summary(summary_str, step)

                if step % 2000 == 0 or (step + 1) == MAX_STEP:
                    checkpoint_path = os.path.join(train_logs_dir, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)

        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()
        coord.join(threads)
Ejemplo n.º 33
0
def run_training():
    
    # you need to change the directories to yours.
	s_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+YuanTu'
	T_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+X_mid'
	logs_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+'
	s_train, s_train_label = input_data.get_files(s_train_dir)
	s_train_batch, s_train_label_batch = input_data.get_batch(s_train,
                                                          s_train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE, 
                                                          CAPACITY)   
	T_train, T_train_label = input_data.get_files(T_train_dir)
    
	T_train_batch, T_train_label_batch = input_data.get_batch(T_train,
                                                          T_train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE, 
                                                          CAPACITY) 

	train_logits = model.inference(s_train_batch,T_train_batch, BATCH_SIZE, N_CLASSES)
	train_loss = model.losses(train_logits, s_train_label_batch)        
	train_op = model.trainning(train_loss, learning_rate)
	train__acc = model.evaluation(train_logits, s_train_label_batch)
       
	summary_op = tf.summary.merge_all()  #汇总操作
	sess = tf.Session()   #定义sess
	train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) #
	saver = tf.train.Saver()    #保存操作
    
	sess.run(tf.global_variables_initializer())#初始化所有变量
	coord = tf.train.Coordinator() #设置多线程协调器
	threads = tf.train.start_queue_runners(sess=sess, coord=coord) #开始Queue Runners(队列运行器)
    
    #开始训练过程
	try:
		for step in np.arange(MAX_STEP):
			if coord.should_stop():
					break
			_, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) 
               
			if step % 50 == 0:
				print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0))
				#运行汇总操作,写入汇总
				summary_str = sess.run(summary_op)
				train_writer.add_summary(summary_str, step)
            
			if step % 800 == 0 or (step + 1) == MAX_STEP:
				#保存当前模型和权重到 logs_train_dir,global_step为当前迭代次数
				checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
				saver.save(sess, checkpoint_path, global_step=step)
                
	except tf.errors.OutOfRangeError:
		print('Done training -- epoch limit reached')
	finally:
		coord.request_stop()
        
	coord.join(threads)
	sess.close()
Ejemplo n.º 34
0
N_CLASSES = 6
IMG_W = 256  # resize the image, if the input image is too large, training will be very slow.
IMG_H = 256
BATCH_SIZE = 16
CAPACITY = 100000
MAX_STEP = 10000 # with current parameters, it is suggested to use MAX_STEP>10k
learning_rate = 0.0001 # with current parameters, it is suggested to use learning rate<0.0001

s_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+YuanTu'
T_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+/CK+X_mid'
logs_train_dir = '/home/hrz/projects/tensorflow/emotion/ck+'
s_train, s_train_label = input_data.get_files(s_train_dir)
#print(s_train)
s_train_batch, s_train_label_batch = input_data.get_batch(s_train,
                                                          s_train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE, 
                                                          CAPACITY) 
#print(s_train_label_batch)
T_train, T_train_label = input_data.get_files(T_train_dir)
    
T_train_batch, T_train_label_batch = input_data.get_batch(T_train,
                                                          T_train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE, 
                                                          CAPACITY) 
#print(len(s_train),len(s_train_label),len(T_train),len(T_train_label))
#print(s_train_batch,s_train_label_batch,T_train_batch,s_train_label_batch)
train_logits = model.inference(s_train_batch,T_train_batch, BATCH_SIZE, N_CLASSES)
train_loss = model.losses(train_logits, s_train_label_batch)