pythonnumpytensorflowkerasdata-generation

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (None, None, None)


I'm new at Tensorflow and I try to classify PDF files with a CNN by converting it to images and feeding it to a model. I created a custom DataGenerator with keras (using this tutorial) and I get a ValueError when running model.fit().

My input layer when i run model.summary() is : input_1 (InputLayer) [(None, 224, 224, 3)]

Below is my code for the __ getitem __ and __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: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (None, None, None)

I tried to use the np.expand_dims() as proposed here, but it doesn't solve my problem.

I suspect the conversion part to be bad, but I have no clue of where could the problem come from.


Solution

  • I made 2 errors in this code:

    1. I replaced

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

      By :

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

    By doing this I changed the shape of each image from (1, 224, 224) to (1, 224, 224, 3). The "3" in the shape means I need a RGB image (3 channels per image), so removing the grayscaling is quite useful !

    1. I replaced

      doc_row = df.loc[i]
      

      By :

      doc_row = df.loc[id]
      

    I had inverted i and id in my for loop.