[英]ValueError: could not broadcast input array from shape (300,300,3) into shape (300,300)
[英]Model.predict() ValueError: Cananot feed value of shape (300,300,3) for Tensor which has shape (?,300,300,3)
我已經訓練了分類器,該分類器運行良好。 但我在此處遇到有關形狀的值錯誤。 我什至調整了測試圖像的形狀(300,300,3)。 請幫忙。
我正在嘗試根據我建立的訓練分類器預測圖像。 但是每次我嘗試執行此操作都會給我這個值錯誤。 我還研究了所有地方,但至今仍未找到任何東西。
我的代碼如下。
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)
我面臨的錯誤如下。
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)'
所以,我嘗試了@ Flika205提到的鏈接,它確實起作用。 但是為了獲得最佳答案,您應該使用np.expand_dims(img,axis = 0)。
我的代碼如下
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.