예제 #1
0
def create_model(modelName,
                 trainingClass,
                 evaluationClass,
                 inputClass,
                 outputClass,
                 *args,
                 projectDirectory='.',
                 dryrun=None,
                 force=None):
    """Generates stubs for the Tensorflow model, data processing class and training script and shell script to run it from the command line. Shell scripts will be located in the project main directory (which should be the current directory when running them) and model files will be located in ``project_name/models/model_name/*.py``.

    After creation the user must implement the ``process_training_batch()`` , ``process_evaluation_batch()``, ``process_input_batch()`` and ``process_output_batch`` member functions that take  ``trainingClass``, ``evaluationClass``, ``inputClass`` and ``outputClass`` respectively.

    The model must implement the ``get_loss()``, ``produce_metrics()`` and ``get_outputs()`` functions (see documentation of :class:`.BaseTensorflowModel` and the ``Tutorial`` for more detailed instructions)

    The training script is generated with example stubs that should be modified to align with the created model.

    Parameters
    ----------
    modelName : string
        Name of the model
    trainingClass : BaseData
        Datamodel class (must exist) of the Dataset that contains the training data
    evaluationClass : BaseData
        Datamodel class (must exist) that will contain the evaluation data
    inputClass : BaseData
        Datamodel class (must exist) that will be used as the input when serving the model
    outputClass : BaseData
        Datamodel class (must exist) that will be returned as output when serving the model
    *args : BaseTensorflowModelBlock
        Names of blocks that will build up the model
    """
    project = HypergolProject(projectDirectory=projectDirectory,
                              dryrun=dryrun,
                              force=force)
    modelName = NameString(modelName)
    trainingClass = NameString(trainingClass)
    evaluationClass = NameString(evaluationClass)
    inputClass = NameString(inputClass)
    outputClass = NameString(outputClass)
    blocks = [NameString(value) for value in args]
    project.check_dependencies(
        [trainingClass, evaluationClass, inputClass, outputClass] + blocks)

    project.create_model_directory(modelName=modelName)
    project.render_simple(templateName='__init__.py.j2',
                          filePath=Path(project.modelsPath, modelName.asSnake,
                                        '__init__.py'))

    content = project.render(templateName='model.py.j2',
                             templateData={
                                 'name': modelName,
                             },
                             filePath=Path(projectDirectory, 'models',
                                           modelName.asSnake,
                                           modelName.asFileName))

    batchProcessorContent = project.render(
        templateName='batch_processor.py.j2',
        templateData={
            'name': modelName,
            'evaluationClass': evaluationClass,
            'outputClass': outputClass,
        },
        filePath=Path(projectDirectory, 'models', modelName.asSnake,
                      f'{modelName.asSnake}_batch_processor.py'))

    trainModelContent = project.render(
        templateName='train_model.py.j2',
        templateData={
            'modelName':
            modelName,
            'trainingClass':
            trainingClass,
            'evaluationClass':
            evaluationClass,
            'blockDependencies':
            [name for name in blocks if project.is_model_block_class(name)],
        },
        filePath=Path(projectDirectory, 'models', modelName.asSnake,
                      f'train_{modelName.asFileName}'))

    scriptContent = project.render_executable(
        templateName='train_model.sh.j2',
        templateData={'snakeName': modelName.asSnake},
        filePath=Path(projectDirectory, f'train_{modelName.asSnake}.sh'))

    serveContent = project.render(
        templateName='serve_model.py.j2',
        templateData={
            'modelName': modelName,
            'inputClass': inputClass,
            'outputClass': outputClass
        },
        filePath=Path(projectDirectory, 'models', modelName.asSnake,
                      f'serve_{modelName.asFileName}'))

    serveScriptContent = project.render_executable(
        templateName='serve_model.sh.j2',
        templateData={'snakeName': modelName.asSnake},
        filePath=Path(projectDirectory, f'serve_{modelName.asSnake}.sh'))

    return project.cli_final_message(
        creationType='Model',
        name=modelName,
        content=(content, batchProcessorContent, trainModelContent,
                 scriptContent, serveContent, serveScriptContent))
예제 #2
0
def create_pipeline(pipeLineName,
                    *args,
                    projectDirectory='.',
                    dryrun=None,
                    force=None,
                    project=None):
    """Generates a pipeline script from the parameters

    Fails if the target file already exists unless ``force=True`` or ``--force`` in CLI is set.

    Generates pipe_line_name.py in pipelines, imports all the classes listed in ``*args`` and creates stubs for them to be filled. Also creates the executable ``pipe_line_name.sh`` in the project directory with examples how to pass parameters from the shell.

    Parameters
    ----------
    pipeLineName : string (CamelCase)
        Name of the pipeline to be created
    projectDirectory : string (default='.')
        Location of the project directory, the code will be created in ``projectDirectory/data_models/class_name.py``.
    dryrun : bool (default=None)
        If set to ``True`` it returns the generated code as a string
    force : bool (default=None)
        If set to ``True`` it overwrites the target file
    *args : List of strings (CamelCase)
        Classes to be imported into the generated code from the data model, fails if class not found in either ``data_models`` or ``tasks``

    Returns
    -------

    content : string
        The generated code if ``dryrun`` is specified
    scriptContent : string
        The generated shell script to run the pipeline if ``dryrun`` is specified

    """
    if project is None:
        project = HypergolProject(projectDirectory=projectDirectory,
                                  dryrun=dryrun,
                                  force=force)
    pipeLineName = NameString(pipeLineName)
    dependencies = [NameString(value) for value in args]
    project.check_dependencies(dependencies)

    content = project.render(
        templateName='pipeline.py.j2',
        templateData={
            'snakeName':
            pipeLineName.asSnake,
            'taskDependencies':
            [name for name in dependencies if project.is_task_class(name)],
            'dataModelDependencies': [
                name for name in dependencies
                if project.is_data_model_class(name)
            ]
        },
        filePath=Path(projectDirectory, 'pipelines', pipeLineName.asFileName))

    scriptContent = project.render_executable(
        templateName='pipeline.sh.j2',
        templateData={'snakeName': pipeLineName.asSnake},
        filePath=Path(projectDirectory, f'{pipeLineName.asSnake}.sh'))

    return project.cli_final_message(creationType='PipeLine',
                                     name=pipeLineName,
                                     content=(content, scriptContent))