试图在keras中向CNN模型添加输入层 [英] trying to add an input layer to CNN model in keras

查看:403
本文介绍了试图在keras中向CNN模型添加输入层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将输入添加到并行路径cnn中,以形成残差的体系结构,但是我遇到了尺寸不匹配的情况。

I tried to add input to a parallel path cnn, to make a residual architecture, but I am getting dimension mismatch.


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) 

我遇到了错误

---------------------------------------------------------------------------
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

我正在使用keras 2.1.6版本。请帮助解决此
final_model.summary()

I am using keras 2.1.6 version. Kindly help to resolve this final_model.summary()

推荐答案

您必须删除以下行:

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

这没有任何意义。您有一个接受输入的方法,分为两个并行模型。它们两个( parallel1 parallel2 )的输出都是长度为 512的向量。然后,您可以连接以使其长度为 1024 添加的长度再次为 512 。然后, concat 会经过进一步的密集层。

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. Then you can either Concatenate them to have a length of 1024 or Add them to have a length of 512 again. The concat then goes through further Dense layers.

简而言之,删除以下行:

So in short, remove the following line:

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

如果要连接并具有长度向量1024,保持其余代码不变,否则,如果要添加它们并使用长度为512的向量,则替换以下行:

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:

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



with this:

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

这篇关于试图在keras中向CNN模型添加输入层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆