您当前的位置:首页 > IT编程 > 深度学习
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:在keras中合奏resnet50和densenet121

51自学网 2020-11-09 11:37:07
  深度学习
这篇教程在keras中合奏resnet50和densenet121写得很实用,希望能帮到您。

在keras中合奏resnet50和densenet121

我想做一个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 张量,而不是创建自己的单独输入张量 .


keras模型集成
keras: 用预训练的模型提取特征
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。