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
Module
instance 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
DataParallel
orDistributedDataParallel
that automatically fetches the attributes of the wrapped model.- modelο
torch
model to be wrapped byDataParallel
orDistributedDataParallel
.- Type:
- Parameters:
model (Module) β
torch
model to be wrapped byDataParallel
orDistributedDataParallel
.
- 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;
torch
loss 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.Optimizer
using this modelβs parameters.- Parameters:
optimiser (Optimizer) β
torch.optim.Optimizer
model 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
optimiser
isNone
.NotImplementedError β If
criterion
isNone
.
- 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.
True
for training step which will then clear theoptimiser
, and perform a backward pass of the network then update theoptimiser
. IfFalse
for a validation or testing step, these actions are not taken.
- Returns:
tuple
of the loss computed by the loss function and the model outputs.- Return type:
- class MinervaOnnxModel(model: Module, *args, **kwargs)ο
Special model class for enabling
onnx
models 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
torch
model class inMinervaModel
so it can be used inminerva
.- Parameters:
model (Module | Callable[..., Module]) β The
torch
model object or constructor to, initialise and then wrap inmodel
.criterion (Module) β Optional;
torch
loss 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
Module
instance 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:
Tensor
of 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
MinervaDataParallel
or :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_name
inmodels
.- 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
torchvision
via 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.
None
if 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
MinervaModel
while accounting for models that are :class:~`torch._dynamo.eval_frame.OptimizedModule` fromtorch.compile()
usage.- Parameters:
model (Module) β Torch model to be evaluated.
- Returns:
True
if model (or model wrapped in a compiled model) is aMinervaModel
orMinervaDataParallel
, 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:
True
if model (or model wrapped in a compiled model) issubtype
elseFalse
.- Return type:
Added in version 0.27.