AI4AnimationPy Documentation

repository·main·Indexed 24 days ago

https://github.com/facebookresearch/ai4animationpy

A Python-based framework for AI-driven character animation that unifies PyTorch/NumPy neural network training with real-time rendering and motion capture processing. It features an Entity-Component-System (ECS) architecture, support for GLB, FBX, and BVH imports, and multiple execution modes including Standalone, Headless, and Manual. The framework includes specialized optimization tools such as the AdamW optimizer and the CyclicLRWithRestarts scheduler with various learning rate policies.

Tokens
1.3K
Snippets
4
Records
7
Agent score
35%

What's inside AI4AnimationPy

  1. Overview of AI4AnimationPy

    main

    AI4AnimationPy is a Python framework for AI-driven character animation using neural networks. It is designed to unify model research (PyTorch/NumPy) with visualization and animation engineering, removing the need for external game engines like Unity.

    Key capabilities include:

    • Training neural networks on motion capture data.
    • Real-time visualization of training and inference.
    • Modular architecture using an Entity-Component-System (ECS).
    • Support for various execution modes: Standalone (with rendering), Headless (for server-side training), and Manual (for custom update loop control).
  2. Use AdamW optimizer and Cosine Annealing with Restarts

    main

    The AdamW implementation decouples weight decay from batch gradient calculations as described in the paper "Decoupled Weight Decay Regularization".

    When used with the CyclicLRWithRestarts scheduler, the learning rate is adjusted on every batch update (rather than every epoch) to follow a cosine annealing schedule. This allows the model to converge to different local minima upon each restart. The scheduler also normalizes the weight decay hyperparameter according to the length of the restart period.

    optimizer = AdamW(model.parameters(), lr=1e-3, weight_decay=1e-5)
    scheduler = CyclicLRWithRestarts(optimizer, batch_size, epoch_size, restart_period=5, t_mult=1.2, policy="cosine")
  3. Understand the execution modes in AI4AnimationPy

    main

    The framework supports three primary execution modes depending on your needs:

    1. Standalone: Uses the built-in rendering pipeline for full visualization.
    2. Headless: Runs without a GUI, ideal for server-side training.
    3. Manual: Provides manual control over the update loop, allowing you to specify exactly when and at what intervals the update loop is invoked. This is useful for running code locally or remotely on a server with custom timing requirements.
  4. Implement CyclicLRWithRestarts in a training loop

    main

    To use the CyclicLRWithRestarts scheduler correctly, you must call scheduler.step() at the start of each epoch and scheduler.batch_step() after every optimizer step (batch update), as the scheduler operates on a per-batch basis.

        batch_size = 32
        epoch_size = 1024
        model = resnet()
        optimizer = AdamW(model.parameters(), lr=1e-3, weight_decay=1e-5)
        scheduler = CyclicLRWithRestarts(optimizer, batch_size, epoch_size, restart_period=5, t_mult=1.2, policy="cosine")
        for epoch in range(100):
            scheduler.step()
            train_for_every_batch(...)
                ...
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                scheduler.batch_step()
            validate(...)
  5. Import motion capture data

    main

    AI4AnimationPy supports importing mesh, skin, and animation data from GLB, FBX, and BVH files. The framework's internal motion format is .npz, which stores 3D positions and 4D quaternions for each skeleton joint per frame.

    You can load these files using the Motion class and save them to the internal .npz format for optimized use within the framework.

    from ai4animation import Motion
    
    motion = Motion.LoadFromGLB("character.glb")
    motion = Motion.LoadFromFBX("character.fbx")
    motion = Motion.LoadFromBVH("character.bvh", scale=0.01)
    motion.SaveToNPZ("character")
  6. Configure Cyclical Learning Rate policies

    main

    The CyclicLRWithRestarts scheduler supports several learning rate policies. You can select a policy by passing the policy parameter:

    • policy="cosine": Standard cosine annealing.
    • policy="arccosine": Uses an arccosine profile which has a steeper profile at the limiting points.
    • policy="triangular": Implements the triangular policy. The ratio of increasing to decreasing phases can be adjusted using the triangular_step parameter. The minimum learning rate is controlled by min_lr.
    • policy="triangular2": Reduces the maximum learning rate by half on each restart cycle. Alternatively, you can use policy="triangular" combined with eta_on_restart_cb=ReduceMaxLROnRestart(ratio=0.5).
    • policy="exp_range": Exponentially scales the maximum learning rate based on the iteration count. The base of the exponentiation is controlled by the gamma parameter.

    All schedules can be combined with shrinking/expanding restart periods and weight decay normalization.