Home > front end >  The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape re
The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape re

Time:07-11

I am trying to build a binary Prediction model through Keras TensorFlow. And I am having trouble when I add data augmentation inside. This is my code.

train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_dir = 'C:/train'
test_dir = 'C:/test'
train_data = train_datagen.flow_from_directory(train_dir,target_size=(224,224),class_mode='binary',seed=42)
test_data = test_datagen.flow_from_directory(test_dir,target_size=(224,224),class_mode='binary',seed=42)
tf.random.set_seed(42)
from tensorflow.keras.layers.experimental import preprocessing

data_augmentation = keras.Sequential([
   preprocessing.RandomFlip("horizontal"),
   preprocessing.RandomZoom(0.2),
   preprocessing.RandomRotation(0.2),
   preprocessing.RandomHeight(0.2),
   preprocessing.RandomWidth(0.2),
], name='data_augmentation')

model_1 = tf.keras.Sequential([
  tf.keras.layers.Input(shape=(224,224,3),name='input_layer'),
  data_augmentation,
  tf.keras.layers.Conv2D(20,3,activation='relu'),
  tf.keras.layers.MaxPool2D(pool_size=2),
  tf.keras.layers.Conv2D(20,3,activation='relu'),
  tf.keras.layers.MaxPool2D(pool_size=2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(1,activation='sigmoid')
])


model_1.compile(loss=tf.keras.losses.binary_crossentropy,
                optimizer='Adam',
                metrics=['accuracy'])

model_1.fit(train_data,epochs=10,validation_data=test_data)

I ready tried this way but error again

inputs = tf.keras.layers.Input(shape=(224,224,3),name='input_layer')
x = data_augmentation(inputs)
x = tf.keras.layers.Conv2D(20,3,activation='relu')(x)
x = tf.keras.layers.Conv2D(20,3,activation='relu')(x)
x = tf.keras.layers.MaxPool2D(pool_size=2)(x)
x = tf.keras.layers.Flatten()(x)
outputs = tf.keras.layers.Dense(1,activation='sigmoid')(x)
model_1 = tf.keras.Model(inputs,outputs)

and this is error message:

Traceback (most recent call last):
  File "C:/Users/pondy/PycharmProjects/pythonProject2/main.py", line 60, in <module>
    model_1 = tf.keras.Sequential([
  File "C:\Users\pondy\PycharmProjects\pythonProject2\venv\lib\site-packages\tensorflow\python\training\tracking\base.py", line 530, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\pondy\PycharmProjects\pythonProject2\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\pondy\PycharmProjects\pythonProject2\venv\lib\site-packages\keras\layers\core\dense.py", line 139, in build
    raise ValueError('The last dimension of the inputs to a Dense layer '
ValueError: The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None)

Process finished with exit code 1

if didn't add data_augmentation inside it's not error thank you for help<3

CodePudding user response:

your code will work if you remove

preprocessing.RandomHeight(0.2),
preprocessing.RandomWidth(0.2),
  • Related