简体   繁体   English

如何使用两个 DataIterator(s) 训练 CNN model 的双流输入?

[英]How to train a dual streams inputs of CNN model with two DataIterator(s)?

I'm building a convolutional neural network (CNN) model consisting of dual stream image data input of 'RGB' channels and 'grayscale' channel converging into singular stream of shape (None, width, height, 4*C) , then Dense() . I'm building a convolutional neural network (CNN) model consisting of dual stream image data input of 'RGB' channels and 'grayscale' channel converging into singular stream of shape (None, width, height, 4*C) , then Dense() .

流式基础设施的虚拟示例

With big dataset, I must / forced to utilise <class 'ImageDataGenerator'> :对于大数据集,我必须/被迫使用<class 'ImageDataGenerator'>

from tensorflow.keras.preprocessing.image import ImageDataGenerator 

datagen = ImageDataGenerator()

and flow the dataset via directory:并通过目录流动数据集:

train_C = datagen.flow_from_directory(
    ... , 
    color_mode = 'rgb', 
    ...
)

train_gr = datagen.flow_from_directory(
    ... , 
    color_mode = 'grayscale', 
    ... 
)

Unfortunately, to train the model with fit method as the following :不幸的是,要使用以下fit方法训练 model:

model.fit( x = [input_1, input_2], ... )

it raised the following error:它引发了以下错误:

ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'keras_preprocessing.image.directory_iterator.DirectoryIterator'>"}), <class 'NoneType'>

So, how should I resolve this issue?那么,我应该如何解决这个问题呢?

Note: Python 3.X & Tensorflow 2.X注: Python 3.X & Tensorflow 2.X

  1. Prepare your combined train generator: train_generator = zip(train_C, train_gr)准备您的组合火车生成器: train_generator = zip(train_C, train_gr)
  2. Concatenate the output of last layers: fused_1 = concatenate([averagepool, maxpool])连接最后一层的 output: fused_1 = concatenate([averagepool, maxpool])
  3. Create conv or other dense layers: top_model = Dense(4096, activation='relu',name="dense_one")(fused_1)创建 conv 或其他密集层: top_model = Dense(4096, activation='relu',name="dense_one")(fused_1)
  4. Create final model: model = Model(inputs=[model1.input, model2.input], outputs=[top_model])创建最终 model: model = Model(inputs=[model1.input, model2.input], outputs=[top_model])
  5. Fit the model: history = model.fit_generator(train_generator, ...)适合 model: history = model.fit_generator(train_generator, ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM