【问题标题】:how to feed different data formats when training a multi-input Keras model如何在训练多输入 Keras 模型时提供不同的数据格式
【发布时间】:2018-05-16 16:47:45
【问题描述】:

我正在尝试构建用于分类医学图像的多输入 Keras 模型。多输入将包括 (i) 将通过 CNN 传递的原始图像,以及 (ii) 作为辅助输入包含的传统计算机视觉特征。然后将它们连接起来并用作一个密集层的小型神经网络的输入。模型架构描述请看下图:

multi-input model architecture

问题是我无法使用model.fit 训练模型,因为我有一个大型图像数据集(超过一百万),这不适合内存。因此我需要使用model.fit_generator,它使我可以访问flow_from_directory 调用,该调用将从目录中读取图像。但是,现在的问题是无法通过model.fit_generator 输入辅助输入(手动计算机视觉特征,即每张图像 11 个特征的向量)。

如何训练这个自定义网络?

如果我的问题需要更具体或需要更多信息,请告诉我。

【问题讨论】:

    标签: tensorflow keras keras-layer


    【解决方案1】:

    解决方案是在多输入模型的中间使用连接层。见7.1.2 of Deep Learning with Python部分

    from keras.models import Model
    from keras import layers
    from keras import Input
    
    text_vocabulary_size = 10000
    question_vocabulary_size = 10000
    answer_vocabulary_size = 500
    
    # The text input is a variable-length sequence of integers. 
    # Note that you can optionally name the inputs.
    text_input = Input(shape=(None,), dtype='int32', name='text')
    # Embeds the inputs into a sequence of vectors of size 64
    # embedded_text = layers.Embedding(64, text_vocabulary_size)(text_input)
    # embedded_text = layers.Embedding(output_dim=64, input_dim=text_vocabulary_size)(text_input)
    embedded_text = layers.Embedding(text_vocabulary_size,64)(text_input)
    # Encodes the vectors in a single vector via an LSTM
    encoded_text = layers.LSTM(32)(embedded_text)
    # Same process (with different layer instances) for the question
    question_input = Input(shape=(None,),dtype='int32',name='question')
    # embedded_question = layers.Embedding(32, question_vocabulary_size)(question_input)
    # embedded_question = layers.Embedding(output_dim=32, input_dim=question_vocabulary_size)(question_input)
    embedded_question = layers.Embedding(question_vocabulary_size,32)(question_input)
    encoded_question = layers.LSTM(16)(embedded_question) 
    # Concatenates the encoded question and encoded text
    concatenated = layers.concatenate([encoded_text, encoded_question],axis=-1)
    # Adds a softmax classifier on top
    answer = layers.Dense(answer_vocabulary_size, activation='softmax')(concatenated)
    # At model instantiation, you specify the two inputs and the output.
    model = Model([text_input, question_input], answer)
    model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['acc'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-13
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多