0

Thanks for your help. This question has bothered me for 2 days. I searched many sites and did not solve.

Background: I am learning mnist, and it goes right at first, but when I save the model and restore, there is an Error, told me placeholder_1 must be fed. I am confused.

Code: Below code goes right.

import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("mnist data/", one_hot=True)

import tensorflow as tf
# 操作符号变量来描述这些可交互的操作单元
x = tf.placeholder(tf.float32, [None, 784])


# 权重值和偏置量
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
# 实现模型
y = tf.nn.softmax(tf.matmul(x,W) + b)
# 添加一个新的占位符用于输入正确值
y_ = tf.placeholder("float", [None,10])
# 计算交叉熵:
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
# 要求TensorFlow用梯度下降算法(gradient descent algorithm)以0.01的学习速率最小化交叉熵
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# 添加一个操作来初始化我们创建的变量
init = tf.initialize_all_variables()

saver = tf.train.Saver()
sess = tf.Session()
sess.run(init)
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

save_path = saver.save(sess, "./model_mnist.ckpt",write_meta_graph=False) 
print("Model saved in life:", save_path) 

import cv2
import numpy as np

img = cv2.imread('lena.png')
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) 
arr = []
for i in range(28):
    for j in range(28):
        gray = 1 - img[i,j]/255
        arr.append(gray)

arr_mnist = np.array([arr])
#print(arr_mnist)

result = sess.run(y, feed_dict={x:arr_mnist})
print(result)
#print(np.argmax(result[0]))
#print(np.sum(result[0]))
print("预测值为:",np.argmax(result[0]),";概率为:",np.max(result[0])/np.sum(result[0]))
#print(tf.argmax(result,1))

But, when I want to use the model to restore. it goes wrong.

import cv2
import numpy as np
import tensorflow as tf

img = cv2.imread('lena.png')
img = cv2.resize(img, (28,28))
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) 

arr = []

for i in range(28):
    for j in range(28):
        gray = 1 - img[i,j]/255
        arr.append(gray)

arr_mnist = np.array([arr])
#print(arr_mnist)

tf.reset_default_graph()

x = tf.placeholder("float", shape=[None, 784])
y = tf.placeholder("float", shape=[None, 10])
#keep_prob = tf.placeholder("float")

sess = tf.Session()

saver = tf.train.import_meta_graph('./model_mnist.ckpt.meta')  

saver.restore(sess, './model_mnist.ckpt')

result = sess.run(y, feed_dict={x:arr_mnist})
print(result)
print("预测值为:",np.argmax(result[0]),";概率为:",np.max(result[0])/np.sum(result[0]))

the Error is :

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

so, I suppose the problem should be in the model save or restore process, but I cannot figure out. how can I correct the code? thank you!

Anh Pham
  • 695
  • 1
  • 6
  • 21
xuchenxing
  • 41
  • 4

1 Answers1

0

When restoring the graph, you are declaring two placeholders, and only feed one when running the session. The y placeholder is the one matching the error name.

But you do not need to declare these placeholders in the run script. The restoration takes care of it. Note that you can specify the feed dictionary with string keys:

feed_dict: { 'x': [1,2,3] }
Eric Platon
  • 9,819
  • 6
  • 41
  • 48