fovi.training.loading
Restore training state from an existing experiment.
- class fovi.training.loading.Sequence
Bases:
Reversible,CollectionAll the operations on a read-only sequence.
Concrete subclasses must override __new__ or __init__, __getitem__, and __len__.
- count(value) integer -- return number of occurrences of value
- index(value[, start[, stop]]) integer -- return first index of value.
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- class fovi.training.loading.OmegaConf[source]
Bases:
objectOmegaConf primary class
- static structured(obj: Any, parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) Any[source]
- static create(obj: str, parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) DictConfig | ListConfig[source]
- static create(obj: List[Any] | Tuple[Any, ...], parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) ListConfig
- static create(obj: DictConfig, parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) DictConfig
- static create(obj: ListConfig, parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) ListConfig
- static create(obj: Dict[Any, Any] | None = None, parent: BaseContainer | None = None, flags: Dict[str, bool] | None = None) DictConfig
- static save(config: Any, f: str | Path | IO[Any], resolve: bool = False) None[source]
Save as configuration object to a file
- Parameters:
config – omegaconf.Config object (DictConfig or ListConfig).
f – filename or file object
resolve – True to save a resolved config (defaults to False)
- static from_dotlist(dotlist: List[str]) DictConfig[source]
Creates config from the content sys.argv or from the specified args list of not None
- Parameters:
dotlist – A list of dotlist-style strings, e.g.
["foo.bar=1", "baz=qux"].- Returns:
A
DictConfigobject created from the dotlist.
- static merge(*configs: DictConfig | ListConfig | Dict[str | bytes | int | Enum | float | bool, Any] | List[Any] | Tuple[Any, ...] | Any) ListConfig | DictConfig[source]
Merge a list of previously created configs into a single one
- Parameters:
configs – Input configs
- Returns:
the merged config object.
- static unsafe_merge(*configs: DictConfig | ListConfig | Dict[str | bytes | int | Enum | float | bool, Any] | List[Any] | Tuple[Any, ...] | Any) ListConfig | DictConfig[source]
Merge a list of previously created configs into a single one This is much faster than OmegaConf.merge() as the input configs are not copied. However, the input configs must not be used after this operation as will become inconsistent.
- Parameters:
configs – Input configs
- Returns:
the merged config object.
- static register_new_resolver(name: str, resolver: Callable[[...], Any], *, replace: bool = False, use_cache: bool = False) None[source]
Register a resolver.
- Parameters:
name – Name of the resolver.
resolver – Callable whose arguments are provided in the interpolation, e.g., with ${foo:x,0,${y.z}} these arguments are respectively “x” (str), 0 (int) and the value of
y.z.replace – If set to
False(default), then aValueErroris raised if an existing resolver has already been registered with the same name. If set toTrue, then the new resolver replaces the previous one. NOTE: The cache on existing config objects is not affected, useOmegaConf.clear_cache(cfg)to clear it.use_cache – Whether the resolver’s outputs should be cached. The cache is based only on the string literals representing the resolver arguments, e.g., ${foo:${bar}} will always return the same value regardless of the value of
barif the cache is enabled forfoo.
- static clear_resolvers() None[source]
Clear(remove) all OmegaConf resolvers, then re-register OmegaConf’s default resolvers.
- classmethod clear_resolver(name: str) bool[source]
Clear(remove) any resolver only if it exists.
Returns a bool: True if resolver is removed and False if not removed.
- Parameters:
name – Name of the resolver.
- Returns:
A bool (
Trueif resolver is removed,Falseif not found before removing).
- static masked_copy(conf: DictConfig, keys: str | List[str]) DictConfig[source]
Create a masked copy of of this config that contains a subset of the keys
- Parameters:
conf – DictConfig object
keys – keys to preserve in the copy
- Returns:
The masked
DictConfigobject.
- static to_container(cfg: Any, *, resolve: bool = False, throw_on_missing: bool = False, enum_to_str: bool = False, structured_config_mode: SCMode = SCMode.DICT) Dict[str | bytes | int | Enum | float | bool, Any] | List[Any] | None | str | Any[source]
Resursively converts an OmegaConf config to a primitive container (dict or list).
- Parameters:
cfg – the config to convert
resolve – True to resolve all values
throw_on_missing – When True, raise MissingMandatoryValue if any missing values are present. When False (the default), replace missing values with the string “???” in the output container.
enum_to_str – True to convert Enum keys and values to strings
structured_config_mode –
- Specify how Structured Configs (DictConfigs backed by a dataclass) are handled.
By default (
structured_config_mode=SCMode.DICT) structured configs are converted to plain dicts.If
structured_config_mode=SCMode.DICT_CONFIG, structured config nodes will remain as DictConfig.If
structured_config_mode=SCMode.INSTANTIATE, this function will instantiate structured configs (DictConfigs backed by a dataclass), by creating an instance of the underlying dataclass.
See also OmegaConf.to_object.
- Returns:
A dict or a list representing this config as a primitive container.
- static to_object(cfg: Any) Dict[str | bytes | int | Enum | float | bool, Any] | List[Any] | None | str | Any[source]
Resursively converts an OmegaConf config to a primitive container (dict or list). Any DictConfig objects backed by dataclasses or attrs classes are instantiated as instances of those backing classes.
- This is an alias for OmegaConf.to_container(…, resolve=True, throw_on_missing=True,
structured_config_mode=SCMode.INSTANTIATE)
- Parameters:
cfg – the config to convert
- Returns:
A dict or a list or dataclass representing this config.
- static select(cfg: Container, key: str, *, default: Any = _DEFAULT_MARKER_, throw_on_resolution_failure: bool = True, throw_on_missing: bool = False) Any[source]
- Parameters:
cfg – Config node to select from
key – Key to select
default – Default value to return if key is not found
throw_on_resolution_failure – Raise an exception if an interpolation resolution error occurs, otherwise return None
throw_on_missing – Raise an exception if an attempt to select a missing key (with the value ‘???’) is made, otherwise return None
- Returns:
selected value or None if not found.
- static update(cfg: Container, key: str, value: Any = None, *, merge: bool = True, force_add: bool = False) None[source]
Updates a dot separated key sequence to a value
- Parameters:
cfg – input config to update
key – key to update (can be a dot separated path)
value – value to set, if value if a list or a dict it will be merged or set depending on merge_config_values
merge – If value is a dict or a list, True (default) to merge into the destination, False to replace the destination.
force_add – insert the entire path regardless of Struct flag or Structured Config nodes.
- static to_yaml(cfg: Any, *, resolve: bool = False, sort_keys: bool = False) str[source]
returns a yaml dump of this config object.
- Parameters:
cfg – Config object, Structured Config type or instance
resolve – if True, will return a string with the interpolations resolved, otherwise interpolations are preserved
sort_keys – If True, will print dict keys in sorted order. default False.
- Returns:
A string containing the yaml representation.
- static resolve(cfg: Container) None[source]
Resolves all interpolations in the given config object in-place.
- Parameters:
cfg – An OmegaConf container (DictConfig, ListConfig) Raises a ValueError if the input object is not an OmegaConf container.
- static missing_keys(cfg: Any) Set[str][source]
Returns a set of missing keys in a dotlist style.
- Parameters:
cfg – An
OmegaConf.Container, or a convertible object viaOmegaConf.create(dict, list, …).- Returns:
set of strings of the missing keys.
- Raises:
ValueError – On input not representing a config.
- fovi.training.loading.find_config(base_fn: str, load: bool, model_dirs: Sequence[str | Path] | None = None, device: str | device = 'cuda') tuple[DictConfig, dict[str, dict[str, Tensor]] | None, str | None][source]
Search explicit local locations, then download a model from the Hub.
An existing local model with malformed configuration or missing weights raises immediately. It must not silently select a different checkpoint from the Hub.
- class fovi.training.loading.HiddenPrints(enabled=True)[source]
Bases:
objectContext manager to suppress stdout output.
Temporarily redirects stdout to devnull to hide print statements from called functions.
- Parameters:
enabled (bool, optional) – Whether to suppress prints. If False, acts as a no-op. Defaults to True.
Example
>>> with HiddenPrints(): ... print("This won't be shown") >>> print("This will be shown") This will be shown
- class fovi.training.loading.Trainer(gpu, cfg: DictConfig, load_checkpoint=True)[source]
Bases:
object- __init__(gpu, cfg: DictConfig, load_checkpoint=True)[source]
Initialize trainer with hydra configuration
- Parameters:
gpu – which gpu to run on (or None to use cpu)
cfg – Hydra configuration object
load_checkpoint – Whether to load checkpoint
- create_optimizer()[source]
Create and configure optimizers for model and probes.
Sets up separate optimizers for the main model and linear probes, with appropriate weight decay settings and learning rate scaling.
- create_train_loader(train_dataset, subset=None, batches_ahead=3, phase='train')[source]
Create training data loader with appropriate transforms and augmentation.
- Parameters:
- Returns:
Configured data loader for training
- Return type:
- create_val_loader(val_dataset, subset=None, ratio=1.)[source]
Create validation data loader with center crop transforms.
- Parameters:
- Returns:
Configured data loader for validation
- Return type:
- create_standard_loader(dataset, batch_size, num_workers, resolution)[source]
Create standard data loader with basic transforms.
- Parameters:
- Returns:
- Standard PyTorch data loader with basic image transforms
(ToTensor, Resize, Normalize) applied
- Return type:
DataLoader
- create_model_and_scaler()[source]
Create and configure the neural network model and gradient scaler. :returns:
- (model, scaler) where model is the configured neural network
and scaler is the gradient scaler for mixed precision training
- Return type:
- train()[source]
Execute the main training loop.
Runs training for the specified number of epochs, performing validation at regular intervals and saving checkpoints. Handles learning rate scheduling and early stopping.
- Returns:
Training statistics for all epochs
- Return type:
- load_checkpoint(ckpt=None)[source]
Load model and optimizer state from checkpoint.
- Parameters:
ckpt (dict, optional) – Checkpoint dictionary. If None, loads from default checkpoint file in log folder.
- save_checkpoint(epoch)[source]
Save model and optimizer state to checkpoint file.
- Parameters:
epoch (int) – Current training epoch
- val_loop(return_preds=False, repeats=None)[source]
Execute validation loop.
Computes validation metrics for all n_fixations values in self.n_fixations_val. Runs a single forward pass with max(n_fixations_val) and slices outputs to evaluate at each fixation count.
- compute_activations(loader, layer_names=['projector'], fixation_size=None, area_range=None, training=False, n_fixations=None, max_batches=None, setting='supervised', do_postproc=False, **kwargs)[source]
Extract activations from specified layers for a given data loader.
Runs the model on data from the loader and captures intermediate activations from the specified layers using forward hooks.
- Parameters:
loader – Data loader to iterate over.
layer_names (list, optional) – List of layer names to capture activations from. Defaults to [‘projector’].
fixation_size (int or tuple, optional) – Size of fixation patches. Defaults to None.
area_range (list, optional) – [min, max] range of crop areas. Defaults to None.
training (bool, optional) – Whether to use training mode. Defaults to False.
n_fixations (int, optional) – Number of fixations per image. Defaults to None.
max_batches (int, optional) – Maximum number of batches to process. Defaults to None.
setting (str, optional) – Forward pass setting (‘supervised’ or ‘ssl’). Defaults to ‘supervised’.
do_postproc (bool, optional) – Whether to apply post-processing. Defaults to False.
**kwargs – Additional arguments passed to get_activations.
- Returns:
- (outputs, activations, targets) where:
outputs (np.ndarray): Model outputs of shape (N, …).
activations (dict): Dict mapping layer names to activation arrays.
targets (np.ndarray): Target labels of shape (N,).
- Return type:
- initialize_remote_logger()[source]
Initialize remote logging (e.g., wandb) for experiment tracking.
- classmethod exec(gpu, cfg)[source]
Execute training with the given configuration.
- Parameters:
gpu (int) – GPU device ID
cfg (DictConfig) – Training configuration
- classmethod launch_from_args(cfg)[source]
Launch training with the given configuration.
- Parameters:
cfg (DictConfig) – Training configuration
- fovi.training.loading.get_trainer_from_base_fn(base_fn: str, load: bool = True, load_strict: bool = True, quiet: bool = False, allow_distributed: bool = False, gpu: int | None = 0, model_dirs: Sequence[str] = ('../models', SAVE_DIR + '/logs', SLOW_DIR + '/logs'), **kwargs: str | float | bool | None) Trainer[source]
Get a Trainer instance based on a base filename and optional parameters.
This function loads a model configuration and optionally its weights from a specified directory, creates a Trainer instance with the loaded configuration, and returns it.
- Parameters:
base_fn (str) – The base filename to look for in the logs directory.
load (bool, optional) – Whether to load the model weights. Defaults to True.
load_strict (bool, optional) – Whether to strictly enforce matching keys when loading weights. Defaults to True.
quiet (bool, optional) – Whether to suppress print statements. Defaults to False.
allow_distributed (bool, optional) – Whether to allow distributed training configuration. Defaults to False.
**kwargs – Additional keyword arguments to override or add to the configuration.
- Returns:
An instance of Trainer with the specified configuration and optionally loaded weights.
- Return type:
Note
The function searches for the model in both SLOW_DIR and SAVE_DIR. It prioritizes loading final weights over non-final weights if available.