Search code examples
machine-learningdeep-learningkeraskeras-layer

Concatenating layers in Keras (None,512) and (18577,4)


How do I concatenate 2 layers in keras, when one of layers has its dimensions (None,512) and the other has dimensions (18577,4). I tried using Concatenate

concat_layer = Concatenate()([z1,agp]

But this throw me an error telling:

ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 512), (18577, 4)]

The model looks something like this:

a1= (Convolution2D(32, filter_dim, activation='linear', 
                    padding='same',kernel_regularizer=regularizers.l2(reg)))(input_img) 
b1 = (BatchNormalization())(a1)
c1 = (PReLU())(b1)
d1 = (Convolution2D(32, filter_dim, activation='linear',kernel_regularizer=regularizers.l2(reg)))(c1)
e1 = (BatchNormalization())(d1)
f1 = (PReLU())(e1)
g1 = (MaxPooling2D(pool_size=(2,2)))(f1)
h1 = (Dropout(0.2))(g1)

i1= (Convolution2D(64, filter_dim, activation='linear', padding='same',kernel_regularizer=regularizers.l2(reg)))(h1)
j1 = (BatchNormalization())(i1)
k1 = (PReLU())(j1)
l1 = (Convolution2D(64, filter_dim, activation='linear',kernel_regularizer=regularizers.l2(reg)))(k1)
m1 = (BatchNormalization())(k1)
n1 = (PReLU())(m1)
o1 = (MaxPooling2D(pool_size=(2,2)))(n1)
p1 = (Dropout(0.2))(o1)

q1= (Convolution2D(128, filter_dim, activation='linear', padding='same',kernel_regularizer=regularizers.l2(reg)))(p1)
r1=q1
s1 = (BatchNormalization())(r1)
t1 = (PReLU())(s1)
u1 = (Convolution2D(128, filter_dim, activation='linear',kernel_regularizer=regularizers.l2(reg)))(t1)
v1 = (BatchNormalization())(u1)
w1 = (PReLU())(v1)
x1 = (MaxPooling2D(pool_size=(3,3)))(w1)
y1 = (Dropout(0.2))(x1)



z1 = (Flatten())(y1)
agp=tf.convert_to_tensor(agp,np.float32)
z1 = Concatenate(axis=1)([z1,agp])

a2 = (Dense(128, activation='linear',kernel_regularizer=regularizers.l2(reg)))(z1)
b2 = (BatchNormalization())(a2)
c2 = (PReLU())(b2)
d2 = (Dropout(0.2))(c2)

e2 = (Dense(32, activation='linear',kernel_regularizer=regularizers.l2(reg)))(d2)
f2 = (BatchNormalization())(e2)
g2 = (PReLU())(f2)
h2 = (Dropout(0.3))(g2)

My input image has dimensions (32,32,3). I want to concatenate z1(None,512) with agp (18577,4)


Solution

  • #!/usr/bin/env python
    
    
    def create_model(nb_classes, input_shape):
        """Create a NN model."""
        # from keras.layers import Dropout
        from keras.layers import Activation, Input
        from keras.layers import Dense, Concatenate
        from keras.models import Model
    
        input_ = Input(shape=input_shape)
        x = input_
    
        # Branch in two directions - this can be more
        # complex, of course
        x1 = Dense(512, activation='relu')(x)
        x2 = Dense(4, activation='relu')(x)
    
        # And this is how you use concatenation
        x = Concatenate(axis=-1)([x1, x2])
    
        # And then finish it
        x = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=input_, outputs=x)
        return model
    
    
    model = create_model(10, (512, ))
    print(model.summary())
    

    gives

    Using TensorFlow backend.
    ____________________________________________________________________________________________________
    Layer (type)                     Output Shape          Param #     Connected to                     
    ====================================================================================================
    input_1 (InputLayer)             (None, 512)           0                                            
    ____________________________________________________________________________________________________
    dense_1 (Dense)                  (None, 512)           262656      input_1[0][0]                    
    ____________________________________________________________________________________________________
    dense_2 (Dense)                  (None, 4)             2052        input_1[0][0]                    
    ____________________________________________________________________________________________________
    concatenate_1 (Concatenate)      (None, 516)           0           dense_1[0][0]                    
                                                                       dense_2[0][0]                    
    ____________________________________________________________________________________________________
    dense_3 (Dense)                  (None, 10)            5170        concatenate_1[0][0]              
    ====================================================================================================
    Total params: 269,878
    Trainable params: 269,878
    Non-trainable params: 0
    ____________________________________________________________________________________________________
    None