actually I have the first experience about computer vision project and run my code ,and get this error .in fact I wrote one code for color_mode='rgb' and the other for color_mode='grayescale'... when I run the code for RGB is ok but for grayscale, I got this error.
input depth must be evenly divisible by filter depth: 1 vs 3 [[{{node model/conv1_conv/Conv2D}}]]
I just change color mode '''
BATCH_SIZE=32
totalTrain=3980
totalVal=334
TARGET_SIZE=(640,480)
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
valid_generate = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train = train_datagen.flow_from_dataframe(
dataframe=train_df,
directory=t_dir,
x_col="filename",
y_col="class",
target_size=TARGET_SIZE,
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=True)
validation = valid_generate.flow_from_dataframe(
dataframe=valid_df,
directory=t_dir,
x_col="filename",
y_col="class",
target_size=TARGET_SIZE,
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=True)
gen = ImageDataGenerator(rescale=1./255)
test = gen.flow_from_directory(
directory=test_path, class_mode="categorical", target_size=TARGET_SIZE,color_mode='grayscale', batch_size=BATCH_SIZE)
base_model = ResNet50(include_top=False, weights='imagenet',
input_shape=(TARGET_SIZE[0], TARGET_SIZE[1], 3))
x = base_model.output
x = GlobalAveragePooling2D(name='avg_pool')(x)
x = Dense(2, activation='softmax', name='probs')(x)
model = Model(base_model.input, x)
model.compile(optimizer=Adam(lr=LEARNING_RATE),
loss='categorical_crossentropy',
metrics=['accuracy'])
print("start training ...")
history = model.fit(train,
steps_per_epoch=totalTrain // BATCH_SIZE,
epochs=EPOCHS,
validation_data=validation,
validation_steps=totalVal//BATCH_SIZE)
'''
it is solved.instead of use color_mode='grayescale', keep it in RGB and add this function to pass it as preprocessing_function
def gray_to_rgb(img):
x=np.dot(img[...,:3], [0.2989, 0.5870, 0.1140])
mychannel=np.repeat(x[:, :, np.newaxis], 3, axis=2)
return mychannel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With