简体   繁体   English

Keras model.predict检查输入时出错:预期conv2d_input具有4维,但数组的形状为(128,56)

[英]Keras model.predict Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (128, 56)

I used a DirectoryIterator to read images from a directory and train my model. 我使用DirectoryIteratorDirectoryIterator中读取图像并训练我的模型。 I want to be able to verify that it works so I tried using model.predict on a numpy array that contains an image but I get the following error 我想能够验证它是否有效,所以我尝试在包含图像的numpy数组上使用model.predict ,但出现以下错误

ValueError: Error when checking input: expected conv2d_input to have 4 
dimensions, but got array with shape (128, 56)

I'm not sure what kind of shape or attributes the DirectoryIteratory from flow_from_directory has so I'm not sure what kind of input model.predict is expecting. 我不确定flow_from_directoryDirectoryIteratory具有什么样的形状或属性,所以我不确定期望的输入model.predict是哪种。 This is what my code looks like 这就是我的代码

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

From your code snippet it seems that you're using this blog post. 从您的代码片段来看,您似乎正在使用博客文章。 So your ConvNet's first layer is a convolutional layer, expecting the input shape to be (150, 150) . 因此,您的ConvNet的第一层是卷积层,期望输入形状为(150, 150) Let's look at your error message: 让我们看看您的错误消息:

ValueError : Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (128, 56) ValueError :检查输入时出错:预期conv2d_input具有4个维,但数组的形状为(128,56)

The error says two things: 该错误说明两件事:

  1. Your input should have 4 dimensions. 您的输入应具有4个维度。
  2. Got array with shape (128, 56). 得到了形状为(128,56)的数组。

So first, your numpy array shape should be in the shape of (150, 150) (because of your ConvNet's input shape), and you should expand the dimensions of your image to have 4 dimensions. 因此,首先,您的numpy数组形状应为(150, 150)形状(由于ConvNet的输入形状),并且应将图像的尺寸扩展为4个尺寸。 For example (assuming your numpy array to be x ): 例如(假设您的numpy数组是x ):

x = x.reshape(1,150,150,3).astype('float')
x /= 255

pred = model.predict(x)

If you're reading the image from your hard disk, you could use the following code: 如果要从硬盘读取图像,则可以使用以下代码:

img = keras.preprocessing.image('image.jpg', target_size=(150,150))
x = keras.preprocessing.image.img_to_array(img)
x = x.reshape(1,150,150,3).astype('float')
x /= 255

pred = model.predict(x)

Hope it helps. 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了具有形状的数组 - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到的数组具有形状(无,1) - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (None, 1) 检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 (28708, 1) 的数组 - Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (28708, 1) Keras 数字数据集错误:预期 conv2d_input 有 4 个维度,但得到的数组形状为 (60000, 28, 28) - Error in Keras Digit Dataset: expected conv2d_input to have 4 dimensions, but got array with shape (60000, 28, 28) 检查模型输入时出错:预期conv2d_175_input具有4个维,但数组的形状为(0,1) - Error when checking model input: expected conv2d_175_input to have 4 dimensions, but got array with shape (0, 1) model.predict() == ValueError: 检查输入时出错:预期 flatten_input 有 3 个维度,但得到了形状为 (1, 2) 的数组 - model.predict() == ValueError: Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (1, 2) Python 神经网络 - 检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到形状为 (700, 128, 33) 的数组 - Python Neural Networks - Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (700, 128, 33) 未捕获的错误:检查时出错:预期conv2d_input具有4维,但数组的形状为[275,183,3] - Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [275,183,3] Keras CNN:检查输入时出错:预期conv1d_46_input具有3个维,但数组的形状为(3780,6) - Keras CNN: Error when checking input: expected conv1d_46_input to have 3 dimensions, but got array with shape (3780, 6) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但在与 model 拟合时得到形状为 (999, 12, 1) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (999, 12, 1) while fitting with model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM