コード例 #1
0
    def parse_and_load_model(self) -> ParserResult:
        """
        Parses the command line arguments, and creates configuration objects for the model itself, and for the
        Azure-related parameters. Sets self.azure_config and self.model_config to their proper values. Returns the
        parser output from parsing the model commandline arguments.
        If no "model" argument is provided on the commandline, self.model_config will be set to None, and the return
        value is None.
        """
        # Create a parser that will understand only the args we need for an AzureConfig
        parser1 = create_runner_parser()
        parser_result = parse_args_and_add_yaml_variables(parser1,
                                                          yaml_config_file=self.yaml_config_file,
                                                          project_root=self.project_root,
                                                          fail_on_unknown_args=False)
        azure_config = AzureConfig(**parser_result.args)
        azure_config.project_root = self.project_root
        self.azure_config = azure_config
        self.model_config = None
        if not azure_config.model:
            raise ValueError("Parameter 'model' needs to be set to tell InnerEye which model to run.")
        model_config_loader: ModelConfigLoader = ModelConfigLoader(**parser_result.args)
        # Create the model as per the "model" commandline option. This can return either a built-in config
        # of type DeepLearningConfig, or a LightningContainer.
        config_or_container = model_config_loader.create_model_config_from_name(model_name=azure_config.model)

        def parse_overrides_and_apply(c: object, previous_parser_result: ParserResult) -> ParserResult:
            assert isinstance(c, GenericConfig)
            parser = type(c).create_argparser()
            # For each parser, feed in the unknown settings from the previous parser. All commandline args should
            # be consumed by name, hence fail if there is something that is still unknown.
            parser_result = parse_arguments(parser,
                                            settings_from_yaml=previous_parser_result.unknown_settings_from_yaml,
                                            args=previous_parser_result.unknown,
                                            fail_on_unknown_args=True)
            # Apply the overrides and validate. Overrides can come from either YAML settings or the commandline.
            c.apply_overrides(parser_result.known_settings_from_yaml)
            c.apply_overrides(parser_result.overrides)
            c.validate()
            return parser_result

        # Now create a parser that understands overrides at model/container level.
        parser_result = parse_overrides_and_apply(config_or_container, parser_result)

        if isinstance(config_or_container, LightningContainer):
            self.lightning_container = config_or_container
        elif isinstance(config_or_container, ModelConfigBase):
            # Built-in InnerEye models use a fake container
            self.model_config = config_or_container
            self.lightning_container = InnerEyeContainer(config_or_container)
        else:
            raise ValueError(f"Don't know how to handle a loaded configuration of type {type(config_or_container)}")
        if azure_config.extra_code_directory:
            exist = "exists" if Path(azure_config.extra_code_directory).exists() else "does not exist"
            logging.info(f"extra_code_directory is {azure_config.extra_code_directory}, which {exist}")
        else:
            logging.info("extra_code_directory is unset")
        return parser_result
コード例 #2
0
 def parse_and_load_model(self) -> Optional[ParserResult]:
     """
     Parses the command line arguments, and creates configuration objects for the model itself, and for the
     Azure-related parameters. Sets self.azure_config and self.model_config to their proper values. Returns the
     parser output from parsing the model commandline arguments.
     If no "model" argument is provided on the commandline, self.model_config will be set to None, and the return
     value is None.
     """
     # Create a parser that will understand only the args we need for an AzureConfig
     parser1 = create_runner_parser()
     parser1_result = parse_args_and_add_yaml_variables(parser1,
                                                        yaml_config_file=self.yaml_config_file,
                                                        project_root=self.project_root,
                                                        args=self.command_line_args,
                                                        fail_on_unknown_args=False)
     azure_config = AzureConfig(**parser1_result.args)
     azure_config.project_root = self.project_root
     self.azure_config = azure_config
     self.model_config = None  # type: ignore
     if not azure_config.model:
         return None
     model_config_loader: ModelConfigLoader = ModelConfigLoader(**parser1_result.args)
     # Create the model as per the "model" commandline option
     model_config = model_config_loader.create_model_config_from_name(
         model_name=azure_config.model
     )
     # This model will be either a classification model or a segmentation model. Those have different
     # fields that could be overridden on the command line. Create a parser that understands the fields we need
     # for the actual model type. We feed this parser will the YAML settings and commandline arguments that the
     # first parser did not recognize.
     parser2 = type(model_config).create_argparser()
     parser2_result = parse_arguments(parser2,
                                      settings_from_yaml=parser1_result.unknown_settings_from_yaml,
                                      args=parser1_result.unknown,
                                      fail_on_unknown_args=True)
     # Apply the overrides and validate. Overrides can come from either YAML settings or the commandline.
     model_config.apply_overrides(parser1_result.unknown_settings_from_yaml)
     model_config.apply_overrides(parser2_result.overrides)
     model_config.validate()
     # Set the file system related configs, they might be affected by the overrides that were applied.
     logging.info("Creating the adjusted output folder structure.")
     model_config.create_filesystem(self.project_root)
     if azure_config.extra_code_directory:
         exist = "exists" if Path(azure_config.extra_code_directory).exists() else "does not exist"
         logging.info(f"extra_code_directory is {azure_config.extra_code_directory}, which {exist}")
     else:
         logging.info("extra_code_directory is unset")
     self.model_config = model_config
     return parser2_result