2

我是 TensorFlow 的新手,对读取数据的机制有些迷惑。我在 mnist 数据上设置了一个 TensorFlow 图,但我想对其进行修改,以便我可以运行一个程序来训练它+保存模型,然后运行另一个程序来加载所述图、进行预测和计算测试准确性.

我感到困惑的地方是如何绕过训练图中的原始 I/O 系统并“注入”图像以进行预测或测试数据的(图像,标签)元组以进行准确性测试。要读取训练数据,我使用以下代码:

_, input_data = util.read_examples(
    paths_to_files,
    batch_size,
    shuffle=shuffle,
    num_epochs=None)

feature_map = {
    'label': tf.FixedLenFeature(
    shape=[], dtype=tf.int64, default_value=[-1]),
    'image': tf.FixedLenFeature(
    shape=[NUM_PIXELS * NUM_PIXELS], dtype=tf.int64),
}
example = tf.parse_example(input_data, features=feature_map)

然后我将示例提供给卷积层等并生成输出。

现在想象一下,我使用指定输入的代码来训练我的图表,保存图表和权重,然后在另一个脚本中恢复图表和权重以进行预测——我想拍摄(比如说)10 张图像并将它们提供给生成预测的图表。我如何“注入”这 10 张图像,以便预测从另一端出来?

我玩过提要字典和占位符,但我不确定它们是否适合我使用......似乎它们依赖于内存中的数据,而不是从测试数据队列中读取, 例如。

谢谢!

4

1 回答 1

0

A feed dictionary with placeholders would make sense if you wanted to perform a small number of inferences/evaluations (i.e. enough to fit in memory) - e.g. if you were serving a simple model or running small eval loops.

If you specifically want to infer or evaluate large batches then you should use the same approach you've used for training, but with a different path to your test/eval/live data. e.g.

_, eval_data = util.read_examples(
    paths_to_files,  # CHANGE THIS BIT
    batch_size,
    shuffle=shuffle,
    num_epochs=None)

You can use this as a normal python variable and set up successive, dependent steps to use this as a provided variable. e.g.

def get_example(data):
    return tf.parse_example(data, features=feature_map)

sess.run([get_example(path_to_your_data)])
于 2016-10-18T20:07:08.873 回答