[英]ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3
I'm trying to run the following code.我正在尝试运行以下代码。
model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (200,200,3)),
tf.keras.layers.MaxPool2D(2,2),
#
tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (200,200,3)),
tf.keras.layers.MaxPool2D(2,2),
#
tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (200,200,3)),
tf.keras.layers.MaxPool2D(2,2),
##
tf.keras.layers.Flatten(),
##
tf.keras.layers.Dense(512,activation= 'relu'),
##
tf.keras.layers.Dense(1,activation='sigmoid')
])
image_dir = r"C:\Users\Shreya\Desktop\Project\basedata\testing\testing\tomato"
img_list = os.listdir(image_dir)
for i in img_list:
path = os.path.join(image_dir, i)
img = image.load_img(path, target_size = (150, 150))
img = np.asarray(img)
array = image.img_to_array(img)
pred = model.predict_classes((img/255).reshape((150,150,3)))
plt.figure('img')
plt.imshow(img,cmap='gray')
plt.title('pred:'+str(pred[0]), fontsize=22)
plt.show()
After executing this, I'm getting the following error:执行此操作后,我收到以下错误:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 150, 3]
please help请帮忙
From the error its sounds like, probably you have not added batch_axis to your training dataset.从错误的声音来看,您可能尚未将 batch_axis 添加到您的训练数据集中。 That is [batch, w, h, channel]
即[batch, w, h, channel]
Working sample code工作示例代码
x_train, x_test = x_train.reshape(-1,200,200,1), x_test.reshape(-1,200,200,1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.