首页 文章

为功能性keras模型格式化具有多个类别的多个输入并将其提供给模型

提问于
浏览
0

我无法弄清楚如何正确地将训练数据提供给功能性keras模型 . 我有两种输入类型:图像数据和浮点数,每个数字属于一个图像 . 该数据分为6类 . 如何格式化输入数据以及如何在keras网络中定义?图像数据由CNN分析,然后应与浮点数连接 . 然后,使用三个密集层进行分类 . 似乎没有与我的问题类似的示例或教程 .

1 回答

  • 1

    两个独立的输入:

    imageInput = Input(image_shape) #often, image_shape is (pixelsX, pixelsY, channels)    
    floatInput = Input(float_shape) #if one number per image, shape is: (1,)
    

    卷积部分:

    convOut = SomeConvLayer(...)(imageInput)
    convOut = SomeConvLayer(...)(convOut)
    #...
    convOut = SomeConvLayer(...)(convOut)
    

    如有必要,请执行与其他输入类似的操作 .

    加入两个分支:

    #Please make sure you use compatible shapes
    #You should probably not have spatial dimensions anymore at this point
    #Probably some kind of GloobalPooling:
    convOut = GlobalMaxPooling2D()(convOut)
    
    #concatenate the values:
    joinedOut = Concatenate()([convOut,floatInput]) 
        #or some floatOut if there were previous layers in the float side
    

    使用您的联接输出执行更多操作:

    joinedOut = SomeStuff(...)(joinedOut)
    joinedOut = Dense(6, ...)(joinedOut)
    

    使用两个输入创建模型:

    model = Model([imageInput,floatInput], joinedOut)
    

    火车:

    model.fit([X_images, X_floats], classes, ...)
    

    其中 classes 是"one-hot encoded"张量,包含每个图像的正确类别 .


    但是,没有“一个正确的解决方案” . 你可以尝试很多不同的东西,例如在卷积中间的某处“添加数字”,或者将它相乘,或者在你设法以某种方式连接数值后创建更多的卷积......这就是艺术 .

    输入数据

    输入和输出数据应为 numpy arrays .

    阵列的形状应为:

    - Image input: `(number_of_images, side1, side2, channels)`    
    - Floats input: `(number_of_images, number_of_floats_per_image)`    
    - Outputs: `(number_of_images, number_of_classes)`
    

    Keras将从这些形状知道所有必要的东西,所有阵列中的第0行将是图像0,第1行将是图像1,依此类推 .

相关问题