如何在训练好的Tensorflow模型上进行简单预测?

3

我刚刚训练了一个类似这样的模型:

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个回答

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

谢谢,我还不确定(理解)你的意思。所以我正在使用:x = tf.placeholder(tf.float32, (None, 32, 32, 3))y = tf.placeholder(tf.int32, (None))。我不确定在这种情况下pred会是什么。 - Bob van Luijt
2
Pred 可能是您网络的最后一层,它应该是您应用 softmax 函数的张量。 - lgvaz
啊哈,更新了问题...那应该是correct_prediction吗?试图完全理解这最后一部分。我在softmax方面缺少一些东西... - Bob van Luijt
非常抱歉,logits并不是一个标量值,而是一组标量值的向量。当您应用softmax函数后,会得到一组概率的向量。 - lgvaz
以下是有关编程的内容,从英语翻译成中文。仅返回已翻译的文本:https://dev59.com/pVsX5IYBdhLWcg3whvwn#33713962 - 用简单的回归模型进行解释 - aqavi_paracha
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接