简体   繁体   中英

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. 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.

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. Can I still use the batching method detailed in the link above or should I use feed_dict instead? 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. Conv_nn is simply a class that contains the convolutional neural network detailed in the expert MNIST tutorial. 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.

So if your model had Y labels and X inputs then

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM