I did a neural network machine learning on colored images (3 channels). It worked but now I want to try to do it in grayscale to see if I can improve accuracy. Here is the code:
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary',
shuffle=True)
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
color_mode='grayscale',
class_mode='binary',
shuffle=True)
model = tf.keras.Sequential()
input_shape = (img_width, img_height, 1)
model.add(Conv2D(32, 2, input_shape=input_shape, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(32, 2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(64, 2, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(128))
model.add(Dense(len(classes)))
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_generator,
validation_data=validation_generator,
epochs=EPOCHS)
You can see that I have changed the input_shape to have 1 single channel for grayscale. I'm getting an error:
Node: 'sequential_26/conv2d_68/Relu' Fused conv implementation does not support grouped convolutions for now. [[{{node sequential_26/conv2d_68/Relu}}]] [Op:__inference_train_function_48830]
Any idea how to fix this?
CodePudding user response:
Your train_generator
does not seem to have the colormode='grayscale'
. Try:
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary',
colormode='grayscale',
shuffle=True)
CodePudding user response:
This error arises when the no. channel differs from the model. Maybe due to the input_shape, you've given a 3d shape tensor. This MAY help you.:)
input_shape = (img_width, img_height, 1)