【问题标题】:Shift images to the right in TensorFlow在 TensorFlow 中向右移动图像
【发布时间】:2016-06-27 09:01:57
【问题描述】:

我学习了 Tensorflow (MNIST),并将权重保存在 .ckpt 中。 现在我想在这个权重上测试我的神经网络,将相同的图像翻译成右侧和底部的几个像素。 加载重量效果很好,但是当我打印一个 eval 时,Tensorflow 总是显示相同的结果(测试为 0.9630),无论翻译是大约 1 像素还是 14 像素。 这是我打印 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 似乎忽略了我的图像。 有人可以帮我吗? 谢谢!

【问题讨论】:

    标签: python neural-network tensorflow


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢!我认为我的翻译工作(我已经显示了图像并看到了翻译),但是使用轴 arg 它工作得很好,并且 Tensorflow 打印了正确的值!
    猜你喜欢
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多