pysdic.build_chains_transformation_term#
- build_chains_transformation_term(seq_transformations, seq_chains, seq_inputs, seq_outputs, seq_parameters=None, *, seq_kwargs=None, mode='rJ', weight=1.0, loss=None, loss_scale=1.0, filter_nans=False, sparse=False, use_cache=False)[source]#
Build a
pysolvegn.Termto optimize the parameters of several transformations combined in chains.Lets \((T_0, T_1, ..., T_{N_T-1})\) be a sequence of transformations and \((C_0, C_1, ..., C_{N_C-1})\) a sequence of chains of indices of these transformations (a transformation can be shared by several chains). The term minimizes the distance between the transformed input points and the output points of all the chains:
\[\min_{\vec{p}} \frac{1}{2} \sum_c \sum_j \rho\left( \| C_c(X_{c,j}; \vec{p}) - Y_{c,j} \|^2 \right)\]where \(\vec{p}\) is the concatenation of the parameters of all the transformations, in the order of
seq_transformations, and \(\rho\) is the loss function.All the chains are gathered in a single term with the same loss and weight. To give different losses or weights to some chains, call this function once per group of chains with the same
seq_transformations(so the parameter vectors are identical) and give the list of terms topysolvegn.solve.The term can be built in two modes:
"rJ": residuals and Jacobian of all the equations. Robust losses ("soft_l1","cauchy","huber", …) are available, with a soft thresholdloss_scalein the unit of the output points."gH": gradient \(J^T \vec{r}\) and Hessian \(J^T J\) assembled chain by chain. The full Jacobian is never stored, which saves a lot of memory for many points, but only the"linear"loss is available.
Note
The term works on copies of the transformations: the given transformations are never modified. To optimize only some parameters of a transformation, give a
pysdic.ReparametrizedTransformationor use theparametrizationargument ofpysolvegn.solve.See also
Function
pysdic.build_transformation_term()for a single transformation.Function
pysdic.optimize_chains_transformation_parameters()to directly solve the problem.
- Parameters:
seq_transformations (Sequence[Transformation]) – The transformations \((T_0, T_1, ..., T_{N_T-1})\) whose parameters are optimized. Each transformation must appear only once and must not be locked.
seq_chains (Sequence[Sequence[Integral]]) – The chains, each chain being a non-empty sequence of indices of
seq_transformations(without duplicates), in the order of application. For example(1, 4, 8)is \(T_8 \circ T_4 \circ T_1\).seq_inputs (Sequence[ArrayLike]) – The input points \(X_c\) of each chain with shape \((\dots, n_i)\).
seq_outputs (Sequence[ArrayLike]) – The output points \(Y_c\) of each chain with shape \((\dots, n_o)\).
seq_parameters (Optional[Sequence[Optional[ArrayLike]]], optional (default:
None)) – The initial parameters of each transformation.Nonemeans the current parameters.seq_kwargs (Optional[Sequence[Optional[Dict[str, Any]]]], optional (default:
None)) – Additional keyword arguments passed to theapplymethod of each transformation: one dictionary (or None) per transformation ofseq_transformations. They are the same for all the chains using the transformation.mode (str, optional (default:
"rJ")) – The mode of the term,"rJ"or"gH".weight (Real, optional (default:
1.0)) – The weight of the term.loss (Optional[Union[str, Callable]], optional (default:
None)) – The loss function of the term (seepysolvegn.Term), only for the mode"rJ".Nonemeans"linear".loss_scale (Real, optional (default:
1.0)) – The soft threshold \(C\) of the loss function in the unit of the output points (for example in pixels): the residuals with \(|r| \lesssim C\) are treated as inliers and the larger ones as outliers. Only for the mode"rJ", no effect on the"linear"loss.filter_nans (bool, optional (default:
False)) – If True, the equations with non-finite residuals or Jacobians are set to zero.sparse (bool, optional (default:
False)) – If True, the Jacobian ("rJ") or the Hessian ("gH") is ascipy.sparse.csr_matrix.use_cache (bool, optional (default:
False)) –The evaluation strategy of the functions of the term:
False: each function computes only what it returns and nothing is kept in memory. The residuals ("rJ") and the cost ("gH") are computed without the Jacobian, which is efficient when many evaluated points are trial points whose Jacobian is never required (damped steps of Levenberg-Marquardt). In mode"gH", the gradient and the Hessian each compute the Jacobian.True: the first call for given parameters computes all the operators of the mode (residuals and Jacobian in"rJ", gradient, Hessian and cost in"gH") and keeps them in cache, so the other calls for the same parameters are free. It is efficient when the Jacobian is required at every evaluated point (Gauss-Newton without damping).
Note
With
filter_nans=True, the residuals and the cost computed without the Jacobian only filter the equations with non-finite residuals, so they can differ from those computed with the Jacobian (equations with finite residuals but non-finite Jacobians).
- Returns:
pysolvegn.Term – The term to give to
pysolvegn.solve.numpy.ndarray – The initial parameters \(\vec{p}\) with shape \((N_p,)\), to use as
p0.
- Return type:
Examples
Calibrate one camera observing a target in two positions. The camera parameters (
intrinsic,distortion) are shared by the two chains:seq_transformations = [extrinsic_0, extrinsic_1, distortion, intrinsic] seq_chains = [(0, 2, 3), (1, 2, 3)] term, p0 = build_chains_transformation_term( seq_transformations, seq_chains, [world_0, world_1], [image_0, image_1], loss="soft_l1", loss_scale=1.0, # outliers above about 1 pixel ) result = pysolvegn.solve(term, p0, max_iteration=50, xtol=1e-10, damping="lm-diag")