I’m trying to take two retrained clients in a federated learning pipeline and average their weights. It worked for the first training, but now the second time I’m getting this error can I have tried to fix, but I can’t figure out what the issue is.
Here is the error which originates from the line main(model_FTM_path, model_Stanford_path, save_path)
ValueError: The name “model” is used 2 times in the model. All layer names should be unique.
# Takes the two trained clients and weights them into one file
import tensorflow as tf
from Client_Trainer import CustomLoss
# Deactivate GPU
tf.config.set_visible_devices([], 'GPU')
# Custom loss function
loss_function = CustomLoss.MaskedMSE
def load_model(model_path, prefix):
model = tf.keras.models.load_model(model_path, custom_objects = {'MaskedMSE': loss_function})
return model
def ensemble(model_FTM, model_Stanford):
# Create input layer
input_shape = model_FTM.input.shape[1:]
ensemble_input = tf.keras.layers.Input(shape = input_shape, name = 'ensemble_input')
# Get the outputs of both models
output_FTM = model_FTM(ensemble_input)
output_Stanford = model_Stanford(ensemble_input)
# Average the outputs
averaged_output = tf.keras.layers.Average(name = "ensemble_average")([output_FTM, output_Stanford])
# Create the ensemble model
ensemble_model = tf.keras.models.Model(inputs = ensemble_input, outputs = averaged_output, name = "ensemble_model")
return ensemble_model
def main(model_FTM_path, model_Stanford_path, save_path):
# Load the models
model_FTM = load_model(model_FTM_path, "FTM")
model_Stanford = load_model(model_Stanford_path, "Stanford")
# Build the ensemble model
ensemble_model = ensemble(model_FTM, model_Stanford)
# Compile the ensemble model
ensemble_model.compile(optimizer = 'adam', loss = loss_function, metrics = ['mse'])
# print("nEnsemble Model Summary:")
# ensemble_model.summary()
# Save the ensemble model
ensemble_model.save(save_path)
if __name__ == "__main__":
model_FTM_path = 'FTMRetrained_round2.h5'
model_Stanford_path = 'StanfordRetrained_round2.h5'
save_path = 'weighted_2clients_round2.h5'
main(model_FTM_path, model_Stanford_path, save_path)