Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a simple prediction in Tensorflow on a trained model?

I have just trained a model like this:

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    num_examples = len(X_train)

    print("W00T IT IS TRAINING 😊")
    print()
    for i in range(EPOCHS):
        X_train, y_train = shuffle(X_train, y_train)
        for offset in range(0, num_examples, BATCH_SIZE):
            end = offset + BATCH_SIZE
            batch_x, batch_y = X_train[offset:end], y_train[offset:end]
            sess.run(training_operation, feed_dict={x: batch_x, y: batch_y})

        validation_accuracy = evaluate(X_validation, y_validation)
        print("EPOCH {} ...".format(i+1))
        print("Validation Accuracy = {:.3f}".format(validation_accuracy))
        print()

saver.save(sess, 'LeNet')
print("Model saved")

Now I have loaded an image like this: img1 = img.imread('./images_32x32/test_1.png')

Now the only thing I would like to do is to make a prediction based on img1.

How do I do this?

UPDATE

Added my softmax function:

logits = LeNet(x)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits, one_hot_y)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
training_operation = optimizer.minimize(loss_operation)

correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()
like image 507
Bob van Luijt Avatar asked Oct 27 '25 05:10

Bob van Luijt


1 Answers

That depends on how you defined your graph, and depends on how you defined the shape of the 'x' placeholder.
Supposing 'x' is defined like this:

x = tf.placeholder(shape=[None, IMG_WIDTH, IMG_HEIGHT, NUM_COLOR_CHANNELS], dtype=tf.float32)

And supposing 'pred' is the tensor that that gives you the predictions, you just need to evaluate this tensor:

predictions = sess.run(pred, feed_dict={x: img1})
like image 76
lgvaz Avatar answered Oct 29 '25 20:10

lgvaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!