将两个训练有素的模型与2个不同的类(数据集)结合起来进行预测 [英] Combining two Pre Trained models with 2 different classes (dataset) for Prediction

查看:41
本文介绍了将两个训练有素的模型与2个不同的类(数据集)结合起来进行预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个预先训练并保存的盗梦空间模型.

I have two Pre-Trained and saved Inception Models.

模型1 =具有Imagenet类的初始模型-它可以预测1000个类中的图像.

Model 1 = Inception Model with Imagenet Classes - It can predict an image in 1000 classes.

模型2 =具有我自己的分类法的初始模型(20个类别)-它可以预测20个类别的图像.执行转移学习并保存模型.

Model 2 = Inception Model with my own classiciation (20 Classes) - It can predict an image in 20 classes. Performed transfer learning and saved the model.

我想将两者结合起来以预测1020个类别的图像.

I would like to combine this both to predict images with 1020 Classes.

Model1 = inception_v3.InceptionV3(weights='imagenet')

预测1000个类别的图像

Predicts image in 1000 classes

predictions1 = Model1.predict(processed_image)

Model2 = InceptionV3(weights='imagenet',
              include_top=False,
              input_shape=(224, 224, 3))

我已经在20堂课上进行了转移学习.两个模型的输入形状相同.预测20个类别的图像

I have performed transfer learning with my own 20 classes. Same input shape for both models. Predicts image in 20 classes

predictions = Model2.predict_classes(precessed_image)

我如何结合2个预训练的初始模型来预测Imagenet类(1000个)和我自己的分类器(20个类)=预测1020个类上的图像?

请给我您的解决方案,并附上一个小片段(代码),以使您更好地理解.我对Keras相当陌生.

How do I combine 2 Pre-Trained Inception Model to predict Imagenet classes (1000) and my own classifiers (20 Classes) = predict images on 1020 classes?

Please give me your solution with a small snippet(code) as an example for better understanding. I am pretty new to Keras.

推荐答案

此处没有正确的答案.例如,您可以做的是通过两个分类器运行图像,并查看其最佳预测具有较高的置信度.其他选择可能是创建一个简单的分类器,通过它您可以运行图像,并且其预测将是二进制的,并会告诉您要使用的模型(1-inception,0-your模型).另一个选择是查看预测分布.例如,如果通过分类器运行某些图像,如果图像中的对象不在这20个类别之一中,则预测分布将大致平均地分散在少数几个类别中,而没有一个类别会脱颖而出,它们都将具有更多类别或更少的信心.这通常意味着图像不在分类器的范围内-模型不知道将图像放置在何处,因为该图像不是它以前见过的对象,因此无法确定.

There is no right answer here. What you can do is for example, run the image through both classifiers and see whose best prediction has higher confidence. Other option could be creating a simple classifier through which you will run your image and whose prediction will be binary and will tell you which model to use (1-inception, 0-your model). Another option is to see the prediction distribution. For example, if you run some image through your classifier, if the object in an image is not in one of those 20 classes, prediction distribution will be scattered approximately equally between few classes with none of them standing out, all of them will have more or less the same confidence. That usually means that the image is not in the scope of the classifier - the model doesn't know where to put an image because that's not an object it has seen before so it cannot decide.

编辑

比方说, model 是简单的二进制分类器,可将图像分类为 1 的初始图像,并将模型的图像分类为 0 的模型.然后您可以执行以下操作:

Let's say model is the simple binary classifier that classifies images for inception in class 1 and images for your model in class 0. Then you could do something like this:

# open image
img = Image.open(path_to_image)

if model.predict(img): # positive case (classifier classified img to class '1')
  prediction = inception_model.predict(img)
else: # negative case (classifier classified img to class '0')
  prediction = my_model.predict(img)

现在,预测结果位于变量 prediction 中.在这里,我使用了一个简单的分类器( model )来预测哪个模型将实际用于对图像进行分类(初始或您的模型).我已使用Inception,预测将是一个1000维矢量,如果使用 my_model prediction 将是20维矢量.但这并不重要,因为无论向量大小如何,您都可以获得最高的预测值.

Now the result of the prediction is in variable prediction. Here I used that simple classifier (model) to predict which model will actually be used to classify the image (inception or your model). I inception has been used, prediction will be a 1000-dimensional vector and if my_model has been used, prediction will be a 20-dimensional vector. But it doesn't matter because you can just get the highest value of prediction regardless of the vector size and work with that.

这篇关于将两个训练有素的模型与2个不同的类(数据集)结合起来进行预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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