pycvcam.optimize_parameters_lm#
- optimize_parameters_lm(transform, input_points, output_points, *, guess=None, mask=None, transform_kwargs=None, max_iterations=None, ftol=None, xtol=None, gtol=None, auto=False, loss=None, filter_nans=False, verbose_level=0, return_result=False, inplace=False)[source]#
Optimize the
parametersof aTransformobject such that the transformed input points match the output points using thescipy.optimize.least_squaresmethod. The computation is done with the Levenberg-Marquardt algorithm.Lets consider a set of input points \(\vec{X}_I\) with shape (…, input_dim) and a set of output points \(\vec{X}_O\) with shape (…, output_dim). We search \(\lambda\) such that:
\[\vec{X}_O = \text{Transform}(\vec{X}_I, \lambda) = T(\vec{X}_I, \lambda)\]Note
The current parameters of the transformation are not directly modified.
We define the residual vector as:
\[R(\lambda) = \vec{X}_O - T(\vec{X}_I, \lambda)\]And the jacobian matrix as:
\[J(\lambda) = \frac{\partial R(\lambda)}{\partial \lambda} = -\frac{\partial T(\vec{X}_I, \lambda)}{\partial \lambda}\]Then the optimization problem is solve by minimizing the loss function using the Levenberg-Marquardt algorithm of scipy.optimize.least_squares.
Important
At least one of the stopping criteria (
ftol,xtol, orgtol) must be specified for the optimization to stop. You can also setautoto True to use1e-8for all stopping criteria.- Parameters:
transform (
Transform) – The transformation object to be optimized. Theconstantsattribute of the transformation must be set before calling this function. If theparametersattribute of the transformation is set, it will be used as the initial guess for the optimization if the guess parameter is None. Note that the inputTransformobject is not modified during the optimization process, a copy of the object is created and modified internally to perform the optimization if inplace is False.input_points (ArrayLike) – The input points with shape (…, input_dim) such that their transformation is expected to match the output points.
output_points (ArrayLike) – The output points to be matched with shape (…, output_dim).
guess (Optional[ArrayLike], optional) – The initial guess for the parameters of the transformation with shape (n_params,). If None, the current parameters of the transformation are used.
mask (Optional[ArrayLike], optional) – A mask array of shape (n_params,) indicating which parameters should be optimized. Elements with a value of True are optimized, while elements with a value of False are kept fixed. Default is None, which means all parameters are optimized.
transform_kwargs (Optional[Dict], optional) – Additional keyword arguments for the
transform._transformmethod. Default is None, which means no additional keyword arguments are passed to the transformation.max_iterations (Optional[Integral], optional) – Stop criterion by the number of iterations. The optimization process is stopped when the number of iterations exceeds
max_iterations. Default is None, which means no limit on the number of iterations.ftol (Optional[Real], optional) – Stop criterion by the change of the cost function. The optimization process is stopped when
dF < ftol * F. The default value is None, which means the ftol criterion is not used for stopping the optimization.xtol (Optional[Real], optional) – Stop criterion by the change of the parameters. The optimization process is stopped when
norm(dx) < xtol * (xtol + norm(x)). The default value is None, which means the xtol criterion is not used for stopping the optimization.gtol (Optional[Real], optional) – Stop criterion by the norm of the gradient. The optimization process is stopped when
norm(g_scaled, ord=numpy.inf) < gtolwhere g_scaled is the value of the gradient scaled to account for the presence of the bounds. The default value is None, which means the gtol criterion is not used for stopping the optimization.auto (bool, optional) – If True, the stopping criteria (
ftol,xtol, andgtol) are all set to1e-8. If any of the stopping criteria is already specified, it will be overridden by the user-specified value.loss (Optional[str], optional) – If specified, the optimization will use a robust loss function to reduce the influence of outliers in the optimization. The available loss functions are ‘huber’, ‘cauchy’, ‘arctan’, ‘soft_l1’, and ‘linear’. Default is None, which means the standard least squares loss is used (i.e., ‘linear’).
filter_nans (bool, optional) –
If True, NaN values in the residuals are filtered out before computing the cost and the Jacobian. This can help improve the robustness of the optimization in the presence of outliers or invalid data. Default is False.
Warning
The optimization can try to expulse all points as outliers to reduce the cost to zero, which can lead to a failure of the optimization. Use with caution.
verbose_level (int, optional) – Level of algorithm’s verbosity: - 0 (default) : work silently. - 1 : display a termination report. - 2 : display progress during iterations. - 3 : display initial jacobian analysis and progress during iterations.
return_result (bool, optional) – If True, the function returns the
scipy.optimize.OptimizeResultobject containing information about the convergence of the optimization process. Default is False, which means only the optimized parameters are returned. Ifn_paramsis 0, or all parameters are masked, the result output will be None.inplace (bool, optional) – If True, the optimization is performed in-place, modifying the parameters of the input transformation. If False (default), a copy of the transformation is created and modified internally to perform the optimization, leaving the input transformation unchanged.
- Returns:
parameters (numpy.ndarray) – The optimized parameters of the transformation with shape (n_params,). This array contains both the optimized parameters (corresponding to True values in the mask) and the fixed parameters (corresponding to False values in the mask), where the fixed parameters are equal to their initial values.
result (scipy.optimize.OptimizeResult, optional) – The result of the optimization process containing information about the convergence of the optimization. Returned only if return_result is True.
Warning
Only contains the parameters that were optimized (i.e., the parameters corresponding to True values in the mask).
- Return type:
See also
scipy.optimize.least_squaresFor more information about the optimization method.
pycvcam.optimize.optimize_parameters_gnOptimize the parameters of a transformation using the Gauss-Newton method.
pycvcam.optimize.optimize_camera_lmOptimize the parameters of a camera transformation using the least squares method with the Levenberg-Marquardt algorithm.
pycvcam.optimize.optimize_chains_lmOptimize the parameters of a set of transformations organized in chains using the least squares method with the Levenberg-Marquardt algorithm.