Tensorflow:sess.run返回相同的输出标签

时间:2018-05-26 15:32:52

标签: tensorflow machine-learning image-recognition tensor

我已经实现了张量流图像分类器,最后,sess.run每次都返回相同的np数组。我不知道为什么会这样?

有人能说出我做错了吗?

def predict():

  train = data.train
  test = data.test
  tf.reset_default_graph()

  (n_x, m) = train.images.T.shape #n_x is 784
  X = xPlaceholder(n_x) ##return X of shape(784,)

  with tf.Session() as sess:
      new_saver = tf.train.import_meta_graph('~/trained-model.ckpt.meta')
      new_saver.restore(sess, '~/trained-model.ckpt')
      #y_pred is the softmax of last output layer; 
      y_pred = tf.get_default_graph().get_tensor_by_name('y_pred:0')
      #imageprepare function return (784,) np array
      output_label = sess.run(pred, feed_dict={X: imageprepare('jean.jpeg')}) 
      print(output_label)

输出标签作为np数组返回,形状为[10,],其中10是标签数。提前谢谢。

1 个答案:

答案 0 :(得分:0)

在以下代码中,您根本不执行恢复的图形。您创建了一个单独的占位符X,它不属于已恢复的图形管道。并执行变量pred,这超出了您提供的范围(此处未定义)。

如果您只需要推理,可以将session.run操作(变量,占位符)名称作为字符串传递给:

session.run('y_pred:0', feed_dict={'x_placheholder:0':numpy_image})

确保在构造图形时设置这些变量名称(即tf.placeholder(shape, name='x_placholder'))。