jeudi 24 août 2017

Tensorflow / CNN / test one image

I have a simple request but the result seems complicated to reach.

I have done a simple model in Tensorflow based on images divided in 3 labels (quite simple).

After data importation, the model is trained + tested up to 60% of good results.

Then, I'd like to test a single image using the existing model and see if the model guesses the correct label.

The whole process is quite simple up to the test process:

#%%
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

test_image = mpimg.imread(os.path.join("images","cnn","cnn_test.png"))[:, :, :channels]
plt.imshow(test_image)
plt.axis("off")
plt.show()


X_test = test_image.reshape(-1,height, width, channels)

# initialize the variables
#sess.run(tf.global_variables_initializer())

X_test = test_image.reshape(-1, height, width, channels)

tf.reset_default_graph()


# x is the input array, which will contain the data from an image 

saver = tf.train.import_meta_graph('./02_CNN/data/my_model8.ckpt.meta')

print('meta graph imported')


X_test = test_image.reshape(-1, height, width, channels)

sess = tf.Session()
saver.restore(sess, './02_CNN/data/my_model8.ckpt')
print('model graph restored')

feed_dict = {x: X_test}

prediction = sess.run(feed_dict)


max_index = np.argmax(prediction)

print(max_index)

I have the following result:

TypeError: Fetch argument array([[[[ 0.03137255,  0.20392157,  0.36862746],
         [ 0.02745098,  0.20784314,  0.37254903],
         [ 0.02352941,  0.21176471,  0.3764706 ],
         ..., 
      ....
         [ 0.08627451,  0.08235294,  0.09019608],
         [ 0.12941177,  0.12156863,  0.12941177],
         [ 0.13725491,  0.1254902 ,  0.13725491]]]], dtype=float32) 
has invalid type <class 'numpy.ndarray'>, must be a string or Tensor. 
(Can not convert a ndarray into a Tensor or Operation.)

Any idea about how to solve this issue?

Many thanks in advance,

Nicolas

Aucun commentaire:

Enregistrer un commentaire