Utils
Script for utility functions needed by the LVAE model.
Interpolate
Bases: Module
Wrapper for torch.nn.functional.interpolate.
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.
StableLogVar
Class that provides a numerically stable implementation of Log-Variance.
Specifically, it uses the exp() and log() formulas defined in StableExponential class.
is_3D
property
Check if the _lv tensor is 3D.
Recall that, in this framework, tensors have shape (B, C, [Z], Y, X).
centercrop_to_size(size)
get_var()
Get Variance from Log-Variance.
StableMean
is_3D
property
Check if the _mean tensor is 3D.
Recall that, in this framework, tensors have shape (B, C, [Z], Y, X).
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.
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
|
|
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:
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