[英]How can i use my mnist trained model to predict an image
I'm new to Tensorflow. 我是Tensorflow的新手。 I've done MNIST training by this example 我已经通过此示例完成了MNIST培训
steps = 5000
with tf.Session() as sess:
sess.run(init)
for i in range(steps):
batch_x , batch_y = mnist.train.next_batch(50)
sess.run(train,feed_dict={x:batch_x,y_true:batch_y,hold_prob:0.5})
# PRINT OUT A MESSAGE EVERY 100 STEPS
if i%100 == 0:
print('Currently on step {}'.format(i))
print('Accuracy is:')
# Test the Train Model
matches = tf.equal(tf.argmax(y_pred,1),tf.argmax(y_true,1))
acc = tf.reduce_mean(tf.cast(matches,tf.float32))
print(sess.run(acc,feed_dict=
{x:mnist.test.images,y_true:mnist.test.labels,hold_prob:1.0}))
print('\n')
now I want to do prediction by this model. 现在我想用这个模型做预测。 I open and process the image with these lines of code. 我用这些代码行打开并处理图像。
image = cv2.imread("Untitled.jpg")
image = np.multiply(image, 1.0/255.0)
images=tf.reshape(image,[-1,28,28,1])
when I use this: 当我使用这个:
feed_dict1 = {x: images}
classification = sess.run(y_pred, feed_dict1)
print (classification)
It returns this error. 它返回此错误。
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.
You try to feed into your placeholder the tf-object: 您尝试将tf对象输入占位符:
images = tf.reshape(image,[-1,28,28,1])
but you cannot do that since placeholder expects number for example np.array
. 但是您不能这样做,因为占位符需要数字,例如np.array
。 So use numpy.reshape
instead of tf.reshape
. 因此,请使用numpy.reshape
而不是tf.reshape
。 Second one you can use inside of your session. 您可以在会话中使用第二个。 For example you can feed flat arrays into placeholder and then create a node inside of your session that reshape this array into 2D matrix. 例如,您可以将平面数组输入占位符,然后在会话内部创建一个节点,以将该数组重塑为2D矩阵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.