Home > Back-end >  How to fix the error when data augmenting TensorFlow training data?
How to fix the error when data augmenting TensorFlow training data?

Time:11-14

I am trying to data augment my TensorFlow model's training data. My model runs without data augmentation. I want to augment training data to improve results. This is my attempt:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
 
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(
    directory_testData,
    #validation_split=0.2,
    target_size=(150, 150),
    batch_size=32,
    class_mode='binary')
 
validation_generator = test_datagen.flow_from_directory(
    directory_testData,
    #validation_split=0.2,
    target_size=(150, 150),
    batch_size=32,
    class_mode='binary')
 
model = create_functionalModel()
 
model.fit(
    train_generator,
    steps_per_epoch=2000,
    epochs=50,
    validation_data=validation_generator,
    validation_steps=800)

I then ran it and am getting these errors:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-25-1c89b4a3fc84> in <module>()
     30     epochs=50,
     31     validation_data=validation_generator,
---> 32     validation_steps=800)

 
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     57     ctx.ensure_initialized()
     58     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 59                                         inputs, attrs, num_outputs)
     60   except core._NotOkStatusException as e:
     61     if name is not None:

InvalidArgumentError:  Input to reshape is a tensor with 663552 values, but the requested shape requires a multiple of 30976
     [[node model_4/flatten_4/Reshape
 (defined at /usr/local/lib/python3.7/dist-packages/keras/layers/core/flatten.py:96)
]] [Op:__inference_train_function_4555]

It seems that this issue is related to the input shapes required by my model. Can you please help me understand how to resolve this issue? Thank you.

CodePudding user response:

Your model requires an input of shape (180, 180) But you are resizing your images to (150, 150).

Changing:

target_size=(150, 150),

To:

target_size=(180, 180),

Should fix it.

  • Related