尝试在 Keras 中拟合模型时如何修复失败的断言“输出通道应按组整除”?
How to fix failed assertion `output channels should be divisible by group' when trying to fit the model in Keras?
我正在尝试将 ImageDataGenerator() 用于我的图像数据集。
这是我的图像增强代码:
batch_size = 16
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
# Use flow from dataframe
train_generator = train_datagen.flow_from_dataframe(
dataframe=train,
directory="data/train",
x_col="id",
y_col=["not_ready", "ready"],
target_size=(300, 300),
batch_size=batch_size,
class_mode="raw",
validate_filenames=False)
validation_generator = test_datagen.flow_from_dataframe(
dataframe=validation,
directory="data/validation",
x_col="id",
y_col=["not_ready", "ready"],
target_size=(300, 300),
batch_size=batch_size,
class_mode="raw",
validate_filenames=False)
然后将该插件用于我的模型:
model = Sequential([
layers.Conv2D(filters=16, kernel_size=(3, 3), activation='relu', input_shape=(300, 300, 1)),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Dropout(0.5),
layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Dropout(0.5),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dropout(0.5),
layers.Dense(32, activation='relu'),
layers.Dropout(0.5),
layers.Dense(2, activation='sigmoid')
])
使用 EarlyStopping:
early_stopping = EarlyStopping(monitor='val_loss',mode='min',verbose=1,patience=10, restore_best_weights=True)
编译并拟合模型:
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(
train_generator,
steps_per_epoch=train_generator.n // batch_size,
epochs=100,
validation_data=validation_generator,
validation_steps=validation_generator.n // batch_size,
callbacks=[early_stopping])
那是代码崩溃的时候,给出了这个错误信息。
/AppleInternal/Library/BuildRoots/8d3bda53-8d9c-11ec-abd7-fa6a1964e34e/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSNDArray/Kernels/MPSNDArrayConvolution.mm:2317: failed assertion `output channels should be divisible by group'
我尝试更改输出神经元,但这不起作用。我不知道该怎么办了。请帮我。非常感谢。
知道了。因为我使用灰度图像。所以我必须在 flow_from_dataframe() 中添加 color_mode 关键字参数并将其设置为等于 "grayscale"
train_generator = train_datagen.flow_from_dataframe(
dataframe=train,
directory="data/train",
x_col="id",
y_col=["not_ready", "ready"],
target_size=(300, 300),
batch_size=batch_size,
class_mode="raw",
color_mode="grayscale")
validation_generator = test_datagen.flow_from_dataframe(
dataframe=validation,
directory="data/validation",
x_col="id",
y_col=["not_ready", "ready"],
target_size=(300, 300),
batch_size=batch_size,
class_mode="raw",
color_mode="grayscale")
我正在尝试将 ImageDataGenerator() 用于我的图像数据集。 这是我的图像增强代码:
batch_size = 16
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
# Use flow from dataframe
train_generator = train_datagen.flow_from_dataframe(
dataframe=train,
directory="data/train",
x_col="id",
y_col=["not_ready", "ready"],
target_size=(300, 300),
batch_size=batch_size,
class_mode="raw",
validate_filenames=False)
validation_generator = test_datagen.flow_from_dataframe(
dataframe=validation,
directory="data/validation",
x_col="id",
y_col=["not_ready", "ready"],
target_size=(300, 300),
batch_size=batch_size,
class_mode="raw",
validate_filenames=False)
然后将该插件用于我的模型:
model = Sequential([
layers.Conv2D(filters=16, kernel_size=(3, 3), activation='relu', input_shape=(300, 300, 1)),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Dropout(0.5),
layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Dropout(0.5),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dropout(0.5),
layers.Dense(32, activation='relu'),
layers.Dropout(0.5),
layers.Dense(2, activation='sigmoid')
])
使用 EarlyStopping:
early_stopping = EarlyStopping(monitor='val_loss',mode='min',verbose=1,patience=10, restore_best_weights=True)
编译并拟合模型:
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(
train_generator,
steps_per_epoch=train_generator.n // batch_size,
epochs=100,
validation_data=validation_generator,
validation_steps=validation_generator.n // batch_size,
callbacks=[early_stopping])
那是代码崩溃的时候,给出了这个错误信息。
/AppleInternal/Library/BuildRoots/8d3bda53-8d9c-11ec-abd7-fa6a1964e34e/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSNDArray/Kernels/MPSNDArrayConvolution.mm:2317: failed assertion `output channels should be divisible by group'
我尝试更改输出神经元,但这不起作用。我不知道该怎么办了。请帮我。非常感谢。
知道了。因为我使用灰度图像。所以我必须在 flow_from_dataframe() 中添加 color_mode 关键字参数并将其设置为等于 "grayscale"
train_generator = train_datagen.flow_from_dataframe(
dataframe=train,
directory="data/train",
x_col="id",
y_col=["not_ready", "ready"],
target_size=(300, 300),
batch_size=batch_size,
class_mode="raw",
color_mode="grayscale")
validation_generator = test_datagen.flow_from_dataframe(
dataframe=validation,
directory="data/validation",
x_col="id",
y_col=["not_ready", "ready"],
target_size=(300, 300),
batch_size=batch_size,
class_mode="raw",
color_mode="grayscale")