简体   繁体   English

keras功能api和多个合并层

[英]keras functional api and multiple merge layers

I am trying to design a neural network using keras. 我正在尝试使用keras设计神经网络。 The model.summary() output is different compared to defined layers 与定义的图层相比,model.summary()输出是不同的

import numpy as np
np.random.seed(1337) 

from keras.models import Model
from keras.layers import Input, Convolution2D, MaxPooling2D, Activation, Flatten, merge

from keras import backend as K
K.set_image_dim_ordering('th')

input_shape = (3, 225, 225)
inp = Input(input_shape)

seq0 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode="same")(inp)
seq1 = Convolution2D(32, 1, 1, border_mode="same", activation="relu")(seq0)
seq2 = Convolution2D(32, 1, 1, border_mode="same", activation="relu")(seq1)
seq3 = merge([seq2, seq1], mode="concat", concat_axis=1)
seq4 = Convolution2D(32, 1, 1, border_mode="same", activation="relu")(seq3)
seq5 = merge([seq1, seq3], mode="concat", concat_axis=1)
seq6 = Convolution2D(128, 5, 5, border_mode="same", activation="relu")(seq5)
seq7 = merge([seq4, seq3], mode="concat", concat_axis=1)
seq8 = Convolution2D(512, 3, 3, border_mode="same", activation="relu")(seq7)
seq9 = merge([seq5, seq2], mode="concat", concat_axis=1)

seq = Flatten()(seq9)
out = Activation('softmax')(seq)


model = Model(input=inp, output=out)  
model.summary()

model.summary() output model.summary()输出

Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 3, 225, 225)   0                                            
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D)    (None, 3, 113, 113)   0           input_1[0][0]                    
____________________________________________________________________________________________________
convolution2d_1 (Convolution2D)  (None, 32, 113, 113)  128         maxpooling2d_1[0][0]             
____________________________________________________________________________________________________
convolution2d_2 (Convolution2D)  (None, 32, 113, 113)  1056        convolution2d_1[0][0]            
____________________________________________________________________________________________________
merge_1 (Merge)                  (None, 64, 113, 113)  0           convolution2d_2[0][0]            
                                                                   convolution2d_1[0][0]            
____________________________________________________________________________________________________
merge_2 (Merge)                  (None, 96, 113, 113)  0           convolution2d_1[0][0]            
                                                                   merge_1[0][0]                    
____________________________________________________________________________________________________
merge_4 (Merge)                  (None, 128, 113, 113) 0           merge_2[0][0]                    
                                                                   convolution2d_2[0][0]            
____________________________________________________________________________________________________
flatten_1 (Flatten)              (None, 1634432)       0           merge_4[0][0]                    
____________________________________________________________________________________________________
activation_1 (Activation)        (None, 1634432)       0           flatten_1[0][0]                  
====================================================================================================

seq4, seq6, seq8 layers are missing in the model.summary() output. model.summary()输出中缺少seq4,seq6,seq8层。 Am I doing something wrong? 难道我做错了什么?

You are not using them to compute the output. 您没有使用它们来计算输出。

Take for example seq4 : you feed it to seq7, which is fed to seq8 which doesnt go anywhere. 以seq4为例:你把它喂给seq7,它被送到seq8,它不会去任何地方。

There is a problem in the structure of the tree of you model. 您的模型树的结构存在问题。

In the one that is summarised, they took all the layers that lead to output=out from input=inp , the ones that aren't used in that "path" won't be part of the graph of your model. 在汇总的那个中,它们从input=inp output=out导出output=out所有层,那些未在“path”中使用的层将不是模型图的一部分。

The flow going through seq4, seq6, seq7 and seq8 are not leading to the output of your model. 通过seq4,seq6,seq7和seq8的流程不会导致模型的输出。

Does that help you? 这对你有帮助吗?

EDIT : 编辑:

The merge layer functions like this example from your code : 合并层的功能类似于您的代码中的示例:

seq3 = merge([seq2, seq1], mode="concat", concat_axis=1)

Here you take what comes out of the layers seq2 and seq1, they have shapes = (None,32,113,113) . 在这里你可以得到层seq2和seq1的结果,它们有shapes = (None,32,113,113) That's the input of that layer, two different tensors coming out of seq2 and seq1. 这是该层的输入,两个不同的张量来自seq2和seq1。 You precised that you want to concatenate those 4D tensors following the axis 1. Therefore, the output of that merge layer will be of shape = (None,64,113,113) . 你精确地想要在轴1之后连接那些4D张量。因此,该合并层的输出将是shape = (None,64,113,113) the two 32 have been added together during concatenation. 在连接期间,两个32已被添加在一起。 You can read what I've just explained at the line "merge_1" of your model.summary() 您可以阅读我刚刚在您的model.summary() “merge_1”行解释的内容

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

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