Home > Mobile >  How to fix failed assertion `output channels should be divisible by group' when trying to fit t
How to fix failed assertion `output channels should be divisible by group' when trying to fit t

Time:03-20

I'm trying to use ImageDataGenerator() for my image datasets. Here is my image augmentation code:

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)

Then use that plug into my model:

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')
])

Use EarlyStopping:

early_stopping = EarlyStopping(monitor='val_loss',mode='min',verbose=1,patience=10, restore_best_weights=True)

Compile and Fit the model:

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])

That is when the code crash, and gives this error message.

/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'

I try to change the output neurons but that doesn't work. I don't know what to do anymore. Please help me. Thank you so much.

CodePudding user response:

Got it. Because I use grayscale images. So I have to add color_mode keyword argument in both flow_from_dataframe() and set it equal to "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")
  • Related