Ejemplo n.º 1
0
def test_get_batch_data():
    import time
    a = next(get_batch_data())
    images = a['images']
    groundtruth_texts = a['groundtruth_texts']
    for image, groundtruth_text in zip(images, groundtruth_texts):
        plt.imshow(image)
        print(groundtruth_text)
        plt.show()
        plt.clf()
Ejemplo n.º 2
0
def main():
    batch_size = 32
    images_placeholder = tf.placeholder(shape=[None, 100, 100, 3],
                                        dtype=tf.float32)
    labels_placeholder = tf.placeholder(shape=[None], dtype=tf.int64)
    output_tensor_dict = classfier(images_placeholder)
    logits_tensor, preds_tensor = output_tensor_dict[
        'logits'], output_tensor_dict['preds']
    loss_tensor = tf.reduce_mean(
        tf.losses.sparse_softmax_cross_entropy(
            logits=logits_tensor,
            labels=labels_placeholder,
        ))
    train_op = tf.train.AdadeltaOptimizer().minimize(loss_tensor)
    batch_tensor_dict = input_data.get_batch_data()

    sess = tf.Session()

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    sess.run(init_op)

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

    try:
        for step in range(1, 100000):
            if coord.should_stop():
                break

            batch_dict = sess.run(batch_tensor_dict)
            feed_dict = {
                images_placeholder: batch_dict['images'],
                labels_placeholder:
                batch_dict['groundtruth_text'].astype(np.int64)
            }
            _, loss = sess.run([train_op, loss_tensor], feed_dict=feed_dict)

            if step % 100 == 0:
                print('step {} loss {}'.format(step, loss))
                preds = sess.run(preds_tensor, feed_dict=feed_dict)
                labels = batch_dict['groundtruth_text']
                print('preds\n', preds[:10])
                print('labels\n', labels[:10])
    except tf.errors.OutOfRangeError:
        print('All finished')
    finally:
        coord.request_stop()
        coord.join(threads)

    sess.close()
Ejemplo n.º 3
0
def evaluation():
    save_path = tf.train.latest_checkpoint(FLAGS.exp_dir)
    meta_file_path = save_path + '.meta'
    tf.reset_default_graph()
    saver = tf.train.import_meta_graph(meta_file_path)

    sess = tf.Session()
    sess = tfdbg.LocalCLIDebugWrapperSession(sess) if FLAGS.debug else sess
    sess.run(get_init_op())
    saver.restore(sess, save_path=save_path)  # restore sess

    graph = tf.get_default_graph()
    global_step = graph.get_tensor_by_name('global_step:0')
    image_placeholder = graph.get_tensor_by_name('input/Placeholder:0')
    groundtruth_placeholder = graph.get_tensor_by_name('input/Placeholder_1:0')
    output_eval_text_tensor = graph.get_tensor_by_name(
        'attention_decoder/ReduceJoin_1:0')
    output_train_text_tensor = graph.get_tensor_by_name(
        'attention_decoder/ReduceJoin:0')
    print('Restore graph from meta file {}'.format(meta_file_path))
    print('Restore model from {} successful, step {}'.format(
        save_path, sess.run(global_step)))

    batch_generator = get_batch_data(FLAGS.batch_size)
    for step in range(1, FLAGS.run_steps):
        batch_dict = next(batch_generator)
        images = batch_dict['images']
        groundtruth_texts = batch_dict['groundtruth_texts']
        print('generator {}'.format(len(images)))
        feed_eval = {image_placeholder: images}
        feed_train = {
            image_placeholder: images,
            groundtruth_placeholder: groundtruth_texts
        }

        eval_text = sess.run(output_eval_text_tensor, feed_eval)
        train_text = sess.run(output_train_text_tensor, feed_train)
        print('==STEP_{}=='.format(step))
        print('eval_text\n', eval_text)
        print('train_text\n', train_text)
        print('groundtruth_text\n', groundtruth_texts)
        print()
        print()
    sess.close()
Ejemplo n.º 4
0
def main(unused_argv):
    if FLAGS.exp_dir:
        checkpoint_dir = os.path.join(FLAGS.exp_dir, 'model.ckpt')
        train_log_write_dir = os.path.join(FLAGS.exp_dir, 'log/train')

    global_step = tf.Variable(0, name='global_step', trainable=False)
    with tf.name_scope('input'):
        image_placeholder = tf.placeholder(shape=[None, 100, 100, 3],
                                           dtype=tf.float32)
        groundtruth_text_placeholder = tf.placeholder(shape=[
            None,
        ],
                                                      dtype=tf.string)
        tf.summary.image('input_image', image_placeholder, FLAGS.batch_size)
    print('image_placeholder', image_placeholder)
    print('groundtruth_placeholder', groundtruth_text_placeholder)

    output_tensor_dict, eval_output_tensor_dict = inference(
        image_placeholder, groundtruth_text_placeholder, FLAGS.single_seq)
    loss_tensor = output_tensor_dict['loss']
    output_labels_tensor = output_tensor_dict['labels']
    output_predict_text_tensor = output_tensor_dict['predict_text']
    print('output_predict_text_tensor', output_predict_text_tensor)
    probabilities_tensor = output_tensor_dict['probabilities']

    output_eval_text_tensor = eval_output_tensor_dict[
        'predict_text']  # For EVAL
    print('output_eval_text_tensor', output_eval_text_tensor)

    train_op = get_train_op(loss_tensor, global_step)
    batch_tensor_dict = get_batch_data(FLAGS.tfrecord_file_path,
                                       mode='train',
                                       batch_size=FLAGS.batch_size)

    decoder_inputs_tensor = tf.get_default_graph().get_tensor_by_name(
        "attention_decoder/concat:0")
    decoder_targets_tensor = tf.get_default_graph().get_tensor_by_name(
        "attention_decoder/concat_1:0")

    sess = tf.Session()
    train_writer = tf.summary.FileWriter(train_log_write_dir, sess.graph)
    summary_merge_tensor = tf.summary.merge_all()
    sess.run(get_init_op())

    total_loss = 0.0
    begin_step = 0
    saver = tf.train.Saver()

    if os.path.exists(os.path.join(FLAGS.exp_dir,
                                   'checkpoint')) and FLAGS.restore:
        save_path = tf.train.latest_checkpoint(FLAGS.exp_dir)
        saver.restore(sess, save_path=save_path)
        begin_step = sess.run(global_step)
        print(
            'Restore model from {} successful, continue training from step {}'.
            format(save_path, begin_step))

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

    try:
        for step in range(begin_step, FLAGS.max_steps):
            if coord.should_stop():
                break
            batch_dict = sess.run(batch_tensor_dict)
            images = batch_dict['images']
            groundtruth_text = np.char.lower(
                batch_dict['groundtruth_text'].astype('str'))
            feed_dict = {
                image_placeholder: images,
                groundtruth_text_placeholder: groundtruth_text
            }
            _, loss = sess.run([train_op, loss_tensor], feed_dict=feed_dict)
            total_loss += loss

            if step % 100 == 0:
                summary, output_labels, output_predict_text, decoder_inputs, decoder_targets = sess.run(
                    [
                        summary_merge_tensor, output_labels_tensor,
                        output_predict_text_tensor, decoder_inputs_tensor,
                        decoder_targets_tensor
                    ],
                    feed_dict=feed_dict)
                probabilities = sess.run(probabilities_tensor, feed_dict)
                eval_text = sess.run(output_eval_text_tensor,
                                     feed_dict={image_placeholder: images})
                train_writer.add_summary(summary, step)

                print('Step {}, loss {}'.format(step, total_loss / 100))
                print('out_labels\n', output_labels[:5])
                print('predict_text\n', output_predict_text[:5])
                print('probabilities\n', probabilities[:5])

                print('groundtruth_text\n', groundtruth_text[:5])
                print('decoder_inputs\n', decoder_inputs[:5])
                print('decoder_targets\n', decoder_targets[:5])
                print('eval_text\n', eval_text[:5])
                sample_image = images[:1]
                print(
                    'Use a sample: ',
                    sess.run(output_eval_text_tensor,
                             feed_dict={image_placeholder: sample_image}))
                print()
                print()
                total_loss = 0.0

            if step % 1000 == 0:
                saver.save(sess,
                           save_path=checkpoint_dir,
                           global_step=global_step)
                print('Write checkpoint {}'.format(sess.run(global_step)))

    except tf.errors.OutOfRangeError:
        print('All finished')
    finally:
        coord.request_stop()
        coord.join(threads)
    sess.close()