Esempio n. 1
0
def restore_model(checkpoint_filename, video_retriever_generator, selector,
                  extractor):
    """This function restores a model from a tf checkpoint

    Json filename is asserted to be the same as checpoint filename, but with json file extension

    -Recovers the model's parameters via the json file
    -Builds the model using this parameters
    -Prepare the tensorflow graph and get neural network operations
    """

    json_filename = checkpoint_filename + '.json'
    with open(json_filename, 'r', encoding='utf-8') as json_file:
        params = json.load(json_file)

    builder = ModelBuilder(params["training_videos_names"],
                           params["testing_videos_names"],
                           params["n_captions_per_video"],
                           params["feature_n_frames"],
                           video_retriever_generator, selector, extractor)
    model = params["model"]
    builder.create_model(model["enc_units"], model["dec_units"],
                         model["rnn_layers"], model["embedding_dims"],
                         model["learning_rate"], model["dropout_rate"],
                         model["bi_encoder"])

    builder.prepare_training(params["batch_size"])

    model_saver = ModelSaver(os.path.dirname(checkpoint_filename),
                             os.path.basename(checkpoint_filename))

    return builder, model_saver, params