Exemplo n.º 1
0
def test():
    input_data = tf.placeholder(tf.int32, [batchSize, maxSeqLength])
    labels = tf.placeholder(tf.int32, [batchSize])

    data = tf.nn.embedding_lookup(wordVectors, input_data)
    data = tf.reshape(data, [batchSize, maxSeqLength, numDimensions, 1])

    train_logits = CNN_model.inference1(data, batchSize, N_CLASSES)
    train_loss = CNN_model.losses(train_logits, labels)
    train_acc = CNN_model.evaluation(train_logits, labels)

    all_accuracy = 0
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, './models/pretrained_CNN.ckpt-20000')

        for i in range(2000):
            train_batch, train_label_batch = input_text_data.get_allTest(i)
            tra_loss, tra_acc = sess.run([train_loss, train_acc], {
                input_data: train_batch,
                labels: train_label_batch
            })
            all_accuracy = all_accuracy + tra_acc
        print('All_accuracy:')
        print(all_accuracy / 2000)
Exemplo n.º 2
0
def training():
    input_data = tf.placeholder(tf.int32, [batchSize, maxSeqLength])
    labels = tf.placeholder(tf.int32, [batchSize])

    data = tf.nn.embedding_lookup(wordVectors, input_data)
    data = tf.reshape(data, [batchSize, maxSeqLength, numDimensions, 1])

    train_logits = CNN_model.inference1(data, batchSize, N_CLASSES)
    train_loss = CNN_model.losses(train_logits, labels)
    train_op = CNN_model.trainning(train_loss, learning_rate)
    train_acc = CNN_model.evaluation(train_logits, labels)

    sess = tf.InteractiveSession()
    tf.summary.scalar('Loss', train_loss)
    tf.summary.scalar('Accuracy', train_acc)
    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter(logs_train_dir, sess.graph)

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1)
    config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options)
    with tf.Session(config=config) as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())

        for i in range(MAX_STEP):
            train_batch, train_label_batch = input_text_data.getTrainBatch()
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc],
                                            {
                                                input_data: train_batch,
                                                labels: train_label_batch
                                            })

            # 汇总到Tensorboard
            if i % 1000 == 0:
                print("Step %d, train loss = %.2f, train accuracy = %.2f%%" %
                      (i, tra_loss, tra_acc))

                test_batch, test_label_batch = input_text_data.getTestBatch()
                summary = sess.run(merged, {
                    input_data: test_batch,
                    labels: test_label_batch
                })
                writer.add_summary(summary, i)
                test_loss, test_acc = sess.run([train_loss, train_acc], {
                    input_data: test_batch,
                    labels: test_label_batch
                })
                print(
                    "*************, test loss = %.2f, test accuracy = %.2f%%" %
                    (test_loss, test_acc))

            if i % 2000 == 0 or (i + 1) == MAX_STEP:
                save_path = saver.save(sess,
                                       "./models/pretrained_CNN.ckpt",
                                       global_step=i)
                print("saved to %s" % save_path)
        writer.close()
Exemplo n.º 3
0
def run_training():
    #目录
    train_dir="./image/"
    logs_train_dir ="./log"

    train,train_label=image_P.get_files(train_dir)
    train_batch,train_label_batch=image_P.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#线程停止
            _,temp_loss,temp_acc=sess.run([train_op,train_loss,train_acc])
            
            #每迭代50次打印一次结果
            if step%50 == 0:
                print('Step %d,train loss = %.2f,train accuracy = %.2f'%(step,temp_loss,temp_acc))
                summary_str=sess.run(summary_op)
                train_writer.add_summary(summary_str,step)
            
            #每迭代200次或到达最后一次保存一次模型
            if step%200 == 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('Failed!')
    finally:
        coord.request_stop()

    #结束线程
    coord.join(threads)

    sess.close()
Exemplo n.º 4
0
def run_training():
    # log dir
    logs_train_dir = logs_dir

    # read data
    test, train, validate, test_label, train_label, validate_label = rd.read_file(
    )

    # get batch
    train_batch, train_label_batch = rd.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()
Exemplo n.º 5
0
def evaluate_all_image():
    '''
    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 = '/home/kevin/tensorflow/cats_vs_dogs/data/test/'
    # N_CLASSES = 2
    print('-------------------------')
    test, train, validate, test_label, train_label, validate_label = rd.read_file(
    )
    BATCH_SIZE = len(test)
    print('There are %d test images totally..' % BATCH_SIZE)
    print('-------------------------')
    test_batch, test_label_batch = rd.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 = logs_dir
    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))
    coord.request_stop()
    coord.join(threads)
    sess.close()
def run_training():
    train_dir = "./image/"
    logs_train_dir = "./log_" + str(MAX_STEP) + "_cap_" + str(CAPACITY)

    train, train_label = image_P.get_files(train_dir)
    train_batch, train_label_batch = image_P.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(allow_soft_placement=True,
                            gpu_options=tf.GPUOptions(
                                per_process_gpu_memory_fraction=0.7,
                                allow_growth=True))

    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())

    tf.train.start_queue_runners(sess=sess)

    for step in np.arange(9999999):
        sess.run([train_op, train_loss, train_acc])

        print(step)

        if step == MAX_STEP:
            print("Finished")
            break

    log_dir = train_dir

    images_dir = './image/'

    images_cat = open("imagelist.txt")
    #保存所有图像经过模型计算之后的数组
    images_tested = []

    num_photos = 0
    outfile = open("test-new-" + str(MAX_STEP) + "-" + str(CAPACITY) + ".txt",
                   "w")

    filelines = images_cat.readlines()
    for line in filelines:
        image_name = line.strip('\n')
        image_array = get_one_image(images_dir + image_name)
        image_array = np.reshape(image_array, [1, 300, 400, 3])

        xName = tf.placeholder(tf.float32, shape=[1, 300, 400, 3])
        prediction = sess.run(train_logits, feed_dict={xName: image_array})
        prediction = np.array(prediction, dtype='float32')
        images_tested.append([image_name, prediction])

        num_photos += 1
        print("Test:" + str(num_photos))

        outfile.writelines(image_name)
        t = str(prediction)
        outfile.writelines(t)
        outfile.close()
        outfile = open(
            "test-new-" + str(MAX_STEP) + "-" + str(CAPACITY) + ".txt", "a")

    outfile.close()
    outfile2 = open(
        "nearesttest-" + str(MAX_STEP) + "-" + str(CAPACITY) + ".txt", "w")
    outfile2.write("result = {\n")
    outfile2.close()
    num_photos = 0
    for line in filelines:
        num_photos += 1
        print("Find Near:" + str(num_photos))
        image_name = line.strip('\n')
        image_array = get_one_image(images_dir + image_name)
        image_array = np.reshape(image_array, [1, 300, 400, 3])
        outfile2 = open(
            "nearesttest-" + str(MAX_STEP) + "-" + str(CAPACITY) + ".txt", "a")
        outfile2.write("'" + image_name + "': [\n")
        xName = tf.placeholder(tf.float32, shape=[1, 300, 400, 3])
        prediction = sess.run(train_logits, feed_dict={xName: image_array})
        prediction = np.array(prediction, dtype='float32')

        test_result = []
        for sample in images_tested:
            distance = np.sqrt(np.sum(np.square(sample[1] - prediction)))
            distance.astype('float32')
            test_result.append([sample[0], distance])

        #将结果排序
        test_result = np.array(test_result)
        test_result = test_result[np.lexsort(test_result.T)]
        for i in range(11):
            outfile2.write("'" + test_result[i][0] + "', ")
        outfile2.write("],\n")
        outfile2.close()

    outfile2 = open(
        "nearesttest-" + str(MAX_STEP) + "-" + str(CAPACITY) + ".txt", "a")
    outfile2.write("}\n")
    outfile2.close()

    sess.close()