Example #1
0
def environment():
    """
    Return TaskEnvironment
    """
    car = LabelEntity(id=ID(123456789),
                      name="car",
                      domain=Domain.DETECTION,
                      is_empty=True)
    person = LabelEntity(id=ID(987654321),
                         name="person",
                         domain=Domain.DETECTION,
                         is_empty=True)
    labels_list = [car, person]
    dummy_template = __get_path_to_file("./dummy_template.yaml")
    model_template = parse_model_template(dummy_template)
    hyper_parameters = model_template.hyper_parameters.data
    params = ote_config_helper.create(hyper_parameters)
    labels_schema = LabelSchemaEntity.from_labels(labels_list)
    environment = TaskEnvironment(
        model=None,
        hyper_parameters=params,
        label_schema=labels_schema,
        model_template=model_template,
    )
    return environment
def find_and_parse_model_template(path_or_id):
    """
    In first function attempts to read a model template from disk under assumption that a path is passed.
    If the attempt is failed, it tries to find template in registry under assumption that an ID is passed.
    """

    if os.path.exists(path_or_id):
        return parse_model_template(path_or_id)
    return Registry(".").get(path_or_id)
Example #3
0
    def _run_ote_training(self, data_collector):
        logger.debug(f"self.template_path = {self.template_path}")

        print(
            f"train dataset: {len(self.dataset.get_subset(Subset.TRAINING))} items"
        )
        print(f"validation dataset: "
              f"{len(self.dataset.get_subset(Subset.VALIDATION))} items")

        logger.debug("Load model template")
        self.model_template = parse_model_template(self.template_path)

        logger.debug("Set hyperparameters")
        params = ote_sdk_configuration_helper_create(
            self.model_template.hyper_parameters.data)
        if self.num_training_iters != KEEP_CONFIG_FIELD_VALUE:
            params.learning_parameters.num_iters = int(self.num_training_iters)
            logger.debug(f"Set params.learning_parameters.num_iters="
                         f"{params.learning_parameters.num_iters}")
        else:
            logger.debug(f"Keep params.learning_parameters.num_iters="
                         f"{params.learning_parameters.num_iters}")

        if self.batch_size != KEEP_CONFIG_FIELD_VALUE:
            params.learning_parameters.batch_size = int(self.batch_size)
            logger.debug(f"Set params.learning_parameters.batch_size="
                         f"{params.learning_parameters.batch_size}")
        else:
            logger.debug(f"Keep params.learning_parameters.batch_size="
                         f"{params.learning_parameters.batch_size}")

        logger.debug("Setup environment")
        self.environment, self.task = self._create_environment_and_task(
            params, self.labels_schema, self.model_template)

        logger.debug("Train model")
        self.output_model = ModelEntity(
            self.dataset,
            self.environment.get_model_configuration(),
            model_status=ModelStatus.NOT_READY,
        )

        self.copy_hyperparams = deepcopy(self.task._hyperparams)

        self.task.train(self.dataset, self.output_model)
        assert (self.output_model.model_status == ModelStatus.SUCCESS
                ), "Training was failed"

        score_name, score_value = self._get_training_performance_as_score_name_value(
        )
        logger.info(f"performance={self.output_model.performance}")
        data_collector.log_final_metric("metric_name",
                                        self.name + "/" + score_name)
        data_collector.log_final_metric("metric_value", score_value)
Example #4
0
def get_config_and_task_name(
        template_file_path: str) -> Tuple[ConfigurableParameters, str]:
    """Return configurable parameters and model name given template path

    Args:
        template_file_path (str): template path

    Returns:
        Tuple[ConfigurableParameters, str]: Configurable parameters, model name
    """
    model_template: ModelTemplate = parse_model_template(template_file_path)
    hyper_parameters: dict = model_template.hyper_parameters.data
    config: ConfigurableParameters = create(hyper_parameters)
    return config, model_template.name
    def __init__(self, templates_dir=None, templates=None):
        if templates is None:
            if templates_dir is None:
                templates_dir = os.getenv("TEMPLATES_DIR")

            if templates_dir is None:
                raise RuntimeError("The templates_dir is not set.")

            template_filenames = glob.glob(
                os.path.join(templates_dir, "**", "template.yaml"), recursive=True
            )
            template_filenames = [os.path.abspath(p) for p in template_filenames]

            self.templates = []

            for template_file in template_filenames:
                self.templates.append(parse_model_template(template_file))
        else:
            self.templates = copy.deepcopy(templates)

        self.task_types = self.__collect_task_types(self.templates)
Example #6
0
def template_paths_fx(ote_templates_root_dir_fx):
    """
    Return mapping model names to template paths, received from globbing the
    folder pointed by the fixture ote_templates_root_dir_fx.
    Note that the function searches files with name `template.yaml`, and for each such file
    the model name is the name of the parent folder of the file.
    """
    root = ote_templates_root_dir_fx
    assert osp.isabs(
        root
    ), f"Error: ote_templates_root_dir_fx is not an absolute path: {root}"
    template_glob = glob.glob(f"{root}/**/template*.yaml", recursive=True)
    data = {}
    for cur_path in template_glob:
        assert osp.isabs(cur_path), f"Error: not absolute path {cur_path}"
        name = parse_model_template(cur_path).model_template_id
        if name in data:
            raise RuntimeError(
                f"Duplication of names in {root} folder: {data[name]} and {cur_path}"
            )
        assert name != ROOT_PATH_KEY, f"Wrong model name {name}"
        data[name] = cur_path
    data[ROOT_PATH_KEY] = ""
    return data
def setup_configurable_parameters(template_dir, max_num_epochs=10):
    model_template = parse_model_template(
        osp.join(template_dir, 'template.yaml'))
    hyper_parameters = create(model_template.hyper_parameters.data)
    hyper_parameters.learning_parameters.max_num_epochs = max_num_epochs
    return hyper_parameters, model_template
def test_reading_mobilenet_v3_large_075():
    parse_model_template(
        osp.join('configs', 'ote_custom_classification',
                 'mobilenet_v3_large_075', 'template.yaml'))
def test_reading_efficientnet_b0():
    parse_model_template(
        osp.join('configs', 'ote_custom_classification', 'efficientnet_b0',
                 'template.yaml'))
def test_template(path):
    template = parse_model_template(path)
    assert template.hyper_parameters.data
    def test_model_entity_sets_values(self):
        """
        <b>Description:</b>
        Check that ModelEntity correctly returns the set values

        <b>Expected results:</b>
        Test passes if ModelEntity correctly returns the set values

        <b>Steps</b>
        1. Check set values in the ModelEntity
        """
        def __get_path_to_file(filename: str):
            """
            Return the path to the file named 'filename', which lives in the tests/entities directory
            """
            return str(Path(__file__).parent / Path(filename))

        car = LabelEntity(name="car", domain=Domain.DETECTION)
        labels_list = [car]
        dummy_template = __get_path_to_file("./dummy_template.yaml")
        model_template = parse_model_template(dummy_template)
        hyper_parameters = model_template.hyper_parameters.data
        params = ote_config_helper.create(hyper_parameters)
        labels_schema = LabelSchemaEntity.from_labels(labels_list)
        environment = TaskEnvironment(
            model=None,
            hyper_parameters=params,
            label_schema=labels_schema,
            model_template=model_template,
        )

        item = self.generate_random_image()
        dataset = DatasetEntity(items=[item])
        score_metric = ScoreMetric(name="Model accuracy", value=0.5)

        model_entity = ModelEntity(train_dataset=self.dataset(),
                                   configuration=self.configuration())

        set_params = {
            "configuration": environment.get_model_configuration(),
            "train_dataset": dataset,
            "id": ID(1234567890),
            "creation_date": self.creation_date,
            "previous_trained_revision": 5,
            "previous_revision": 2,
            "version": 2,
            "tags": ["tree", "person"],
            "model_status": ModelStatus.TRAINED_NO_STATS,
            "model_format": ModelFormat.BASE_FRAMEWORK,
            "performance": Performance(score_metric),
            "training_duration": 5.8,
            "precision": [ModelPrecision.INT8],
            "latency": 328,
            "fps_throughput": 20,
            "target_device": TargetDevice.GPU,
            "target_device_type": "notebook",
            "optimization_methods": [OptimizationMethod.QUANTIZATION],
            "optimization_type": ModelOptimizationType.MO,
            "optimization_objectives": {
                "param": "Test param"
            },
            "performance_improvement": {"speed", 0.5},
            "model_size_reduction": 1.0,
        }

        for key, value in set_params.items():
            setattr(model_entity, key, value)
            assert getattr(model_entity, key) == value

        assert model_entity.is_optimized() is True