【问题标题】:ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (None, None, None)ValueError:检查输入时出错:预期 input_1 有 4 个维度,但得到的数组具有形状(无、无、无)
【发布时间】:2021-12-03 12:58:30
【问题描述】:

我是 Tensorflow 的新手,我尝试使用 CNN 对 PDF 文件进行分类,方法是将其转换为图像并将其提供给模型。 我使用 keras(使用 this tutorial)创建了一个自定义 DataGenerator,运行 model.fit() 时出现 ValueError。

我运行 model.summary() 时的输入层是:input_1 (InputLayer) [(None, 224, 224, 3)]

以下是我的 __ getitem __ 和 __data_generation 代码:

    def __getitem__(self, index):
    index = self.index[index * self.batch_size:(index + 1) * self.batch_size]
    batch = [self.indices[k] for k in index]
    X, y = self.__data_generation(batch)
    return X, y

    def __data_generation(self, batch):
        df = self.df
        X = np.empty((self.batch_size, *self.dim))
        y = np.empty((self.batch_size), dtype=int)
        for i, id in enumerate(batch):
            
            # Loading the image :
            doc_row = df.loc[i]
            path = str(doc_row['PATH'])
            path = os.path.join(dataset_path,path)
            typologie = str(doc_row['TYPOLOGIE'])
            img_i = convert_from_path(path)[0]

            # Converting the image :
            img_i = img_i.resize((224,224), Image.ANTIALIAS)
            gray_img_i = ImageOps.grayscale(img_i)
            array_image_i = np.array(gray_img_i,dtype='float32')
            array_image_i = np.expand_dims(array_image_i, axis=0)
            X[i,] = array_image_i
            y[i] = self.map_classes[typologie]
        X = [np.array(X)]
        Y = np.array(y)
        Y = tf.keras.utils.to_categorical(Y, num_classes = self.num_classes)
        return X, Y

ValueError:检查输入时出错:预期 input_1 有 4 个维度,但得到的数组具有形状(无、无、无)

我尝试按照here 的建议使用 np.expand_dims(),但这并没有解决我的问题。

我怀疑转换部分不好,但我不知道问题出在哪里。

【问题讨论】:

  • 如果您在“X = [np.array(X)]”行之前打印它,您能否提供“np.shape(np.array(X))”的输出?跨度>
  • @MarcFelix 形状为 (1, 224, 224)。刚刚查了一下发现错误,谢谢!我需要删除灰度以获得正确的形状

标签: python numpy tensorflow keras data-generation


【解决方案1】:

我在这段代码中犯了 2 个错误:

  1. 我换了

    gray_img_i = ImageOps.grayscale(img_i)
    array_image_i = np.array(gray_img_i,dtype='float32')
    

    作者:

    array_image_i = np.array(img_i,dtype='float32')
    

通过这样做,我将每个图像的形状从 (1, 224, 224) 更改为 (1, 224, 224, 3)。 形状中的“3”表示我需要一张 RGB 图像(每张图像 3 个通道),因此去除灰度非常有用!

  1. 我换了

    doc_row = df.loc[i]
    

    作者:

    doc_row = df.loc[id]
    

我在 for 循环中颠倒了 iid

【讨论】:

    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 2019-12-22
    • 2019-09-03
    • 2022-01-15
    • 2019-11-18
    • 2018-08-03
    • 2018-01-09
    • 2021-11-24
    相关资源
    最近更新 更多