VidTok

repository·main·Indexed 19 days ago

https://github.com/microsoft/vidtok

A family of video tokenizers from Microsoft designed for continuous and discrete tokenization. VidTok utilizes efficient spatial-temporal sampling and Finite Scalar Quantization (FSQ) to handle high-resolution and long-duration video reconstruction. The library supports causal and non-causal models, providing tools for video reconstruction, performance evaluation (PSNR, SSIM, LPIPS), and a two-stage training strategy for efficient scaling from low to high resolution.

Tokens
7K
Snippets
18
Records
28
Agent score
65%

What's inside VidTok

  1. Perform cross-reenactment reconstruction

    main

    VidTwin allows combining the Structure Latent from one video ($A$) with the Dynamics Latent from another video ($B$) to generate a new output $\mathcal{D}(u^A_{\boldsymbol{S}}, u^B_{\boldsymbol{D}})$.

    Use the inference_vidtwin_cross_reconstruct.py script. This script requires two input video paths: one for the structure and one for the dynamics.

  2. Launch VidTwin training

    main

    Start training using main.py. You can specify a configuration file and a log directory.

    To use Weights & Biases for visualization, use the --wandb flag and provide your entity and project names.

    Note: You can also use torchrun to start the training process.

    # Basic training
    python main.py -b CONFIG --logdir LOGDIR
    
    # Training with Weights & Biases
    python main.py -b CONFIG --logdir LOGDIR --wandb --wandb_entity ENTITY --wandb_project PROJECT
  3. Evaluate reconstruction performance with VidTok v1.1

    main

    To evaluate the reconstruction performance of VidTok v1.1, use the scripts/inference_evaluate.py script.

    Arguments:

    • --config: Path to the configuration file.
    • --ckpt: Path to the checkpoint file.
    • --data_dir: Directory containing the evaluation data.
    • --input_height: Input video height.
    • --input_width: Input video width.
    • --sample_fps: Sampling frame rate.
    • --chunk_size: Size of temporal chunks.
    • --read_long_video: Flag to enable long video support.
    python scripts/inference_evaluate.py --config CONFIG_v1_1 --ckpt CKPT_v1_1 --data_dir DATA_DIR --input_height 256 --input_width 256 --sample_fps 30 --chunk_size CHUNK_SIZE --read_long_video
  4. Prepare data for training and validation

    main

    To train or fine-tune VidTok, you must organize your video files and create a metadata file.

    1. Directory Structure: Place all training videos under a root directory (DATA_DIR). You can use subdirectories to organize them.
    2. Metadata File: Create a .csv file containing the relative paths of the videos with respect to DATA_DIR. The first line of the CSV should be the header videos.

    Example directory structure:

    └── DATA_DIR
        ├── subset1
        │   ├── videoname11.mp4
        │   └── videoname12.mp4
        └── subset2
            └── subsubset1
                └── videoname211.mp4

    Example .csv content:

    videos
    subset1/videoname11.mp4
    subset2/subsubset1/videoname211.mp4

    Follow these same steps for validation data.

    videos
    subset1/videoname11.mp4
    subset2/subsubset1/videoname211.mp4
  5. Fine-tune VidTok on custom data

    main

    To fine-tune VidTok, select a configuration file from the configs folder and modify the following parameters:

    • Initialize from checkpoint: Set model.params.ckpt_path to your pre-trained checkpoint path.
    • Configure Data: Update the data section with your data_dir and meta_path.
      • sample_num_frames: Set to 17 for causal models and 16 for non-causal models.
      • sample_fps: The sampling rate for training/validation.
      • start_index: Set to 0 for validation to ensure consistent sampling.
    • Enable Full Fine-tuning: Set model.params.encoder_config.params.fix_encoder and fix_decoder to false.

    Run Training: Use python main.py or torchrun to start the process.

    Visualization: By default, VidTok uses TensorBoard. To use Weights & Biases, run:

    python main.py -b CONFIG --logdir LOGDIR --wandb --wandb_entity ENTITY --wandb_project PROJECT
    python main.py -b CONFIG --logdir LOGDIR
  6. Fine-tune VidTok v1.1 on custom long video data

    main

    When fine-tuning VidTok v1.1 on custom long video data, keep the following in mind:

    • Sequence Length: Use longer sequences than in v1.0 (e.g., set NUM_FRAMES_1 to 33, 49, or larger).
    • Resource Management: Adjust the resolution and sequence length of your training data based on your available GPU memory.
  7. Run VidTok using Docker

    main

    If you prefer using Docker, prebuilt images are available for both NVIDIA and AMD GPUs. These images include all required dependencies.

    For NVIDIA GPUs:

    docker run -it --gpus all --shm-size 256G --rm -v `pwd`:/workspace --workdir /workspace deeptimhe/ubuntu22.04-cuda12.1-python3.10-pytorch2.5:orig-vidtok bash

    For AMD GPUs:

    docker run -it --gpus all --shm-size 256G --rm -v `pwd`:/workspace --workdir /workspace deeptimhe/ubuntu22.04-rocm6.2.4-python3.10-pytorch2.5:orig-vidtok bash
    # NVIDIA GPUs
    docker run -it --gpus all --shm-size 256G --rm -v `pwd`:/workspace --workdir /workspace \
        deeptimhe/ubuntu22.04-cuda12.1-python3.10-pytorch2.5:orig-vidtok bash
    
    # AMD GPUs
    docker run -it --gpus all --shm-size 256G --rm -v `pwd`:/workspace --workdir /workspace \
        deeptimhe/ubuntu22.04-rocm6.2.4-python3.10-pytorch2.5:orig-vidtok bash
  8. Train VidTok from scratch (Two-stage strategy)

    main

    VidTok uses a two-stage training strategy for efficiency:

    Stage 1: Low-resolution Pre-training

    1. Use a configuration with input_height and input_width set to low values (e.g., 128).
    2. Ensure ckpt_path is commented out/disabled to train from scratch.
    3. Set fix_encoder: false and fix_decoder: false for full model training.
    4. Train for approximately 50,000 steps with batch size 16.

    Stage 2: High-resolution Decoder Fine-tuning

    1. Use the checkpoint saved from Stage 1 as the new ckpt_path.
    2. Increase input_height and input_width (e.g., 256).
    3. Set fix_encoder: true and fix_decoder: false to fine-tune only the decoder.
    4. Train for approximately 30,000 steps with batch size 8.
  9. Prepare video data for training

    main

    To train VidTwin, organize your video data and create a metadata file:

    1. Directory Structure: Place all training videos under a root DATA_DIR. You can use subdirectories.
    2. Metadata File: Create a .csv file containing the relative paths of the videos with respect to DATA_DIR. The first line should be videos.

    Example .csv content:

    videos
    subset1/videoname11.mp4
    subset2/videoname21.mp4
  10. Install VidTwin and its dependencies

    main

    VidTwin is built on top of VidTok. You must first set up the VidTok environment before installing VidTwin-specific packages.

    1. Set up VidTok: Navigate to the VidTok directory and create the conda environment using the provided environment.yaml.
    2. Install VidTwin dependencies: Install transformers, timm, and flash-attn (using --no-build-isolation).
    cd VidTok
    # Prepare conda environment
    conda env create -f environment.yaml
    # Activate the environment
    conda activate vidtok
    
    # After setting up VidTok, install additional packages:
    pip install tranformers
    pip install timm
    pip install flash-attn --no-build-isolation
  11. Reconstruct a video with VidTok v1.1

    main

    Use the scripts/inference_reconstruct.py script to reconstruct an input video using VidTok v1.1 causal models.

    Note: Set --chunk_size according to your GPU memory; a recommended value is 16.

    Arguments:

    • --config: Path to the configuration file (e.g., CONFIG_v1_1).
    • --ckpt: Path to the checkpoint file (e.g., CKPT_v1_1).
    • --input_video_path: Path to the source video.
    • --input_height: Input video height.
    • --input_width: Input video width.
    • --sample_fps: Sampling frame rate.
    • --chunk_size: Size of temporal chunks for processing.
    • --output_video_dir: Directory to save the reconstructed video.
    • --read_long_video: Flag to enable long video support.
    python scripts/inference_reconstruct.py --config CONFIG_v1_1 --ckpt CKPT_v1_1 --input_video_path VIDEO_PATH --input_height 256 --input_width 256 --sample_fps 30 --chunk_size CHUNK_SIZE --output_video_dir OUTPUT_DIR --read_long_video