Beispiel #1
0
def detail(model_id: str = typer.Argument(..., help='Model ID')):
    """Show a single model."""
    with requests.get(
            f'http://{SERVER_HOST}:{SERVER_PORT}/api/v1/model/{model_id}'
    ) as r:
        data = r.json()
        model_detailed_view(MLModel.parse_obj(data))
Beispiel #2
0
def register_model_from_yaml(file_path: Union[Path, str]):
    # check if file exist
    file_path = Path(file_path)
    assert file_path.exists(
    ), f'Model definition file at {str(file_path)} does not exist'

    # read yaml
    with open(file_path) as f:
        model_config = yaml.safe_load(f)
    model_yaml = MLModelFromYaml.parse_obj(model_config)
    # copy model weight to cache directory
    model_in_saved_path = model_yaml.saved_path
    if model_in_saved_path != model_yaml.weight:
        copy2(model_yaml.weight, model_in_saved_path)

    # zip weight folder
    if model_yaml.engine == Engine.TFS:
        weight_dir = model_yaml.weight
        make_archive(weight_dir.with_suffix('.zip'), 'zip', weight_dir)

    model_data = model_yaml.dict(exclude_none=True,
                                 exclude={'convert', 'profile'})
    model = MLModel.parse_obj(model_data)
    register_model(model,
                   convert=model_yaml.convert,
                   profile=model_yaml.profile)
Beispiel #3
0
def get_by_id(id: str) -> MLModel:
    """Get a MLModel object by its ID.
    """
    model_data = _collection.find_one(filter={'_id': ObjectId(id)})
    if model_data is not None:
        return MLModel.parse_obj(model_data)
    else:
        raise ServiceException(f'Model with id={id} does not exist.')
Beispiel #4
0
def list_models(
    architecture: Optional[str] = typer.Option(None,
                                               '-n',
                                               '--name',
                                               help='Model architecture name'),
    framework: Optional[Framework] = typer.Option(None,
                                                  '-fw',
                                                  '--framework',
                                                  case_sensitive=False,
                                                  help='Framework'),
    engine: Optional[Engine] = typer.Option(None,
                                            '-e',
                                            '--engine',
                                            case_sensitive=False,
                                            help='Serving engine'),
    version: Optional[int] = typer.Option(None,
                                          '-v',
                                          '--version',
                                          help='Version'),
    list_all: Optional[bool] = typer.Option(
        False,
        '-a',
        '--all',
        is_flag=True,
        help=
        'Display queried models. otherwise, only partial result will be shown.'
    ),
):
    """Show a table that lists all models published in MLModelCI"""

    payload = remove_dict_null({
        'architecture': architecture,
        'framework': framework,
        'engine': engine,
        'version': version
    })
    with requests.get(f'{app_settings.api_v1_prefix}/model',
                      params=payload) as r:
        model_list = r.json()
        model_view([MLModel.parse_obj(model) for model in model_list],
                   list_all=list_all)
Beispiel #5
0
def convert(
        id: str = typer.Option(None, '-i', '--id', help='ID of model.'),
        yaml_file: Optional[Path] = typer.
    Option(
        None,
        '-f',
        '--yaml-file',
        exists=True,
        file_okay=True,
        help=
        'Path to configuration YAML file. You should either set the `yaml_file` field or fields '
        '(`FILE_OR_DIR`, `--name`, `--framework`, `--engine`, `--version`, `--task`, `--dataset`,'
        '`--metric`, `--input`, `--output`).'),
        register: bool = typer.Option(
            False,
            '-r',
            '--register',
            is_flag=True,
            help='register the converted models to modelhub, default false')):
    model = None
    if id is None and yaml_file is None:
        typer.echo(
            "WARNING: Please assign a way to find the target model! details refer to --help"
        )
        raise RuntimeError
    if id is not None and yaml_file is not None:
        typer.echo("WARNING: Do not use -id and -path at the same time!")
        raise RuntimeError
    elif id is not None and yaml_file is None:
        if ModelDB.exists_by_id(id):
            model = ModelDB.get_by_id(id)
        else:
            typer.echo(f"model id: {id} does not exist in modelhub")
    elif id is None and yaml_file is not None:
        # get MLModel from yaml file
        with open(yaml_file) as f:
            model_config = yaml.safe_load(f)
        model_yaml = MLModelFromYaml.parse_obj(model_config)
        model_in_saved_path = model_yaml.saved_path
        if model_in_saved_path != model_yaml.weight:
            copy2(model_yaml.weight, model_in_saved_path)
        if model_yaml.engine == Engine.TFS:
            weight_dir = model_yaml.weight
            make_archive(weight_dir.with_suffix('.zip'), 'zip', weight_dir)

        model_data = model_yaml.dict(exclude_none=True,
                                     exclude={'convert', 'profile'})
        model = MLModel.parse_obj(model_data)

    # auto execute all possible convert and return a list of save paths of every converted model
    generated_dir_list = generate_model_family(model)
    typer.echo(f"Converted models are save in: {generated_dir_list}")
    if register:
        model_data = model.dict(
            exclude={'weight', 'id', 'model_status', 'engine'})
        for model_dir in generated_dir_list:
            parse_result = parse_path_plain(model_dir)
            engine = parse_result['engine']
            model_cvt = MLModel(**model_data,
                                weight=model_dir,
                                engine=engine,
                                model_status=[ModelStatus.CONVERTED])
            ModelDB.save(model_cvt)
            typer.echo(
                f"converted {engine} are successfully registered in Modelhub")
Beispiel #6
0
def update(
        model_id: str = typer.Argument(..., help='Model ID'),
        architecture: Optional[str] = typer.Option(None,
                                                   '-n',
                                                   '--name',
                                                   help='Architecture'),
        framework: Optional[Framework] = typer.Option(None,
                                                      '-fw',
                                                      '--framework',
                                                      help='Framework'),
        engine: Optional[Engine] = typer.Option(None,
                                                '-e',
                                                '--engine',
                                                help='Engine'),
        version: Optional[int] = typer.Option(None,
                                              '-v',
                                              '--version',
                                              min=1,
                                              help='Version number'),
        task: Optional[Task] = typer.Option(None, '-t', '--task', help='Task'),
        dataset: Optional[str] = typer.Option(None,
                                              '-d',
                                              '--dataset',
                                              help='Dataset name'),
        metric: Optional[Dict[Metric, float]] = typer.
    Option(
        None,
        help='Metrics in the form of mapping JSON string. The map type is '
        '`Dict[types.models.mlmodel.Metric, float]`. An example is \'{"acc": 0.76}.\'',
    ),
        inputs: Optional[List[IOShape]] = typer.
    Option(
        [],
        '-i',
        '--input',
        help=
        'List of shape definitions for input tensors. An example of one shape definition is '
        '\'{"name": "input", "shape": [-1, 3, 224, 224], "dtype": "TYPE_FP32", "format": "FORMAT_NCHW"}\'',
    ),
        outputs: Optional[List[IOShape]] = typer.
    Option(
        [],
        '-o',
        '--output',
        help=
        'List of shape definitions for output tensors. An example of one shape definition is '
        '\'{"name": "output", "shape": [-1, 1000], "dtype": "TYPE_FP32"}\'',
    )):
    model = ModelUpdateSchema(
        architecture=architecture,
        framework=framework,
        engine=engine,
        version=version,  # noqa
        dataset=dataset,
        metric=metric,
        task=task,
        inputs=inputs,
        outputs=outputs)

    with requests.patch(f'{app_settings.api_v1_prefix}/model/{model_id}',
                        data=model.json(exclude_defaults=True)) as r:
        data = r.json()
        model_detailed_view(MLModel.parse_obj(data))
Beispiel #7
0
def detail(model_id: str = typer.Argument(..., help='Model ID')):
    """Show a single model."""
    with requests.get(f'{app_settings.api_v1_prefix}/model/{model_id}') as r:
        data = r.json()
        model_detailed_view(MLModel.parse_obj(data))
Beispiel #8
0
def models(name, framework, engine, version, list_all, quiet):
    payload = remove_dict_null({'name': name, 'framework': framework, 'engine': engine, 'version': version})
    with requests.get(f'{app_settings.api_v1_prefix}/model', params=payload) as r:
        model_list = r.json()
        model_view([MLModel.parse_obj(model) for model in model_list], list_all=list_all, quiet=quiet)