0

So basically, I'm creating a CNN with Keras and Tensorflow backend. I'm at the point where I want to insert two layers that have the same input layer and then concatenate them, like so:

model = Sequential()

model.add(Convolution1D(128, (4), activation='relu',input_shape=(599,128))
model.add(MaxPooling1D(pool_size=(4)))
model.add(Convolution1D(256, (4), activation='relu')
model.add(MaxPooling1D(pool_size=(2)))
model.add(Convolution1D(256, (4), activation='relu')
model.add(MaxPooling1D(pool_size=(2)))
model.add(Convolution1D(512, (4), activation='relu')

# output 1 = GlobalMaxPooling1D() # from last conv layer
# output 2 = GlobalAveragePooling1D() # from last conv layer
# model.add(Concatenate((output 1, output 2))
# at this point output should have a shape of 1024,1 (from 512 * 2)

model.add(Dense(1024))
model.add(Dense(512))

To show this graphically in a simple way:

    ...
    cv4
    / \
   /   \
gMaxP|gAvrgP   (each 512,)
   \   /
    \ /
   dense(1024,)

I have the feeling I am missing something stupidly obvious. Can anybody wake me up?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103

1 Answers1

0

Use the Model class API, then it should be something like this:

inputs = Input(shape=(599,128), name='image_input')

x = Convolution1D(128, (4), activation='relu')(inputs)
x = MaxPooling1D(pool_size=(4))(x)
x = Convolution1D(256, (4), activation='relu')(x)
x = MaxPooling1D(pool_size=(2))(x)
x = Convolution1D(256, (4), activation='relu')(x)
x = MaxPooling1D(pool_size=(2))(x)
x = Convolution1D(512, (4), activation='relu')(x)


output_1 = GlobalMaxPooling1D()(x) # from last conv layer
output_2 = GlobalAveragePooling1D()(x) # from last conv layer
x = concatenate([output_1, output_2])
# at this point output should have a shape of 1024,1 (from 512 * 2)

x = Dense(1024)(x)
x = Dense(512)(x)
dahoiv
  • 85
  • 1
  • 5
  • I like your method, but I was hoping to find something that i can use with the Sequential.add method. Besides, i tried running your code and i get the same error i got before. `AttributeError: 'Concatenate' object has no attribute 'get_shape'` – Filippo Capurso Apr 29 '17 at 06:23
  • Obs, should be concatenate. I have updated the answer now. – dahoiv Apr 29 '17 at 07:05
  • Your model is not sequential, so it is not possible to use the Sequential.add method. – dahoiv Apr 29 '17 at 07:33
  • I was about to ask that. Had to look up again the definition of sequential ;) Cheers – Filippo Capurso Apr 29 '17 at 07:35