2017-12-16 17 views
1

Tensorflow를 처음 사용했습니다. 나는이 예를 통해 MNIST 훈련을했다.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') 

지금이 모델에 의한 예측을하고 싶다. 이 코드 줄을 사용하여 이미지를 열고 처리합니다.

feed_dict1 = {x: images} 
    classification = sess.run(y_pred, feed_dict1) 
    print (classification) 

그것은이 오류를 반환

image = cv2.imread("Untitled.jpg") 
image = np.multiply(image, 1.0/255.0) 
images=tf.reshape(image,[-1,28,28,1]) 

I이 사용

.

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles. 
+0

numpy 재구성을 사용해 보셨습니까? –

+0

나는 그것을 시도했다. –

+0

동일한 문제가 있는데 feed_dict는 입력 및 출력 값의 간단한 배열을 필요로한다는 것입니다. 그래서 내 1 차원 네트워크는 단지 당신이 그것을 먹이 동일한 값을 출력 : 'inputs = [[0], [0.5], [1]]' 'outputs = [[0], [0.5], [1]]' 'feed_dict = {inputType : inputs, outputType : outputs}' tf.reshape는 텐서를 반환합니다. –

답변

0

당신은 당신의 자리에 TF 객체 공급하려고 :

images = tf.reshape(image,[-1,28,28,1]) 

을하지만 당신은 할 수없는 자리 예를 np.array에 대한 수를 예상하기 때문에 그. 따라서 tf.reshape 대신 numpy.reshape을 사용하십시오. 둘째 세션에서 사용할 수 있습니다. 예를 들어 평면 배열을 자리 표시 자로 공급 한 다음이 배열을 2D 행렬로 바꾼 세션 내부에 노드를 만들 수 있습니다.