简体   繁体   English

我如何使用经过mnist训练的模型来预测图像

[英]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.

相关问题 Tensorflow:如果我有训练有素的 MNIST model,如何检测图像中的手写数字? - Tensorflow: how can I detect handwriting numbers in an image if I have a trained MNIST model? 如何使用经过训练的 Image to Emotion Pytorch 模型进行预测 - How do I predict using a trained Image to Emotion Pytorch model 如何在Keras中使用训练模型预测输入图像? - How to predict input image with trained model in Keras? 如何导入经过训练的模型来预测单个图像? - How to import a trained model to predict a single image? 如何在 Python 中使用 tensorflow 训练图像分类器模型并在 Java 应用程序中使用训练后的模型? - How can i train a Image classifier model with tensorflow in Python and use the trained model in Java application? 如何使用Keras训练模型预测输入图像? - How to predict input image using trained model in Keras? 如何从我训练并保存的 pytorch model 预测 output? - how can I predict an output from a pytorch model I have trained and saved? 错误“IndexError:如何在 Keras 中使用经过训练的 model 预测输入图像? - Error “IndexError: How to predict input image using trained model in Keras? 如何从 keras 中预训练的 model 预测图像 - How to predict image from a pre-trained model in keras 如何为我训练有素的机器学习 model 提供输入以预测 output? - How to give input to my trained machine learning model to predict the output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM