[英]Tensorflow - Using batching to make predictions
I'm attempting to make predictions using a trained convolutional neural network, slightly modified from the example in the example expert tensorflow tutorial. 我正在尝试使用经过训练的卷积神经网络进行预测,该网络已从示例专家tensorflow教程中的示例进行了一些修改。 I have followed the instructions at https://www.tensorflow.org/versions/master/how_tos/reading_data/index.html to read data from a CSV file. 我已按照https://www.tensorflow.org/versions/master/how_tos/reading_data/index.html上的说明从CSV文件读取数据。
I have trained the model and evaluated its accuracy. 我已经训练了模型并评估了其准确性。 I then saved the model and loaded it into a new python script for making predictions. 然后,我保存了模型并将其加载到新的python脚本中以进行预测。 Can I still use the batching method detailed in the link above or should I use feed_dict
instead? 我是否仍可以使用上面链接中详细说明的批处理方法,还是应该改用feed_dict
? Most tutorials I've seen online use the latter. 我在网上看到的大多数教程都使用后者。
My code is shown below, I have essentially duplicated the code for reading from my training data, which was stored as lines within a single .csv file. 我的代码如下所示,从本质上讲,我已经复制了用于从训练数据中读取的代码,这些数据存储为单个.csv文件中的行。 Conv_nn is simply a class that contains the convolutional neural network detailed in the expert MNIST tutorial. Conv_nn只是一个类,其中包含专家MNIST教程中详细介绍的卷积神经网络。 Most of the content is probably not very useful except for the part where I run the graph. 除了我运行图形的那一部分外,大多数内容可能不是很有用。
I suspect I have badly mixed up training and prediction - I'm not sure if the test images are being fed to the prediction operation correctly or if it is valid to use the same batch operations for both datasets. 我怀疑我在训练和预测方面混合得很差-我不确定测试图像是否正确地馈送到了预测操作,或者对两个数据集使用相同的批处理操作是否有效。
filename_queue = tf.train.string_input_producer(["data/test.csv"],num_epochs=None)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Defaults force key value and label to int, all others to float.
record_defaults = [[1]]+[[46]]+[[1.0] for i in range(436)]
# Reads in a single row from the CSV and outputs a list of scalars.
csv_list = tf.decode_csv(value, record_defaults=record_defaults)
# Packs the different columns into separate feature tensors.
location = tf.pack(csv_list[2:4])
bbox = tf.pack(csv_list[5:8])
pix_feats = tf.pack(csv_list[9:])
onehot = tf.one_hot(csv_list[1], depth=98)
keep_prob = 0.5
# Creates batches of images and labels.
image_batch, label_batch = tf.train.shuffle_batch(
[pix_feats, onehot],
batch_size=50,num_threads=4,capacity=50000,min_after_dequeue=10000)
# Creates a graph of variables and operation nodes.
nn = Conv_nn(x=image_batch,keep_prob=keep_prob,pixels=33*13,outputs=98)
# Launch the default graph.
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
saver.restore(sess, 'model1.ckpt')
print("Model restored.")
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord)
prediction=tf.argmax(nn.y_conv,1)
pred = sess.run([prediction])
coord.request_stop()
coord.join(threads)
This question is old, but I am going to answer anyway, as it has been viewed nearly 1000 times. 这个问题很旧,但是无论如何我都要回答,因为它已经被浏览了近1000次。
So if your model had Y labels and X inputs then 因此,如果您的模型具有Y标签和X输入,则
prediction=tf.argmax(Y,1)
result = prediction.eval(feed_dict={X: [data]}, session=sess)
This evaluates a single input, for example a single mnist image, but it can be a batch. 这将评估单个输入,例如单个mnist图像,但它可以是一个批处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.