我对彩色图像(3 个通道)进行了神经网络机器学习。它有效,但现在我想尝试以灰度方式进行,看看是否可以提高准确性。 这是代码:
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)
您可以看到我已将 input_shape 更改为具有 1 个单通道灰度。 我收到错误:
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]
知道如何解决这个问题吗?
你的
train_generator
似乎没有colormode='grayscale'
。尝试:
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)
当没有时会出现此错误。渠道与型号不同。也许由于 input_shape,您给出了 3d 形状张量。这可能对你有帮助。:)
input_shape = (img_width, img_height, 1)
如果您正在处理彩色图像,请添加 ImageDataGenerator 中的 color_mode='灰度'
它对我有用!