【问题标题】:how can i load a weight file with n classes in a CNN model with n+1 classes如何在具有 n+1 个类的 CNN 模型中加载具有 n 个类的权重文件
【发布时间】:2019-05-06 04:56:22
【问题描述】:

我已经建立了一个带有 3 个类别标签的 CNN 模型,并且还为它生成了权重(.h5 文件)。我想使用相同模型的权重文件,但唯一的区别是它现在有 4 个类而不是 3 个。

我尝试过使用

checkpoint = model.load_weights("filename.h5") 

但我总是将错误视为类不匹配。

任何人都可以帮助我提出任何建议

【问题讨论】:

  • 你是如何创建模型的,Sequential model APIfunctional API
  • 序列模型API

标签: python keras


【解决方案1】:

也许可以遵循 Keras: removing layers with model.layers.pop() doesn't work 中的 yanboliang 建议,用 4 类输出的新层覆盖模型的最后一层?

【讨论】:

  • 我尝试将其发布为答案,但由于某种原因我不能。所以我将它作为评论发布,希望它可以帮助某人。由于“model.layers.pop()”对我不起作用,我所做的就是调用旧模型,将旧模型添加到新模型中,然后添加 Dense 层。这是执行此操作的代码: prev_model = load_model("model name") prev_model.summary() new_model = Sequential() new_model.add(prev_model) new_model.add(Dense(1024)) new_model.add(LeakyReLU(alpha= 0.1)) new_model.add(Dropout(0.5)) new_model.add(Dense(4,activation='softmax'))
猜你喜欢
  • 2014-02-03
  • 2021-01-19
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
相关资源
最近更新 更多