gpt4 book ai didi

machine-learning - 在keras中集成resnet50和densenet121

转载 作者:行者123 更新时间:2023-11-30 08:53:45 24 4
gpt4 key购买 nike

我想做 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时,它们有自己的输入层,即使我使输入形状相同。不同的输入层会导致冲突。这只是我的猜测,我不知道如何解决它

最佳答案

您可以在创建基础模型时设置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 张量,而不是创建自己的单独输入张量。

关于machine-learning - 在keras中集成resnet50和densenet121,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49136762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com