def load_model(model_name: str) -> Model: """ Loads and returns a model. Attaches the model name and that model's history. """ model_path = path_from_model_name(model_name) model_dir = os.path.dirname(model_path) model = kload_model(model_path) history = load_history_from_path(model_dir) # Attach our extra data to the model model.__asf_model_name = model_name model.__asf_model_history = history return model
def load_model(saved_model_dir, model_name, custom_objects={}): """ Loads a model from a .h5 file. Also returns the metadata required to use this model. The .h5 can either consist of weights + architecture + optimizer state, or just the weights. If the latter, an architection json file of the same file basename is expected. Parameters ---------- saved_model_dir: String. Path to a directory where the model is saved. model_name: String. Model name, which is the file basename (without the extension). Returns ------- Two objects: - A keras model, not yet compiled. - Metadata required to run this model. """ file = os.path.join(saved_model_dir, model_name) metadata = get_metadata(saved_model_dir, model_name) if metadata[ "save_weights_only"]: # The .h5 file contains only the weights. # load architecture with open(file + ".json", "r") as f2: model = model_from_json(f2.read(), custom_objects=custom_objects) # load weights model.load_weights(file + ".h5") else: # The .h5 file contains weights + architecture + optimizer state. model = kload_model(file + ".h5", custom_objects=custom_objects) return model, metadata