【问题标题】:Ensemble resnet50 and densenet121 in keras在 keras 中集成 resnet50 和 densenet121
【发布时间】:2018-03-06 17:30:54
【问题描述】:

我想做一个 resnet50 和 desnsenet121 的合奏,但是出错了:

图形断开连接:无法在“input_8”层获取张量 Tensor("input_8:0", shape=(?, 224, 224, 3), dtype=float32) 的值。访问以下先前层没有问题:[]

以下是我的集成代码:

from keras import applications
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.models import Model, Input
#from keras.engine.topology import Input
from keras.layers import Average

def resnet50():
    base_model = applications.resnet50.ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
    last = base_model.output
    x = Flatten()(last)
    x = Dense(2000, activation='relu')(x)
    preds = Dense(200, activation='softmax')(x)
    model = Model(base_model.input, preds)
    return model

def densenet121():
    base_model = applications.densenet.DenseNet121(weights='imagenet', include_top=False, input_shape=(224,224, 3))
    last = base_model.output
    x = Flatten()(last)
    x = Dense(2000, activation='relu')(x)
    preds = Dense(200, activation='softmax')(x)
    model = Model(base_model.input, preds)
    return model

resnet50_model = resnet50()
densenet121_model = densenet121()
ensembled_models = [resnet50_model,densenet121_model]
def ensemble(models,model_input):
    outputs = [model.outputs[0] for model in models]
    y = Average()(outputs)
    model = Model(model_input,y,name='ensemble')
    return model

model_input = Input(shape=(224,224,3))
ensemble_model = ensemble(ensembled_models,model_input)

我认为原因是当我将reset50和densenet121结合起来时,它们有自己的输入层,即使我使输入形状相同。不同的输入层会导致冲突。这只是我的猜测,我不确定如何解决它

【问题讨论】:

    标签: machine-learning computer-vision deep-learning keras


    【解决方案1】:

    您可以在创建基础模型时设置input_tensor=model_input。

    def resnet50(model_input):
        base_model = applications.resnet50.ResNet50(weights='imagenet', include_top=False, input_tensor=model_input)
        # ...
    
    def densenet121(model_input):
        base_model = applications.densenet.DenseNet121(weights='imagenet', include_top=False, input_tensor=model_input)
        # ...
    
    model_input = Input(shape=(224, 224, 3))
    resnet50_model = resnet50(model_input)
    densenet121_model = densenet121(model_input)
    

    然后,基本模型将使用提供的 model_input 张量,而不是创建自己的单独输入张量。

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 2018-04-24
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多