You need to enable JavaScript to run this app. 如何基于双输入Keras CNN适配双图像数据集完成火灾分类训练
优惠活动
大模型
产品
解决方案
定价
更多

如何基于双输入Keras CNN适配双图像数据集完成火灾分类训练

异构双图像输入CNN火灾二分类实现与效果说明

模型适配改造与训练流程搭建

原参考代码的结构为单图像输入+数值型输入的双分支,可直接将数值输入分支替换为第二个图像输入分支,适配两个不同尺寸、不同通道的异构数据集。实现时需重点保证两个数据集的样本批次对齐、标签统一,完整可运行的代码如下:

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Input, Dense, Flatten, concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing import image_dataset_from_directory

# 分支1:匹配64*64*3 RGB图像(数据集1)
input_rgb = Input(shape=(64, 64, 3))
x_rgb = Conv2D(32, (3, 3), activation='relu')(input_rgb)
x_rgb = MaxPooling2D((2, 2))(x_rgb)
x_rgb = Conv2D(64, (3, 3), activation='relu')(x_rgb)
x_rgb = MaxPooling2D((2, 2))(x_rgb)
x_rgb = Flatten()(x_rgb)
branch_rgb = Dense(64, activation='relu')(x_rgb)

# 分支2:匹配120*120*1 灰度图像(数据集2)
input_gray = Input(shape=(120, 120, 1))
x_gray = Conv2D(32, (3, 3), activation='relu')(input_gray)
x_gray = MaxPooling2D((2, 2))(x_gray)
x_gray = Conv2D(64, (3, 3), activation='relu')(x_gray)
x_gray = MaxPooling2D((2, 2))(x_gray)
x_gray = Conv2D(128, (3, 3), activation='relu')(x_gray)
x_gray = MaxPooling2D((2, 2))(x_gray)
x_gray = Flatten()(x_gray)
branch_gray = Dense(64, activation='relu')(x_gray)

# 特征融合与最终输出
fused_feat = concatenate([branch_rgb, branch_gray])
fused_feat = Dense(32, activation='relu')(fused_feat)
output = Dense(1, activation='sigmoid')(fused_feat)

# 初始化并编译双输入模型
model = Model(inputs=[input_rgb, input_gray], outputs=output)
model.compile(
    optimizer='adam',
    loss='binary_crossentropy',
    metrics=['accuracy']
)
model.summary()

# 按给定路径加载数据集
batch_size = 32
# 数据集1路径加载
train_ds1 = image_dataset_from_directory(
    '/content/drive/MyDrive/Thesis_Dataset/Devide_Cnn/gt/train',
    image_size=(64, 64),
    color_mode='rgb',
    batch_size=batch_size
)
valid_ds1 = image_dataset_from_directory(
    '/content/drive/MyDrive/Thesis_Dataset/Devide_Cnn/gt/valid',
    image_size=(64, 64),
    color_mode='rgb',
    batch_size=batch_size
)
test_ds1 = image_dataset_from_directory(
    '/content/drive/MyDrive/Thesis_Dataset/Devide_Cnn/gt/test',
    image_size=(64, 64),
    color_mode='rgb',
    batch_size=batch_size
)

# 数据集2路径加载
train_ds2 = image_dataset_from_directory(
    '/content/drive/MyDrive/Thesis_Dataset/Devide_Cnn/rgb/train',
    image_size=(120, 120),
    color_mode='grayscale',
    batch_size=batch_size
)
valid_ds2 = image_dataset_from_directory(
    '/content/drive/MyDrive/Thesis_Dataset/Devide_Cnn/rgb/valid',
    image_size=(120, 120),
    color_mode='grayscale',
    batch_size=batch_size
)
test_ds2 = image_dataset_from_directory(
    '/content/drive/MyDrive/Thesis_Dataset/Devide_Cnn/rgb/test',
    image_size=(120, 120),
    color_mode='grayscale',
    batch_size=batch_size
)

# 构建配对数据集,保证两个输入的样本、标签一一对应
def pair_ds(ds_rgb, ds_gray):
    for (img_rgb, label), (img_gray, _) in zip(ds_rgb, ds_gray):
        yield [img_rgb, img_gray], label

train_paired = pair_ds(train_ds1, train_ds2)
valid_paired = pair_ds(valid_ds1, valid_ds2)
test_paired = pair_ds(test_ds1, test_ds2)

# 启动训练流程
model.fit(
    train_paired,
    validation_data=valid_paired,
    epochs=20,
    steps_per_epoch=len(train_ds1),
    validation_steps=len(valid_ds1)
)

# 测试集评估
test_loss, test_acc = model.evaluate(test_paired, steps=len(test_ds1))
print(f"测试集分类准确率: {test_acc:.4f}")

双分支融合结构的精度提升效果

这种结构并非天然带来精度提升,效果完全取决于两个数据集的特征互补性:

  • 当两个数据集携带的有效信息存在明显互补时,精度提升效果非常明确:
    • 64*64的RGB图像携带色彩信息,可快速识别火焰的亮红色、橙黄色等专属颜色特征,对小尺寸燃烧点的识别敏感度高
    • 120*120的灰度图像空间分辨率更高,可捕捉更广视角下的场景结构、早期烟雾的灰度渐变、物体边缘轮廓信息,弥补RGB小尺寸图视野受限的问题
    • 两类特征融合后可覆盖单模态的识别盲区,减少误判漏判
  • 以下场景下该结构不会带来精度增益,甚至会拉低效果:
    • 两个数据集的样本来源完全重叠(比如灰度图只是同批次RGB图像直接转灰度得到,没有额外的独立信息),此时模型参数量翻倍但没有新增有效信息,极易引发过拟合
    • 两个数据集的标签存在系统性偏差,比如灰度数据集把非火灾的暗区标注为火灾,RGB数据集把红色物体标注为火灾,融合后会放大两类标注错误
    • 两个分支的特征维度差距过大,训练时单分支的特征会主导融合结果,相当于浪费了另一个分支的计算开销

内容的提问来源于stack exchange,提问作者A.K.Z. Rasel Rahman 170216

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.15 13:36:04