Modern Robotics and Physical AILesson 6.2 · Imitating human motion
Glossary Ideas map RU
6.2 Part 6 · Humanoid robots

Imitating human motion: motion tracking, an adversarial prior and retargeting

In lesson 6.1, Ada learned to hold a velocity command with a reward of nine terms, but she doesn't walk like a person: at 0.6 m/s, she makes 3.4 steps a second, 18 cm each, and keeps her knees bent all the time. The reward has no “walk like a human” term, and nobody knows how to write one. DeepMimic (Peng et al., 2018) proposed rewarding the policy for staying close to a recording of human motion. The phase of the recording in the policy's input, episodes that start from a random frame of the recording, and episodes cut short by a fall taught simulated characters to walk, run and do flips from 0.5–5 seconds of recording per skill, at a cost of 60 million steps and two days of computation for each skill. AMP (2021) dropped the comparison with a frame as well: a second network learns to tell the policy's motions from the recorded ones, and the policy is rewarded when its motions can't be told apart from them. This lets datasets of dozens of unlabeled recordings go into training, and the gait changes with the velocity command on its own. Between the recording and the robot lies the transfer of motion to a body with different link lengths and joint limits, and ExBody (2024), on the H1 robot, kept only the upper-body motion from the recordings. In this lesson, you'll transfer a person's stair climb and squat to Ada and check whether she stays on her feet, train one policy with two rewards, and see what comes out of the mean of two recordings.

≈ 60 minutes3 labs, an interactive and a quizmissions with a goal
HUMAN → ADATransfer a stair climb and a squat from a human skeleton to Ada's legs in three ways, and find where a foot sinks into a stair, a joint goes past its limit and the center of mass leaves the support
TWO REWARDSTrain one policy with a “repeat the recording's frame” reward and with a reward from a network that tells Ada's motions from the recorded ones, and compare their rhythm, accuracy and speed at a different command
THE MEAN OF TWO RECORDINGSAverage two walking recordings started with different legs, and see what a policy learns from such a mean
FROM THE PAPERSLook at the numbers from DeepMimic, AMP, ExBody and H2O: reward weights, ablations, recording datasets, hours and millions of steps

Start the lesson ↓

1 Naturalness

Naturalness can't be written as reward terms

The policy from lesson 6.1 walks, follows the velocity command and doesn't fall, but its gait isn't human: the steps are short and frequent, and the knees stay bent all the time. You could penalize each such property with a separate reward term: for energy, for joint accelerations, for asymmetric steps. The DeepMimic authors write that naïve objectives such as forward progress or maintaining a desired velocity “often produce gaits that exhibit extraneous motion of the limbs, asymmetric gaits, and other objectionable artifacts.” Crafting terms that remove these artifacts, in their words, “requires a substantial degree of human insight, and often yields only modest improvements.” Naturalness can't be stated as a number: we recognize it when we see it, but we can't say what it's made of.

Ada is the course's home humanoid; since lesson 6.1, she has legs instead of a wheeled base. More about her and her home in lesson 0.1.

Examples of natural motion, on the other hand, are everywhere: people walk, squat and wave their arms every day, and these motions can be recorded. With recordings, you don't need to describe naturalness in words and weights. You can reward the policy for moving like the recording. This approach is called motion imitation. The policy still learns by reinforcement in a physics simulation, as in lesson 6.1, but the reward is computed from the recording. This lesson covers two ways of saying what “like the recording” means: compare the pose with the frame of the recording at the same phase of the motion, or train a second network to tell the policy's motions from the recorded ones. First, let's look at what a motion recording is and why you can't simply hand it to the robot.

2 Recordings

Human motion recordings and the robot's body

Human motion is recorded with motion capture. Several cameras film markers on a person's body, and the positions of the joints and the angles between the links of the skeleton are reconstructed from the markers. The result is a sequence of frames, and each frame stores a pose: the position of the pelvis and the angles of all joints. There are plenty of such recordings. DeepMimic took its motions from the open CMU and SFU libraries, 0.5–5 seconds of recording per skill. For its “locomotion” dataset, AMP collected 56 recordings of eight people, 434 seconds in total. H2O (He et al., 2024), which lesson 6.3 covers in detail, used the AMASS library, which holds 40 hours of motion capture, and ExBody selected 780 recordings with a total length of 3.7 hours from the CMU library. The recordings in this lesson's labs are simpler. They are our own curves of hip, knee and ankle angles over the phase of a step, similar to human walking, plus a squat and arm waving. The pelvis in them is reconstructed so that the support foot stands on the floor and doesn't slip. That's enough to take the methods apart.

You can't hand the recording to the robot directly: it captures a person, and a different body will repeat it. The robot has different link lengths and fewer degrees of freedom, its joints have their own limits, and its actuators have a torque limit. Transferring motion from one body to another is called retargeting. In lesson 4.2, retargeting transferred the motion of the operator's hand to the robot's hand; here the whole body is transferred. The simplest way is to copy the joint angles. That's how DeepMimic transferred recordings to a model of the Atlas robot: the authors “simply copied the local joint rotations from the humanoid to the Atlas, without any further modification.” ExBody transfers recordings to the Unitree H1 robot the same way, and its authors admit that they lose information: H1 has 19 degrees of freedom, and the human model has 69. H2O does it differently. First, it fits the shape of the human model to the robot using 12 joints that both of them have, and then, in each frame, it looks for the robot's joint angles that put these 12 points where they are on the human. Copying angles, the H2O authors write, “can lead to large differences in end-effector positions due to the large difference in kinematic trees,” and without fitting the shape, H1 ended up with an in-toed gait.

Ada's leg proportions are almost human: her thigh and shin are 30 cm each, and the person in our recordings has 45 and 43 cm. But Ada herself is a third shorter, while the floor and the stairs in the home are the same as for a person. And her hip bends forward only up to 86°, versus 125° for a person. Which of these gets in the way of the transfer, and can it be fixed with joint angles alone?

🔬 Lab 1

Human → Ada retargeting: proportions, joint limits and balance

Two scenes show the same motion frame by frame: one shows the person from the recording, the other shows Ada after the transfer. The motion repeats in a loop. The first recording is a climb up two stairs 17 cm high, one stair at a time with both feet: the person puts the leg nearer to the viewer on the stair, shifts weight onto it and brings the other foot up next to it, and then goes back down. The readout under the scenes says what goes wrong over a cycle. We look at how far a foot sinks into a stair or the floor, whether the foot the person is standing on at that moment hangs above the support, and whether joints go past their limits. The stair climb and the squat are slow, so for them we also check Ada's center of mass: she can stay up only if it stays over her feet. The recording and the transfer method unlock as you go through the missions.

Side view; the figures move forward. One scene shows the person from the recording, with a blue outline and a round head, the other shows Ada. The stairs are drawn as a hatched profile of the same height on both scenes, and in this recording the camera stays still. In the other recordings, the pelvis of both figures stays in place, and the marks on the floor run backward, as in walking. The lighter leg and arm are the ones farther from the viewer. A red foot has sunk below the floor or into a stair, and a red ring on a joint means its angle is past the limit of Ada's joint. In slow motions, a circle with a cross on Ada's scene marks the center of mass, and a dashed line shows its projection down. Segments under the feet show the support: they're green when the center of mass is over the support and red when it's off the support.

Under the hood: the skeleton, the recordings, three transfer methods and the center of mass

Skeleton. The model is flat, seen from the side: a pelvis, a torso with a head, two arms (shoulder and elbow) and two legs (hip, knee, ankle and a foot with a heel and a toe). Angles are measured as in lesson 6.1. The torso pitch θ\theta is positive forward, the hip flexion qhq_h is measured from the torso, the knee flexion is qk≥0q_k \ge 0, and the ankle angle qaq_a is positive when the toe rises. The directions of the links from the vertical are: for the thigh α1=qh−θ\alpha_1 = q_h - \theta, for the shin α2=α1−qk\alpha_2 = \alpha_1 - q_k, for the foot β=α2+qa\beta = \alpha_2 + q_a, and at β=0\beta = 0 the foot lies flat on the floor. The person's thigh is 0.45 m and shin 0.43 m, the ankle is 8 cm above the sole, the heel 6 cm behind the ankle, and the toe 17 cm in front of it. The person's joint limits: hip from −30 to 125°, knee from 0 to 145°, ankle from −45 to 35°. Ada's lengths are the same as in lesson 6.1: thigh and shin 0.30 m each, the ankle 5 cm above the sole, the heel 6 cm behind the ankle, and the toe 12 cm in front. Her joint limits: hip from −57 to 86°, knee from 0 to 132°, ankle from −52 to 52°.

Recordings. In the walking recording, the hip angle changes as qh(φ)=0.10+0.36cos⁡2πφq_h(\varphi) = 0.10 + 0.36\cos 2\pi\varphi, the knee flexion is the sum of two “bumps” of the form exp⁡(−(Δφ/w)2)\exp(-(\Delta\varphi/w)^2) centered at phases 0.14 and 0.72, and the ankle pushes off at phase 0.62. The other leg runs with phase φ+0.5\varphi + 0.5. A cycle lasts 1.2 s and has 60 frames, 50 per second. In the squat, both legs move the same way: at the lowest point, the thigh is almost horizontal, the torso leans forward by 43°, the hip is bent by 123°, the knee by 109° and the ankle by 29°, and the foot lies flat on the floor; a cycle lasts 2.4 s. In arm waving, the legs stand still, and the arm nearer to the viewer is raised and swings. In these three recordings, the pelvis in every frame is placed so that the lowest point of the sole lies on the floor and the support foot doesn't slip. That gives the speed of the walking recording: 1.22 m/s for the person. The stair climb is built differently, from the feet. The staircase of two stairs, 17 cm high and 28 cm deep, starts at x = 0. For each stair, the near foot rises in place, moves over the edge and comes down on the stair; then the weight shifts forward, the rear foot rolls onto its toe and is brought up next to the other one. The descent repeats the climb backward in time, and the whole cycle lasts 6 s. The torso leans forward the most, up to 29°, while the weight shifts. The pelvis in every frame is placed so that the person's center of mass (from the mass fractions of the links in Winter's tables) is over the support and the legs reach the feet; the joint angles come from inverse kinematics. The person's hip bends up to 81° in this motion.

Three methods. “Angles as is”: Ada's joint angles equal the person's, and the position of the pelvis is multiplied by the ratio of leg lengths s=0.65/0.96=0.68s = 0.65 / 0.96 = 0.68. The floor and the stairs are not scaled. “Keypoints”: the ankles and the foot tilt are placed relative to the support where they are on the person. On a flat floor, this is the ankle position relative to the pelvis multiplied by ss. On the stairs, Ada's feet go where the person's feet are, the pelvis stays above the same spot, its height above the support is multiplied by ss, and the pelvis doesn't rise higher than the legs can reach. The hip and knee angles come from the inverse kinematics of a two-link leg, as in lesson 0.3. “Points, limits and floor”: the same points, but if the required hip flexion exceeds the limit, the torso leans less by the missing angle. The direction of the thigh and the position of the foot don't change, and the arms keep their direction in the world. Then the other angles are clipped to their limits, and the pelvis is placed on the support: in frames where the person's foot is on the support, the lowest point of Ada's sole also lies on the support. The keypoint error on a flat floor is the mean distance between Ada's knees, ankles and toes and the same points of the person multiplied by ss.

Center of mass and support. Ada's center of mass is computed from the masses of her body's links from lesson 6.1: the torso with the head and arms, 18 kg, at the middle of the torso, and the thigh 2.6 kg, the shin 1.8 kg and the foot 0.6 kg at the middles of the links. The support is the segment between the points of the sole that touch the floor or a stair or are closer to it than 1 cm. The check is static, so it only suits slow motions: in walking, the center of mass keeps leaving the support foot, and balance is held by the motion itself.

When the angles are copied, the person's step shrinks along with Ada, while the floor and the stairs stay the same: for each 17 cm stair, a step at Ada's scale lifts her only 11.5 cm, the shortfall adds up, and the foot sinks into the second stair by up to 11.2 cm. Keypoints put the feet on the real floor and stairs, but for Ada these stairs are taller: the hip goes past its limit in 19% of frames, by up to 7°. The third method leans the torso 7° less than the person's, and for the stairs that's enough: the joints are within their limits, and the center of mass is over the support. In the squat, the same correction needs 39°, and the center of mass moves past the heels by up to 2.7 cm. Each pose correction fixes one violation and creates another, and kinematics doesn't check balance at all.

Whether Ada stays up is decided by forces, and only physics computes them. So the recording shouldn't just be played back; a policy should learn from it right in a physics simulation and find for itself how to keep its balance while staying close to the recording. How do you build such a reward?

3 DeepMimic

DeepMimic: a reward for closeness to the frame, the phase, reference state initialization and early termination

A recording gives kinematics, that is, angles and positions without forces. The policy, as in lesson 6.1, outputs target joint angles, PD controllers track them, and the motion itself comes out of a physics simulation that has both gravity and a floor. DeepMimic (Peng, Abbeel, Levine, van de Panne, 2018) connects the two with a reward: at every step, the policy gets more the closer its pose is to the frame of the recording. This approach is called motion tracking, and the policy is called a tracker. The DeepMimic policy outputs target angles 30 times a second, and the physics runs at 1.2 kHz. To know which frame to compare against, a phase variable φ\varphi is added to the policy's input. It's a number from 0 to 1 that shows where the recording is now: zero means its start and one its end, and for cyclic motions like walking, the phase starts again from zero after the end.

The imitation reward is the sum of four terms: matching the joint angles, their velocities, the positions of the hands and feet, and the position of the center of mass, with weights 0.65, 0.1, 0.15 and 0.1. Each term is the exponential of the negative error and therefore lies between 0 and 1. A task reward is added to the imitation, for example for the direction and speed of walking, hitting a target or throwing a ball, with a weight of 0.3 versus 0.7 for the imitation. The DeepMimic policy is a network of two layers with 1024 and 512 neurons, and it is trained with PPO from lesson 5.2.

Two decisions turned out to matter more than the architecture. The first is reference state initialization (RSI): an episode starts not from the same pose every time but from a random frame of the recording, with its angles and velocities. Without it, the policy learns the motion strictly in order. As the authors write, “before mastering the earlier phases, little progress can be made on the later phases,” and a character that can't land yet won't find out that the jump of a backflip pays off. The second decision is early termination: an episode ends when the torso or the head touches the floor, and the reward for the rest of the episode is zero. This makes falling unprofitable, and the experience doesn't fill up with states of the character, in the authors' words, “struggling on the ground in vain.”

The DeepMimic loop. The recording and the physics are shown in blue, the policy and the target angles in orange, and the phase clock, the comparison and the reward in purple. The human and Ada recordings run in step with the phase clock φ, and the current frame is highlighted. The pale Ada in the physics block shows the frame of the recording at phase φ, the bright one shows Ada in the simulation, and the closer they are to each other, the higher the reward. Every new episode starts from a random frame of the recording, and a fall ends the episode, with zero reward for the rest of it. The term weights come from the paper; the reward values on the diagram are illustrative. Click the diagram to pause it.

Table 5 of the paper shows where these two decisions change the outcome. For walking, the three variants are almost equal, while for the backflip without RSI, in the authors' words, “the policy never learns to perform a full mid-air flip. Instead, it performs a small backwards hop while remaining upright.”

SkillRSI and terminationTermination onlyRSI only
Backflip0.7910.7300.379
Sideflip0.8230.7170.355
Spinkick0.8480.8580.358
Walk0.9800.9810.974

The normalized return of an episode is 0 for the lowest possible total reward and 1 for the highest. The numbers come from Table 5 of the DeepMimic paper.

The same for our flat Ada and the policy you'll train in Lab 2: tracking the walking recording, three seeds, 100 iterations. Hover over a row or click it.

VariantCheck return, seeds 1–3Pose errorCheckChecks with a fall

In our case, even walking goes worse without RSI. Our training is much weaker than DeepMimic's: 100 iterations of random search versus tens of millions of PPO steps, and a policy that starts every episode standing simply doesn't have time to learn to get going. For ExBody, which we'll see at the end of the lesson, training without RSI didn't work at all.

DeepMimic's price shows up in two places. The first is training. According to the authors, a policy for an individual skill typically needs about 60 million steps of experience, which takes “about 2 days on an 8-core machine,” and policies have to be trained separately for each skill. The second is the phase. The policy needs a variable “synchronized with the reference motion, which advances linearly with time,” and this, as the authors write, “limits the ability of the policy to adjust the timing of the motion.” For the character to walk faster or slower, you need a different recording. For it to switch from one motion to another, you need the max of the reward over several recordings, the recording's number in the input, or several policies picked between by their value functions. Can you say “like the recording” without pointing to a frame?

4 AMP

AMP: a discriminator instead of a per-frame reward

DeepMimic's reward is a measure of pose closeness that people designed and whose weights were tuned by hand, and it has to be checked again for every new motion. Large datasets also need a way to choose which recording to imitate right now. AMP (adversarial motion priors; Peng, Ma, Abbeel, Levine, Kanazawa, 2021) replaces the designed measure with a learned one. A prior here means a learned notion of which motions look like the recorded ones, and it's called adversarial because it's learned in a contest between two networks. The second network, the discriminator D(s,s′)D(s, s'), looks at a transition between two neighboring states and answers whether it comes from the recordings or was produced by the policy. It's transitions that are compared, not “state, action” pairs, because a recording has no actions. The discriminator is trained like a regression: it should give transitions from the recordings a 1 and the policy's transitions a −1. The policy gets a style reward rS=max⁡[0,  1−0.25 (D−1)2]r^S = \max[0,\; 1 - 0.25\,(D - 1)^2], which lies between 0 and 1 and is higher the closer the score is to “recording.” A task reward is added to it, and both have a weight of 0.5. There's no phase and no choice of recording: as the authors write, “composition of disparate skills emerges automatically from the motion prior, without requiring a high-level motion planner or other task-specific annotations of the motion clips.”

Adversarial training, where two networks learn against each other, is known for its instability, and AMP rests on three decisions. The first concerns the discriminator's features: the joint velocities and rotations, the linear and angular velocity of the pelvis, and the positions of the hands and feet relative to the pelvis, with nothing about the task. Without velocities, a character doing a roll, in the authors' words, “is prone to converging to a strategy of holding a fixed pose on the ground, instead of performing a roll.” The second decision is a gradient penalty: the squared gradient of the discriminator with respect to the features of the recorded transitions is added to its loss with a coefficient of 10, so that its score changes smoothly near the real data. In the authors' ablations, this is “the most vital component,” and models trained without it “tend to exhibit large performance fluctuations.” The third decision is a buffer of old policy transitions, so that the discriminator doesn't adapt to the latest batch. RSI and early termination from DeepMimic stay.

The AMP loop. The recordings and the task are shown in blue, the policy and Ada's transitions in orange, and the discriminator and the style reward in purple. The pairs of tiles running to the discriminator are transitions (s,s′)(s, s'): from the recordings and from Ada in physics. The discriminator learns to give the recordings a 1 and Ada a −1, and Ada gets a style reward for each of her transitions and adds it to the task reward. Which frame of the recording to compare with doesn't matter: there's no phase, and Ada in physics steps to her own rhythm. The formula and the weights come from the paper; the discriminator's scores on the diagram are illustrative. Click the diagram to pause it.

The authors show what this gives on the “locomotion” dataset: 56 recordings of walking, running and jogging by eight people, 434 seconds in total. The task is to move in a given direction at a speed of 1 to 5 m/s, and the policy switches on its own from walking at about 1 m/s to jogging at about 2.5 m/s and to running at about 4.5 m/s. With a dataset of walking recordings alone, the policy, in the authors' words, does “not show any transitions to faster running gaits even at faster target speeds” and can't meet the fast commands. Recordings of walking and of punching, none of which shows a person walking up to a target to punch it, produce a policy that walks up and punches.

The price is accuracy and stability. On a single walking recording, DeepMimic's tracking gives a pose error of 0.018 m and AMP 0.030 m, after time alignment. The authors explain it this way: “Since the tracking-based policies are synchronized with their respective reference motions, they are generally able to learn faster and achieve lower errors.” On the front flip, with AMP, “instead of performing a flip, the character learns to simply shuffle forwards in order to avoid falling,” and on large datasets AMP, like many adversarial models, is prone to mode collapse: out of many motions, the policy imitates only some. Training one AMP policy takes 100–300 million steps, 30–140 hours on 16 cores.

So the same policy can have two rewards: with a phase and a comparison to the frame, or without a phase, with a discriminator. How will the gaits differ if both learn from one recording, and what happens when the velocity command doesn't match the recording's speed?

🔬 Lab 2

Tracking versus AMP: one recording, two rewards, a different speed

The recording is human walking transferred to Ada with the third method from Lab 1. On Ada, the recorded walk moves at 0.83 m/s, and a step cycle, that is, a step with one leg and a step with the other, lasts 1.2 s, which is 0.83 cycles a second. Both methods use the same policy: a joint rhythm over the phase plus feedback on the torso pitch, the pelvis height and the speed, 35 numbers in all. It is trained by augmented random search (ARS), like the stepping rhythm in Lab 3 of lesson 6.1: 100 iterations, each with 33 episodes of 2.4 s with a random velocity command and a start from a random frame of the recording. Only the reward differs. A check lasts 4 s and starts from the first frame of the recording. We compare the policies' accuracy by the pose error after alignment: the mean deviation of Ada's joint angles from the recording after the recording has been shifted in phase to best match Ada's gait.

Side view: Ada walks forward, the camera follows her pelvis, and the marks on the floor show meters. The pale Ada is a ghost, that is, the frame of the recording on its clock from the same starting point. Ada's outline is blue for the tracker and orange for the AMP policy. A red cross marks a fall. The scene's label gives the policy, the velocity command and the current speed.

Summary. On the learning curves, the horizontal axis shows the iteration and the vertical axis the mean reward per step in the check, from 0 to 1. The blue line shows the tracker, the orange one the AMP policy, and a red dot marks a check with a fall. On the speed summary, the bars show the mean speed over 4 s at each command, the dashed line shows the command itself, and a pale bar means the policy fell.

Under the hood: the physics, the policy, the two rewards and the training

Physics. The model is simpler than in lesson 6.1: the legs are massless, and all of Ada's mass, 28 kg, is assigned to the torso, with the center of mass at the middle of the torso, 23 cm above the pelvis, and a moment of inertia of 1.2 kg·m². The joints obey q¨=kp(q∗−q)−kdq˙\ddot q = k_p (q^\ast - q) - k_d \dot q with kp=420k_p = 420 s⁻² and kd=30k_d = 30 s⁻¹, and an acceleration limit of 260 rad/s² plays the role of the torque limit. The angle limits are the same as Ada's in Lab 1. The floor pushes on the heel and the toe with a 24,000 N/m spring with damping, and friction works as a spring from the contact point, limited by μ=0.9\mu = 0.9. A fall is a torso pitch of more than 1 rad, the pelvis lower than 45% of the leg length, or the top of the torso or the head touching the floor. The physics step is 2.5 ms, and each policy step covers 8 physics steps (50 Hz). This way, training two policies in the browser takes seconds, while Ada's link lengths, joint limits and mass stay the same as in lesson 6.1.

Policy. The target angle of joint jj of a leg with phase φs\varphi_s (one leg follows phase φ\varphi, the other φ+0.5\varphi + 0.5) is

qj∗=cj+aj(Δv)∑k=12(ajkcos⁡2πkφs+bjksin⁡2πkφs)+∑i=15Gjifi,q^\ast_j = c_j + a_j(\Delta v)\sum_{k=1}^{2}\big(a_{jk}\cos 2\pi k\varphi_s + b_{jk}\sin 2\pi k\varphi_s\big) + \sum_{i=1}^{5} G_{ji} f_i,

where f=[θ,  0.2 ω,  (y−y0)/0.1,  (v−v∗)/0.5,  (v∗−vref)/0.5]f = [\theta,\; 0.2\,\omega,\; (y - y_0)/0.1,\; (v - v^\ast)/0.5,\; (v^\ast - v_{\text{ref}})/0.5] are the torso pitch and angular velocity, the pelvis height, the speed error and the command itself relative to the recording's speed, and the amplitude changes with the command as aj(Δv)=1+sj(v∗−vref)a_j(\Delta v) = 1 + s_j (v^\ast - v_{\text{ref}}). For the tracker, the phase φ\varphi runs at the recording's frequency, and for AMP, at its own frequency f0+f1(v∗−vref)f_0 + f_1 (v^\ast - v_{\text{ref}}), which the policy learns itself, starting from f0=1f_0 = 1 Hz and f1=0f_1 = 0. 35 parameters in all.

The tracker's reward is built as in DeepMimic: r=0.7 rI+0.3 rGr = 0.7\, r^I + 0.3\, r^G, where

rI=0.65 e−2∑j(qj−q^j)2+0.1 e−0.1∑j(q˙j−q˙^j)2+0.15 e−40∑feet∥p−p^∥2+0.1 e−10∥ppelvis−p^pelvis∥2.\begin{aligned} r^I ={}& 0.65\, e^{-2\sum_j (q_j - \hat q_j)^2} + 0.1\, e^{-0.1\sum_j (\dot q_j - \hat{\dot q}_j)^2} \\ &+ 0.15\, e^{-40\sum_{\text{feet}}\|p - \hat p\|^2} + 0.1\, e^{-10\|p_{\text{pelvis}} - \hat p_{\text{pelvis}}\|^2}. \end{aligned}

Here q^\hat q and p^\hat p are taken from the frame of the recording at phase φ\varphi, and the foot and pelvis positions are computed in world coordinates on the recording's clock. The task reward rG=e−10(v∗−v)2r^G = e^{-10 (v^\ast - v)^2} rewards following the velocity command, as in AMP, only on our scale of speeds. The AMP reward is r=0.5 rG+0.5 rSr = 0.5\, r^G + 0.5\, r^S, where rS=max⁡[0,1−0.25(D−1)2]r^S = \max[0, 1 - 0.25 (D - 1)^2]. The discriminator is a 34 → 24 → 1 network on the features of two neighboring states: joint angles and velocities, torso pitch, speed, pelvis height and foot heights. It is trained with the loss

min⁡D  Eref (D−1)2+Eπ (D+1)2+102 Eref ∥∇D∥2,\min_D\; \mathbb E_{\text{ref}}\,(D - 1)^2 + \mathbb E_{\pi}\,(D + 1)^2 + \tfrac{10}{2}\, \mathbb E_{\text{ref}}\,\|\nabla D\|^2,

and we estimate the gradient penalty with the difference D(x+εu)−D(x−εu)D(x + \varepsilon u) - D(x - \varepsilon u) along a random direction uu rather than with the exact gradient used in the paper. Each iteration has 6 Adam steps of 32 transitions, and the policy buffer holds the latest 12,000 transitions.

Training. Augmented random search (ARS) from lesson 6.1: 16 random directions of size 0.06 and an update along the 8 best with a step of 0.03, normalized by the spread; for AMP's stepping frequency, the directions are 3 times longer. Each episode has its own random command from 0.5 to 1.5 times the recording's speed, a start from a random frame of the recording, and early termination on a fall; an episode lasts 2.4 s. There are 100 iterations, and the result is the policy with the best check in the last third of training. We compute the pose error after alignment: we try phase shifts of the recording in steps of 1/60 of a cycle and take the smallest mean ∣q−q^∣|q - \hat q| over the frames after 0.5 s, until Ada falls. This is a simplified time alignment, similar to DTW (dynamic time warping, aligning sequences in time) in the AMP paper.

The same in any browser. The engine's exponential, logarithm, sine, cosine and arctangent are built from additions and multiplications, as in lessons 5.2–6.1, and all randomness uses seed 3. That's why Node, Chrome and Safari compute exactly the same thing.

Ten seeds. On seeds 1–10, at the recording's command, both policies walk without falling and cover 3.2–3.6 m in 4 s. The pose error after alignment is 5.4–7.0° for the tracker and 6.2–11.0° for the AMP policy, and on every seed it's higher for AMP. At the recording's command, the AMP policy's step cycle is 0.82–1.17 Hz, and it differs from the recording's frequency, 0.83 Hz, by more than 0.03 Hz on 9 seeds out of 10. At a command of 1.25×, the AMP policy is faster than the tracker on all ten seeds (0.94–1.10 versus 0.83–0.92 m/s), and on all ten it learned to step more often as the command grows: at 1.25×, its step cycle is 0.95–1.30 Hz. Between commands of 0.75× and 1.25×, its speed changes more than the tracker's, again on all ten, and at 0.75× neither policy falls.

In 4 seconds, the tracker covers 3.26 m at 0.81 m/s, almost like the recording, and stays close to its ghost: the pose error after alignment is 5.4°. The AMP policy covers 3.47 m at 0.86 m/s, but its step cycle runs at 0.96 Hz versus 0.83 for the recording, so the ghost sometimes gets ahead of it and sometimes falls behind. Its pose error is 10.4°, and the style reward in the check is 0.67. At commands of 0.62, 0.83 and 1.04 m/s, the tracker walks at 0.65, 0.81 and 0.92 m/s, and the AMP policy at 0.69, 0.86 and 1.03 m/s, stepping at 0.81, 0.96 and 1.12 Hz. The tracker is tied to the recording's tempo: its clock runs at the recording's frequency, and the reward for the pelvis and foot positions on that clock pulls it toward the recording's speed. The AMP policy changes both the frequency and the length of its steps as long as its transitions still look like the recorded ones.

Both policies learned from one recording. Real datasets have hundreds of recordings, and they often disagree with each other: one person starts walking with the left leg, another with the right. What happens if you train a tracker on the mean of such recordings?

🔬 Lab 3

Mixing recordings: the mean of two recordings versus picking one

Recording A is Ada's walking from Lab 2. Recording B is the same walking started with the other leg, so its frames are shifted by half a cycle. In lesson 1.3, the teacher drove around the kettle sometimes on one side and sometimes on the other, and a regression trained on the mean squared error steered Ada straight into the kettle. Here it's the same story with motion: shift recording B in phase and see what comes out of the mean of the two recordings. Then we'll train two trackers, as in Lab 2: one on the mean, and one with the max of the reward over the two recordings, as in DeepMimic.

Side view; each figure's pelvis stays in place. Until the trackers are trained, the scene shows three poses at the same moment: recording A, recording B with the phase shift from the slider, and their mean with an orange outline; a red foot has sunk below the floor. After training, the scene shows two trackers: the one on the mean (orange outline) and the one with the max over the recordings (blue). The pale Ada next to each tracker is the frame of its recording on the recording's clock. The marks on the floor run backward at Ada's speed, and the labels under the figures give the step length and the imitation reward.

Under the hood: the frame-by-frame mean and the max over recordings

Mean. Recording B is recording A with the phase shifted by half a cycle, so the legs swap roles in it. The frame-by-frame mean is qˉ(φ)=12[qA(φ)+qB(φ)]\bar q(\varphi) = \tfrac12 [q_A(\varphi) + q_B(\varphi)], and the torso pitch and the pelvis height are averaged the same way. At a 50% shift, qB(φ)=qA(φ+0.5)q_B(\varphi) = q_A(\varphi + 0.5), and the angles of the two legs in the mean coincide: a step length of 0 cm, both feet at the same height, while the pelvis of the mean moves forward at the same speed as before. The step length is the largest difference between the ankle positions along the floor over a cycle.

Two trackers. The policy and the training are the same as for the tracker in Lab 2. The first tracker is rewarded for closeness to the averaged recording. The second gets rI=max⁡(rAI,rBI)r^I = \max(r^I_A, r^I_B): at each step, the reward is taken from whichever recording the pose is closer to. That's how DeepMimic combined five recordings of walking and turning. The second tracker's ghost on the scene is the frame of the recording its pose is closer to.

Ten seeds. On seeds 1–10, both trackers walk without falling. The imitation reward of the tracker on the mean is 0.45–0.54 versus 0.68–0.81 for the tracker with the max, the pose error is 8.2–11.0° versus 5.1–7.4°, and on every seed the tracker on the mean is worse on both metrics. Its step length is 43–52 cm, even though in its recording it's zero.

At a 50% shift, the mean of the two recordings puts the legs together: a step length of 0 cm, while the pelvis glides forward at 0.83 m/s, and no walking looks like that. The tracker on the mean still walks, 3.67 m in 4 seconds with a step length of 49 cm, even though its recording has no steps. It can't reproduce such a recording, and its imitation reward is 0.51 versus 0.68 for the tracker with the max over the recordings, with a pose error of 8.7° versus 7.3°. The tracker with the max picks one of the two recordings in every frame and follows it. AMP doesn't have this difficulty at all: the discriminator looks at transitions, and recordings A and B have the very same ones. AMP's weakness lies elsewhere: on large datasets, in the authors' words, its policy “is prone to imitating only a small subset of the example behaviors,” and the choice between recordings comes back in a different form. Lesson 6.3 covers how this is handled.

5 Today

On real humanoids: upper body from the recording, legs from the command

Motion imitation moved from simulation to real robots with caveats, and the first one concerns the legs. ExBody (Cheng et al., 2024) trains a single policy for the Unitree H1 robot: 19 degrees of freedom, a mass of about 51.5 kg, a height of about 1.8 m. There are 780 recordings, from the CMU library, and they are transferred by copying angles. The authors' key idea is “to NOT mimic exactly the same as the reference motion.” Nine joints and six keypoints of the upper body, that is, the shoulders, elbows and hands, are rewarded for matching the recording, while the legs only have to follow a command for the body: velocity, roll, pitch, yaw and height, taken from the same recording or from a joystick. Tracking the whole body gave the authors worse results on all metrics, because “while the reference motion is designed for a single step, the robot executes multiple steps in an attempt to stabilize.” Without RSI, training didn't work at all: a mean episode length of 0.23 versus 16.87. The authors tried AMP as an addition to the reward. Episodes with it lasted longer, but the robot tracked the velocity command worse, walked with almost straight legs and low foot clearance, and stumbled, and on the real robot the roll and pitch of the body deviated by 0.087 rad on average versus 0.051 without AMP. The authors write that “for such a complex system, AMP reward itself is not sufficient.” The trained robot walks on gravel and grass, shakes hands and dances with a person, repeating motions from recordings and from a diffusion model that generates motion from a text description.

H2O (He et al., 2024) went further in retargeting. It fits the shape of the human model to the robot, then fits 12 keypoints, and then checks the recordings with physics. A tracker with privileged information, like the teacher from lesson 5.5, tries to reproduce all 10,000 retargeted recordings in simulation, and the roughly 1,500 that it can't manage are thrown out: among them are cartwheels and steps wider than the robot's legs are long. This is exactly the check that kinematics lacked in Lab 1. A single whole-body tracker learns from the remaining roughly 8,500 recordings and imitates a person standing in front of an ordinary camera in real time.

6 Check yourself

Eight common misconceptions

7 Takeaways

Lesson takeaways

  1. Natural motion can't be specified with reward terms: according to the DeepMimic authors, such terms require human insight and give modest improvements. Instead, recordings of human motion are used, and the policy is rewarded for resembling them. The recordings in this lesson are synthetic, while in the papers they come from the CMU, SFU and AMASS libraries.
  2. Retargeting stands between the recording and the robot. When angles are copied, the person's step shrinks along with Ada, while the floor and the stairs stay the same: on a staircase with 17 cm stairs, Ada's foot sinks into the second stair by up to 11.2 cm. Keypoints put the feet in place, but Ada's hip goes past its limit, and correcting the torso lean in the squat moves the center of mass past the heels: kinematics doesn't check balance.
  3. DeepMimic trains the policy in physics with a reward for closeness to the frame of the recording at phase φ\varphi: angles, velocities, hands and feet, center of mass, with weights 0.65, 0.1, 0.15 and 0.1, plus a task with a weight of 0.3. Starting from a random frame of the recording and early termination decide the outcome for flips. The price is 60 million steps and two days of computation per skill, plus being tied to the recording's tempo.
  4. AMP replaces the comparison with a frame by a discriminator on transitions (s,s′)(s, s') and a style reward 1−0.25(D−1)21 - 0.25(D - 1)^2. There's no phase and no choice of recording, so dozens of unlabeled recordings go into training, and the gait changes with the command. The price is accuracy (0.030 versus 0.018 m for tracking on walking) and a tendency to imitate only part of a large dataset.
  5. In the lab, one policy learned with two rewards. The tracker walks next to the recording's ghost with a pose error of 5.4°, while the AMP policy steps to its own rhythm, 0.96 Hz versus 0.83 for the recording, with an error of 10.4°, and at a command of 1.25× it walks at 1.03 m/s versus 0.92 m/s for the tracker. The mean of two recordings started with different legs puts the legs together while the pelvis moves: a tracker on it still steps, but its imitation reward is 0.51 versus 0.68 for the tracker with the max over the recordings.
  6. On robots, ExBody imitates the recording only with the upper body and gives the legs a command for the body, because whole-body tracking made H1's gait brittle. H2O fits the body shape and keypoints and filters out the infeasible with a tracker in simulation: out of 10,000 recordings, about 8,500 remain.

Open questions

Similar to whom?

DeepMimic's reward was designed by people and AMP's discriminator is learned, but both compare motion with human recordings, while the robot has a different body, different actuators and different joint limits. A recording after retargeting is no longer human, and neither method defines what counts as natural for a body whose hip bends only up to 86°. ExBody answers by giving up leg tracking, H2O by filtering out the infeasible, and there's no general answer.

How many recordings can one policy handle?

Tracking with a phase trains one policy on one recording, or on the max over a few similar ones. AMP takes dozens of recordings but tends to imitate only some of them. H2O trains one tracker on 8,500 recordings and, instead of a phase, feeds it the keypoints of the target frame. Where the limit on the number of motions lies and how not to lose the rare ones is covered in lesson 6.3 and the work that came after it.

Paper guide

X. B. Peng, P. Abbeel, S. Levine, M. van de Panne, “DeepMimic: Example-Guided Deep Reinforcement Learning of Physics-Based Character Skills”, 2018.

Other papers in this lesson: AMP (Peng, Ma, Abbeel, Levine, Kanazawa, 2021), with the discriminator on transitions, the gradient penalty and Table 3 with the errors of tracking and AMP; ExBody (Cheng et al., 2024), with upper-body tracking on H1 and the ablations in Table IV; H2O (He et al., 2024), with retargeting through fitting the body shape and filtering recordings with a tracker in simulation.

Resources

Ada can learn from a motion recording: a tracker imitates one recording almost frame for frame, and an AMP policy takes the style from the recordings and adjusts its steps to the command. But tracking with a phase trains one policy per recording, AMP imitates only part of the motions in a large dataset, and H2O was left with about 8,500 recordings after filtering, each of which a policy has to be able to reproduce.

Lesson 6.3 · Whole-body control: how a single policy reproduces any whole-body motion and follows different commands

Was the lesson clear?
Found a mistake or inaccuracy? Report it on GitHub
Found this lesson useful? Say thanks — that is how the author knows the course is being read.