0

I'm trying to train a CNN which can detect. Therefore, I have images in a folder called "allImages". I'm storing the information of the corresponding image in a csv-file. In order to handle the large amount of data I'm using the flow_from_dataframe function.

The CSV file is looking like this: CSV_file

Firstly, I've initialized the flow_from_DataFrame method

traindf=pd.read_csv("face_features.csv",)

columns = ['LTX','LTY','LBX','LBY','RBX','RBY','RTX','RTY','TLLX','TLLY','TLRX','TLRY','BottomX','BottomY','TRLX','TRLY','TRRX','TRRY']

datagen=ImageDataGenerator(rescale=1./255.,validation_split=0.25)

train_generator=datagen.flow_from_dataframe(
dataframe=traindf,
directory="allImages/",
x_col='StringName',
y_col=columns,
subset="training",
batch_size=32,
seed=42,
class_mode = "input",
shuffle=True,
target_size=(158,163))

Then, I'm trying to build and fit the model.


model_layers = [
               
   tf.keras.layers.Conv2D( 158 , input_shape=(32, 158,163,3) , kernel_size=( 3 , 3 ), activation='relu' ),

   tf.keras.layers.Conv2D( 158 , kernel_size=( 3 , 3 ) , strides=2 , activation='relu' ),
   tf.keras.layers.BatchNormalization(),

   tf.keras.layers.Conv2D( 18 , kernel_size=( 3 , 3 ) , strides=1 , activation='relu' ),
   tf.keras.layers.Conv2D( 18 , kernel_size=( 3 , 3 ) , strides=1 , activation='relu' ),
   tf.keras.layers.Conv2D( 18 , kernel_size=( 3 , 3 ) , strides=1 ),

]
model = tf.keras.Sequential(model_layers)
model.compile( loss=tf.keras.losses.mean_squared_error , optimizer=tf.keras.optimizers.Adam( lr=0.0001 ) , metrics=[ 'mse' ] )



model.fit_generator(generator=train_generator,
                   steps_per_epoch=20,
                   epochs=20)

But unfortunately keras is throwing the input-error.

WARNING:tensorflow:Model was constructed with shape (None, 32, 158, 163, 
3) for input Tensor("conv2d_92_input:0", shape=(None, 32, 158, 163, 3), 
dtype=float32), but it was called on an input with incompatible shape (None, None, None, None).

When I'm trying to get the x shape of the train_generator. I'm getting (32, 158, 163, 3).

Can anybody help to resolve the issue?

Appreciate your time.

Bruno Eigenmann
  • 346
  • 3
  • 16

1 Answers1

1

You do not need to mention the batch size dimension in the input_shape of the first layer in the model:

Replace -> tf.keras.layers.Conv2D( 158 , input_shape=(32, 158,163,3), kernel_size=( 3 , 3 ), activation='relu' )

with -> tf.keras.layers.Conv2D( 158, kernel_size=( 3 , 3 ), activation='relu', input_shape=(158,163,3))

Further reading: You can look up the Shapes in Keras.

Frightera
  • 4,773
  • 2
  • 13
  • 28
Abhilash Rajan
  • 349
  • 1
  • 7
  • Thanks you've mentioned that. But somehow I'm getting a new error: `Incompatible shapes: [32,55,58,18] vs. [32,158,163,3] [[node gradient_tape/mean_squared_error/BroadcastGradientArgs (defined at /Users/Desktop/face_landmarks_cleaned/untitled12.py:90) ]] [Op:__inference_train_function_2400] Function call stack: train_function ` – Bruno Eigenmann Feb 28 '21 at 14:03