【发布时间】:2021-07-10 18:25:42
【问题描述】:
在这里做了很多努力之后是我的问题,
我有两个模型,两个模型都可以检测 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)
【问题讨论】:
-
看起来您需要更改连接输出的轴。
outputs = tf.concat([y_1, y_2], axis=1)应该可以工作。 -
array([[0., 1., 1., 0.]],试过了,但这是错误的。我认为只有一个应该为 1,其余的应该为 0 -
@Kaveh 我会尝试的。在此之前,请阅读我在以下答案中添加的 cmets。让我知道您建议的解决方案是否可行
-
@Kaveh datascience.stackexchange.com/questions/97733/…这也是我的问题
标签: python tensorflow keras