Tools and Utilities¶
Tools¶
Frozen Class¶
Frozen class.
Example of usage can be found in catalyst.core.runner.IRunner
.
Time Manager¶
Simple timer.
Typing¶
All Catalyst custom types are defined in this module.
-
catalyst.tools.typing.
Model
¶ alias of
torch.nn.modules.module.Module
-
catalyst.tools.typing.
Criterion
¶ alias of
torch.nn.modules.module.Module
-
class
catalyst.tools.typing.
Optimizer
(params, defaults)[source]¶ Bases:
object
Base class for all optimizers.
Warning
Parameters need to be specified as collections that have a deterministic ordering that is consistent between runs. Examples of objects that don’t satisfy those properties are sets and iterators over values of dictionaries.
- Parameters
params (iterable) – an iterable of
torch.Tensor
s ordict
s. Specifies what Tensors should be optimized.defaults – (dict): a dict containing default values of optimization options (used when a parameter group doesn’t specify them).
-
add_param_group
(param_group)[source]¶ Add a param group to the
Optimizer
s param_groups.This can be useful when fine tuning a pre-trained network as frozen layers can be made trainable and added to the
Optimizer
as training progresses.- Parameters
param_group (dict) – Specifies what Tensors should be optimized along with group
optimization options. (specific) –
-
load_state_dict
(state_dict)[source]¶ Loads the optimizer state.
- Parameters
state_dict (dict) – optimizer state. Should be an object returned from a call to
state_dict()
.
-
state_dict
()[source]¶ Returns the state of the optimizer as a
dict
.It contains two entries:
- state - a dict holding current optimization state. Its content
differs between optimizer classes.
param_groups - a dict containing all parameter groups
-
catalyst.tools.typing.
Scheduler
¶ alias of
torch.optim.lr_scheduler._LRScheduler
-
class
catalyst.tools.typing.
Dataset
[source]¶ Bases:
object
An abstract class representing a
Dataset
.All datasets that represent a map from keys to data samples should subclass it. All subclasses should overwrite
__getitem__()
, supporting fetching a data sample for a given key. Subclasses could also optionally overwrite__len__()
, which is expected to return the size of the dataset by manySampler
implementations and the default options ofDataLoader
.Note
DataLoader
by default constructs a index sampler that yields integral indices. To make it work with a map-style dataset with non-integral indices/keys, a custom sampler must be provided.
Meters¶
The meters from torchnet.meters.
Every meter implements catalyst.tools.meters.meter.Meter
interface.
Meter¶
Meters provide a way to keep track of important statistics in an online manner.
AP Meter¶
The APMeter measures the average precision per class.
-
class
catalyst.tools.meters.apmeter.
APMeter
[source]¶ Bases:
catalyst.tools.meters.meter.Meter
The APMeter is designed to operate on NxK Tensors output and target, and optionally a Nx1 Tensor weight where:
1. The output contains model output scores for N examples and K classes that ought to be higher when the model is more convinced that the example should be positively labeled, and smaller when the model believes the example should be negatively labeled (for instance, the output of a sigmoid function).
2. The target contains only values 0 (for negative examples) and 1 (for positive examples).
3. The weight ( > 0) represents weight for each sample.
-
add
(output: torch.Tensor, target: torch.Tensor, weight: torch.Tensor = None) → None[source]¶ Add a new observation.
- Parameters
output (Tensor) – NxK tensor that for each of the N examples indicates the probability of the example belonging to each of the K classes, according to the model. The probabilities should sum to one over all classes
target (Tensor) – binary NxK tensort that encodes which of the K classes are associated with the N-th input (eg: a row [0, 1, 0, 1] indicates that the example is associated with classes 2 and 4)
weight (optional, Tensor) – Nx1 tensor representing the weight for each example (each weight > 0)
-
AUC Meter¶
The AUCMeter measures the area under the receiver-operating characteristic (ROC) curve for binary classification problems. The area under the curve (AUC) can be interpreted as the probability that, given a randomly selected positive example and a randomly selected negative example, the positive example is assigned a higher score by the classification model than the negative example.
-
class
catalyst.tools.meters.aucmeter.
AUCMeter
[source]¶ Bases:
catalyst.tools.meters.meter.Meter
The AUCMeter is designed to operate on one-dimensional Tensors output and target, where:
1. The output contains model output scores that ought to be higher when the model is more convinced that the example should be positively labeled, and smaller when the model believes the example should be negatively labeled (for instance, the output of a sigmoid function)
2. The target contains only values 0 (for negative examples) and 1 (for positive examples).
Average Value Meter¶
Average value meter
-
class
catalyst.tools.meters.averagevaluemeter.
AverageValueMeter
[source]¶ Bases:
catalyst.tools.meters.meter.Meter
Average value meter stores mean and standard deviation for population of input values. Meter updates are applied online, one value for each update. Values are not cached, only the last added.
-
add
(value, batch_size) → None[source]¶ Add a new observation.
Updates of mean and std are going online, with Welford’s online algorithm.
- Parameters
value (float) – value for update, can be scalar number or PyTorch tensor
batch_size (int) – batch size for update
Note
Because of algorithm design, you can update meter values with only one value a time.
-
Class Error Meter¶
-
class
catalyst.tools.meters.classerrormeter.
ClassErrorMeter
(topk=None, accuracy=False)[source]¶ Bases:
catalyst.tools.meters.meter.Meter
@TODO: Docs. Contribution is welcome.
Confusion Meter¶
Maintains a confusion matrix for a given classification problem.
-
class
catalyst.tools.meters.confusionmeter.
ConfusionMeter
(k: int, normalized: bool = False)[source]¶ Bases:
catalyst.tools.meters.meter.Meter
ConfusionMeter constructs a confusion matrix for a multi-class classification problems. It does not support multi-label, multi-class problems: for such problems, please use MultiLabelConfusionMeter.
-
__init__
(k: int, normalized: bool = False)[source]¶ - Parameters
k (int) – number of classes in the classification problem
normalized (boolean) – determines whether or not the confusion matrix is normalized or not
-
add
(predicted: torch.Tensor, target: torch.Tensor) → None[source]¶ Computes the confusion matrix of
K x K
size whereK
is no of classes.- Parameters
predicted (tensor) – Can be an N x K tensor of predicted scores obtained from the model for N examples and K classes or an N-tensor of integer values between 0 and K-1
target (tensor) – Can be a N-tensor of integer values assumed to be integer values between 0 and K-1 or N x K tensor, where targets are assumed to be provided as one-hot vectors
-
Map Meter¶
The mAP meter measures the mean average precision over all classes.
-
class
catalyst.tools.meters.mapmeter.
mAPMeter
[source]¶ Bases:
catalyst.tools.meters.meter.Meter
This meter is a wrapper for
catalyst.tools.meters.apmeter.APMeter
. The mAPMeter is designed to operate on NxK Tensors output and target, and optionally a Nx1 Tensor weight where:1. The output contains model output scores for N examples and K classes that ought to be higher when the model is more convinced that the example should be positively labeled, and smaller when the model believes the example should be negatively labeled (for instance, the output of a sigmoid function)
2. The target contains only values 0 (for negative examples) and 1 (for positive examples)
3. The weight ( > 0) represents weight for each sample.
-
add
(output: torch.Tensor, target: torch.Tensor, weight: Optional[torch.Tensor] = None) → None[source]¶ Update self.apmeter.
- Parameters
output (Tensor) – Model output scores as NxK tensor
target (Tensor) – Target scores as NxK tensor
weight (Tensor, optional) – Weight values for each sample as Nx1 Tensor
-
Moving Average Value Meter¶
Moving average meter calculates average for moving window of values.
-
class
catalyst.tools.meters.movingaveragevaluemeter.
MovingAverageValueMeter
(windowsize)[source]¶ Bases:
catalyst.tools.meters.meter.Meter
MovingAverageValueMeter stores mean and standard deviation for population of array that is handled like a queue during updates. Queue(window) is filled with zeros from the start by default. Meter updates are applied online, one value for each update. Meter values are moving average and moving standard deviation.
MSE Meter¶
MSE and RMSE meters.
-
class
catalyst.tools.meters.msemeter.
MSEMeter
(root: bool = False)[source]¶ Bases:
catalyst.tools.meters.meter.Meter
This meter can handle MSE and RMSE. Root calculation can be toggled(not calculated by default).
-
__init__
(root: bool = False)[source]¶ - Parameters
root (bool) – Toggle between calculation of RMSE (True) and MSE (False)
-
Precision-Recall-F1 Meter¶
In this module precision, recall and F1 score calculations are defined in separate functions.
PrecisionRecallF1ScoreMeter
can keep track for all three of these.
-
class
catalyst.tools.meters.ppv_tpr_f1_meter.
PrecisionRecallF1ScoreMeter
(threshold=0.5)[source]¶ Bases:
catalyst.tools.meters.meter.Meter
Keeps track of global true positives, false positives, and false negatives for each epoch and calculates precision, recall, and F1-score based on those metrics. Currently, this meter works for binary cases only, please use multiple instances of this class for multi-label cases.
-
add
(output: torch.Tensor, target: torch.Tensor) → None[source]¶ Thresholds predictions and calculates the true positives, false positives, and false negatives in comparison to the target.
- Parameters
output (torch.Tensor) – prediction after activation function shape should be (batch_size, …), but works with any shape
target (torch.Tensor) – label (binary), shape should be the same as output’s shape
-
Utils¶
All utils are gathered in catalyst.utils
for easier access.
Note
Everything from catalyst.contrib.utils
is included in catalyst.utils
Checkpoint¶
-
catalyst.utils.checkpoint.
pack_checkpoint
(model=None, criterion=None, optimizer=None, scheduler=None, **kwargs)[source]¶ @TODO: Docs. Contribution is welcome.
-
catalyst.utils.checkpoint.
unpack_checkpoint
(checkpoint, model=None, criterion=None, optimizer=None, scheduler=None) → None[source]¶ Load checkpoint from file and unpack the content to a model (if not None), criterion (if not None), optimizer (if not None), scheduler (if not None).
- Parameters
checkpoint (str) – checkpoint to load
model (torch.nn.Module) – model where should be updated state
criterion – criterion where should be updated state
optimizer – optimizer where should be updated state
scheduler – scheduler where should be updated state
-
catalyst.utils.checkpoint.
save_checkpoint
(checkpoint: Dict, logdir: Union[pathlib.Path, str], suffix: str, is_best: bool = False, is_last: bool = False, special_suffix: str = '', saver_fn: Callable = <function save>)[source]¶ Saving checkpoint to a file.
- Parameters
checkpoint (dict) – data to save.
logdir (Path/str) – directory where checkpoint should be stored.
suffix (str) – checkpoint file name.
is_best (bool) – if
True
then also will be generated best checkpoint file.is_last (bool) – if
True
then also will be generated last checkpoint file.special_suffix (str) – suffix to use for saving best/last checkpoints.
saver_fn (Callable) – function to use for saving data to file, default is
torch.save
Components¶
-
catalyst.utils.components.
process_components
(model: torch.nn.modules.module.Module, criterion: torch.nn.modules.module.Module = None, optimizer: torch.optim.optimizer.Optimizer = None, scheduler: torch.optim.lr_scheduler._LRScheduler = None, distributed_params: Dict = None, device: Union[str, torch.device] = None) → Tuple[torch.nn.modules.module.Module, torch.nn.modules.module.Module, torch.optim.optimizer.Optimizer, torch.optim.lr_scheduler._LRScheduler, Union[str, torch.device]][source]¶ Returns the processed model, criterion, optimizer, scheduler and device.
- Parameters
model (Model) – torch model
criterion (Criterion) – criterion function
optimizer (Optimizer) – optimizer
scheduler (Scheduler) – scheduler
distributed_params (dict, optional) – dict with the parameters for distributed and FP16 method
device (Device, optional) – device
- Returns
tuple with processed model, criterion, optimizer, scheduler and device.
- Raises
ValueError – if device is None and TPU available, for using TPU need to manualy move model/optimizer/scheduler to a TPU device and pass device to a function.
NotImplementedError – if model is not nn.Module or dict for multi-gpu, nn.ModuleDict for DataParallel not implemented yet
Config¶
-
catalyst.utils.config.
load_config
(path: Union[str, pathlib.Path], ordered: bool = False, data_format: str = None, encoding: str = 'utf-8') → Union[Dict, List][source]¶ Loads config by giving path. Supports YAML and JSON files.
Examples
>>> load(path="./config.yml", ordered=True)
- Parameters
path (str) – path to config file (YAML or JSON)
ordered (bool) – if true the config will be loaded as
OrderedDict
data_format (str) –
yaml
,yml
orjson
.encoding (str) – encoding to read the config
- Returns
config
- Return type
Union[Dict, List]
- Raises
Exception – if path
path
doesn’t exists or file format is not YAML or JSON
Adapted from https://github.com/TezRomacH/safitty/blob/v1.2.0/safitty/parser.py#L63 which was adapted from https://github.com/catalyst-team/catalyst/blob/v19.03/catalyst/utils/config.py#L10
-
catalyst.utils.config.
save_config
(config: Union[Dict, List], path: Union[str, pathlib.Path], data_format: str = None, encoding: str = 'utf-8', ensure_ascii: bool = False, indent: int = 2) → None[source]¶ Saves config to file. Path must be either YAML or JSON.
- Parameters
config (Union[Dict, List]) – config to save
path (Union[str, Path]) – path to save
data_format (str) –
yaml
,yml
orjson
.encoding (str) – Encoding to write file. Default is
utf-8
ensure_ascii (bool) – Used for JSON, if True non-ASCII
are escaped in JSON strings. (characters) –
indent (int) – Used for JSON
Adapted from https://github.com/TezRomacH/safitty/blob/v1.2.0/safitty/parser.py#L110 which was adapted from https://github.com/catalyst-team/catalyst/blob/v19.03/catalyst/utils/config.py#L38
Distributed¶
-
catalyst.utils.distributed.
check_ddp_wrapped
(model: torch.nn.modules.module.Module) → bool[source]¶ Checks whether model is wrapped with DataParallel/DistributedDataParallel.
-
catalyst.utils.distributed.
check_torch_distributed_initialized
() → bool[source]¶ Checks if torch.distributed is available and initialized.
-
catalyst.utils.distributed.
assert_fp16_available
() → None[source]¶ Asserts for installed and available Apex FP16.
-
catalyst.utils.distributed.
initialize_apex
(model, optimizer=None, **distributed_params)[source]¶ @TODO: Docs. Contribution is welcome.
-
catalyst.utils.distributed.
get_nn_from_ddp_module
(model: torch.nn.modules.module.Module) → torch.nn.modules.module.Module[source]¶ Return a real model from a torch.nn.DataParallel, torch.nn.parallel.DistributedDataParallel, or apex.parallel.DistributedDataParallel.
- Parameters
model – A model, or DataParallel wrapper.
- Returns
A model
-
catalyst.utils.distributed.
get_rank
() → int[source]¶ Returns the rank of the current worker.
- Returns
rank
if torch.distributed is initialized, otherwise-1
- Return type
int
-
catalyst.utils.distributed.
get_distributed_mean
(value: Union[float, torch.Tensor])[source]¶ Computes distributed mean among all nodes.
-
catalyst.utils.distributed.
get_distributed_env
(local_rank, rank, world_size, use_cuda_visible_devices=True)[source]¶ @TODO: Docs. Contribution is welcome.
-
catalyst.utils.distributed.
is_wrapped_with_ddp
(model: torch.nn.modules.module.Module) → bool[source]¶ Checks whether model is wrapped with DataParallel/DistributedDataParallel.
Deprecated since version 20.05: This will be removed in 20.06. Use check_ddp_wrapped instead.
-
catalyst.utils.distributed.
is_torch_distributed_initialized
() → bool[source]¶ Checks if torch.distributed is available and initialized.
Deprecated since version 20.05: This will be removed in 20.06. Use check_torch_distributed_initialized instead.
Hash¶
Initialization¶
-
catalyst.utils.initialization.
get_optimal_inner_init
(nonlinearity: torch.nn.modules.module.Module, **kwargs) → Callable[[torch.nn.modules.module.Module], None][source]¶ Create initializer for inner layers based on their activation function (nonlinearity).
- Parameters
nonlinearity – non-linear activation
**kwargs – extra kwargs
- Returns
optimal initialization function
- Raises
NotImplementedError – if nonlinearity is out of sigmoid, tanh, relu, `leaky_relu
Misc¶
-
catalyst.utils.misc.
copy_directory
(input_dir: pathlib.Path, output_dir: pathlib.Path) → None[source]¶ Recursively copies the input directory.
- Parameters
input_dir (Path) – input directory
output_dir (Path) – output directory
-
catalyst.utils.misc.
format_metric
(name: str, value: float) → str[source]¶ Format metric.
Metric will be returned in the scientific format if 4 decimal chars are not enough (metric value lower than 1e-4).
- Parameters
name (str) – metric name
value (float) – value of metric
- Returns
formatted metric
- Return type
str
-
catalyst.utils.misc.
get_fn_default_params
(fn: Callable[[...], Any], exclude: List[str] = None)[source]¶ Return default parameters of Callable.
- Parameters
fn (Callable[.., Any]) – target Callable
exclude (List[str]) – exclude list of parameters
- Returns
contains default parameters of fn
- Return type
dict
-
catalyst.utils.misc.
get_fn_argsnames
(fn: Callable[[...], Any], exclude: List[str] = None)[source]¶ Return parameter names of Callable.
- Parameters
fn (Callable[.., Any]) – target Callable
exclude (List[str]) – exclude list of parameters
- Returns
contains parameter names of fn
- Return type
list
-
catalyst.utils.misc.
get_utcnow_time
(format: str = None) → str[source]¶ Return string with current utc time in chosen format.
- Parameters
format (str) – format string. if None “%y%m%d.%H%M%S” will be used.
- Returns
formatted utc time string
- Return type
str
-
catalyst.utils.misc.
is_exception
(ex: Any) → bool[source]¶ Check if the argument is of
Exception
type.
-
catalyst.utils.misc.
maybe_recursive_call
(object_or_dict, method: str, recursive_args=None, recursive_kwargs=None, **kwargs)[source]¶ Calls the
method
recursively for theobject_or_dict
.- Parameters
object_or_dict (Any) – some object or a dictionary of objects
method (str) – method name to call
recursive_args – list of arguments to pass to the
method
recursive_kwargs – list of key-arguments to pass to the
method
**kwargs – Arbitrary keyword arguments
- Returns
result of method call
-
catalyst.utils.misc.
fn_ends_with_pass
(fn: Callable[[...], Any])[source]¶ Check that function end with pass statement (probably does nothing in any way). Mainly used to filter callbacks with empty on_{event} methods.
- Parameters
fn (Callable[.., Any]) – target Callable
- Returns
True if there is pass in the first indentation level of fn and nothing happens before it, False in any other case.
- Return type
bool
Numpy¶
-
catalyst.utils.numpy.
get_one_hot
(label: int, num_classes: int, smoothing: float = None) → numpy.ndarray[source]¶ Applies OneHot vectorization to a giving scalar, optional with label smoothing as described in Bag of Tricks for Image Classification with Convolutional Neural Networks.
- Parameters
label (int) – scalar value to be vectorized
num_classes (int) – total number of classes
smoothing (float, optional) – if specified applies label smoothing from
Bag of Tricks for Image Classification with Convolutional Neural Networks
paper
- Returns
a one-hot vector with shape
(num_classes,)
- Return type
np.ndarray
Parser¶
Scripts¶
-
catalyst.utils.scripts.
import_module
(expdir: pathlib.Path)[source]¶ @TODO: Docs. Contribution is welcome
-
catalyst.utils.scripts.
import_experiment_and_runner
(expdir: pathlib.Path)[source]¶ @TODO: Docs. Contribution is welcome
-
catalyst.utils.scripts.
dump_base_experiment_code
(src: pathlib.Path, dst: pathlib.Path)[source]¶ @TODO: Docs. Contribution is welcome
-
catalyst.utils.scripts.
distributed_cmd_run
(worker_fn: Callable, distributed: bool = True, *args, **kwargs) → None[source]¶ Distributed run
- Parameters
worker_fn (Callable) – worker fn to run in distributed mode
distributed (bool) – distributed flag
args – additional parameters for worker_fn
kwargs – additional key-value parameters for worker_fn
Sys¶
-
catalyst.utils.sys.
get_environment_vars
() → Dict[str, Any][source]¶ Creates a dictionary with environment variables.
- Returns
environment variables
- Return type
Dict
-
catalyst.utils.sys.
list_conda_packages
() → str[source]¶ Lists conda installed packages.
- Returns
list with conda installed packages
- Return type
str
-
catalyst.utils.sys.
list_pip_packages
() → str[source]¶ Lists pip installed packages.
- Returns
string with pip installed packages
- Return type
str
-
catalyst.utils.sys.
dump_environment
(experiment_config: Dict, logdir: str, configs_path: List[str] = None) → None[source]¶ Saves config, environment variables and package list in JSON into logdir.
- Parameters
experiment_config (dict) – experiment config
logdir (str) – path to logdir
configs_path – path(s) to config
Torch¶
-
catalyst.utils.torch.
get_optimizable_params
(model_or_params)[source]¶ Returns all the parameters that requires gradients.
-
catalyst.utils.torch.
get_optimizer_momentum
(optimizer: torch.optim.optimizer.Optimizer) → float[source]¶ Get momentum of current optimizer.
- Parameters
optimizer – PyTorch optimizer
- Returns
momentum at first param group
- Return type
float
-
catalyst.utils.torch.
set_optimizer_momentum
(optimizer: torch.optim.optimizer.Optimizer, value: float, index: int = 0)[source]¶ Set momentum of
index
‘th param group of optimizer tovalue
.- Parameters
optimizer – PyTorch optimizer
value (float) – new value of momentum
index (int, optional) – integer index of optimizer’s param groups, default is 0
-
catalyst.utils.torch.
get_device
() → torch.device[source]¶ Simple returning the best available device (TPU > GPU > CPU).
-
catalyst.utils.torch.
get_available_gpus
()[source]¶ Array of available GPU ids.
Examples
>>> os.environ["CUDA_VISIBLE_DEVICES"] = "0,2" >>> get_available_gpus() [0, 2]
>>> os.environ["CUDA_VISIBLE_DEVICES"] = "0,-1,1" >>> get_available_gpus() [0]
>>> os.environ["CUDA_VISIBLE_DEVICES"] = "" >>> get_available_gpus() []
>>> os.environ["CUDA_VISIBLE_DEVICES"] = "-1" >>> get_available_gpus() []
- Returns
available GPU ids
- Return type
iterable
-
catalyst.utils.torch.
get_activation_fn
(activation: str = None)[source]¶ Returns the activation function from
torch.nn
by its name.
-
catalyst.utils.torch.
any2device
(value, device: Union[str, torch.device])[source]¶ Move tensor, list of tensors, list of list of tensors, dict of tensors, tuple of tensors to target device.
- Parameters
value – Object to be moved
device (Device) – target device ids
- Returns
Same structure as value, but all tensors and np.arrays moved to device
-
catalyst.utils.torch.
prepare_cudnn
(deterministic: bool = None, benchmark: bool = None) → None[source]¶ Prepares CuDNN benchmark and sets CuDNN to be deterministic/non-deterministic mode
- Parameters
deterministic (bool) – deterministic mode if running in CuDNN backend.
benchmark (bool) – If
True
use CuDNN heuristics to figure out which algorithm will be most performant for your model architecture and input. Setting it toFalse
may slow down your training.
-
catalyst.utils.torch.
process_model_params
(model: torch.nn.modules.module.Module, layerwise_params: Dict[str, dict] = None, no_bias_weight_decay: bool = True, lr_scaling: float = 1.0) → List[Union[torch.nn.parameter.Parameter, dict]][source]¶ Gains model parameters for
torch.optim.Optimizer
.- Parameters
model (torch.nn.Module) – Model to process
layerwise_params (Dict) – Order-sensitive dict where each key is regex pattern and values are layer-wise options for layers matching with a pattern
no_bias_weight_decay (bool) – If true, removes weight_decay for all
bias
parameters in the modellr_scaling (float) – layer-wise learning rate scaling, if 1.0, learning rates will not be scaled
- Returns
parameters for an optimizer
- Return type
iterable
Example:
>>> model = catalyst.contrib.models.segmentation.ResnetUnet() >>> layerwise_params = collections.OrderedDict([ >>> ("conv1.*", dict(lr=0.001, weight_decay=0.0003)), >>> ("conv.*", dict(lr=0.002)) >>> ]) >>> params = process_model_params(model, layerwise_params) >>> optimizer = torch.optim.Adam(params, lr=0.0003)
-
catalyst.utils.torch.
get_requires_grad
(model: torch.nn.modules.module.Module)[source]¶ Gets the
requires_grad
value for all model parameters.Example:
>>> model = SimpleModel() >>> requires_grad = get_requires_grad(model)
- Parameters
model (torch.nn.Module) – model
- Returns
value
- Return type
requires_grad (Dict[str, bool])
-
catalyst.utils.torch.
set_requires_grad
(model: torch.nn.modules.module.Module, requires_grad: Union[bool, Dict[str, bool]])[source]¶ Sets the
requires_grad
value for all model parameters.Example:
>>> model = SimpleModel() >>> set_requires_grad(model, requires_grad=True) >>> # or >>> model = SimpleModel() >>> set_requires_grad(model, requires_grad={""})
- Parameters
model (torch.nn.Module) – model
requires_grad (Union[bool, Dict[str, bool]]) – value
-
catalyst.utils.torch.
get_network_output
(net: torch.nn.modules.module.Module, *input_shapes_args, **input_shapes_kwargs)[source]¶ # noqa: D202 For each input shape returns an output tensor
Examples
>>> net = nn.Linear(10, 5) >>> utils.get_network_output(net, (1, 10)) tensor([[[-0.2665, 0.5792, 0.9757, -0.5782, 0.1530]]])
- Parameters
net (Model) – the model
*input_shapes_args – variable length argument list of shapes
**input_shapes_kwargs – key-value arguemnts of shapes
- Returns
tensor with network output
-
catalyst.utils.torch.
detach
(tensor: torch.Tensor) → numpy.ndarray[source]¶ Detach a pytorch tensor from graph and convert it to numpy array
- Parameters
tensor – PyTorch tensor
- Returns
numpy ndarray
-
catalyst.utils.torch.
trim_tensors
(tensors)[source]¶ Trim padding off of a batch of tensors to the smallest possible length. Should be used with catalyst.data.DynamicLenBatchSampler.
Adapted from Dynamic minibatch trimming to improve BERT training speed.
- Parameters
tensors ([torch.tensor]) – list of tensors to trim.
- Returns
list of trimmed tensors.
- Return type
List[torch.tensor]
Metrics¶
Accuracy¶
- Various accuracy metrics:
-
catalyst.utils.metrics.accuracy.
accuracy
(outputs: torch.Tensor, targets: torch.Tensor, topk: Sequence[int] = (1, ), activation: Optional[str] = None) → Sequence[torch.Tensor][source]¶ Computes multi-class accuracy@topk for the specified values of topk.
- Parameters
outputs (torch.Tensor) – model outputs, logits with shape [bs; num_classes]
targets (torch.Tensor) – ground truth, labels with shape [bs; 1]
activation (str) – activation to use for model output
topk (Sequence[int]) – topk for accuracy@topk computing
- Returns
list with computed accuracy@topk
-
catalyst.utils.metrics.accuracy.
multi_label_accuracy
(outputs: torch.Tensor, targets: torch.Tensor, threshold: Union[float, torch.Tensor], activation: Optional[str] = None) → torch.Tensor[source]¶ Computes multi-label accuracy for the specified activation and threshold.
- Parameters
outputs (torch.Tensor) – NxK tensor that for each of the N examples indicates the probability of the example belonging to each of the K classes, according to the model.
targets (torch.Tensor) – binary NxK tensort that encodes which of the K classes are associated with the N-th input (eg: a row [0, 1, 0, 1] indicates that the example is associated with classes 2 and 4)
threshold (float) – threshold for for model output
activation (str) – activation to use for model output
- Returns
computed multi-label accuracy
Computes multi-label accuracy for the specified activation and threshold.
- param outputs
NxK tensor that for each of the N examples indicates the probability of the example belonging to each of the K classes, according to the model.
- type outputs
torch.Tensor
- param targets
binary NxK tensort that encodes which of the K classes are associated with the N-th input (eg: a row [0, 1, 0, 1] indicates that the example is associated with classes 2 and 4)
- type targets
torch.Tensor
- param threshold
threshold for for model output
- type threshold
float
- param activation
activation to use for model output
- type activation
str
- returns
computed multi-label accuracy
AUC¶
-
catalyst.utils.metrics.auc.
auc
(outputs: torch.Tensor, targets: torch.Tensor) → torch.Tensor[source]¶ AUC metric.
- Parameters
outputs – [bs; num_classes] estimated scores from a model.
targets – [bs; num_classes] ground truth (correct) target values.
- Returns
Tensor with [num_classes] shape of per-class-aucs
- Return type
torch.Tensor
CMC score¶
Function to count CMC from distance matrix and conformity matrix.
- param distances
distance matrix shape of (n_embeddings_x, n_embeddings_y)
- param conformity_matrix
binary matrix with 1 on same label pos and 0 otherwise
- param topk
number of top examples for cumulative score counting
- returns
cmc score
-
catalyst.utils.metrics.cmc_score.
cmc_score
(query_embeddings: torch.Tensor, gallery_embeddings: torch.Tensor, conformity_matrix: torch.Tensor, topk: int = 1) → float[source]¶ Function to count CMC score from query and gallery embeddings.
- Parameters
query_embeddings – tensor shape of (n_embeddings, embedding_dim) embeddings of the objects in querry
gallery_embeddings – tensor shape of (n_embeddings, embedding_dim) embeddings of the objects in gallery
conformity_matrix – binary matrix with 1 on same label pos and 0 otherwise
topk – number of top examples for cumulative score counting
- Returns
cmc score
-
catalyst.utils.metrics.cmc_score.
cmc_score_count
(distances: torch.Tensor, conformity_matrix: torch.Tensor, topk: int = 1) → float[source]¶ Function to count CMC from distance matrix and conformity matrix.
- Parameters
distances – distance matrix shape of (n_embeddings_x, n_embeddings_y)
conformity_matrix – binary matrix with 1 on same label pos and 0 otherwise
topk – number of top examples for cumulative score counting
- Returns
cmc score
Dice¶
Dice metric.
-
catalyst.utils.metrics.dice.
dice
(outputs: torch.Tensor, targets: torch.Tensor, eps: float = 1e-07, threshold: float = None, activation: str = 'Sigmoid')[source]¶ Computes the dice metric.
- Parameters
outputs (list) – a list of predicted elements
targets (list) – a list of elements that are to be predicted
eps (float) – epsilon
threshold (float) – threshold for outputs binarization
activation (str) – An torch.nn activation applied to the outputs. Must be one of [“none”, “Sigmoid”, “Softmax2d”]
- Returns
Dice score
- Return type
float
-
catalyst.utils.metrics.dice.
calculate_dice
(true_positives: numpy.array, false_positives: numpy.array, false_negatives: numpy.array) → numpy.array[source]¶ Calculate list of Dice coefficients.
- Parameters
true_positives – true positives numpy tensor
false_positives – false positives numpy tensor
false_negatives – false negatives numpy tensor
- Returns
dice score
- Return type
np.array
- Raises
ValueError – if dice is out of [0; 1] bounds
F1 score¶
F1 score.
-
catalyst.utils.metrics.f1_score.
f1_score
(outputs: torch.Tensor, targets: torch.Tensor, beta: float = 1.0, eps: float = 1e-07, threshold: float = None, activation: str = 'Sigmoid')[source]¶ - Parameters
outputs (torch.Tensor) – A list of predicted elements
targets (torch.Tensor) – A list of elements that are to be predicted
eps (float) – epsilon to avoid zero division
beta (float) – beta param for f_score
threshold (float) – threshold for outputs binarization
activation (str) – An torch.nn activation applied to the outputs. Must be one of [“none”, “Sigmoid”, “Softmax2d”]
- Returns
F_1 score
- Return type
float
Focal¶
- Focal losses:
-
catalyst.utils.metrics.focal.
sigmoid_focal_loss
(outputs: torch.Tensor, targets: torch.Tensor, gamma: float = 2.0, alpha: float = 0.25, reduction: str = 'mean')[source]¶ Compute binary focal loss between target and output logits.
- Parameters
outputs – tensor of arbitrary shape
targets – tensor of the same shape as input
gamma – gamma for focal loss
alpha – alpha for focal loss
reduction (string, optional) – specifies the reduction to apply to the output:
"none"
|"mean"
|"sum"
|"batchwise_mean"
."none"
: no reduction will be applied,"mean"
: the sum of the output will be divided by the number of elements in the output,"sum"
: the output will be summed.
- Returns
computed loss
-
catalyst.utils.metrics.focal.
reduced_focal_loss
(outputs: torch.Tensor, targets: torch.Tensor, threshold: float = 0.5, gamma: float = 2.0, reduction='mean') → torch.Tensor[source]¶ Compute reduced focal loss between target and output logits.
It has been proposed in Reduced Focal Loss: 1st Place Solution to xView object detection in Satellite Imagery paper.
Note
size_average
andreduce
params are in the process of being deprecated, and in the meantime, specifying either of those two args will overridereduction
.Source: https://github.com/BloodAxe/pytorch-toolbelt
- Parameters
outputs – tensor of arbitrary shape
targets – tensor of the same shape as input
threshold – threshold for focal reduction
gamma – gamma for focal reduction
reduction (string, optional) – specifies the reduction to apply to the output:
"none"
|"mean"
|"sum"
|"batchwise_mean"
."none"
: no reduction will be applied,"mean"
: the sum of the output will be divided by the number of elements in the output,"sum"
: the output will be summed."batchwise_mean"
computes mean loss per sample in batch. Default: “mean”
- Returns: # noqa: DAR201
torch.Tensor: computed loss
IoU¶
IoU metric. Jaccard metric refers to IoU here, same functionality.
-
catalyst.utils.metrics.iou.
iou
(outputs: torch.Tensor, targets: torch.Tensor, classes: List[str] = None, eps: float = 1e-07, threshold: float = None, activation: str = 'Sigmoid') → torch.Tensor[source]¶ - Parameters
outputs (torch.Tensor) – A list of predicted elements
targets (torch.Tensor) – A list of elements that are to be predicted
classes (List[str]) – if classes are specified we reduce across all dims except channels
eps (float) – epsilon to avoid zero division
threshold (float) – threshold for outputs binarization
activation (str) – An torch.nn activation applied to the outputs. Must be one of [“none”, “Sigmoid”, “Softmax2d”]
- Returns
IoU (Jaccard) score(s)
- Return type
Union[float, List[float]]
-
catalyst.utils.metrics.iou.
jaccard
(outputs: torch.Tensor, targets: torch.Tensor, classes: List[str] = None, eps: float = 1e-07, threshold: float = None, activation: str = 'Sigmoid') → torch.Tensor¶ - Parameters
outputs (torch.Tensor) – A list of predicted elements
targets (torch.Tensor) – A list of elements that are to be predicted
classes (List[str]) – if classes are specified we reduce across all dims except channels
eps (float) – epsilon to avoid zero division
threshold (float) – threshold for outputs binarization
activation (str) – An torch.nn activation applied to the outputs. Must be one of [“none”, “Sigmoid”, “Softmax2d”]
- Returns
IoU (Jaccard) score(s)
- Return type
Union[float, List[float]]
Precision¶
Computes the average precision.
- param outputs
NxK tensor that for each of the N examples indicates the probability of the example belonging to each of the K classes, according to the model.
- type outputs
torch.Tensor
- param targets
binary NxK tensort that encodes which of the K classes are associated with the N-th input (eg: a row [0, 1, 0, 1] indicates that the example is associated with classes 2 and 4)
- type targets
torch.Tensor
- param weights
importance for each sample
- type weights
torch.Tensor
- returns
tensor of [K; ] shape, with average precision for K classes
- rtype
torch.Tensor
Functional¶
-
catalyst.utils.metrics.functional.
get_default_topk_args
(num_classes: int) → Sequence[int][source]¶ Calculate list params for
Accuracy@k
andmAP@k
.Examples
>>> get_default_topk_args(num_classes=4) >>> [1, 3] >>> get_default_topk_args(num_classes=8) >>> [1, 3, 5]
- Parameters
num_classes (int) – number of classes
- Returns
array of accuracy arguments
- Return type
iterable
-
catalyst.utils.metrics.functional.
preprocess_multi_label_metrics
(outputs: torch.Tensor, targets: torch.Tensor, weights: Optional[torch.Tensor] = None) → Tuple[torch.Tensor, torch.Tensor, torch.Tensor][source]¶ General preprocessing and check for multi-label-based metrics.
- Parameters
outputs (torch.Tensor) – NxK tensor that for each of the N examples indicates the probability of the example belonging to each of the K classes, according to the model.
targets (torch.Tensor) – binary NxK tensor that encodes which of the K classes are associated with the N-th input (eg: a row [0, 1, 0, 1] indicates that the example is associated with classes 2 and 4)
weights (torch.Tensor) – importance for each sample
- Returns
processed
outputs
andtargets
with [batch_size; num_classes] shape
-
catalyst.utils.metrics.functional.
wrap_class_metric2dict
(metric_fn: Callable, class_args: Sequence[str] = None) → Callable[source]¶ # noqa: D202 Logging wrapper for metrics with torch.Tensor output and [num_classes] shape. Computes the metric and sync each element from the output Tensor with passed class argument.
- Parameters
metric_fn (Callable) – metric function to compute
class_args (Sequence[str]) – class names for logging. default: None - class indexes will be used.
- Returns
wrapped metric function with List[Dict] output
- Return type
(Callable)
-
catalyst.utils.metrics.functional.
wrap_topk_metric2dict
(metric_fn: Callable, topk_args: Sequence[int]) → Callable[source]¶ Logging wrapper for metrics with Sequence[Union[torch.Tensor, int, float, Dict]] output. Computes the metric and sync each element from the output sequence with passed topk argument.
- Parameters
metric_fn (Callable) – metric function to compute
topk_args (Sequence[int]) – topk args to sync outputs with
- Returns
wrapped metric function with List[Dict] output
- Return type
(Callable)
- Raises
NotImplementedError – if metrics returned values are out of torch.Tensor, int, float, Dict union.