برای اینکار دو راه وجو داره
روش اول احتمال کلاس های (خروجی softmax) مدل ها را میانگین بگیرید
روش دوم میانگین logit های (لایه dense قبل از softmax) مدل ها را میانگین بگیرید روش دوم دقیق تره.
اگر برای لایه logit اسم درنظر گرفته باشید به راحتی می تونید با کد زیر اینکارو انجام بدید:
def get_validation_dataset(ordered=False):
dataset = load_dataset(VALIDATION_FILENAMES, ordered=ordered)
dataset = dataset.batch(BATCH_SIZE)
dataset = dataset.cache()
dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)
return dataset
validation_dataset = get_validation_dataset(ordered=True)
def load_model_and_predict_logits(model_name, dataset):
print("Loading model", model_name)
with strategy.scope():
load_locally = tf.saved_model.LoadOptions(experimental_io_device='/job:localhost')
model = tf.keras.models.load_model(model_name, options=load_locally)
logit_model = tf.keras.Model(inputs=model.inputs,
outputs=[model.get_layer('flower_logits').output, model.outputs])
print("Running model", model_name)
logits, probabilities = logit_model.predict(dataset)
return logits, probabilities
# get logits from all three models
logits1, probabilities1 = load_model_and_predict_logits('trained_model1', validation_dataset)
logits2, probabilities2 = load_model_and_predict_logits('trained_model2', validation_dataset)
logits3, probabilities3 = load_model_and_predict_logits('trained_model3', validation_dataset)
روش اول:
# average logits and compute predictions
probabilities = tf.stack([probabilities1, probabilities2, probabilities3], axis=3)
probabilities = tf.squeeze(probabilities, axis=0)
weights = [0.4, 0.4, 0.2] # the average can be weighted
probabilities = tf.math.reduce_mean(probabilities*weights, axis=2)
predictions = np.argmax(probabilities, axis=-1)
#compare with correct values
correct_labels = get_all_labels(validation_dataset, NUM_VALIDATION_IMAGES)
print("Correct labels: ", correct_labels.shape, correct_labels)
print("Predicted labels: ", predictions.shape, predictions)
cmat, score, precision, recall = compute_confusion_metrics(correct_labels, predictions)
display_confusion_matrix(cmat, score, precision, recall)
روش دوم:
# average logits and compute predictions
logits = tf.stack([logits1, logits2, logits3], axis=2)
weights = [0.4, 0.5, 0.1] # the average can be weighted
logits = tf.math.reduce_mean(logits*weights, axis=2)
probabilities = tf.nn.softmax(logits, axis=1)
predictions = np.argmax(probabilities, axis=-1)
#compare with correct values
correct_labels = get_all_labels(validation_dataset, NUM_VALIDATION_IMAGES)
print("Correct labels: ", correct_labels.shape, correct_labels)
print("Predicted labels: ", predictions.shape, predictions)
cmat, score, precision, recall = compute_confusion_metrics(correct_labels, predictions)
display_confusion_matrix(cmat, score, precision, recall)