我想做一个resnet50和desnsenet121的集合,但得到一个错误:
图表已断开连接:无法在图层“input_8”处获取张量张量(“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)
您可以在创建基本模型时设置 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
张量,而不是创建自己的单独输入张量 .