Home > OS >  Trying to merge two loaded h5 models into a single one
Trying to merge two loaded h5 models into a single one

Time:12-20

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: single input merged model

CodePudding user response:

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()

merged model summary

  • Related