I have two models, A and B, A is not sequential (it is a UNet that computes the mask of an image), and B is sequential (it gives a label to the masked image). I want to merge these two models into a single one, however, I get an error. The models are the following: model A model B
import tensorflow as tf
import numpy as np
from tensorflow.keras.layers.experimental.preprocessing import Resizing
from tensorflow.keras import Sequential
import keras.backend as K
from keras.layers import Concatenate
pretrainedmodel_files = ["/kaggle/input/model-neaugmentat/model.h5", "/kaggle/input/clasificare-neaugmentat/model_neaugmentat_clasificare.h5"]
A,B = [tf.keras.models.load_model(filename) for filename in pretrainedmodel_files]
new_model = tf.keras.Sequential()
new_model.add(A)
new_model.add(Resizing(400,400))
combined = tf.keras.layers.Concatenate(axis=3)([new_model.output, B.input])
new_model.add(combined)
x = new_model.output
outputs = B(x)
model_merged = tf.keras.Model(A.input, outputs, name = 'merged_model')
model_merged.summary()
Update
import tensorflow
from tensorflow.keras.layers.experimental.preprocessing import Resizing
from tensorflow.keras import Sequential
import keras
import keras.backend as K
from keras.layers import Concatenate, Reshape, Permute
input_a = keras.Input(shape=(256,256,1))
input_b = keras.Input(shape=(400,400,3))
x = A(input_a)
y = Resizing(400,400)(x)
x = A(y)
x = Concatenate(axis=3)([y, x])
x = Reshape((-1,400,400))(x)
x = Resizing(3,400)(x)
x = Permute((2,3,1))(x)
x = B(x)
model_merged = tf.keras.Model(input_a, x)
model_merged.summary()
combined = tf.keras.layers.Concatenate(axis=3)([new_model.output, B.input])
new_model.add(combined)
New model must be a Layer, found a Tensor
You problem is right there. Concatenate(axis=3)
makes a layer. ([new_model.output, B.input])
calls it and returns a tensor, you can't add tensors top a model, only layers.
It looks like you're trying to add a second input to the Model. You can't do that with the sequential API. Forget about the Sequential here and use the functional API. It looks like you're trying to do this:
import tensorflow
from tensorflow.keras.layers.experimental.preprocessing import Resizing
from tensorflow.keras import Sequential
import keras
import keras.backend as K
from keras.layers import Concatenate
input_a = keras.Input(shape=(256,256,1))
input_b = keras.Input(shape=(400,400,2))
x = A(input_a)
x = Resizing(400,400)(x)
x = Concatenate(axis=3)([x, input_b])
x = B(x)
model_merged = tf.keras.Model([input_a, input_b], x)
model_merged.summary()