简体   繁体   English

我将如何从这个集成模型中获得预测?

[英]How will I get prediction from this ensembled model?

# Load the hdf5 files
from keras.models import load_model

resnet50 = keras.models.load_model('/content/drive/MyDrive/HDF5 Files/RestNet 50 best_model.hdf5')
resnet152 = keras.models.load_model('/content/drive/MyDrive/HDF5 Files/best_model_4.hdf5')

# Get the predictions from each model
predictions1 = resnet50.predict(images)
predictions3 = resnet152.predict(images)

# Combine the predictions using a majority vote
predictions = np.array([predictions1,  predictions3])
predictions = np.mean(predictions, axis=0)
print(predictions)

. . The output I'm getting is [[9.9993783e-01 1.3912816e-06 6.0800008e-05 2.9077312e-09]].我得到的输出是 [[9.9993783e-01 1.3912816e-06 6.0800008e-05 2.9077312e-09]]。 What does this mean?这是什么意思?

Its not clear from your question how many images you are passing and how many categories for classification you have in your problem.从您的问题中不清楚您传递了多少图像以及您的问题中有多少分类类别。 I will try to answer in a generic sense.我将尝试从一般意义上回答。

Say you are passing a batch of 4 images to each of the model restnet50 and resnet152 and both of them are trained on 5 categories then each model will give prediction with shape (4,5).假设您将一批 4 张图像传递给模型restnet50resnet152中的每一个,并且它们都在 5 个类别上进行了训练,那么每个模型都会给出形状为 (4,5) 的预测。

>> predictions1.shape
(4,5)
>> predictions3.shape
(4,5)

To combine them for majority vote, you should code as follows:要将它们组合起来进行多数表决,您应该编写如下代码:

>>predictions = np.dstack((predictions1, predictions3)) # stack along the third axis.
>>predictions.shape
(4,5,2)
>> mean_predictions = np.mean(predictions, axis=2) # find the mean probabilities of both predictions in each category.
>> mean_predictions.shape
(4,5)
>> class_index = np.argmax(mean_predictions,axis=1) # Find the index having highest probability.
>> class_index.shape
(4,)

The final class_index gives the index of class or category to which each of the 4 images belong.最终的 class_index 给出了 4 个图像中的每一个所属的类或类别的索引。 I hope this helps.我希望这有帮助。

BETTER WAY You can create ensemble model in tensorflow in a much better way as follows:更好的方式您可以通过以下更好的方式在 tensorflow 中创建集成模型:

from tensorflow.keras.applications.resnet50 import preprocess_input as process_resnet50
from tensorflow.keras.applications.resnet_v2 import preprocess_input as process_resnetv2
from tensorflow.keras.layers import Lambda,Dense,GlobalMaxPool2D,Concatenate
from tensorflow.keras import Input, Model
from keras.models import load_model

resnet50 = keras.models.load_model('/content/drive/MyDrive/HDF5 Files/RestNet 50 best_model.hdf5')
resnet152 = keras.models.load_model('/content/drive/MyDrive/HDF5 Files/best_model_4.hdf5')


category_count = 5 # you can give your own category count.
inp = Input(input_shape)
resnet_prep50 = tf.keras.layers.Lambda(process_resnet50)(inp)
res_net50 = resnet50(resnet_prep50)
x_resnet50 = GlobalMaxPool2D()(res_net50)
x_resnet50 = Dense(128, activation='relu')(x_resnet50)

resnet_prep152 = tf.keras.layers.Lambda(process_resnetv2)(inp)
res_net152 = resnet152(resnet_prep152)
x_resnet152 = GlobalMaxPool2D()(res_net152)
x_resnet152 = Dense(128, activation='relu')(x_resnet152)

x = Concatenate()([x_resnet50, x_resnet152])
out = Dense(category_count, activation='softmax')(x)

ensemble_model = Model(inputs=inp, outputs = out)
predictions = ensemble_model.predict(images)
predictions = np.argmax(predictions, axis=1)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM