简体   繁体   English

尝试在 keras 中向 CNN model 添加输入层

[英]trying to add an input layer to CNN model in keras

I tried to add input to a parallel path cnn, to make a residual architecture, but I am getting dimension mismatch.我尝试将输入添加到并行路径 cnn,以制作残差架构,但我发现尺寸不匹配。


from keras import layers, Model
input_shape = (128,128,3) # Change this accordingly
my_input = layers.Input(shape=input_shape) # one input
def parallel_layers(my_input, parallel_id=1):
  x = layers.SeparableConv2D(32, (9, 9), activation='relu', name='conv_1_'+str(parallel_id))(my_input)
  x = layers.MaxPooling2D(2, 2)(x)
  x = layers.SeparableConv2D(64, (9, 9), activation='relu', name='conv_2_'+str(parallel_id))(x)
  x = layers.MaxPooling2D(2, 2)(x)
  x = layers.SeparableConv2D(128, (9, 9), activation='relu', name='conv_3_'+str(parallel_id))(x)
  x = layers.MaxPooling2D(2, 2)(x)
  x = layers.Flatten()(x)
  x = layers.Dropout(0.5)(x)
  x = layers.Dense(512, activation='relu')(x)

  return x

parallel1 = parallel_layers(my_input, 1)
parallel2 = parallel_layers(my_input, 2)

concat = layers.Concatenate()([parallel1, parallel2])
concat=layers.Add()(concat,my_input)
x = layers.Dense(128, activation='relu')(concat)
x = Dense(7, activation='softmax')(x)

final_model = Model(inputs=my_input, outputs=x)

final_model.fit_generator(train_generator, steps_per_epoch = 
    nb_train_samples // batch_size, epochs = epochs, validation_data = validation_generator,
    validation_steps = nb_validation_samples // batch_size) 

I am getting the error我收到错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-163442df0d4c> in <module>()
      1 concat = layers.Concatenate()([parallel1, parallel2])
----> 2 concat=layers.Add()(concat,my_input)
      3 x = layers.Dense(128, activation='relu')(parallel2)
      4 x = Dense(7, activation='softmax')(x)
      5 

TypeError: __call__() takes 2 positional arguments but 3 were given

I am using keras 2.1.6 version.我正在使用 keras 2.1.6 版本。 Kindly help to resolve this final_model.summary()请帮助解决这个 final_model.summary()

define your add layer in this way以这种方式定义您的添加层

concat=layers.Add()([concat,my_input])

You have to remove the following line:您必须删除以下行:

concat=layers.Add()(concat,my_input)

It does not make any sense.这没有任何意义。 You have a method that takes an input, branches into two parallel models.你有一个方法,它接受一个输入,分支成两个并行模型。 The outputs of both of them ( parallel1 and parallel2 )are vectors of length 512 .它们( parallel1parallel2 )的输出都是长度为512的向量。 Then you can either Concatenate them to have a length of 1024 or Add them to have a length of 512 again.然后,您可以将它们Concatenate起来使其长度为1024 ,或者Add它们再次添加长度为512 The concat then goes through further Dense layers. concat然后通过进一步的Dense层。

So in short, remove the following line:简而言之,删除以下行:

concat=layers.Add()(concat,my_input)

If you want to concatenate and have a vector of length 1024, keep the rest of the code as it is, otherwise, if you want to add them and have a vector of length 512 instead, replace the following line:如果要连接并具有长度为 1024 的向量,请保持代码的 rest 不变,否则,如果要添加它们并具有长度为 512 的向量,请替换以下行:

concat = layers.Concatenate()([parallel1, parallel2])

with this:有了这个:

concat = layers.Add()([parallel1, parallel2])

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

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