16#include <Eigen/Eigenvalues>
24 std::vector<Eigen::Index> result;
26 if (activeParams.test(i)) {
36 Eigen::Ref<
const Eigen::VectorX<T>> fullVec,
37 const std::vector<Eigen::Index>& subsetIndexToFullParamIndex) {
38 const Eigen::Index nSubset = subsetIndexToFullParamIndex.size();
39 Eigen::VectorX<T> result(nSubset);
40 for (Eigen::Index iSubset = 0; iSubset < nSubset; ++iSubset) {
41 result(iSubset) = fullVec(subsetIndexToFullParamIndex[iSubset]);
48 Eigen::Ref<
const Eigen::MatrixX<T>> fullMat,
49 const std::vector<Eigen::Index>& subsetIndexToFullParamIndex) {
50 const Eigen::Index nSubset = subsetIndexToFullParamIndex.size();
51 Eigen::MatrixX<T> result(fullMat.rows(), nSubset);
52 for (Eigen::Index iSubset = 0; iSubset < nSubset; ++iSubset) {
53 result.col(iSubset) = fullMat.col(subsetIndexToFullParamIndex[iSubset]);
60 Eigen::Ref<
const Eigen::VectorX<T>> subsetVec,
61 const std::vector<Eigen::Index>& subsetIndexToFullParamIndex,
62 Eigen::Index fullVecSize) {
63 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(fullVecSize);
64 for (
size_t iSubset = 0; iSubset < subsetIndexToFullParamIndex.size(); ++iSubset) {
65 result[subsetIndexToFullParamIndex[iSubset]] = subsetVec[iSubset];
76 Eigen::Ref<
const Eigen::MatrixX<T>> jacobian,
77 Eigen::Ref<
const Eigen::VectorX<T>> v) {
92 Eigen::JacobiSVD<Eigen::MatrixX<T>> svd(jacobian, Eigen::ComputeThinV);
99 Eigen::VectorX<T> tmp = svd.matrixV().transpose() * v;
100 for (Eigen::Index i = 0; i < tmp.size(); ++i) {
101 const float sSqr =
sqr(svd.singularValues()(i));
108 return 0.5f * svd.matrixV() * tmp;
118 Eigen::Ref<
const Eigen::VectorX<T>> dLoss_dModelParams,
120 std::vector<ErrorFunctionDerivativesT<T>> result(solverFunction.
getErrorFunctions().size());
122 const std::vector<Eigen::Index> subsetIndexToFullParamIndex =
126 Eigen::MatrixX<T> jacobian_full;
127 Eigen::VectorX<T> residual_full;
167 size_t actualRows = 0;
168 solverFunction.
getJacobian(modelParameters.
v, jacobian_full, residual_full, actualRows);
170 const Eigen::MatrixX<T> jacobian_subset =
172 const Eigen::VectorX<T> gradient_subset = 2.0f * jacobian_subset.transpose() * residual_full;
173 if (gradientRmse !=
nullptr) {
174 *gradientRmse = std::sqrt(gradient_subset.squaredNorm() / gradient_subset.size());
181 Jinv_times_dLoss_dModelParams_subset, subsetIndexToFullParamIndex, nFullParams);
183 MT_LOGD(
"Jinv_times_dLoss_dModelParams = {}", Jinv_times_dLoss_dModelParams_full.transpose());
193 for (
size_t iErr = 0; iErr < errorFunctions.size(); ++iErr) {
194 const auto& errf = errorFunctions[iErr];
195 Eigen::VectorX<T> dGrad_dWeight =
197 errf->getGradient(modelParameters, skelState, meshState, dGrad_dWeight);
198 if (errf->getWeight() != 0) {
199 dGrad_dWeight /= errf->getWeight();
201 MT_LOGD(
"dGrad_dWeight = {}", dGrad_dWeight.transpose());
223 result[iErr].errorFunction = errf;
224 result[iErr].gradWeight = -dGrad_dWeight.dot(Jinv_times_dLoss_dModelParams_full);
226 auto differentiableErr =
227 std::dynamic_pointer_cast<FullyDifferentiableSkeletonErrorFunctionT<T>>(errf);
228 if (differentiableErr !=
nullptr) {
229 for (
const auto& inputName : differentiableErr->inputs()) {
230 result[iErr].gradInputs.emplace(
232 differentiableErr->d_gradient_d_input_dot(
233 inputName, modelParameters, skelState, -Jinv_times_dLoss_dModelParams_full));
247 Eigen::Ref<
const Eigen::VectorX<float>> dLoss_dModelParams,
248 float* gradientRmse);
256 Eigen::Ref<
const Eigen::VectorX<double>> dLoss_dModelParams,
257 double* gradientRmse);
const Character & getCharacter() const
Provides access to the full character (replaces getSkeleton and getParameterTransform).
const std::vector< std::shared_ptr< SkeletonErrorFunctionT< T > > > & getErrorFunctions() const
bool needsMeshState() const
Checks if any error functions require mesh state.
double getJacobian(const VectorX< T > ¶meters, MatrixX< T > &jacobian, VectorX< T > &residual, size_t &actualRows)
Computes the Jacobian matrix for least squares problems.
libfmt formatter support for Eigen types.
SkeletonT< float > Skeleton
std::vector< ErrorFunctionDerivativesT< T > > d_modelParams_d_inputs(const Skeleton &skeleton, const ParameterTransformT< T > ¶meterTransform, const ParameterSet &activeParams, const ModelParametersT< T > &modelParameters, SkeletonSolverFunctionT< T > &solverFunction, Eigen::Ref< const Eigen::VectorX< T > > dLoss_dModelParams, T *gradientRmse)
Eigen::MatrixX< T > extractSubsetMat(Eigen::Ref< const Eigen::MatrixX< T > > fullMat, const std::vector< Eigen::Index > &subsetIndexToFullParamIndex)
template std::vector< ErrorFunctionDerivativesT< float > > d_modelParams_d_inputs< float >(const Skeleton &skeleton, const ParameterTransformT< float > ¶meterTransform, const ParameterSet &activeParams, const ModelParametersT< float > &modelParameters, SkeletonSolverFunctionT< float > &solverFunction, Eigen::Ref< const Eigen::VectorX< float > > dLoss_dModelParams, float *gradientRmse)
Eigen::VectorX< T > subsetVecToFullVec(Eigen::Ref< const Eigen::VectorX< T > > subsetVec, const std::vector< Eigen::Index > &subsetIndexToFullParamIndex, Eigen::Index fullVecSize)
template std::vector< ErrorFunctionDerivativesT< double > > d_modelParams_d_inputs< double >(const Skeleton &skeleton, const ParameterTransformT< double > ¶meterTransform, const ParameterSet &activeParams, const ModelParametersT< double > &modelParameters, SkeletonSolverFunctionT< double > &solverFunction, Eigen::Ref< const Eigen::VectorX< double > > dLoss_dModelParams, double *gradientRmse)
Eigen::VectorX< T > extractSubsetVec(Eigen::Ref< const Eigen::VectorX< T > > fullVec, const std::vector< Eigen::Index > &subsetIndexToFullParamIndex)
T sqr(const T &x)
Computes the square of a value.
std::vector< Eigen::Index > computeSubsetParameterIndices(const ParameterTransformT< T > ¶meterTransform, const ParameterSet &activeParams)
Eigen::VectorX< T > hessianInverseTimes(Eigen::Ref< const Eigen::MatrixX< T > > jacobian, Eigen::Ref< const Eigen::VectorX< T > > v)
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
A vector describing joint parameters, of size numSkeletonJoints * kParametersPerJoint.
Represents the complete state of a model's mesh.
void update(const ModelParametersT< T > ¶meters, const SkeletonStateT< T > &state, const Character &character)
Updates the mesh state based on model parameters and skeleton state.
A vector of size numModelParams holding the values of all model parameters for a character.
Represents the complete state of a skeleton.