简体   繁体   English

将两个预训练模型与 2 个不同的类(数据集)结合起来进行预测

[英]Combining two Pre Trained models with 2 different classes (dataset) for Prediction

I have two Pre-Trained and saved Inception Models.我有两个预训练并保存的初始模型。

Model 1 = Inception Model with Imagenet Classes - It can predict an image in 1000 classes.模型 1 = 具有 Imagenet 类的 Inception 模型 - 它可以预测 1000 个类中的图像。

Model 2 = Inception Model with my own classiciation (20 Classes) - It can predict an image in 20 classes.模型 2 = 具有我自己的分类的 Inception 模型(20 个类别) - 它可以预测 20 个类别的图像。 Performed transfer learning and saved the model.执行迁移学习并保存模型。

I would like to combine this both to predict images with 1020 Classes.我想将这两者结合起来预测具有 1020 个类别的图像。

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

Predicts image in 1000 classes预测 1000 个类别的图像

predictions1 = Model1.predict(processed_image)

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

I have performed transfer learning with my own 20 classes.我已经用我自己的 20 个班级进行了迁移学习。 Same input shape for both models.两种模型的输入形状相同。 Predicts image in 20 classes预测 20 个类别的图像

predictions = Model2.predict_classes(precessed_image)

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?如何结合 2 个预训练的 Inception 模型来预测 Imagenet 类(1000 个)和我自己的分类器(20 个类)= 预测 1020 个类的图像?

Please give me your solution with a small snippet(code) as an example for better understanding.请以一个小片段(代码)为例给我您的解决方案,以便更好地理解。 I am pretty new to Keras.我对 Keras 很陌生。

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). 其他选择可能是创建一个简单的分类器,通过它您可以运行图像,并且其预测将是二进制的,并会告诉您要使用哪种模型(1-inception,0-your模型)。 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. 例如,如果通过分类器运行一些图像,如果图像中的对象不在这20个类别中,则预测分布将大致平均地分散在几个类别之间,而没有一个类别会脱颖而出,所有类别都会有更多或更少的信心。 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. 这通常意味着图像不在分类器的范围内-模型不知道将图像放置在何处,因为这不是它以前见过的对象,因此无法确定。

EDIT 编辑

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 . 假设model是简单的二进制分类器,它对类别1初始图像和类别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)

Now the result of the prediction is in variable prediction . 现在,预测结果处于变量prediction Here I used that simple classifier ( model ) to predict which model will actually be used to classify the image (inception or your model). 在这里,我使用了一个简单的分类器( 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. 我使用了inception, prediction将是1000维向量,如果使用my_model ,则prediction将是20维向量。 But it doesn't matter because you can just get the highest value of prediction regardless of the vector size and work with that. 但这无关紧要,因为无论向量大小如何,您都可以获得最高的预测值,并且可以使用它。

You would like to combine the pretrained model labels with your own labels, or in other words you are augmenting the pretrained model with new classes. 您希望将预训练的模型标签与自己的标签结合起来,换句话说,您正在使用新的类来扩充预训练的模型。 The practical approach is to use the basis of transfer learning. 实际的方法是使用迁移学习的基础。

But let me tell you this, this is still a hot research topic. 但是,让我告诉你,这仍然是一个热门的研究主题。 It is easier to retrain with your own classes than to add additional classes. 使用您自己的课程进行重新培训比添加其他课程要容易。 Hard, not impossible! 努力,并非没有!

What you should be doing: One way to do this is to change the last softmax layer to identify more classes than it is designed to label. 您应该做的事情:一种方法是更改​​最后一个softmax层,以识别比其设计的标签更多的类。 Network surgery . 网络手术 You will have to train the model again and it will take more time. 您将不得不再次训练模型,这将花费更多时间。

Another way is to create a new custom model with all 1020 labels and train it on the whole dataset, which is not very efficient and you cannot exploit the weights from the pretrained model and you have to perform full training again. 另一种方法是使用所有1020个标签创建一个新的自定义模型,然后在整个数据集上对其进行训练,这不是很有效,您无法利用预训练模型的权重,而必须再次执行完整训练。

A hack can be to use the checkpoint which already predicts 1000 classes. 黑客可以使用已经预测了1000个类的检查点。 Add your data for the new class. 添加新类的数据。 Now you should combine the new class' data along with Imagenet data set, create TFRecords for all 1020 classes and train from the network checkpoint. 现在,您应该将新类别的数据与Imagenet数据集结合起来,为所有1020个类别创建TFRecords,然后从网络检查点进行训练。

What you are trying to do is called " learning without forgetting ". 您尝试做的事情叫做“ 学习不忘 ”。 Please refer to the below paper for more information about how to implement this. 请参阅以下文件,以获取有关如何实现此功能的更多信息。

https://arxiv.org/abs/1606.09282 https://arxiv.org/abs/1606.09282

And the matlab code is available here. 您可以在此处找到matlab代码。

https://github.com/lizhitwo/LearningWithoutForgetting https://github.com/lizhitwo/LearningWithoutForgetting

You may also try tweaking the below file to get the result want. 您也可以尝试调整以下文件以获得所需的结果。

https://github.com/tensorflow/hub/blob/master/examples/image_retraining/retrain.py https://github.com/tensorflow/hub/blob/master/examples/image_retraining/retrain.py

Additionally, To get more information on retraining with pretrained models refer to the below link. 此外,要获得有关使用预训练模型进行再训练的更多信息,请参考以下链接。

https://www.tensorflow.org/tutorials/images/hub_with_keras https://www.tensorflow.org/tutorials/images/hub_with_keras

Now coming to the answer, 现在来回答,

How do we do it: 我们该怎么做呢:

Rebuild the same model with increased number of labels in the final classification layer then restore all weights from the pretrained inception V3 except the final layer and fine tune the model. 在最终分类层中使用更多数量的标签重建相同的模型,然后从最终层中恢复除最终层之外的所有初始权重,并对模型进行微调。

We need specify two flags for this: 我们需要为此指定两个标志:

1. --pretrained_model_checkpoint_path
2. --fine_tune

The code will looks like this. 该代码将如下所示。

# Build the model. Note that we need to make sure the TensorFlow is ready to
# use before this as this command will not build TensorFlow.
cd tensorflow-models/inception
bazel build //inception:custom_train

# Path to the downloaded Inception-v3 model.
MODEL_PATH="${INCEPTION_MODEL_DIR}/inception-v3/model.ckpt-1456935"

# Directory where the data resides.
DATA_DIR=/tmp/custom-data/

# Directory where to save the checkpoint and events files.
TRAIN_DIR=/tmp/custom_train/

# Run the fine-tuning on the flowers data set starting from the pre-trained
# Imagenet-v3 model.
bazel-bin/inception/flowers_train \
  --train_dir="${TRAIN_DIR}" \
  --data_dir="${DATA_DIR}" \
  --pretrained_model_checkpoint_path="${MODEL_PATH}" \
  --fine_tune=True \
  --initial_learning_rate=0.001 \
  --input_queue_memory_factor=1

Refer to the below documentation for more information. 有关更多信息,请参考以下文档。

https://github.com/tensorflow/models/tree/master/research/inception https://github.com/tensorflow/models/tree/master/research/inception

you can combine every two or more pretrained models, the below is my code for combining two Xception and inceptionResnet models which uses them for classification:您可以组合每两个或多个预训练模型,以下是我用于组合两个 Xception 和 inceptionResnet 模型的代码,它使用它们进行分类:

`"""# **InceptionResnet Model**"""
incp_resnet_base_model=InceptionResNetV2(weights="imagenet",include_top=False,input_shape =(224, 224,3))
incp_resnet_base_model.summary ()

for layer in incp_resnet_base_model.layers:
    layer.trainable = False

#get weights and features from base model    
incp_resnet_features = incp_resnet_base_model.output


"""# **Xception Model**"""

xception_base_model=Xception(weights="imagenet",include_top=False,input_shape =(224, 224,3))
xception_base_model.summary ()

for layer in xception_base_model.layers:
    layer.trainable = False

#get weights and features from base model    
xception_features = xception_base_model.output

"""# **Flatten features**"""

xception_features=Flatten(name="flatten_xception")(xception_features)
incp_resnet_features=Flatten(name="flatten_inceptionResnet")(incp_resnet_features)

"""# ** concat models outpur **"""
all_features = concatenate([xception_features, incp_resnet_features])

hybridModel = Dense(500, activation="relu")(all_features)
hybridModel = Dropout(.2)(hybridModel)
hybridModel = Dense(200, activation="relu")(hybridModel)
hybridModel = Dropout(.2)(hybridModel)
hybridModel = Dense(100, activation="relu")(hybridModel)
hybridModel = Dense(1, activation="sigmoid")(hybridModel)
final_model = Model(inputs=[xception_base_model.input,incp_resnet_base_model.input], outputs=hybridModel,name="final output")
final_model.compile(optimizer='Adam', loss=tensorflow.keras.losses.binary_crossentropy,metrics="accuracy" )
model_hist=final_model.fit([X_train,X_train], y_train,
                           validation_data=([X_valid,X_valid], y_valid), epochs=20,batch_size=200)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 组合两个预训练模型的输出(在不同数据集上训练)并使用某种形式的二元分类器来预测图像 - Combine outputs of two Pre Trained models (trained on different dataset) and use some form of binary classifier to predict images 如何连接 keras 中的两个预训练模型? - how to concatenate two Pre trained models in keras? 数据集训练:融合参数调整和预训练模型 - dataset training: converging on parameter tuning and pre-trained models 如何在Tensorflow中使用不带类的预训练模型? - How to use pre-trained models without classes in Tensorflow? 连接来自两个预训练的 Tensorflow 模型的预测 - Concatenate predictions from two pre-trained Tensorflow models 是否有一些预训练的 LSTM、RNN 或 ANN 模型用于时间序列预测? - Are there some pre-trained LSTM, RNN or ANN models for time-series prediction? 有没有办法集成两个为相同课程训练的 keras (h5) 模型 - Is there a way to ensemble two keras (h5) models trained for same classes 结合两种不同的深度学习模型进行评估 - Combining two different deep learning models for evaluation 组合两个或多个查询集或将不同模型组合为单个输出 - Combining two or more queryset or combining different models as a single output 如何使用 OpenVINO 预训练模型? - How to use OpenVINO pre-trained models?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM