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))

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

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

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Monster_H777

我直说吧:你的奖励我的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值