我已经训练了分类器,该分类器运行良好。但我在此处遇到有关形状的值错误。我什至调整了测试图像的形状(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)'
答案 0 :(得分:0)
所以,我尝试了@ 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()