如何在受训模型上对Tensorflow进行简单预测?

时间:2017-01-08 20:35:23

标签: python tensorflow

我刚训练过这样的模型:

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")

现在我已经加载了这样的图像:img1 = img.imread('./images_32x32/test_1.png')

现在我唯一想做的就是根据img1进行预测。

我该怎么做?

更新

添加了我的softmax功能:

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()

1 个答案:

答案 0 :(得分:5)

这取决于您如何定义图表,并取决于您如何定义'x'占位符的形状 假设'x'的定义如下:

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

假设'pred'是给你预测的张量,你只需要评估这个张量:

predictions = sess.run(pred, feed_dict={x: img1})