0

I am working on MNIST dataset, in which X_train = (42000,28,28,1) is the training set. y_train = (42000,10) is the corresponding label set. Now I create an iterator from the image generator using Keras as follows;

iter=datagen.flow(X_train,y_train,batch_size=32)

which works fine.

Then I train the model using;

model.fit_generator(iter,steps_per_epoch=len(X_train)/32,epochs=1)

Here it gives the following error;

ValueError: Error when checking input: expected dense_9_input to have 2 dimensions, but got array with shape (32, 28, 28, 1)

I tried but failed to find the mistake. Also I searched here but there was no answer:

expected dense_218_input to have 2 dimensions, but got array with shape (512, 28, 28, 1)

BTW this is the summary of my model Model structure

Please help me.

Update:

model=Sequential()
model.add(Dense(256,activation='relu',kernel_initializer='he_normal',input_shape=(28,28,1)))
model.add(Flatten())
model.add(Dense(10,activation='softmax',kernel_initializer='he_normal'))
Navdeep
  • 823
  • 1
  • 16
  • 35
  • Dense layer can not process tensors with rank greater than 2. In that case it should be flattened first. – sreagm Apr 27 '20 at 12:39
  • It actually contradicts with `iter=datagen.flow(X_train,y_train,batch_size=32)` as it requires `X_train` to be of rank 4. By flattening that property is violated. What are your inputs? – Navdeep Apr 27 '20 at 14:53

1 Answers1

1

Shape mismatch was the root-cause. Input shape was not matching with what ImageDataGenetor expects. Please check the following example with mnist data. I have used Tensorflow 2.1.

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = tf.expand_dims(x_train,axis=-1)
x_test = tf.expand_dims(x_test,axis=-1)

datagen = ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2)

iter=datagen.flow(x_train,y_train,batch_size=32)

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28,1)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

#model.fit_generator(iter,steps_per_epoch=len(X_train)/32,epochs=1) # deprecated in TF2.1
model.fit_generator(iter,steps_per_epoch=len(iter),epochs=1)
model.evaluate(x_test, y_test) 

Full code is here

Vishnuvardhan Janapati
  • 3,088
  • 1
  • 16
  • 25
  • I was able to work with the model.fit() method earlier also but the problem is with the fit_generator(). Please help me with that. Thanks. – Navdeep Apr 27 '20 at 13:12
  • Updated the code above. Forgot to change last line model.fit_generator(iter,steps_per_epoch=len(iter),epochs=1) – Vishnuvardhan Janapati Apr 27 '20 at 13:28
  • Can you please update my code and share it so that I can tell you what you need to do. `ImageDataGenerator` and `fit_generator` looks for certain shape based on input data. – Vishnuvardhan Janapati Apr 27 '20 at 14:47
  • I have updated my post. Please find in the UPDATE section.so made it work but still, I have a doubt. Does `model.add(Flatten())` flatten the tensor prior to performing `Z=WX+b` that goes into the first hidden layer or does it flatten the output of the first hidden layer? – Navdeep Apr 27 '20 at 14:55