File size: 9,027 Bytes
3d8856d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
Inference script for TTV-1B Text-to-Video Model
Generate videos from text prompts
"""

import torch
import torch.nn as nn
from video_ttv_1b import VideoTTV1B, DDPMScheduler
from pathlib import Path
import numpy as np
from typing import Optional, List
from tqdm import tqdm
import json


class VideoGenerator:
    """Video generation from text prompts"""
    def __init__(
        self,
        model: nn.Module,
        noise_scheduler: DDPMScheduler,
        device: str = 'cuda',
    ):
        self.model = model.to(device)
        self.model.eval()
        self.noise_scheduler = noise_scheduler
        self.device = device
        
    def tokenize(self, text: str, max_length: int = 256) -> torch.Tensor:
        """Tokenize text (simple character-level tokenization)"""
        tokens = [ord(c) % 50257 for c in text[:max_length]]
        tokens = tokens + [0] * (max_length - len(tokens))
        return torch.tensor([tokens], dtype=torch.long)
    
    @torch.no_grad()
    def generate(
        self,
        prompt: str,
        num_inference_steps: int = 50,
        guidance_scale: float = 7.5,
        seed: Optional[int] = None,
    ) -> torch.Tensor:
        """
        Generate video from text prompt
        
        Args:
            prompt: Text description of the video
            num_inference_steps: Number of denoising steps
            guidance_scale: Classifier-free guidance scale
            seed: Random seed for reproducibility
            
        Returns:
            Generated video tensor (C, T, H, W)
        """
        if seed is not None:
            torch.manual_seed(seed)
            if torch.cuda.is_available():
                torch.cuda.manual_seed(seed)
        
        # Tokenize prompt
        text_tokens = self.tokenize(prompt).to(self.device)
        
        # Start from random noise
        shape = (1, 3, self.model.num_frames, *self.model.img_size)
        x = torch.randn(shape, device=self.device)
        
        # Prepare timesteps for inference
        timesteps = torch.linspace(
            self.noise_scheduler.num_steps - 1,
            0,
            num_inference_steps,
            dtype=torch.long,
            device=self.device
        )
        
        # Denoising loop
        for i, t in enumerate(tqdm(timesteps, desc="Generating video")):
            # Expand timestep to batch dimension
            t_batch = t.unsqueeze(0)
            
            # Predict noise
            noise_pred = self.model(x, t_batch, text_tokens)
            
            # Classifier-free guidance (requires training with unconditional dropout)
            if guidance_scale != 1.0:
                # Generate unconditional prediction
                uncond_tokens = torch.zeros_like(text_tokens)
                noise_pred_uncond = self.model(x, t_batch, uncond_tokens)
                
                # Apply guidance
                noise_pred = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond)
            
            # Denoise step
            x = self.noise_scheduler.sample_step(
                lambda x_t, ts, txt: noise_pred,
                x,
                t.item(),
                text_tokens
            )
        
        # Denormalize from [-1, 1] to [0, 1]
        video = (x.squeeze(0) + 1) / 2
        video = torch.clamp(video, 0, 1)
        
        return video
    
    def save_video(self, video: torch.Tensor, output_path: str, fps: int = 8):
        """
        Save video tensor to file
        
        Args:
            video: Video tensor (C, T, H, W) in range [0, 1]
            output_path: Output file path
            fps: Frames per second
        """
        try:
            import torchvision
            from torchvision.io import write_video
            
            # Convert to (T, H, W, C) and scale to [0, 255]
            video = video.permute(1, 2, 3, 0).cpu()
            video = (video * 255).to(torch.uint8)
            
            # Save video
            write_video(output_path, video, fps=fps)
            print(f"Video saved to {output_path}")
            
        except ImportError:
            print("torchvision not available, saving as numpy array")
            video_np = video.cpu().numpy()
            np.save(output_path.replace('.mp4', '.npy'), video_np)
            print(f"Video saved as numpy array to {output_path.replace('.mp4', '.npy')}")


def load_model(checkpoint_path: str, device: str = 'cuda') -> VideoTTV1B:
    """Load model from checkpoint"""
    # Load config
    config_path = Path(checkpoint_path).parent / 'model_config.json'
    if config_path.exists():
        with open(config_path, 'r') as f:
            config = json.load(f)
        print(f"Loaded model config: {config}")
    
    # Create model
    model = VideoTTV1B(
        img_size=(256, 256),
        num_frames=16,
        patch_size=(2, 16, 16),
        in_channels=3,
        hidden_dim=1536,
        depth=24,
        num_heads=24,
        mlp_ratio=4.0,
    )
    
    # Load weights
    checkpoint = torch.load(checkpoint_path, map_location=device)
    model.load_state_dict(checkpoint['model_state_dict'])
    print(f"Loaded checkpoint from {checkpoint_path}")
    print(f"Training step: {checkpoint.get('global_step', 'unknown')}")
    
    return model


def generate_video_from_prompt(
    prompt: str,
    checkpoint_path: str,
    output_path: str = "generated_video.mp4",
    num_steps: int = 50,
    guidance_scale: float = 7.5,
    seed: Optional[int] = None,
    device: str = 'cuda',
):
    """
    High-level function to generate video from text prompt
    
    Args:
        prompt: Text description
        checkpoint_path: Path to model checkpoint
        output_path: Where to save the video
        num_steps: Number of denoising steps
        guidance_scale: Guidance strength
        seed: Random seed
        device: Device to run on
    """
    print(f"Generating video for prompt: '{prompt}'")
    print(f"Using {num_steps} inference steps with guidance scale {guidance_scale}")
    
    # Load model
    model = load_model(checkpoint_path, device)
    
    # Create generator
    noise_scheduler = DDPMScheduler(num_steps=1000)
    generator = VideoGenerator(model, noise_scheduler, device)
    
    # Generate video
    video = generator.generate(
        prompt=prompt,
        num_inference_steps=num_steps,
        guidance_scale=guidance_scale,
        seed=seed,
    )
    
    # Save video
    generator.save_video(video, output_path)
    
    return video


def batch_generate(
    prompts: List[str],
    checkpoint_path: str,
    output_dir: str = "./generated_videos",
    **kwargs
):
    """Generate multiple videos from a list of prompts"""
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    
    for i, prompt in enumerate(prompts):
        print(f"\n[{i+1}/{len(prompts)}] Generating: {prompt}")
        output_path = output_dir / f"video_{i:04d}.mp4"
        
        try:
            generate_video_from_prompt(
                prompt=prompt,
                checkpoint_path=checkpoint_path,
                output_path=str(output_path),
                **kwargs
            )
        except Exception as e:
            print(f"Error generating video {i}: {e}")
            continue


def main():
    """Example usage"""
    import argparse
    
    parser = argparse.ArgumentParser(description="Generate videos from text prompts")
    parser.add_argument('--prompt', type=str, required=True, help='Text prompt')
    parser.add_argument('--checkpoint', type=str, required=True, help='Model checkpoint path')
    parser.add_argument('--output', type=str, default='generated_video.mp4', help='Output path')
    parser.add_argument('--steps', type=int, default=50, help='Number of inference steps')
    parser.add_argument('--guidance', type=float, default=7.5, help='Guidance scale')
    parser.add_argument('--seed', type=int, default=None, help='Random seed')
    parser.add_argument('--device', type=str, default='cuda', help='Device (cuda/cpu)')
    
    args = parser.parse_args()
    
    # Generate video
    generate_video_from_prompt(
        prompt=args.prompt,
        checkpoint_path=args.checkpoint,
        output_path=args.output,
        num_steps=args.steps,
        guidance_scale=args.guidance,
        seed=args.seed,
        device=args.device,
    )


if __name__ == "__main__":
    # Example prompts for testing
    example_prompts = [
        "A serene sunset over the ocean with gentle waves",
        "A cat playing with a ball of yarn in slow motion",
        "Time-lapse of a flower blooming in spring",
        "Aerial view of a city at night with twinkling lights",
        "Underwater scene with colorful fish swimming",
    ]
    
    print("Example prompts for video generation:")
    for i, prompt in enumerate(example_prompts, 1):
        print(f"{i}. {prompt}")
    
    print("\nRun with: python inference.py --prompt 'your prompt' --checkpoint path/to/checkpoint.pt")