Exemplo n.º 1
0
def save_model(keras_model, path='./model/', conda_env=None):
    """
    Save a Keras model to a path on the local file system.

    :param keras_model: Keras model to be saved. 

    :param path: Path to a directory containing model data.
    
    :param conda_env: Either a dictionary representation of a Conda environment or the path to a conda environment yaml file. 
    """
    if (not path.endswith('/')):
        path += '/'
    if not os.path.exists(path):
        os.makedirs(path)

    keras_model.save(os.path.join(path, model_file_name))

    if conda_env is None:
        conda_env = _get_default_conda_env()
    utils.save_conda_env(path, conda_env)

    utils.save_model_spec(path, FLAVOR_NAME, model_file_name)
    utils.generate_ilearner_files(path)  # temp solution, to remove later
Exemplo n.º 2
0
def save_model(pytorch_model,
               path='./model/',
               conda_env=None,
               dependencies=[]):
    """
    Save a PyTorch model to a path on the local file system.

    :param pytorch_model: PyTorch model to be saved. 

    :param path: Path to a directory containing model data.

    :param conda_env: Either a dictionary representation of a Conda environment or the path to a conda environment yaml file. 
    """
    if (not path.endswith('/')):
        path += '/'
    if not os.path.exists(path):
        os.makedirs(path)

    # only save cpu version
    _save_model(pytorch_model.to('cpu'), os.path.join(path, MODEL_FILE_NAME))
    fn = os.path.join(path, MODEL_FILE_NAME)
    print(f'MODEL_FILE: {fn}')

    if conda_env is None:
        conda_env = _get_default_conda_env()
    print(f'path={path}, conda_env={conda_env}')
    utils.save_conda_env(path, conda_env)

    for dependency in dependencies:
        shutil.copy(dependency, path)
    forward_func = getattr(pytorch_model, 'forward')
    args = inspect.getargspec(forward_func).args
    if 'self' in args:
        args.remove('self')

    utils.save_model_spec(path, FLAVOR_NAME, MODEL_FILE_NAME, input_args=args)
    utils.generate_ilearner_files(path)  # temp solution, to remove later