如何使用 Python 的预训练模型开发投票集成?

问题描述

我正在尝试使用 Keras 和 Tensorflow 创建一个自定义 CNN 和预训练的 inceptionV3、MobileNetV2 和 Xception 组成的集合,用于医学图像分类任务。保存后加载模型代码如下:

def load_all_models():
all_models = []
model_names = ['model1.h5','model2.h5','model3.h5']
for model_name in model_names:
    filename = os.path.join('models',model_name)
    model = tf.keras.models.load_model(filename)
    predictsDepth = np.argmax(model.predict(X_test),axis=1)
    all_models.append(model)
    print('loaded:',filename)
    labels = np.array(labels)
return all_models
models = load_all_models()
for i,model in enumerate(models):
for layer in model.layers:
 if layer._name == "Flatten":
    layer._name = "Flatten_{}".format( i )
    layer.trainable = False

enter image description here

加载模型后,我尝试使用投票来构建集成模型。投票代码如下:

from sklearn.ensemble import RandomForestClassifier,VotingClassifier

  model = VotingClassifier(estimators=[('InceptionV3_Accuracy',model1),('MobileNetV2_Accuracy',model2),('Xception_Accuracy',model3)],voting='hard')
  model.fit(X_train,y_train)

当适合合奏时,我收到以下错误

enter image description here

块引用

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)