如何使用Flow_from_directory在TensorFlow中训练图像分类?

发布于 2025-02-12 08:46:07 字数 2477 浏览 0 评论 0原文

我正在培训CNN模型,将灰度图像分类为6个类。当我的代码在RGB图像上运行良好时,当我将其应用于灰度图像时会出现错误。这是代码的一部分:

input_shape=(256, 256,1) # assign "1" to the last channel to account for grayscale.

target_size = (256, 256) # To use it in the flow_from_directory package


model_name='Test1'
model_filename = (model_name+'.hdf5') 

optimizer = Adam(learning_rate=1e-3)
loss=['categorical_crossentropy']
metrics = ['accuracy']


## Here is the model: 

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(6)) # To account for 6 classes
model.add(Activation('softmax'))

model.summary()



train_datagen = ImageDataGenerator(
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)


vaidation_datagen = ImageDataGenerator()



train_generator = train_datagen.flow_from_directory(
        train_path,  # points to the folder containing all training images
        target_size=target_size, 
        color_mode='grayscale', # to specify the grayscale
        batch_size=batch_size,
        shuffle=True,
        class_mode='categorical',
        interpolation='nearest') 


validation_generator = vaidation_datagen.flow_from_directory(
        validation_path,  # points to the folder containing all validation images
        target_size=target_size,
        color_mode='grayscale', # to specify the grayscale
        batch_size=batch_size,
        shuffle=True,
        class_mode='categorical',
        interpolation='nearest')


model.compile(optimizer, loss , metrics)

model_checkpoint = tf.keras.callbacks.ModelCheckpoint((model_path+model_filename), monitor='loss',verbose=1, save_best_only=True)
model.summary()


history = model.fit(
     train_generator,
     steps_per_epoch = num_of_train_img_raw//batch_size,
     epochs = epochs, 
     validation_data = validation_generator,
     validation_steps = num_of_val_img_raw//batch_size,
     callbacks=[model_checkpoint],
     use_multiprocessing = False)


这是我收到的错误:

“输入深度必须通过过滤器深度均匀排除:1 vs 3”,

然后IDE内核冻结!

I am training a CNN model to classify grayscale images into 6 classes. While my code is working well on RGB images, it gives error when I apply it on grayscale images. Here is part of the code:

input_shape=(256, 256,1) # assign "1" to the last channel to account for grayscale.

target_size = (256, 256) # To use it in the flow_from_directory package


model_name='Test1'
model_filename = (model_name+'.hdf5') 

optimizer = Adam(learning_rate=1e-3)
loss=['categorical_crossentropy']
metrics = ['accuracy']


## Here is the model: 

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(6)) # To account for 6 classes
model.add(Activation('softmax'))

model.summary()



train_datagen = ImageDataGenerator(
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)


vaidation_datagen = ImageDataGenerator()



train_generator = train_datagen.flow_from_directory(
        train_path,  # points to the folder containing all training images
        target_size=target_size, 
        color_mode='grayscale', # to specify the grayscale
        batch_size=batch_size,
        shuffle=True,
        class_mode='categorical',
        interpolation='nearest') 


validation_generator = vaidation_datagen.flow_from_directory(
        validation_path,  # points to the folder containing all validation images
        target_size=target_size,
        color_mode='grayscale', # to specify the grayscale
        batch_size=batch_size,
        shuffle=True,
        class_mode='categorical',
        interpolation='nearest')


model.compile(optimizer, loss , metrics)

model_checkpoint = tf.keras.callbacks.ModelCheckpoint((model_path+model_filename), monitor='loss',verbose=1, save_best_only=True)
model.summary()


history = model.fit(
     train_generator,
     steps_per_epoch = num_of_train_img_raw//batch_size,
     epochs = epochs, 
     validation_data = validation_generator,
     validation_steps = num_of_val_img_raw//batch_size,
     callbacks=[model_checkpoint],
     use_multiprocessing = False)


Here is the error I receive:

"input depth must be evenly divisible by filter depth: 1 vs 3"

Then the IDE kernel freezes!

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹倦 2025-02-19 08:46:08

是的,它是浪费/较慢,但是为什么不将灰度图像转换为RGB呢?除非您需要出色的性能或真正想更新模型,否则这两个模型都需要时间。

使用 grayscale_to_rgb (已经内置在tensorflow中)。

Yes it is wasteful/slower but why not just convert the greyscale images into RGB? Unless you need superior performance or really want to update the model both of which will take time to do.

Use grayscale_to_rgb (already built into tensorflow).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文