utils
Script for utility functions needed by the LVAE model.
Interpolate
#
Bases: Module
Wrapper for torch.nn.functional.interpolate.
Source code in src/careamics/models/lvae/utils.py
StableExponential
#
Class that redefines the definition of exp() to increase numerical stability. Naturally, also the definition of log() must change accordingly. However, it is worth noting that the two operations remain one the inverse of the other, meaning that x = log(exp(x)) and x = exp(log(x)) are always true.
Definition: exp(x) = { exp(x) if x<=0 x+1 if x>0 }
log(x) = {
x if x<=0
log(1+x) if x>0
}
NOTE 1: Within the class everything is done on the tensor given as input to the constructor. Therefore, when exp() is called, self._tensor.exp() is computed. When log() is called, torch.log(self._tensor.exp()) is computed instead.
NOTE 2: Given the output from exp(), torch.log() or the log() method of the class give identical results.
Source code in src/careamics/models/lvae/utils.py
StableLogVar
#
Class that provides a numerically stable implementation of Log-Variance. Specifically, it uses the exp() and log() formulas defined in StableExponential
class.
Source code in src/careamics/models/lvae/utils.py
is_3D
property
#
Check if the _lv tensor is 3D.
Recall that, in this framework, tensors have shape (B, C, [Z], Y, X).
__init__(logvar, enable_stable=True, var_eps=1e-06)
#
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logvar | Tensor | The input (true) logvar vector, to be converted in the Stable version. | required |
enable_stable | bool | Whether to compute the stable version of log-variance. Default is | True |
var_eps | float | The minimum value attainable by the variance. Default is | 1e-06 |
Source code in src/careamics/models/lvae/utils.py
centercrop_to_size(size)
#
Centercrop the log-variance tensor to the desired size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size | Sequence[int] | The desired size of the log-variance tensor. | required |
Source code in src/careamics/models/lvae/utils.py
get_var()
#
StableMean
#
Source code in src/careamics/models/lvae/utils.py
is_3D
property
#
Check if the _mean tensor is 3D.
Recall that, in this framework, tensors have shape (B, C, [Z], Y, X).
centercrop_to_size(size)
#
Centercrop the mean tensor to the desired size.
Implemented only in the case of 2D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size | Sequence[int] | The desired size of the log-variance tensor. | required |
Source code in src/careamics/models/lvae/utils.py
allow_numpy(func)
#
All optional arguments are passed as is. positional arguments are checked. if they are numpy array, they are converted to torch Tensor.
Source code in src/careamics/models/lvae/utils.py
crop_img_tensor(x, size)
#
Crops a tensor. Crops a tensor of shape (batch, channels, h, w) to a desired height and width given by a tuple. Args: x (torch.Tensor): Input image size (list or tuple): Desired size (height, width)
Returns:
Type | Description |
---|---|
The cropped tensor | |
Source code in src/careamics/models/lvae/utils.py
kl_normal_mc(z, p_mulv, q_mulv)
#
One-sample estimation of element-wise KL between two diagonal multivariate normal distributions. Any number of dimensions, broadcasting supported (be careful). :param z: :param p_mulv: :param q_mulv: :return:
Source code in src/careamics/models/lvae/utils.py
pad_img_tensor(x, size)
#
Pads a tensor
Pads a tensor of shape (B, C, [Z], Y, X) to desired spatial dimensions.
Parameters:
x (torch.Tensor): Input image of shape (B, C, [Z], Y, X)
size (list or tuple): Desired size ([Z*], Y*, X*)
Returns:
The padded tensor