Exemplo n.º 1
0
def load_model(param_path: str, model_class=None):
    """
    Loads and returns a model.

    Parameters
    ----------
    param_path: str, required
        A json file specifying a DeepQaModel.
    model_class: DeepQaModel, optional (default=None)
        This option is useful if you have implemented a new model
        class which is not one of the ones implemented in this library.

    Returns
    -------
    A ``DeepQaModel`` instance.
    """
    param_dict = pyhocon.ConfigFactory.parse_file(param_path)
    params = Params(replace_none(param_dict))
    prepare_environment(params)

    from deep_qa.models import concrete_models
    if model_class is None:
        model_type = params.pop_choice('model_class', concrete_models.keys())
        model_class = concrete_models[model_type]
    else:
        if params.pop('model_class', None) is not None:
            raise ConfigurationError(
                "You have specified a local model class and passed a model_class argument"
                "in the json specification. These options are mutually exclusive."
            )
    model = model_class(params)
    model.load_model()
    return model
Exemplo n.º 2
0
def main():
    if len(sys.argv) != 2:
        print('USAGE: run_model.py [param_file]')
        sys.exit(-1)

    log_keras_version_info()
    param_file = sys.argv[1]
    params = pyhocon.ConfigFactory.parse_file(param_file)
    params = replace_none(params)
    log_dir = params.get("model_serialization_prefix", None)  # pylint: disable=no-member
    if log_dir is not None:
        sys.stdout = TeeLogger(log_dir + "_stdout.log", sys.stdout)
        sys.stderr = TeeLogger(log_dir + "_stderr.log", sys.stderr)
        handler = logging.FileHandler(log_dir + "_python_logging.log")
        handler.setLevel(logging.INFO)
        handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s'))
        logging.getLogger().addHandler(handler)
        shutil.copyfile(param_file, log_dir + "_model_params.json")
    model_type = get_choice(params, 'model_class', concrete_models.keys())
    model_class = concrete_models[model_type]
    model = model_class(params)

    if model.can_train():
        logger.info("Training model")
        model.train()
    else:
        logger.info("Not enough training inputs.  Assuming you wanted to load a model instead.")
        # TODO(matt): figure out a way to specify which epoch you want to load a model from.
        model.load_model()
    if K.backend() == "tensorflow":
        K.clear_session()
Exemplo n.º 3
0
def run_model(param_path: str, model_class=None):
    """
    This function is the normal entry point to DeepQA. Use this to run a DeepQA model in
    your project. Note that if you care about exactly reproducible experiments,
    you should avoid importing Keras before you import and use this function, as
    Keras relies on random seeds which can be set in this function via a
    JSON specification file.

    Note that this function performs training and will also evaluate the trained
    model on development and test sets if provided in the parameter json.

    Parameters
    ----------
    param_path: str, required.
        A json file specifying a DeepQaModel.
    model_class: ``DeepQaModel``, optional (default=None).
        This option is useful if you have implemented a new model class which
        is not one of the ones implemented in this library.
    """
    param_dict = pyhocon.ConfigFactory.parse_file(param_path)
    params = Params(replace_none(param_dict))
    prepare_environment(params)

    # These have to be imported _after_ we set the random seed,
    # because keras uses the numpy random seed.
    from deep_qa.models import concrete_models
    from keras import backend as K

    log_dir = params.get("model_serialization_prefix", None)  # pylint: disable=no-member
    if log_dir is not None:
        sys.stdout = TeeLogger(log_dir + "_stdout.log", sys.stdout)
        sys.stderr = TeeLogger(log_dir + "_stderr.log", sys.stderr)
        handler = logging.FileHandler(log_dir + "_python_logging.log")
        handler.setLevel(logging.INFO)
        handler.setFormatter(
            logging.Formatter(
                '%(asctime)s - %(levelname)s - %(name)s - %(message)s'))
        logging.getLogger().addHandler(handler)
        shutil.copyfile(param_path, log_dir + "_model_params.json")

    if model_class is None:
        model_type = params.pop_choice('model_class', concrete_models.keys())
        model_class = concrete_models[model_type]
    else:
        if params.pop('model_class', None) is not None:
            raise ConfigurationError(
                "You have specified a local model class and passed a model_class argument"
                "in the json specification. These options are mutually exclusive."
            )
    model = model_class(params)

    if model.can_train():
        logger.info("Training model")
        model.train()
        K.clear_session()
    else:
        raise ConfigurationError(
            "The supplied model does not have enough training inputs.")
Exemplo n.º 4
0
def serve(port: int, config_file: str):
    # read in the Typesafe-style config file
    solver_params = ConfigFactory.parse_file(config_file)
    params = Params(replace_none(solver_params))
    model_type = params.pop_choice('model_class', concrete_models.keys())
    solver_class = concrete_models[model_type]
    solver = solver_class(params)
    global mySolver
    mySolver = SolverServer(solver)
    # start the server on the specified port
    print("starting server")
    app.run(host='0.0.0.0')
Exemplo n.º 5
0
def main():
    if len(sys.argv) != 2:
        print('USAGE: run_model.py [param_file]')
        sys.exit(-1)

    log_keras_version_info()
    param_file = sys.argv[1]
    params = pyhocon.ConfigFactory.parse_file(param_file)
    params = replace_none(params)
    model_type = get_choice(params, 'model_class', concrete_models.keys())
    model_class = concrete_models[model_type]
    model = model_class(params)

    if model.can_train():
        logger.info("Training model")
        model.train()
    else:
        logger.info(
            "Not enough training inputs.  Assuming you wanted to load a model instead."
        )
        # TODO(matt): figure out a way to specify which epoch you want to load a model from.
        model.load_model()
Exemplo n.º 6
0
def serve(port: int, config_file: str):
    # read in the Typesafe-style config file and lookup the port to run on.
    solver_params = ConfigFactory.parse_file(config_file)

    # create the server and add our RPC "servicer" to it
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    model_type = get_choice(solver_params, 'model_class',
                            concrete_models.keys())
    solver_class = concrete_models[model_type]
    solver = solver_class(solver_params)
    message_pb2.add_SolverServiceServicer_to_server(SolverServer(solver),
                                                    server)

    # start the server on the specified port
    server.add_insecure_port('[::]:{0}'.format(port))
    print("starting server")
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)
Exemplo n.º 7
0
def run_model(param_dict: Dict[str, any], model_class=None):
    """
    This function is the normal entry point to DeepQA. Use this to run a DeepQA model in
    your project. Note that if you care about exactly reproducible experiments,
    you should avoid importing Keras before you import and use this function, as
    Keras relies on random seeds which can be set in this function via a
    JSON specification file.

    Note that this function performs training and will also evaluate the trained
    model on development and test sets if provided in the parameter json.

    Parameters
    ----------
    param_dict: Dict[str, any], required.
        A parameter file specifying a DeepQaModel.
    model_class: DeepQaModel, optional (default=None).
        This option is useful if you have implemented a new model class which
        is not one of the ones implemented in this library.
    """
    params = Params(replace_none(param_dict))
    prepare_environment(params)

    # These have to be imported _after_ we set the random seed,
    # because keras uses the numpy random seed.
    from deep_qa.models import concrete_models
    import tensorflow
    from keras import backend as K

    log_dir = params.get("model_serialization_prefix", None)  # pylint: disable=no-member
    if log_dir is not None:
        sys.stdout = TeeLogger(log_dir + "_stdout.log", sys.stdout)
        sys.stderr = TeeLogger(log_dir + "_stderr.log", sys.stderr)
        handler = logging.FileHandler(log_dir + "_python_logging.log")
        handler.setLevel(logging.INFO)
        handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s'))
        logging.getLogger().addHandler(handler)
        serialisation_params = deepcopy(params).as_dict(quiet=True)
        with open(log_dir + "_model_params.json", "w") as param_file:
            json.dump(serialisation_params, param_file)

    num_threads = os.environ.get('OMP_NUM_THREADS')
    config = {
            "allow_soft_placement": True,
            "log_device_placement": params.pop("log_device_placement", False)
    }
    if num_threads is not None:
        config["intra_op_parallelism_threads"] = int(num_threads)
    global_session = tensorflow.Session(config=tensorflow.ConfigProto(**config))
    K.set_session(global_session)

    if model_class is None:
        model_type = params.pop_choice('model_class', concrete_models.keys())
        model_class = concrete_models[model_type]
    else:
        if params.pop('model_class', None) is not None:
            raise ConfigurationError("You have specified a local model class and passed a model_class argument"
                                     "in the json specification. These options are mutually exclusive.")
    model = model_class(params)

    if model.can_train():
        logger.info("Training model")
        model.train()
        K.clear_session()
    else:
        raise ConfigurationError("The supplied model does not have enough training inputs.")