mr2.operators.ConvolutionOp
- class mr2.operators.ConvolutionOp[source]
Bases:
LinearOperatorApply an N-dimensional convolution or cross-correlation filter bank.
For input channels \(x_1,\ldots,x_I\) and convolutional filters \(k_{oi}\), the operator is defined by
\[Cx = \left[\sum_{i=1}^I k_{1i}\ast x_i,\ldots, \sum_{i=1}^I k_{Oi}\ast x_i\right]^T.\]Here \(\ast\) denotes PyTorch-style cross-correlation by default, or mathematical convolution when
correlation=False. One input channel gives an analysis filter bank; one output channel sums filtered coefficient channels as a synthesis filter bank. Both operations are complex-linear in the input:\[k\ast x = \operatorname{Re}(k)\ast\operatorname{Re}(x) -\operatorname{Im}(k)\ast\operatorname{Im}(x) +\mathrm{i}\big(\operatorname{Re}(k)\ast\operatorname{Im}(x) +\operatorname{Im}(k)\ast\operatorname{Re}(x)\big).\]Thus, a real filter acts separately on the real and imaginary parts. The forward operation does not conjugate the kernel.
More precisely, let \(s\) be the spatial kernel size, \(p=(s-1)/2\), and \(E\) extend each input channel by \(p\) samples on both sides according to
pad_mode, retaining the original spatial coordinates. Define the kernel offset\[\begin{split}d(m) = \begin{cases} m-p & \text{if correlation=True}, \\ p-m & \text{if correlation=False}. \end{cases}\end{split}\]For output indices \(n\in\Omega\),
\[(Cx)_o[n] = \sum_{i=1}^I\sum_{0\le m<s} k_{oi}[m]\,(Ex_i)[n+d(m)].\]Indices and inequalities are componentwise over the axes in
dim; other nonchannel axes are batch axes. Spatial sizes are unchanged.Writing \(\widetilde y\) for \(y\) extended by zero outside \(\Omega\), the Hermitian adjoint is
\[\begin{split}(C^H y)_i &= E^H z_i, \\ z_i[t] &= \sum_{o=1}^O\sum_{0\le m<s} \overline{k_{oi}[m]}\,\widetilde y_o[t-d(m)].\end{split}\]Here \(t\) ranges over the extended input domain. \(E^H\) crops constant zero padding or sums contributions from repeated boundary samples for circular, reflect, and replicate padding. The adjoint is generally not an inverse.
- __init__(kernel: Tensor, *, dim: Sequence[int] | None = None, channel_dim: int = 0, correlation: bool = True, pad_mode: Literal['constant', 'reflect', 'replicate', 'circular'] = 'circular') None[source]
Initialize the convolution operator.
- Parameters:
kernel (
Tensor) – Filter bank of shape(out_channels, in_channels, *kernel_size). Channel sizes must be positive; spatial sizes must be positive and odd.dim (
Sequence[int] |None, default:None) – Spatial axes, in the order of the kernel’s spatial dimensions. Defaults to the lastkernel.ndim - 2axes.channel_dim (
int, default:0) – Input and output channel axis. Defaults to the leading axis. Must be distinct from the spatial axes.correlation (
bool, default:True) – Use PyTorch-style cross-correlation (default). Set toFalsefor mathematical convolution, reversing the spatial kernel axes.pad_mode (
Literal['constant','reflect','replicate','circular'], default:'circular') – Boundary padding.'constant'pads with zeros; the other modes use reflected, repeated-edge, or periodic values.
- property kernel: Tensor[source]
Convolution kernel in output-channel, input-channel, spatial order.
- property H: LinearOperator[source]
Adjoint operator.
Obtains the adjoint of an instance of this operator as an
AdjointLinearOperator, which itself is a anLinearOperatorthat can be applied to tensors.Note:
linear_operator.H.H == linear_operator
- property gram: LinearOperator[source]
Gram operator.
For a LinearOperator \(A\), the self-adjoint Gram operator is defined as \(A^H A\).
Note
This is the inherited default implementation.
- __call__(x: Tensor | HasTensorData) tuple[Tensor][source]
Apply the filter bank.
- Parameters:
x (
Tensor|HasTensorData) – Tensor withkernel.shape[1]channels alongchannel_dim.- Returns:
Single tensor with
kernel.shape[0]output channels, retaining the spatial sizes and axis layout of the input.
- adjoint(x: Tensor) tuple[Tensor][source]
Apply the Hermitian adjoint defined in the class docstring.
- forward(x: Tensor) tuple[Tensor][source]
Apply forward of ConvolutionOp.
Note
Prefer calling the operator as
operator(x)over directly calling this method. See this PyTorch discussion.
- operator_norm(initial_value: Tensor, dim: Sequence[int] | None, max_iterations: int = 20, relative_tolerance: float = 1e-4, absolute_tolerance: float = 1e-5, callback: Callable[[Tensor], None] | None = None) Tensor[source]
Power iteration for computing the operator norm of the operator.
- Parameters:
initial_value (
Tensor) – initial value to start the iteration; must be element of the domain. if the initial value contains a zero-vector for one of the considered problems, the function raises aValueError.The dimensions of the tensors on which the operator operates. The choice of
dimdetermines how the operator norm is interpreted. For example, for a matrix-vector multiplication with a batched matrix tensor of shape(batch1, batch2, row, column)and a batched input tensor of shape(batch1, batch2, row):If
dim=None, the operator is considered as a block diagonal matrix with batch1*batch2 blocks and the result is a tensor containing a single norm value (shape(1, 1, 1)).If
dim=(-1),batch1*batch2matrices are considered, and for each a separate operator norm is computed.If
dim=(-2,-1),batch1matrices withbatch2blocks are considered, and for each matrix a separate operator norm is computed.
Thus,
dimdetermines the domain of the operator.max_iterations (
int, default:20) – maximum number of iterationsrelative_tolerance (
float, default:1e-4) – absolute tolerance for the change of the operator-norm at each iteration; if set to zero, the maximal number of iterations is the only stopping criterion used to stop the power iteration.absolute_tolerance (
float, default:1e-5) – absolute tolerance for the change of the operator-norm at each iteration; if set to zero, the maximal number of iterations is the only stopping criterion used to stop the power iteration.callback (
Callable[[Tensor],None] |None, default:None) – user-provided function to be called at each iteration
- Returns:
An estimate of the operator norm. Shape corresponds to the shape of the input tensor
initial_valuewith the dimensions specified indimreduced to a single value. The pointwise multiplication ofinitial_valuewith the result of the operator norm will always be well-defined.
- __add__(other: LinearOperator | Tensor | complex) LinearOperator[source]
- __add__(other: LinearOperatorMatrix) LinearOperatorMatrix
- __add__(other: Operator[Tensor, tuple[Tensor]]) Operator[Tensor, tuple[Tensor]]
Operator addition.
Returns
lambda x: self(x) + other(x)if other is a operator,lambda x: self(x) + otherif other is a tensor. For aLinearOperatorMatrix, the operator is added to the diagonal blocks, seemr2.operators.LinearOperatorMatrix.__radd__.
- __matmul__(other: LinearOperator) LinearOperator[source]
- __matmul__(other: LinearOperatorMatrix) LinearOperatorMatrix
- __matmul__(other: Operator[Unpack[Tin2], tuple[Tensor]] | Operator[Unpack[Tin2], tuple[Tensor, ...]]) Operator[Unpack[Tin2], tuple[Tensor]]
Operator composition.
Returns
lambda x: self(other(x)). For aLinearOperatorMatrix, the operator is applied to each output of the matrix, seemr2.operators.LinearOperatorMatrix.__rmatmul__.
- __mul__(other: Tensor | complex) LinearOperator[source]
Operator elementwise left multiplication with tensor/scalar.
Returns
lambda x: self(x*other)
- __or__(other: LinearOperator) LinearOperatorMatrix[source]
Horizontal stacking of two LinearOperators.
A|Bis aLinearOperatorMatrixwith two columns, with(A|B)(x1,x2) == A(x1) + B(x2). Seemr2.operators.LinearOperatorMatrixfor more information.
- __radd__(other: Tensor | complex) LinearOperator[source]
Operator addition.
Returns
lambda x: self(x) + other*x
- __rmul__(other: Tensor | complex) LinearOperator[source]
Operator elementwise right multiplication with tensor/scalar.
Returns
lambda x: other*self(x)