tomosipo.torch_support module
This module provides functions to interoperate with torch
- class tomosipo.torch_support.AutogradOperator(operator, num_extra_dims=0, is_2d=False)[source]
Bases:
objectA linear tomographic projection operator with torch autograd support
This class has almost the same interface as a normal tomosipo operator, but it supports pytorch autograd, allowing for automatic differentiation. This can be used for example on the reconstruction algorithms in ts_algorithms. Additive operators, numpy inputs and outputs, and transformations are not supported on AutogradOperators.
- property T
The transpose operator
This property returns the transpose (backprojection) operator.
- property astra_compat_pg
- property astra_compat_vg
- property domain
The domain (volume geometry) of the operator
- property domain_shape
The expected shape of the input (volume) data
- property range
The range (projection geometry) of the operator
- property range_shape
The expected shape of the output (projection) data
- transpose()[source]
Return backprojection operator
- class tomosipo.torch_support.Backward(*args, **kwargs)[source]
Bases:
Function- static backward(ctx, grad_output)[source]
- static forward(ctx, input, vg, pg, projector)[source]
- class tomosipo.torch_support.Forward(*args, **kwargs)[source]
Bases:
Function- static backward(ctx, grad_output)[source]
- static forward(ctx, input, vg, pg, projector)[source]
- class tomosipo.torch_support.OperatorFunction(*args, **kwargs)[source]
Bases:
Function- static backward(ctx, grad_output)[source]
- static forward(ctx, input, operator, num_extra_dims=0, is_2d=False)[source]
- tomosipo.torch_support.autograd_operator(volume_geometry, projection_geometry, voxel_supersampling=1, detector_supersampling=1, num_extra_dims=0, is_2d=False)[source]
Create a tomographic operator with PyTorch autograd support.
- Parameters:
volume_geometry (VolumeGeometry) – The domain of the operator.
projection_geometry (ProjectionGeometry) – The range of the operator.
voxel_supersampling (int (optional)) – Specifies the amount of voxel supersampling, i.e., how many (one dimension) subvoxels are generated from a single parent voxel. The default is 1.
detector_supersampling (int (optional)) – Specifies the amount of detector supersampling, i.e., how many rays are cast per detector. The default is 1.
num_extra_dims (int (optional)) – Number of extra dimensions to prepend to the input and output of this operator. This can be useful to add channel and batch dimensions when training neural networks. The default is 0.
is_2d (bool (optional)) – Whether to remove the first dimension of the operator, resulting in a 2D operator. The default is False.
- Returns:
A tomographic operator with PyTorch autograd support.
- Return type:
AutogradOperator
- tomosipo.torch_support.backward(input, vg, pg, projector=None)[source]
- tomosipo.torch_support.forward(input, vg, pg, projector=None)[source]
- tomosipo.torch_support.to_autograd(operator, num_extra_dims=0, is_2d=False)[source]
Converts an operator to an autograd function
Example:
>>> import tomosipo as ts >>> vg = ts.volume(shape=10) >>> pg = ts.parallel(angles=10, shape=10) >>> A = ts.operator(vg, pg)
>>> f = to_autograd(A) >>> vd = torch.randn(*A.domain_shape, requires_grad=True) >>> f(vd).sum().backward() >>> vd.grad is not None True
Likewise, you may use the transpose:
>>> g = to_autograd(A.T) >>> pd = torch.randn(*A.T.domain_shape, requires_grad=True) >>> g(pd).sum().backward() >>> vd.grad is not None True
- Parameters:
operator (Operator) – Tomosipo operator whose behavior will be mimicked
num_extra_dims (int (optional)) – Number of extra dimensions to prepend to the input and output of this operator. Set this to 2 to add channel and batch dimensions when taining neural networks. The default is 0.
is_2d (bool (optional)) – Whether to remove the first dimension of the operator, resulting in a 2D operator. The default is False.
- Returns:
An autograd enabled function
- Return type:
Function