Regularizers
CPU
DeconvOptim.TV — Function
TV(; <keyword arguments>)This function returns a function to calculate the Total Variation regularizer of a n-dimensional array.
Arguments
num_dims=nothing: Dimension of the array that should be regularized. Whennothing, it is inferred from the array upon use.sum_dims=nothing: A array containing the dimensions we want to sum over. Defaults to all dimensions (1:N).weights=nothing: A array containing weights to weight the contribution of different dimensions. Ifweights=nothingall dimensions are weighted equally.step=1: A integer indicating the step width for the array indexingmode="forward": Either"central"or"forward"accounting for different modes of the spatial gradient. Default is "forward".ϵ=1f-8: A constant which is added to the squared spatial gradient before taking the square root, so that the Total Variation is smoothly differentiable:L = sqrt.(grad² + ϵ). In the limitϵ → 0this reduces to the pure gradient magnitudesqrt.(grad²)(i.e.abs.(grad)), while a largerϵsmoothly interpolates towardgrad-squared (compare withTikhonov(mode="spatial_grad_square")).
Examples
To create a regularizer for a 3D dataset where the third dimension has different contribution. For the derivative we use forward mode.
julia> reg = TV(num_dims=2, sum_dims=[1, 2], weights=[1, 1], mode="forward");
julia> reg([1 2 3; 4 5 6; 7 8 9])
12.649111f0DeconvOptim.Tikhonov — Function
Tikhonov(; <keyword arguments>)This function returns a function to calculate the Tikhonov regularizer of a n-dimensional array.
Arguments
num_dims=nothing: Dimension of the array that should be regularized. Whennothing, it is inferred from the array upon use.sum_dims=nothing: A array containing the dimensions we want to sum over. Defaults to all dimensions (1:N).weights=nothing: A array containing weights to weight the contribution of different dimensions. Ifweights=nothingall dimensions are weighted equally.step=1: A integer indicating the step width for the array indexingmode="laplace": Either"laplace","spatial_grad_square","identity"accounting for different modes of the Tikhonov regularizer. Default is"laplace".
Examples
To create a regularizer for a 3D dataset where the third dimension has different contribution.
julia> reg = Tikhonov(num_dims=2, sum_dims=[1, 2], weights=[1, 1], mode="identity");
julia> reg([1 2 3; 4 5 6; 7 8 9])
285DeconvOptim.GR — Function
GR(; <keyword arguments>)This function returns a function to calculate the Good's roughness regularizer of a n-dimensional array.
Arguments
num_dims=nothing: Dimension of the array that should be regularized. Whennothing, it is inferred from the array upon use.sum_dims=nothing: A array containing the dimensions we want to sum over. Defaults to all dimensions (1:N).weights=nothing: A array containing weights to weight the contribution of different dimensions. Ifweights=nothinga weight of1is assumed for each dimension.step=1: A integer indicating the step width for the array indexingmode="forward": Either"central"or"forward"accounting for different modes of the spatial gradient. Default is "forward".ϵ=1f-8: A constant which is added to the array before evaluating the spatial gradient, so that the Good's roughnesssqrt.(x + ϵ)is smoothly differentiable everywhere. In the limitϵ → 0the expression reduces to the puresqrt.(x), while a largerϵsmooths the transition.
Examples
To create a regularizer for a 3D dataset where the third dimension has different contribution. For the derivative we use forward mode.
julia> reg = GR(num_dims=2, sum_dims=[1, 2], weights=[1, 1], mode="forward");
julia> reg([1 2 3; 4 5 6; 7 8 9])
-26.36561871738898DeconvOptim.TH — Function
TH(; <keyword arguments>)This function returns a function to calculate the Total Hessian norm of a n-dimensional array.
Arguments
num_dims=nothing: Number of spatial dimensions (1, 2 or 3) of the array. Whennothing, it is inferred from the array upon use.sum_dims=nothing: An array containing the dimensions over which the Hessian is computed. Defaults to all dimensions (1:num_dims). Dimensions not listed here only contribute to the summation over the array (the@tullioreduction), but not to the Hessian itself.weights=nothing: An array containing weights to weight the contribution of different dimensions. Ifweights=nothingall dimensions are weighted equally. Theweightsare matched positionally tosum_dims, i.e. the weightweights[k]belongs to the dimensionsum_dims[k]. A diagonal term of dimensionienters withweights[i]^2and the cross term between dimensionsiandjenters with2 * weights[i] * weights[j].ϵ=1f-8: A constant which is added to the squared Hessian entries before taking the square root, so that the Total Hessian norm is smoothly differentiable at zero. In the limitϵ → 0the expression reduces to the pure Hessian normsqrt.(H²), while a largerϵsmooths the transition (compare withTV, whereϵplays the same role).
DeconvOptim.HS — Function
HS(; p=1, sum_dims=nothing, weights=nothing)Hessian Schatten norm. p determines which Schatten norm is used.
The Hessian is evaluated over exactly two array dimensions, which are given by sum_dims (defaulting to (1, 2)). The selected dimensions may be a subset of the array dimensions, so e.g. sum_dims=(1, 2) works for both 2D and 3D (or higher dimensional) arrays – the remaining dimensions only take part in the final summation. Because the two rows and columns of the 2×2 pixel-wise Hessian are inverted in closed form for speed, sum_dims is limited to exactly two dimensions; passing more throws an ArgumentError.
weights[k] pair positionally with sum_dims[k]: the diagonal Hessian entry of dimension d enters with weights[k]^2 and the cross entry between the two dimensions enters with 2 * weights[k] * weights[l].
The pixel-wise Hessian uses centered (symmetric) second-order stencils, so the norm has no preferred direction. The computation is regularized by an internal smoothing constant 1f-8 (see ϵ in TV/TH), so that the norm is differentiable.
Arguments
p=1: The orderpof the Schatten norm.p=1uses a cheap fast path.sum_dims=nothing: The two array dimensions over which the Hessian is evaluated. Defaults to(1, 2). Exactly two dimensions are supported.weights=nothing: Weights for the different dimensions, matched positionally tosum_dims. Ifnothingall dimensions are weighted equally.
All regularizers are constructed via keyword arguments and return a function which takes the array to be regularized and returns a scalar. They support:
sum_dims: the array dimensions over which the regularizer is computed; the remaining dimensions only take part in the summation.weights: per-dimension weights, matched positionally tosum_dims.num_dims: the number of spatial dimensions (inferred from the array upon use whennothing).
HS() computes the Hessian over exactly two dimensions (see its docstring).
CUDA
The regularizers TV(), GR(), TH(), Tikhonov() and HS() automatically use the CUDA variants below when called on a CuArray. All of them accept num_dims=nothing, which is also the default, in which case the number of dimensions is inferred from the array upon use. The CUDA variants support the same sum_dims and weights keyword arguments as their CPU counterparts.
DeconvOptim.TV_cuda — Function
TV_cuda(; num_dims=nothing, sum_dims=nothing, weights=nothing, step=1, mode="forward", ϵ=1f-8)This function returns a function to calculate the Total Variation regularizer of a n-dimensional array.
Arguments
num_dims=nothing: Dimension of the array that should be regularized. Whennothing, it is inferred from the array upon use.sum_dims=nothing: A array containing the dimensions we want consider in the TV calculation. Defaults to all dimensions (1:N).weights=nothing: A array containing weights to weight the contribution of different dimensions. Ifweights=nothingall dimensions are weighted equally.step=1: A integer indicating the step width for the array indexingmode="forward": Either"central"or"forward"accounting for different modes of the spatial gradient. Default is "forward".ϵ=1f-8: A constant which allows to smoothly vary between TV and grad^2 regularization: L = sqrt.(grad^2+ϵ).
julia> using CUDA
julia> reg = TV_cuda(num_dims=2);
julia> reg(CuArray([1 2 3; 4 5 6; 7 8 9]))
12.649111f0DeconvOptim.GR_cuda — Function
GR_cuda(; num_dims=nothing, sum_dims=nothing, weights=nothing, step=1, mode="forward", ϵ=1f-8)This function returns a function to calculate the Good's roughness regularizer of a n-dimensional array on CUDA (or CPU) arrays.
Differentiable with Zygote on CuArrays because it avoids Tullio and only uses view/broadcast/sum operations. The math is identical to GR (see GR for a description of the arguments).
If num_dims is nothing, the number of dimensions is inferred from the array upon use. The default weights then assume 1 for each of the num_dims dimensions.
DeconvOptim.TH_cuda — Function
TH_cuda(; num_dims=nothing, sum_dims=nothing, weights=nothing, ϵ=1f-8)This function returns a function to calculate the Total Hessian norm of a n-dimensional array on CUDA (or CPU) arrays.
Differentiable with Zygote on CuArrays. The math is identical to TH (see TH for the arguments).
When sum_dims is given, the Hessian is only computed over the listed dimensions (the remaining dimensions are summed over); the generic TH_view fallback is used then.
DeconvOptim.Tikhonov_cuda — Function
Tikhonov_cuda(; num_dims=nothing, sum_dims=nothing, weights=nothing, step=1, mode="laplace")This function returns a function to calculate the Tikhonov regularizer of an n-dimensional array on CUDA (or CPU) arrays.
Differentiable with Zygote on CuArrays because it avoids Tullio and only uses view/broadcast/sum operations. The math is identical to Tikhonov (see Tikhonov for a description of the arguments).
DeconvOptim.HS_cuda — Function
HS_cuda(; p=1, sum_dims=nothing, weights=nothing)This function returns a function to calculate the Hessian Schatten norm of an n-dimensional array on CUDA (or CPU) arrays.
Differentiable with Zygote on CuArrays because it avoids Tullio and only uses view/broadcast/sum operations. The math is identical to HS (see HS for a description of the arguments).