我如何使用我的 mnist 训练模型来预测图像
How can i use my mnist trained model to predict an image
我是 Tensorflow 的新手。我已经通过这个例子完成了 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')
现在我想用这个模型做预测。我用这些代码行打开并处理图像。
image = cv2.imread("Untitled.jpg")
image = np.multiply(image, 1.0/255.0)
images=tf.reshape(image,[-1,28,28,1])
当我使用这个时:
feed_dict1 = {x: images}
classification = sess.run(y_pred, feed_dict1)
print (classification)
是returns这个错误。
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.
您尝试将 tf-object:
输入到您的占位符中
images = tf.reshape(image,[-1,28,28,1])
但是你不能这样做,因为占位符需要数字,例如 np.array
。所以使用 numpy.reshape
而不是 tf.reshape
。第二个你可以在你的会话中使用。例如,您可以将平面数组输入占位符,然后在会话中创建一个节点,将此数组重塑为二维矩阵。
我是 Tensorflow 的新手。我已经通过这个例子完成了 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')
现在我想用这个模型做预测。我用这些代码行打开并处理图像。
image = cv2.imread("Untitled.jpg")
image = np.multiply(image, 1.0/255.0)
images=tf.reshape(image,[-1,28,28,1])
当我使用这个时:
feed_dict1 = {x: images}
classification = sess.run(y_pred, feed_dict1)
print (classification)
是returns这个错误。
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.
您尝试将 tf-object:
输入到您的占位符中images = tf.reshape(image,[-1,28,28,1])
但是你不能这样做,因为占位符需要数字,例如 np.array
。所以使用 numpy.reshape
而不是 tf.reshape
。第二个你可以在你的会话中使用。例如,您可以将平面数组输入占位符,然后在会话中创建一个节点,将此数组重塑为二维矩阵。