Keras 进行finetune模型拼接出现“’Model‘ object has no attribute ‘add’解决【原创】

Keras 进行模型拼接出现“’Model‘ object has no attribute ‘add’解决

 

1.部分代码如下:

# build the VGG16 network

model = applications.VGG16(weights='imagenet', input_shape=(150,150,3),include_top=False)
print('Model loaded.')

# build a classifier model to put on top of the convolutional model

top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
top_model.load_weights(top_model_weights_path)
model.add(top_model)

跟着官方Keras 给出的代码,想通过sequential 序贯式的方式来进行finetune 

不料发现了诸多报错,其中一个很严重的问题就是

 AttributeError: 'Model' object has no attribute 'add'

作者本人因这个问题尝试了5天,一直百度不到答案,本以为在序贯式模型中无法正常add【拼接】,终于在26号早晨的GitHub上的问答中找到该问题的解决方法,一试就通!开森啊!~

2.解决:

"""将modle.add 替换成如下"""
model = Model(input = model.input,output = top_model(model.output))

后来又在答案堆里看到,应该考虑到Use plurals in the args

3.修改如下:

@JWGS1 
Use plurals in the args:

model = Model(inputs=base_model.input, outputs=top_model(base_model.output))

 作者本人亲自尝试,以上两种都可以正常运行,没有问题。

该文作为技术分享,以及个人的技术成长,与君共勉~

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章