将图像传递到简单MNIST数据模型的Tensorflow

将图像传递到简单MNIST数据模型的Tensorflow,tensorflow,mnist,Tensorflow,Mnist,我有一个简单的MNIST数据分类模型,准确率约为92% 我想知道是否有任何方法,我可以提供数字图像,并得到该数字作为输出标签?图像可以来自mnist测试数据,而不是自定义图像,只是为了避免图像预处理?下面是我的模型的代码 谢谢 import tensorflow as tf #reset graph tf.reset_default_graph() #constants learning_rate = 0.5 batch_size = 100 training_epochs = 5 logs

我有一个简单的MNIST数据分类模型,准确率约为92%

我想知道是否有任何方法,我可以提供数字图像,并得到该数字作为输出标签?图像可以来自mnist测试数据,而不是自定义图像,只是为了避免图像预处理?下面是我的模型的代码

谢谢

import tensorflow as tf

#reset graph
tf.reset_default_graph()

#constants
learning_rate = 0.5
batch_size = 100
training_epochs = 5
logs_path = "/tmp/mnist/2"

#load mnist data set
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

with tf.name_scope('inputs'):
    x = tf.placeholder(tf.float32, shape=[None,784], name = "image-input")
    y_= tf.placeholder(tf.float32, shape=[None, 10], name = "labels-input")
#weights
with tf.name_scope("weights"):
    W = tf.Variable(tf.zeros([784,10]))
#biases
with tf.name_scope("biases"):
    b=  tf.Variable(tf.zeros([10]))

#Activation function softmax
with tf.name_scope("softmax"):
    #y is prediction
    y = tf.nn.softmax(tf.matmul(x,W) +b)

#Cost function
with tf.name_scope('cross_entropy'):
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices=[1])) #????

#Define Optimizer
with tf.name_scope('train'):
    train_optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)

#Accuracy
with tf.name_scope('Accuracy'):
    correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

tf.summary.scalar("cost",cross_entropy)
tf.summary.scalar("accuracy",accuracy)
#Merge all summaries into a single "operation" which will be executed in a session
summary_op = tf.summary.merge_all()

with tf.Session() as sess:
    #initialize variables before using them
    sess.run(tf.global_variables_initializer())
    #log writer object
   # writer = tf.train.SummaryWriter(logs_path, graph=tf.get_default_graph())
    writer = tf.summary.FileWriter(logs_path,graph=tf.get_default_graph())
    #training cycles
    for epoch in range(training_epochs):
        #number of batches in one epoch
        batch_count = int(mnist.train.num_examples/batch_size)
        for i in range(batch_count):
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            _,summary = sess.run([train_optimizer,summary_op], feed_dict={x: batch_x, y_:batch_y})
            writer.add_summary(summary,epoch * batch_count + i)
        if epoch % 5 == 0:
            print("Epoch: ",epoch)
    print("Accuracy: ",accuracy.eval(feed_dict={x: mnist.test.images,y_:mnist.test.labels}))
    print("Done")

训练网络后,您可以通过以下操作获得网络给新图像的标签

new_image_label= sess.run(y, feed_dict={x: new_image})
请注意,
new_image
的格式应与
batch_x
的格式相同。将
new_image
看作一个大小为1的批,因此如果
batch_x
是2D,那么
new_image
也应该是2D(形状为1×784)

此外,如果您对
batch\u x
中的图像进行了一些预处理(例如标准化),则需要对
新图像进行同样的处理


您还可以使用与上面相同的代码同时获得多个图像的标签。只需将
new_image
替换为若干图像的2D数组
new_image

,因此在我的代码下面,我应该添加您答案中的那行代码,其中new_image将是我要测试的图像的路径,它必须与批处理中的图像大小相同?是的,只需将它添加到“with tf.Session()as sess:”,循环(用于范围内的历元(训练历元))结束后