Merge of Two CNN models with Keras Concatenate API

950 views
Skip to first unread message

asad raza

unread,
May 13, 2021, 5:36:52 PM5/13/21
to Keras-users
I have trained two CNN of the same architecture with Different Datasets. The first dataset shape is (414938,41) and the second dataset shape is (432302,19). I want to merge them models to create a new ensemble CNN model. I have used Keras API concatenate() but it gives a bellow error.

layers.concatenate([model.input,host_model.input])

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

Please there is any technique, code, or other Keras API  so that I can merge two CNN models of different shapes to develop a new ensemble CNN model.

Lance Norskog

unread,
May 13, 2021, 6:57:03 PM5/13/21
to asad raza, Keras-users
Trim the final (1,) dimension from each, concatenate the final large axis, add the final (1,) dimension back on:

left = layers.Reshape((-1, 41))(model.input)
right = layers.Reshape((-1, 19))(host_model.input))
combined = layers.Concatenate([left, right])
final = layers.Reshape((-1, 41+19, 1))(combined)



--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/5d01cedc-f722-4553-a36b-6a4a844da0d7n%40googlegroups.com.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA

asad raza

unread,
May 19, 2021, 2:26:16 AM5/19/21
to Lance Norskog, Keras-users
I have applied that code but it gives the following error

ValueError: Attempt to convert a value (<tensorflow.python.keras.layers.merge.Concatenate object at 0x000001B7B31F3B70>) with an unsupported type (<class 'tensorflow.python.keras.layers.merge.Concatenate'>) to a Tensor.

Lance Norskog

unread,
May 19, 2021, 4:03:33 AM5/19/21
to asad raza, Keras-users
It might be:
 layers.Concatenate()([left, right])

I have not used it lately.

asad raza

unread,
May 19, 2021, 4:22:50 AM5/19/21
to Lance Norskog, Keras-users
thanks. It now works.
Reply all
Reply to author
Forward
0 new messages