Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
fully_differentiable_body_ik.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
14
15#include <unordered_map>
16#include <vector>
17
18namespace momentum {
19
20// This is going to be used within Pytorch's automatic differentiation package,
21// so we will assume the existence of a scalar "loss" function that is the thing
22// we are minimizing in Pytorch. To clarify:
23// "Error"/Error function: the thing that momentum's IK solver is minimizing
24// to compute the body pose.
25// "Loss"/Loss function: the thing that Pytorch is minimizing.
26// Here, we will be computing the derivative of the loss function with respect
27// to all the various error function inputs, like "weight" or "target".
28
29template <typename T>
31 std::shared_ptr<const SkeletonErrorFunctionT<T>> errorFunction = nullptr;
32
33 // Gradient of the energy wrt the global weight.
35
36 // Derivative of the energy wrt the various inputs.
37 std::unordered_map<std::string, Eigen::VectorX<T>> gradInputs;
38};
39
40// Takes in the derivative of the loss wrt the model parameters and
41// computes the derivative of the loss wrt the error function inputs,
42// assuming the model parameters are always the ones that (locally)
43// minimize the momentum IK error.
44//
45// The gradient_norm here can be used to determine if the derivatives
46// are valid; the computation only really works when the gradient
47// (wrt the subset of parameters that are allowed to vary) is close to 0.
48template <typename T>
49std::vector<ErrorFunctionDerivativesT<T>> d_modelParams_d_inputs(
50 const Skeleton& skeleton,
51 const ParameterTransformT<T>& parameterTransform,
52 const ParameterSet& activeParams,
53 const ModelParametersT<T>& parameters,
54 SkeletonSolverFunctionT<T>& solverFunction,
55 Eigen::Ref<const Eigen::VectorX<T>> dLoss_dModelParams,
56 T* gradientRmse = nullptr);
57
58} // namespace momentum
SkeletonT< float > Skeleton
Definition fwd.h:263
std::vector< ErrorFunctionDerivativesT< T > > d_modelParams_d_inputs(const Skeleton &skeleton, const ParameterTransformT< T > &parameterTransform, const ParameterSet &activeParams, const ModelParametersT< T > &modelParameters, SkeletonSolverFunctionT< T > &solverFunction, Eigen::Ref< const Eigen::VectorX< T > > dLoss_dModelParams, T *gradientRmse)
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
Definition types.h:430
std::unordered_map< std::string, Eigen::VectorX< float > > gradInputs
std::shared_ptr< const SkeletonErrorFunctionT< float > > errorFunction
A vector of size numModelParams holding the values of all model parameters for a character.
Definition types.h:99
A parameter transform is an abstraction of the joint parameters that maps a model_parameter vector to...