使用tfrecord输入使用自定义CNN创建预测

时间:2018-06-06 19:44:59

标签: tensorflow tfrecord

我的目标是将图像分为十类。我有一个tfrecord文件作为输入。您可以下载它here(30 MB)。我根据答案修改了代码:

$(function() {
  $("#addMore").click(function(e) {
    e.preventDefault();
    $("#fieldList").append("<li>&nbsp;</li>");
    $("#fieldList").append("<li><input type='file' class='form-control topborder' name='postFile[]'></li>");
    $("#fieldList").append("<li><input type='text' class='form-control fixborder' name='postName[]' placeholder='File title / name'></li>");
    $("#fieldList").append("<li><textarea id='image-desc-editor' class='form-control fixborder dynamic-ckeditor-textarea' name='postDesc[]' placeholder='File description'></textarea></li>");
  });
});

不幸的是,我仍然有错误消息:

ValueError:Tensor Tensor(&#34; Softmax:0&#34;,shape =(10,10),dtype = float32)不是此图的元素。

ValueError:Fetch参数不能解释为Tensor。 (Tensor Tensor(&#34; Softmax:0&#34;,shape =(10,10),dtype = float32)不是此图的元素。)

1 个答案:

答案 0 :(得分:2)

问题在于你的训练。您需要使用tf.train.start_queue_runners启动队列,这些队列将运行一些线程来处理和排队示例。创建一个Coordinator并要求队列运行器使用协调器启动其线程。

检查代码更改:

with tf.Session() as sess:
    init_op = [tf.global_variables_initializer(), tf.local_variables_initializer()]
    # Run the init_op, evaluate the model outputs and print the results:
    sess.run(init_op)
    #probabilities = sess.run(probabilities)

    # Create a coordinator, launch the queue runner threads.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    try:
        while not coord.should_stop():
            while True:
                prob = sess.run(probabilities)
                print('Probabilities Shape:')
                print(prob.shape) 

    except tf.errors.OutOfRangeError:
        # When done, ask the threads to stop.
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()
        # Wait for threads to finish.
    coord.join(threads)

    # Save the model
    saver = tf.train.Saver()
    saver.save(sess, './slim_model/custom_model'

输出:

 Probabilities Shape:
 (10, 10)
 Probabilities Shape:
 (10, 10)
 Probabilities Shape:
 (10, 10)
 Probabilities Shape:
 (10, 10)
 Probabilities Shape:
 (10, 10)
Done training -- epoch limit reached

可以从here下载包含上述修复以及保存和恢复模型的代码。