【问题标题】:Passing Input Pipeline to TensorFlow Estimator将输入管道传递给 TensorFlow Estimator
【发布时间】:2017-06-28 12:42:49
【问题描述】:

我是 TF 的菜鸟,所以放轻松。

我必须从带有标签的目录中的一堆图像中训练一个简单的 CNN。看了很多遍后,我编写了这段准备 TF 输入管道的代码,并且能够打印图像数组。

    image_list, label_list = load_dataset()

    imagesq = ops.convert_to_tensor(image_list, dtype=dtypes.string)
    labelsq = ops.convert_to_tensor(label_list, dtype=dtypes.int32)

    # Makes an input queue
    input_q = tf.train.slice_input_producer([imagesq, labelsq],
                                                shuffle=True)

    file_content = tf.read_file(input_q[0])
    train_image = tf.image.decode_png(file_content,channels=3)
    train_label = input_q[1]

    train_image.set_shape([120,120,3])

    # collect batches of images before processing
    train_image_batch, train_label_batch = tf.train.batch(
        [train_image, train_label],
        batch_size=5
        # ,num_threads=1
    )

    with tf.Session() as sess:
        # initialize the variables
        sess.run(tf.global_variables_initializer())
        # initialize the queue threads to start to shovel data
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        # print "from the train set:"
        for i in range(len(image_list)):
             print sess.run(train_image_batch)
        # sess.run(train_image)
        # sess.run(train_label)
        # classifier.fit(input_fn=lambda: (train_image, train_label),
        #                steps=100,
        #                monitors=[logging_hook])

        # stop our queue threads and properly close the session
        coord.request_stop()
        coord.join(threads)
        sess.close()

但是查看 TF 文档中给出的 MNIST 示例,我看到他们使用 cnn_model_fnEstimator 类。

我已经定义了自己的 cnn_model_fn,并希望将两者结合起来。请帮助我了解如何继续前进。此代码不起作用

classifier = learn.Estimator(model_fn=cnn_model_fn, model_dir='./test_model')
classifier.fit(input_fn=lambda: (train_image, train_label),
steps=100,
monitors=[logging_hook])

似乎只有在会话运行时才填充管道,否则它是空的并且它给出一个 ValueError '输入图和层图不一样'

请帮帮我。

【问题讨论】:

    标签: python tensorflow conv-neural-network


    【解决方案1】:

    我自己是 tensorflow 的新手,所以对此持保留态度。

    AFAICT,当您调用任何创建“张量”或“操作”的 tf API 时,它们会被创建到称为 Graph 的上下文中。

    此外,我相信当Estimator 运行时,它会为每次运行创建一个新的空Graph。它通过运行model_fninput_fn 来填充Graph,这两个API 应该调用tf 在这个新Graph 的上下文中添加“张量”和“操作”的API。

    model_fninput_fn 的返回值只是提供参考,以便可以正确连接部件 - Graph 已经包含它们。

    然而,在这个例子中,输入操作已经在Estimator 创建Graph 之前创建,因此它们的相关操作已添加到隐式默认Graph(我相信是自动创建的)。因此,当Estimator 创建一个新模型并使用model_fn 填充模型时,输入和模型将位于两个不同的图上。

    要解决此问题,您需要更改 input_fn。不要只是将 (image, labels) 对包装到 lambda 中,而是将输入的整个构造包装到一个函数中,这样当 Estimator 运行 input_fn 作为所有 API 调用的副作用时,所有输入操作和张量将在正确的 Graph 上下文中创建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      相关资源
      最近更新 更多