Getting the errors when trying to stack deep learning models

I fine tuned 4 deep learning models on my image classification datasets,now i wanted to create an ensemble of these 4 to produce better accuracy.I tried looking into the process o few blogs but when i follow them im getting errors.Please help me resolve it

import tensorflow as tf

from tensorflow.keras import layers

models = [densenet, vgg19, xception, effnet]

inputs = layers.Input(shape=(128,128,3),name=“Input-Layer”)

outputs = [model(inputs) for model in models]

ensemble_output = layers.Concatenate()(outputs)

ensemble_model = tf.keras.Model(inputs=inputs,outputs=ensemble_output)


AttributeError Traceback (most recent call last)
in <cell line: 7>()
5 inputs = layers.Input(shape=(128,128,3),name=“Input-Layer”)
6 outputs = [model(inputs) for model in models]
----> 7 ensemble_output = layers.Concatenate()(outputs)
8 ensemble_model = tf.keras.Model(inputs=inputs,outputs=ensemble_output)

1 frames
/usr/local/lib/python3.10/dist-packages/keras/src/layers/merging/base_merge.py in (.0)
248
249 def compute_output_spec(self, inputs):
→ 250 output_shape = self.compute_output_shape([x.shape for x in inputs])
251 output_sparse = all(x.sparse for x in inputs)
252 return KerasTensor(

AttributeError: Exception encountered when calling Concatenate.call().

‘list’ object has no attribute ‘shape’

Arguments received by Concatenate.call():
• args=([[‘<KerasTensor shape=(None, 4), dtype=float32, sparse=False, name=keras_tensor_3508>’], [‘<KerasTensor shape=(None, 4), dtype=float32, sparse=False, name=keras_tensor_3509>’], [‘<KerasTensor shape=(None, 4), dtype=float32, sparse=False, name=keras_tensor_3510>’], [‘<KerasTensor shape=(None, 4), dtype=float32, sparse=False, name=keras_tensor_3511>’]],)
• kwargs=<class ‘inspect._empty’>

Note:The models array contains the variables to which the saved fine tuned models are loaded

@SaiSant from TF make sure you are importing Concatenate properly; I would also check the output sizes of your models to ensure they are compatible.

See if this site might help in getting an example up once and then tweaking it:

1 Like