在 Keras 中合并多个模型(tensorflow) [英] Merge multiple Models in Keras (tensorflow)

查看:99
本文介绍了在 Keras 中合并多个模型(tensorflow)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里做了很多努力之后,我的问题是,

After doing a lot of effort here is my question,

我有两个模型,两个模型都可以检测 2-2 个类别.众所周知,我们可以使用 FunctionalAPI 合并两个模型.我试过了,但没有得到想要的结果.

I have two models, both models can detect 2-2 classes. As we know that we can merge two models using a FunctionalAPI. I tried it, But I am not getting the desired outcome.

我的目标:我想合并这些模型,更新后的模型应该有(1个输入,4个输出).

My goal: I want to Merge these models, and the updated model should have (1 input, 4 output).

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
__________________________________________________________________________________________________

当我在其中传递图像时,它给出了错误的结果.我不知道我哪里出错了.

When I pass an image in it, it gives the wrong result. I don't know where did I go wrong.

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)

推荐答案

据我所知,您想对 4 个类进行分类,为此,您有 2 个模型,每个模型对 2 个类进行分类.
到目前为止,您的 f1 和 f2 模型输出 softmax 激活 的结果,因此首先,您必须将其删除并仅输出 logits 或仅输出 relu activation.之后,正如@dmg2 所提到的,您必须在 tf.concat 中设置 axis=1 现在最后您必须通过一个新的 <代码>softmax 激活.在那之后,我希望你能训练你的模型.

From what I understand you want to classify 4 classes and for that, you have 2 models which classify 2 classes each.
As of now, your f1 and f2 model outputs the result of softmax activation so first, you have to remove it and output just the logits or just relu activation. After that as mentioned by the @dmg2, you have to set the axis=1 in the tf.concat now at the end you have to pass the output through a new softmax activation. After that, I hope you could train your model.

这篇关于在 Keras 中合并多个模型(tensorflow)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆