在做了很多努力之后,这里是我的问题,
我有两个模型,两个模型都可以检测到2-2个类。正如我们所知道的,我们可以使用FunctionalAPI合并两个模型。我试过了,但是我没有得到想要的结果。
我的目标:我想合并这些模型,更新后的模型应该有(1个输入,4个输出)。
inputs = tf.keras.Input(shape=(50,50,1))
y_1 = f1_Model(inputs)
y_2 = f2(inputs)
outputs = tf.concat([y_1, y_2], axis=0)
new_model = keras.Model(inputs, outputs)
new_model.summary()Model: "functional_5"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_2 (InputLayer) [(None, 50, 50, 1)] 0
__________________________________________________________________________________________________
sequential (Sequential) (None, 2) 203874 input_2[0][0]
__________________________________________________________________________________________________
sequential_1 (Sequential) (None, 2) 203874 input_2[0][0]
__________________________________________________________________________________________________
tf_op_layer_concat (TensorFlowO [(None, 2)] 0 sequential[1][0]
sequential_1[1][0]
==================================================================================================
Total params: 407,748
Trainable params: 407,748
Non-trainable params: 0
__________________________________________________________________________________________________当我在其中传递一个图像时,它给出了错误的结果。我不知道我哪里错了。
prediction = new_model.predict([prepare(img)])
prediction
# index_pred=np.argmax(prediction) (this should return from 0 to 3, but not happening)
array([[1., 0.],
[1., 0.]], dtype=float32)发布于 2021-07-11 17:56:19
据我所知,你想要分类4类,为此,你有2个模型,每个分类2类。
到目前为止,您的f1和f2模型输出的是softmax activation的结果,所以首先,您必须删除它并仅输出logit或relu activation。之后,如@dmg2所述,您必须在tf.concat中设置axis=1。最后,您必须通过一个新的softmax激活来传递输出。在那之后,我希望你能训练你的模型。
https://stackoverflow.com/questions/68330534
复制相似问题