繁体   English   中英

如何在 Tensorflow 中对经过训练的模型进行简单的预测?

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

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

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

这取决于您如何定义图形,也取决于您如何定义“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})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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