trying to add an input layer to CNN model in keras

删除回忆录丶 提交于 2020-06-01 07:41:27

问题


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) 

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. Kindly help to resolve this final_model.summary()


回答1:


define your add layer in this way

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



回答2:


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

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


来源:https://stackoverflow.com/questions/61577390/trying-to-add-an-input-layer-to-cnn-model-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!