coreο
Module containing core utility functions and abstract classes for models.
- class FilterOutputs(indexes: int | list[int])ο
Helper class for use in :class:~`torch.nn.Sequential` to filter previous layerβs outputs by index.
- forward(inputs: Tensor) Tensorο
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class MinervaBackbone(*args, **kwargs)ο
Abstract class to mark a model for use as a backbone.
- freeze_backbone(freeze: bool = True) Noneο
Freeze the backbone so that the weights do not change while the rest of the model trains.
- Parameters:
freeze (bool) β Whether to βfreezeβ the backbone (Set
requires_grad_()to False). Defaults to True.
Added in version 0.28.
- class MinervaDataParallel(model: Module, paralleliser: Type[DataParallel] | Type[DistributedDataParallel], *args, **kwargs)ο
Wrapper for
DataParallelorDistributedDataParallelthat automatically fetches the attributes of the wrapped model.- modelο
torchmodel to be wrapped byDataParallelorDistributedDataParallel.- Type:
- Parameters:
model (Module) β
torchmodel to be wrapped byDataParallelorDistributedDataParallel.
- class MinervaModel(criterion: Module | None = None, input_size: tuple[int, ...] | None = None, n_classes: int | None = None, scaler: GradScaler | None = None)ο
Abstract class to act as a base for all Minerva Models.
Designed to provide inter-compatability with
Trainer.- input_shapeο
Optional; Defines the shape of the input data. Typically in order of number of channels, image width, image height but may vary dependant on model specs.
- output_shapeο
The shape of the output of the network. Determined and set by
determine_output_dim().
- Parameters:
criterion (Module) β Optional;
torchloss function model will use.input_shape (tuple[int, ...]) β Optional; Defines the shape of the input data. Typically in order of number of channels, image width, image height but may vary dependant on model specs.
n_classes (int) β Optional; Number of classes in input data.
- determine_output_dim(sample_pairs: bool = False, change_detection: bool = False) Noneο
Uses
get_output_shape()to find the dimensions of the output of this model and sets to attribute.
- set_criterion(criterion: Module) Noneο
Set the internal criterion.
- Parameters:
criterion (Module) β Criterion (loss function) to set.
- set_optimiser(optimiser: Optimizer) Noneο
Sets the optimiser used by the model.
Warning
MUST be called after initialising a model and supplied with a
torch.optim.Optimizerusing this modelβs parameters.- Parameters:
optimiser (Optimizer) β
torch.optim.Optimizermodel will use, initialised with this modelβs parameters.
- step(x: Tensor, y: Tensor, train: bool = False) tuple[Tensor, Tensor | tuple[Tensor, ...]]ο
- step(x: Tensor, *, train: bool = False) tuple[Tensor, Tensor | tuple[Tensor, ...]]
Generic step of model fitting using a batch of data.
- Raises:
NotImplementedError β If
optimiserisNone.NotImplementedError β If
criterionisNone.
- Parameters:
x (Tensor) β Batch of input data to network.
y (Tensor) β Either a batch of ground truth labels or generated labels/ pairs.
train (bool) β Sets whether this shall be a training step or not.
Truefor training step which will then clear theoptimiser, and perform a backward pass of the network then update theoptimiser. IfFalsefor a validation or testing step, these actions are not taken.
- Returns:
tupleof the loss computed by the loss function and the model outputs.- Return type:
- class MinervaOnnxModel(model: Module, *args, **kwargs)ο
Special model class for enabling
onnxmodels to be used withinminerva.
- class MinervaWrapper(model: Module | Callable[[...], Module], criterion: Module | None = None, input_size: tuple[int, ...] | None = None, n_classes: int | None = None, scaler: GradScaler | None = None, *args, **kwargs)ο
Wraps a
torchmodel class inMinervaModelso it can be used inminerva.- Parameters:
model (Module | Callable[..., Module]) β The
torchmodel object or constructor to, initialise and then wrap inmodel.criterion (Module) β Optional;
torchloss function model will use.input_shape (tuple[int, ...]) β Optional; Defines the shape of the input data. Typically in order of number of channels, image width, image height but may vary dependant on model specs.
n_classes (int) β Optional; Number of classes in input data.
- forward(*inputs) Anyο
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- bilinear_init(in_channels: int, out_channels: int, kernel_size: int) Tensorο
Constructs the weights for the bi-linear interpolation kernel for use in transpose convolutional layers.
Source: https://github.com/haoran1062/FCN-pytorch/blob/master/FCN.py
- Parameters:
- Returns:
Tensorof the initialised bi-linear interpolated weights for the transpose convolutional layerβs kernels.- Return type:
- extract_wrapped_model(model: MinervaModel | MinervaDataParallel | OptimizedModule) MinervaModelο
Extracts the actual model object from within
MinervaDataParallelor :class:~`torch._dynamo.eval_frame.OptimizedModule` and returns.- Parameters:
model (MinervaModel | MinervaDataParallel | OptimizedModule) β Model that may or may not be wrapped.
- Returns:
Extracted model.
- Return type:
Added in version 0.27.
- get_model(model_name: str) Callable[[...], MinervaModel]ο
Returns the constructor of the
model_nameinmodels.- Parameters:
model_name (str) β Name of the model to get.
- Returns:
Constructor of the model requested.
- Return type:
Callable[β¦, MinervaModel]
- get_output_shape(model: Module, image_dim: Sequence[int] | int, sample_pairs: bool = False, change_detection: bool = False) tuple[int, ...]ο
Gets the output shape of a model.
- Parameters:
- Returns:
The shape of the output data from the model.
- Return type:
- get_torch_weights(weights_name: str) WeightsEnum | Noneο
Loads pre-trained model weights from
torchvisionvia Torch Hub API.- Parameters:
weights_name (str) β Name of model weights. See https://pytorch.org/vision/stable/models.html#table-of-all-available-classification-weights for a list of possible pre-trained weights.
- Returns:
API query for the specified weights.
Noneif query cannot be found. See note on use:- Return type:
torchvision.models._api.WeightsEnum | None
Note
This function only returns a query for the API of the weights. To actually use them, you need to call
get_state_dict()to download the weights (if not already in cache).
- is_minerva_model(model: Module) boolο
Checks if model is a
MinervaModelwhile accounting for models that are :class:~`torch._dynamo.eval_frame.OptimizedModule` fromtorch.compile()usage.- Parameters:
model (Module) β Torch model to be evaluated.
- Returns:
Trueif model (or model wrapped in a compiled model) is aMinervaModelorMinervaDataParallel, elseFalse.- Return type:
Added in version 0.27.
- is_minerva_subtype(model: Module, subtype: type) boolο
Checks if model is a specific type while accounting for models that are :class:~`torch._dynamo.eval_frame.OptimizedModule` from
torch.compile()usage.- Parameters:
model (Module) β Torch model to be evaluated.
subtype (type) β Type to check model against.
- Returns:
Trueif model (or model wrapped in a compiled model) issubtypeelseFalse.- Return type:
Added in version 0.27.