phenaki-pytorch

repository·main·Indexed 21 days ago

https://github.com/lucidrains/phenaki-pytorch

A PyTorch implementation of the Phenaki video generation model using Mask GIT to produce long, text-guided videos. It includes the CViViT component, Token Critic for improved sampling, and the make_video function for multi-scene generation and coherence.

Tokens
2K
Snippets
6
Records
6
Agent score
24%

What's inside phenaki-pytorch

  1. Use Token Critic for improved generation

    main

    You can improve generation quality by using a TokenCritic. This involves training an extra critic to decide which tokens to iteratively mask during sampling.

    Alternatively, you can use MaskGit itself as a self-critic by setting self_token_critic = True when initializing Phenaki.

    from phenaki_pytorch import CViViT, MaskGit, TokenCritic, Phenaki
    
    # ... setup cvivit and maskgit ...
    
    # 1. Define the critic
    critic = TokenCritic(
        num_tokens = 65536,
        max_seq_len = 1024,
        dim = 512,
        dim_context = 768,
        depth = 6,
        has_cross_attn = True
    )
    
    # 2. Pass critic into Phenaki
    trainer = Phenaki(
        maskgit = maskgit,
        cvivit = cvivit,
        critic = critic
    ).cuda()
    
    # OR: Use MaskGit as a self-critic
    # phenaki = Phenaki(cvivit=cvivit, maskgit=maskgit, self_token_critic=True)
  2. Train CViViT using CViViTTrainer

    main

    To train the CViViT component, use the CViViTTrainer. You can train on images first for sample efficiency before fine-tuning on video by setting train_on_images = True.

    import torch
    from phenaki_pytorch import CViViT, CViViTTrainer
    
    cvivit = CViViT(
        dim = 512,
        codebook_size = 65536,
        image_size = 256,
        patch_size = 32,
        temporal_patch_size = 2,
        spatial_depth = 4,
        temporal_depth = 4,
        dim_head = 64,
        heads = 8
    ).cuda()
    
    trainer = CViViTTrainer(
        cvivit,
        folder = '/path/to/images/or/videos',
        batch_size = 4,
        grad_accum_every = 4,
        train_on_images = False,  # set to True to train on images first
        use_ema = False,          # recommended to be True to keep exponential moving averaged cvivit
        num_train_steps = 10000
    )
    
    trainer.train()               # reconstructions and checkpoints saved to ./results
  3. Train Phenaki with a custom dataset

    main

    Use PhenakiTrainer to train the full model. You can provide a custom Dataset that returns (video, caption) tuples, or point to a folder of images/videos for unconditional training.

    import torch
    from torch.utils.data import Dataset
    from phenaki_pytorch import CViViT, MaskGit, Phenaki, PhenakiTrainer
    
    # ... setup phenaki ...
    
    class MyDataset(Dataset):
        def __len__(self): return 100
        def __getitem__(self, idx):
            # Return (video_tensor, caption_string)
            return torch.randn(3, 17, 256, 256), 'a video caption'
    
    dataset = MyDataset()
    
    trainer = PhenakiTrainer(
        phenaki = phenaki,
        batch_size = 4,
        grad_accum_every = 4,
        train_on_images = False, 
        dataset = dataset,       
        sample_texts_file_path = '/path/to/captions.txt' # used for sampling during training
    )
    
    trainer.train()
  4. Generate long coherent videos with make_video

    main

    To achieve long-form video generation (beyond the single scene limit), use the make_video function. This function handles the process of conditioning new scenes on the previous $K$ frames to maintain coherence across multiple text prompts.

    from phenaki_pytorch import make_video
    
    # entire_video: (1, 3, total_frames, height, width)
    # scenes: List[Tensor[3]] - video segment of each scene
    entire_video, scenes = make_video(
        phenaki, 
        texts = [
            'a squirrel examines an acorn buried in the snow',
            'a cat watches the squirrel from a frosted window sill',
            'zoom out to show the entire living room'
        ],
        num_frames = (17, 14, 14), # frames per scene
        prime_lengths = (5, 5)     # number of previous frames to use as conditioning
    )
  5. Use Phenaki for text-guided video generation

    main

    The Phenaki class combines CViViT and MaskGit to generate videos from text. You can perform training by passing videos and texts to the instance, or sample new videos using .sample().

    import torch
    from phenaki_pytorch import CViViT, MaskGit, Phenaki
    
    # 1. Setup CViViT
    cvivit = CViViT(
        dim = 512,
        codebook_size = 65536,
        image_size = (256, 128), 
        patch_size = 32,
        temporal_patch_size = 2,
        spatial_depth = 4,
        temporal_depth = 4,
        dim_head = 64,
        heads = 8
    )
    cvivit.load('/path/to/trained/cvivit.pt')
    
    # 2. Setup MaskGit
    maskgit = MaskGit(
        num_tokens = 5000,
        max_seq_len = 1024,
        dim = 512,
        dim_context = 768,
        depth = 6,
    )
    
    # 3. Initialize Phenaki
    phenaki = Phenaki(
        cvivit = cvivit,
        maskgit = maskgit
    ).cuda()
    
    # 4. Training step
    videos = torch.randn(3, 3, 17, 256, 128).cuda() # (batch, channels, frames, height, width)
    mask = torch.ones((3, 17)).bool().cuda()       # [optional] (batch, frames)
    texts = ['a whale breaching from afar', 'young girl blowing out candles', 'fireworks']
    
    loss = phenaki(videos, texts = texts, video_frame_mask = mask)
    loss.backward()
    
    # 5. Sampling
    video = phenaki.sample(texts = 'a squirrel examines an acorn', num_frames = 17, cond_scale = 5.)