简体   繁体   中英

Model.predict() ValueError: Cananot feed value of shape (300,300,3) for Tensor which has shape (?,300,300,3)

I have trained my classifier which is working well. But I am facing a value error here about the shape. I even resize the testing image in shape (300,300,3). please help.

I am trying to predict an image from the training classifier I build. But everytime I try to do this it gives me this value error. Which I also researched every where but couldnt found anything till yet.

My Code is below.

X_train = np.load('D:/ThesisWork/Training_data.npy')#training_images
y_train = np.load('D:/ThesisWork/Training_labels.npy')#training_labels
X_test = np.load('D:/ThesisWork/Testing_data.npy')#testing_images
y_test = np.load('D:/ThesisWork/Testing_labels.npy')#testing_labels


with tf.device('/gpu:0'):
  tf.reset_default_graph()
  convnet = input_data(shape=(None,IMG_SIZE,IMG_SIZE,3),name='input')
  #shape=[None, IMG_SIZE, IMG_SIZE, 1],
  convnet = conv_2d(convnet, 32, 5, activation='relu')
  convnet = max_pool_2d(convnet, 5)
  convnet = conv_2d(convnet, 64, 5, activation='relu')
  convnet = max_pool_2d(convnet, 5)
  convnet = conv_2d(convnet, 128, 5, activation='relu')
  convnet = max_pool_2d(convnet, 5)
  convnet = conv_2d(convnet, 64, 5, activation='relu')
  convnet = max_pool_2d(convnet, 5)
  convnet = conv_2d(convnet, 32, 5, activation='relu')
  convnet = max_pool_2d(convnet, 5)
  convnet = fully_connected(convnet, 1024, activation='relu')
  convnet = dropout(convnet, 0.8)
  convnet = fully_connected(convnet, 163, activation='softmax')
  convnet = regression(convnet, optimizer='adam', loss='categorical_crossentropy', name='targets')
  model = tflearn.DNN(convnet, tensorboard_dir='log', tensorboard_verbose=0)
  model.fit({'input': X_train}, {'targets': y_train}, n_epoch=40,
            validation_set=({'input': X_test}, {'targets': y_test}),
            snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

# =========================
# For Saving The Model
# =========================
model.save('my_trained_model.tflearn')
# np.save('training_finalized_data.npy', model)
# =========================
# For Prediction
# =========================
model_out = model.predict(X_test[0])
print(model_out)
plt.imshow(model_out)
plt.show()
model_out1 = model.predict_label(X_test[0])

print("Model_OUT LABEL", model_out1)

The error I'm facing is below.

Traceback (most recent call last):
  File "d:/DeepLearningThesis/Deep-learning-methods-for-Vehicle-Classification/Classifier_with_one_hot_labels.py", line 202, in <module>
    model_out = model.predict(X_test[0])
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\models\dnn.py", line 257, in predict
    return self.predictor.predict(feed_dict)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\helpers\evaluator.py", line 69, in predict
    return self.session.run(self.tensors[0], feed_dict=feed_dict)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
    run_metadata_ptr)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (300, 300, 3) for Tensor 'input/X:0', which has shape '(?, 300, 300, 3)'

So, I tried the link which @Flika205 mentioned and it does work. But for possible best answer you should use np.expand_dims(img, axis = 0).

My code is below

predictingimage = "D:/compCarsThesisData/data/image/78/12/2012/722894351630dc.jpg" #67/1698/2010/6805eb92ac6c70.jpg"
predictImageRead = mpg.imread(predictingimage)
resizingImage = cv2.cv2.resize(predictImageRead,(IMG_SIZE,IMG_SIZE))
reshapedFinalImage = np.expand_dims(resizingImage, axis=0)
# imagetoarray = np.array(resizingImage)
# reshapedFinalImage = imagetoarray.reshape(1,IMG_SIZE,IMG_SIZE,3)

# =========================
# For Prediction
# =========================
model_out = model.predict(reshapedFinalImage)
print(model_out)
plt.imshow(model_out)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM