首页 > 解决方案 > Tensorflow 以与输入相同的顺序获取输入

问题描述

我正在使用 tensorflow 在测试图像上测试我训练有素的模型。我将图像提供给 tensorflow,如下所示:

image_ab, image_aba = sess.run(fetches, feed_dict={self.image_a: image_a,
                                               self.is_train: False})

我打印了image_aandimage_ab并观察到它image_a与我给出的输入图像的顺序不同。

由于某些原因,我希望输出也与输入图像的顺序相同。

tensorflow 通常以与给定输入相同的顺序接受输入吗?

标签: tensorflow

解决方案


我假设你的意思image_ab是不一样的顺序。因为image_a是您提供给 tensorflow 的输入。如果此输入未正确排序,它将是您的预处理,而不是 tensorflow。

TensorFlow 通常适用于批量数据。对于图像,批量尺寸的约定是:

[batch, x, y, colors]

tensorflow 执行的操作是沿着批处理并行化的。如果您只是将卷积层插入在一起,则应保留批处理的顺序。

但是,肯定可以在 tensorflow 中重新排序:

import numpy as np
import tensorflow as tf

x = tf.placeholder(shape=(2,1), dtype="float32")
y = tf.concat([x[1], x[0]], axis=0)

sess = tf.Session()
sess.run([x,y], feed_dict={x:np.random.rand(2,1)})

此代码将读取 x,更改其条目的顺序并生成 y。所以 tensorflow 可以重新排序你的图像。您可以在代码中搜索类似于我示例中的模式。


推荐阅读