Example #1
0
def create(model_name: str = None,
           model: Union[str, ImageModel] = None,
           folder_path: str = None,
           dataset_name: str = None,
           dataset: Union[str, ImageSet] = None,
           config: Config = config,
           class_dict: dict[str, type[ImageModel]] = class_dict,
           **kwargs) -> ImageModel:
    dataset_name = get_name(name=dataset_name,
                            module=dataset,
                            arg_list=['-d', '--dataset'])
    model_name = get_name(name=model_name,
                          module=model,
                          arg_list=['-m', '--model'])
    if dataset_name is None:
        dataset_name = config.get_full_config()['dataset']['default_dataset']
    if model_name is None:
        model_name = config.get_config(
            dataset_name=dataset_name)['model']['default_model']
    result = config.get_config(
        dataset_name=dataset_name)['model']._update(kwargs)
    model_name = model_name if model_name is not None else result[
        'default_model']

    ModelType: type[ImageModel] = class_dict[get_model_class(model_name)]
    if folder_path is None and isinstance(dataset, ImageSet):
        folder_path = os.path.join(result['model_dir'], dataset.data_type,
                                   dataset.name)
    return ModelType(name=model_name,
                     dataset=dataset,
                     folder_path=folder_path,
                     **result)
Example #2
0
def create(model_name: str = None,
           model: Union[str, ImageModel] = None,
           layer: int = None,
           dataset_name: str = None,
           dataset: Union[str, ImageSet] = None,
           config: Config = config,
           class_dict: dict[str, type[ImageModel]] = class_dict,
           **kwargs) -> ImageModel:
    dataset_name = get_name(name=dataset_name,
                            module=dataset,
                            arg_list=['-d', '--dataset'])
    if dataset_name is None:
        dataset_name = config.get_full_config()['dataset']['default_dataset']
    model_name = get_name(name=model_name,
                          module=model,
                          arg_list=['-m', '--model'])
    if model_name is None:
        model_name = config.get_config(
            dataset_name=dataset_name)['model']['default_model']
    model_name, layer = split_name(model_name, layer=layer)
    return trojanzoo.models.create(model_name=model_name,
                                   model=model,
                                   dataset_name=dataset_name,
                                   dataset=dataset,
                                   config=config,
                                   class_dict=class_dict,
                                   layer=layer,
                                   **kwargs)
Example #3
0
def add_argument(
    parser: argparse.ArgumentParser,
    model_name: str = None,
    model: Union[str, ImageModel] = None,
    config: Config = config,
    class_dict: dict[str, type[ImageModel]] = class_dict
) -> argparse._ArgumentGroup:
    dataset_name = get_name(arg_list=['-d', '--dataset'])
    if dataset_name is None:
        dataset_name = config.get_full_config()['dataset']['default_dataset']
    model_name = get_name(name=model_name,
                          module=model,
                          arg_list=['-m', '--model'])
    if model_name is None:
        model_name = config.get_config(
            dataset_name=dataset_name)['model']['default_model']
    model_name = get_model_class(model_name)
    return trojanzoo.models.add_argument(parser=parser,
                                         model_name=model_name,
                                         model=model,
                                         config=config,
                                         class_dict=class_dict)