如何在已保存的Keras顺序模型中添加新类 [英] How add new class in saved keras sequential model

查看:105
本文介绍了如何在已保存的Keras顺序模型中添加新类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10个类别的数据集,因此我的准确率达到了85%,在保存的模型上达到了相同的准确度. 现在我想添加一个新类,如何向保存的模型中添加一个新类. 我尝试删除最后一层并进行训练,但是模型变得过拟合,并且在预测中,每个Images都显示相同的结果(新添加的类).

I have 10 class dataset with this I got 85% accuracy, got the same accuracy on a saved model. now I want to add a new class, how to add a new class To the saved model. I tried by deleting the last layer and train but model get overfit and in prediction every Images show same result (newly added class).

这就是我所做的

model.pop()
base_model_layers = model.output
pred = Dense(11, activation='softmax')(base_model_layers)
model = Model(inputs=model.input, outputs=pred)
# compile and fit step

我已经训练了10类的模型,我想用11类的数据加载模型火车并给出预测.

I have trained model with 10 class I want to load the model train with class 11 data and give predictions.

推荐答案

我假设问题出在单标签-多类分类上,即一个样本将仅属于11个类中的1个.

I am assuming that the problem is singlelabel-multiclass classification i.e. a sample will belong to only 1 of the 11 classes.

这个答案将完全基于实现人类学习机器的方式.因此,这不会为您提供执行该操作的正确代码,但是会告诉您该怎么做,并且您可以轻松地在keras中实现它.

This answer will be completely based on implementing the way humans learn into machines. Hence, this will not provide you with a proper code of how to do that but it will tell you what to do and you will be able to easily implement it in keras.

当您教孩子一个新事物时,他将如何学习?首先,我们要求他忘记旧事物并学习新事物.这实际上并不意味着旧的学习是无用的,而是意味着在他学习新知识的时候,旧的知识不应受到干扰,因为它将使大脑混乱.因此,孩子只会在一段时间内学习新知识.

How does a human child learn when you teach him new things? At first, we ask him to forget the old and learn the new. This does not actually mean that the old learning is useless but it means that for the time while he is learning the new, the old knowledge should not interfere as it will confuse the brain. So, the child will only learn the new for sometime.

但是这里的问题是,事情是相关的.假设孩子学习了C编程语言,然后学习了编译器.编译器和编程语言之间存在联系.如果孩子单独学习这些学科,他们将无法掌握计算机科学,对吗?至此,我们引入了智能"一词.

But the problem here is, things are related. Suppose, the child learned C programming language and then learned compilers. There is a relation between compilers and programming language. The child cannot master computer science if he learns these subjects separately, right? At this point we introduce the term 'intelligence'.

了解自己以前学到的东西与现在所学到的东西之间存在某种联系的孩子是智能的".发现这两者之间存在实际联系的孩子是聪明的". (深入了解这是没有主题的)

The kid who understands that there is a relation between the things he learned before and the things he learned now is 'intelligent'. And the kid who finds the actual relation between the two things is 'smart'. (Going deep into this is off-topic)

我想说的是:

  • 让模型分别学习新课程.
  • 然后,使模型找到先前学习的类和新类之间的关系.

为此,您需要训练两种不同的模型:

To do this, you need to train two different models:

  1. 该模型将学习在新类上进行分类:该模型将是一个二进制分类器.如果样本属于第11类,则预测为1,否则为0.现在,您已经具有属于第11类的样本的训练数据,但可能没有不属于第11类的样本的数据.为此,您可以随机选择属于第1到第10类的样本.但是请注意为了正确训练模型,属于11类的样本与不属于11类的样本之比必须为1:1.这意味着50%的样本必须属于11类.
  2. 现在,您有两个单独的模型:一个模型预测1-10类,另一个模型预测11类.现在,将这两个模型(最后第二层)的输出与一个新创建的具有11个节点的Dense层连接起来然后让整个模型重新训练自己,以调整预训练的两个模型的权重并学习密集层的新权重.保持低学习率.

最终模型是第三个模型,它是两个模型(没有最后一个Dense层)+一个新的Dense层的组合.

The final model is the third model which is a combination of two models (without last Dense layer) + a new Dense layer.

谢谢..

这篇关于如何在已保存的Keras顺序模型中添加新类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆