Tensorflow:使用“keras.utils.Sequence”作为输入时不支持“y”参数
Tensorflow: `y` argument is not supported when using `keras.utils.Sequence` as input
我正在 3 类“CorrectMask”、“UncorrectMask”、“NoMask”上创建一个 mask_detection 模型。我正在创建我的 CNN,但出现以下错误:
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.
这是我创建 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!")
我也阅读过有关 Stack Overflow 的各种内容,但我不知道如何将它应用到我的案例中。有人可以帮助我吗?
更改拟合函数调用以显式设置纪元参数:
model.fit(train_generator, epochs = EPOCHS)
正在发生的事情是 fit
使用 EPOCHS
作为第二个参数的输入,即您收到错误的 y
参数。
我正在 3 类“CorrectMask”、“UncorrectMask”、“NoMask”上创建一个 mask_detection 模型。我正在创建我的 CNN,但出现以下错误:
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.
这是我创建 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!")
我也阅读过有关 Stack Overflow 的各种内容,但我不知道如何将它应用到我的案例中。有人可以帮助我吗?
更改拟合函数调用以显式设置纪元参数:
model.fit(train_generator, epochs = EPOCHS)
正在发生的事情是 fit
使用 EPOCHS
作为第二个参数的输入,即您收到错误的 y
参数。