experiment

fgvc.utils.experiment.get_optimizer_and_scheduler(model: Module, config: dict) Tuple[Optimizer, ReduceLROnPlateau | CosineLRScheduler | CosineAnnealingLR]

Create optimizer and learning rate scheduler.

Options from YAML configuration file:
  • optimizer - name of optimizer, for example “sgd” or “adam”.

  • scheduler - name of LR scheduler, for example “plateau”, “cyclic_cosine”, or “cosine”.

  • learning_rate

  • epochs

Parameters:
  • model – PyTorch model.

  • config – A dictionary with experiment configuration. It should contain optimizer, learning_rate, scheduler, and epochs.

Returns:

  • optimizer – PyTorch optimizer.

  • scheduler – PyTorch or timm optimizer.

fgvc.utils.experiment.load_args(args: list | None = None, *, add_arguments_fn: Callable | None = None, test_args: bool = False) Tuple[Namespace, dict] | Namespace

Load train script arguments using argparse library.

Parameters:
  • args – Optional list of arguments that will be passed to method parser.parse_known_args(args).

  • add_arguments_fn – Callback function for including additional args. The function gets parser as an input.

  • test_args – Whether to load test (True) or training (False) arguments.

Returns:

  • args – Namespace with parsed known args.

  • extra_args – Dictionary with parsed unknown args.

fgvc.utils.experiment.load_config(config_path: str, extra_args: dict | None = None, run_name_fmt: str = 'architecture-loss-augmentations', *, create_dirs: bool = True, resume_exp_name: str | None = None) dict

Load training configuration from YAML file, create run name and experiment name.

If argument resume_exp_name is passed training configuration is loaded from JSON file in the experiment directory.

Options from YAML configuration file:
  • root_path - Path to store runs directory with all runs and experiments.

Parameters:
  • config_path – Path to YAML configuration file.

  • extra_args – Optional dictionary with parsed unknown args.

  • run_name_fmt – Format of a run name. It should contain attribute names from configuration file separated by “-“.

  • create_dirs – If True, the method will create run and experiment directory.

  • resume_exp_name – Experiment name to resume training from the last training checkpoint.

Returns:

Dictionary with experiment configuration.

Return type:

config

fgvc.utils.experiment.load_model(config: dict, checkpoint_path: str | None = None, strict: bool = True) Tuple[Module, tuple, tuple]

Load model with pre-trained checkpoint.

Options from YAML configuration file:
  • architecture - any architecture name from timm library.

  • number_of_classes - integer value.

  • (optional) pretrained_checkpoint - options:
    • “timm” (default) - pre-trained checkpoint from timm.

    • “none” - randomly initialized weights.

    • <path> - path to a custom checkpoint.

  • (optional) multigpu - if true, use nn.DataParallel model wrapper.

Pre-trained checkpoint can be set using config dictionary or checkpoint_path argument.

Priority:
  • Use checkpoint_path path to a custom checkpoint when checkpoint_path is specified.

  • Otherwise use configuration from config.

Parameters:
  • config – A dictionary with experiment configuration. It should contain architecture, number_of_classes, and optionally multigpu.

  • checkpoint_path – Path to the pre-trained model checkpoint. The argument overrides pretrained_checkpoint setting in config dictionary.

  • strict – Whether to strictly enforce the keys in state_dict to match between the model and checkpoint weights from file. Used when argument checkpoint_path is specified.

Returns:

  • model – PyTorch model.

  • model_mean – Tuple with mean used to normalize images during training.

  • model_std – Tuple with standard deviation used to normalize images during training.

fgvc.utils.experiment.load_test_args(args: list | None = None, *, add_arguments_fn: Callable | None = None) Namespace

Load test script arguments using argparse library.

Parameters:
  • args – Optional list of arguments that will be passed to method parser.parse_known_args(args).

  • add_arguments_fn – Callback function for including additional args. The function gets parser as an input.

Returns:

Namespace with parsed known args.

Return type:

args

fgvc.utils.experiment.load_test_metadata(test_metadata: str) DataFrame

Load metadata of the test set.

Parameters:

test_metadata – File path to the test metadata csv or parquet file.

Returns:

Test metadata DataFrame.

Return type:

test_df

fgvc.utils.experiment.load_train_args(args: list | None = None, *, add_arguments_fn: Callable | None = None) Tuple[Namespace, dict]

Load train script arguments using argparse library.

Parameters:
  • args – Optional list of arguments that will be passed to method parser.parse_known_args(args).

  • add_arguments_fn – Callback function for including additional args. The function gets parser as an input.

Returns:

  • args – Namespace with parsed known args.

  • extra_args – Dictionary with parsed unknown args.

fgvc.utils.experiment.load_train_metadata(train_metadata: str, valid_metadata: str) Tuple[DataFrame, DataFrame]

Load metadata of the training and validation sets.

Parameters:
  • train_metadata – File path to the training metadata csv or parquet file.

  • valid_metadata – File path to the validation metadata csv or parquet file.

Returns:

  • train_df – Training metadata DataFrame.

  • valid_df – Validation metadata DataFrame.

fgvc.utils.experiment.parse_unknown_args(unknown_args: list) dict

Dynamically parse ‘unknown’ script arguments.

Parameters:

unknown_args – List of unknown args returned by method parser.parse_known_args().

Returns:

Dictionary with parsed unknown args.

Return type:

extra_args

fgvc.utils.experiment.save_config(config: dict)

Save configuration JSON into experiment directory.

Parameters:

config – Dictionary with training configuration.