在TensorFlow中将图片移到右侧

时间:2016-06-27 09:01:58

标签: python neural-network tensorflow

我已经学习了Tensorflow(MNIST),并且我已经将权重保存在.ckpt中。 现在我想在这个权重上测试我的神经网络,将相同的图像转换为几个像素到右边和底部。 加载重量很好,但是当我打印一个eval时,Tensorflow显示总是相同的结果(测试为0.9630),无论翻译是1还是14px。 这是我打印eval函数的代码:

def eval_translation(sess, eval_correct, images_pl, labels_pl, dataset):
    print('Test Data Eval:')
    for i in range(28):
        true_count = 0  # Counts the number of correct predictions.
        steps_per_epoch = dataset.num_examples // FLAGS.batch_size
        nb_exemples = steps_per_epoch * FLAGS.batch_size
        for step in xrange(steps_per_epoch):
            images_feed, labels_feed = dataset.next_batch(FLAGS.batch_size)
            feed_dict = {images_pl: translate_right(images_feed, i), labels_pl: labels_feed}
            true_count += sess.run(eval_correct, feed_dict=feed_dict)
        precision = true_count / nb_exemples
        print('Translation: %d  Num examples: %d  Num correct: %d  Precision @ 1: %0.04f' % (i, nb_exemples, true_count, precision))

这是我加载数据的功能,我打印测试结果。 这是我的翻译功能:

def translate_right(images, dev):
    for i in range(len(images)):
        for j in range(len(images[i])):
            images[i][j] = np.roll(images[i][j], dev)
    return images

我在初始化所有变量之后调用此函数代替学习:

with tf.Graph().as_default():
    # Generate placeholders for the images and labels.
    images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size)

    # Build a Graph that computes predictions from the inference model.
    weights, logits = mnist.inference(images_placeholder, neurons)

    # Add to the Graph the Ops for loss calculation.
    loss = mnist.loss(logits, labels_placeholder)

    # Add to the Graph the Ops that calculate and apply gradients.
    train_op = mnist.training(loss, learning_rate)

    # Add the Op to compare the logits to the labels during evaluation.
    eval_correct = mnist.evaluation(logits, labels_placeholder)

    # Build the summary operation based on the TF collection of Summaries.
    summary_op = tf.merge_all_summaries()

    # Create a saver for writing training checkpoints.
    save = {}
    for i in range(len(weights)):
        save['weights' + str(i)] = weights[i]
    saver = tf.train.Saver(save)

    # Create a session for running Ops on the Graph.
    sess = tf.Session()
    init = tf.initialize_all_variables()
    sess.run(init)

    # load weights
    saver.restore(sess, restore_path)

    # Instantiate a SummaryWriter to output summaries and the Graph.
    summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph)

    temps_total = time.time()

    eval_translation(sess, eval_correct, images_placeholder, labels_placeholder, dataset.test)

我不知道我的代码有什么问题,以及为什么Tensorflow似乎忽略了我的图像。 有人可以帮我吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

您的功能translate_right不起作用,因为images[i, j]只是一个像素(如果您有灰度图像,则包含1个值)。

您应该使用axis的参数np.roll

def translate_right(images, dev):
    return np.roll(images, dev, axis=1)