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:
JavaScript
x
44
44
1
train_datagen = ImageDataGenerator(
2
rescale=1. / 255,
3
shear_range=0.2,
4
zoom_range=0.2,
5
horizontal_flip=True)
6
7
test_datagen = ImageDataGenerator(rescale=1. / 255)
8
9
train_generator = train_datagen.flow_from_directory(
10
train_data_dir,
11
target_size=(img_width, img_height),
12
batch_size=batch_size,
13
class_mode='binary',
14
shuffle=True)
15
16
validation_generator = test_datagen.flow_from_directory(
17
validation_data_dir,
18
target_size=(img_width, img_height),
19
batch_size=batch_size,
20
color_mode='grayscale',
21
class_mode='binary',
22
shuffle=True)
23
model = tf.keras.Sequential()
24
input_shape = (img_width, img_height, 1)
25
model.add(Conv2D(32, 2, input_shape=input_shape, activation='relu'))
26
model.add(MaxPooling2D(pool_size=2))
27
28
model.add(Conv2D(32, 2, activation='relu'))
29
model.add(MaxPooling2D(pool_size=2))
30
31
model.add(Conv2D(64, 2, activation='relu'))
32
model.add(MaxPooling2D(pool_size=2))
33
34
model.add(Flatten())
35
model.add(Dense(128))
36
model.add(Dense(len(classes)))
37
38
model.compile(optimizer='adam',
39
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
40
metrics=['accuracy'])
41
history = model.fit(train_generator,
42
validation_data=validation_generator,
43
epochs=EPOCHS)
44
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?
Advertisement
Answer
Your train_generator
does not seem to have the colormode='grayscale'
. Try:
JavaScript
1
8
1
train_generator = train_datagen.flow_from_directory(
2
train_data_dir,
3
target_size=(img_width, img_height),
4
batch_size=batch_size,
5
class_mode='binary',
6
colormode='grayscale',
7
shuffle=True)
8