I am creating a mask_detection model on 3 classes "CorrectMask", "UncorrectMask", "NoMask". I am creating my CNN, but I have the following error:
Traceback (most recent call last):
File "/home/andrea/Scrivania/Biometrics/covid_mask_train.py", line 70, in <module>
model.fit(train_generator, 25)
File "/home/andrea/.local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/home/andrea/.local/lib/python3.9/site-packages/keras/engine/data_adapter.py", line 919, in __init__
raise ValueError("`y` argument is not supported when using "
ValueError: `y` argument is not supported when using `keras.utils.Sequence` as input.
and this is my code to create my CNN:
datagen = ImageDataGenerator(
validation_split = 0.3,
rescale = 1./255,
horizontal_flip = True,
zoom_range = 0.2,
brightness_range = [1,2]
)
train_generator = datagen.flow_from_directory(
DATASET_DIR,
target_size = DIM_IMG,
batch_size = BATCH_SIZE,
class_mode = "binary",
subset = "training"
)
test_generator = datagen.flow_from_directory(
DATASET_DIR,
target_size = DIM_IMG,
batch_size = BATCH_SIZE,
class_mode = "binary",
subset = "validation"
)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), padding='same',activation='relu', input_shape=(224,224, 3)))
model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.5))
model.add(Conv2D(64, kernel_size=(3,3), padding='same',activation='relu', ))
model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.5))
model.add(Conv2D(128, kernel_size=(3,3), padding='same',activation='relu', ))
model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256,activation='relu'))
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='softmax')) # uso softamx perchè ho più di due classi
model.summary()
model.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ["accuracy"])
model.fit(train_generator, EPOCHS)
metrics_train = model.evaluate(train_generator)
metrics_test = model.evaluate(test_generator)
print(f"TRAIN_SET: {metrics_train}")
print("--------------------------------------------")
print(f"TEST_SET: {metrics_test}")
# save the model
model.save("model_MaskDetect_25_epochs.h5")
print("Saved!")
I've read various things about stackoverflow too but I can't figure out how to apply it to my case. Someone can help me??
CodePudding user response:
Change your fit function call to explicitly set the epoch parameter:
model.fit(train_generator, epochs = EPOCHS)
What is happening is fit
is using EPOCHS
as the input for the second parameter which is the y
argument you are getting an error for.