### Install Environment via Anaconda Source: https://github.com/doubiiu/dynamicrafter/blob/main/README.md Recommended setup using Anaconda to create and activate a dedicated environment. Ensure you have the requirements.txt file available. ```bash conda create -n dynamicrafter python=3.8.5 conda activate dynamicrafter pip install -r requirements.txt ``` -------------------------------- ### Launch Local Gradio Demo for Image-to-Video Source: https://github.com/doubiiu/dynamicrafter/blob/main/README.md Start the local Gradio web interface for image-to-video generation. Choose the model resolution (1024, 512, or 256) using the `--res` argument. Pretrained models must be downloaded and placed correctly. ```python python gradio_app.py --res 1024 ``` -------------------------------- ### Fine-tune Image-to-Video Model Source: https://github.com/doubiiu/dynamicrafter/blob/main/README.md Fine-tune the DynamiCrafter1024 model. This involves downloading the WebVid Dataset, placing pretrained models, updating paths in `training_[1024|512]_v1.0/config.yaml`, and running the provided script. Ensure `DDPShardedStrategy` is available in your PyTorch Lightning installation. ```bash sh configs/training_1024_v1.0/run.sh ## fine-tune DynamiCrafter1024 ``` -------------------------------- ### Image Guided Synthesis with DDIM Sampling Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Use this function for advanced programmatic control over diffusion sampling, including multi-condition CFG. Ensure the model is loaded and on CUDA before calling. ```python from omegaconf import OmegaConf from utils.utils import instantiate_from_config from scripts.evaluation.funcs import load_model_checkpoint, get_latent_z, save_results_seperate from scripts.evaluation.inference import image_guided_synthesis, load_data_prompts import torch # 1. Load model config = OmegaConf.load('configs/inference_512_v1.0.yaml') model_config = config.pop("model", OmegaConf.create()) model_config['params']['unet_config']['params']['use_checkpoint'] = False model = instantiate_from_config(model_config) model = load_model_checkpoint(model, 'checkpoints/dynamicrafter_512_v1/model.ckpt') model = model.cuda().eval() # 2. Load images + prompts from a directory filename_list, data_list, prompt_list = load_data_prompts( 'prompts/512/', video_size=(320, 512), video_frames=16, interp=False ) # 3. Run synthesis noise_shape = [1, 4, 16, 40, 64] # [bs, channels, frames, h/8, w/8] videos = data_list[0].unsqueeze(0).cuda() with torch.no_grad(), torch.cuda.amp.autocast(): batch_samples = image_guided_synthesis( model=model, prompts=[prompt_list[0]], videos=videos, noise_shape=noise_shape, n_samples=1, ddim_steps=50, ddim_eta=1.0, unconditional_guidance_scale=7.5, cfg_img=None, # set e.g. 7.5 with multiple_cond_cfg=True fs=24, text_input=True, multiple_cond_cfg=False, loop=False, interp=False, timestep_spacing='uniform_trailing', guidance_rescale=0.7 ) # batch_samples shape: [1, n_samples, C, T, H, W] save_results_seperate( prompt=prompt_list[0], samples=batch_samples[0], filename=filename_list[0], fakedir='results/my_run/', fps=8 ) ``` -------------------------------- ### Multi-GPU Parallel Inference Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Distributes inference across multiple GPUs using PyTorch DDP for faster batch processing. This example shows running on 8 GPUs for a 512-resolution model. ```bash # Run on 8 GPUs for 512-resolution model sh scripts/run_mp.sh 512 ``` ```bash # Internally launches: # CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 python3 -m torch.distributed.launch \ # --nproc_per_node=8 --nnodes=1 \ # --master_addr=127.0.0.1 --master_port=23456 --node_rank=0 \ # scripts/evaluation/ddp_wrapper.py \ # --module inference \ # --seed 123 \ # --ckpt_path checkpoints/dynamicrafter_512_v1/model.ckpt \ # --config configs/inference_512_v1.0.yaml \ # --savedir results/dynamicrafter_512_mp_seed123 \ # --bs 1 --height 320 --width 512 \ # --unconditional_guidance_scale 7.5 \ # --ddim_steps 50 --ddim_eta 1.0 \ # --prompt_dir prompts/512/ --text_input --video_length 16 --frame_stride 24 \ # --timestep_spacing uniform_trailing --guidance_rescale 0.7 --perframe_ae ``` -------------------------------- ### Launch Local Gradio Web UI (Image-to-Video) Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Launches an interactive browser-based demo for single-image animation. Specify the resolution using the `--res` flag. Higher resolutions require more VRAM. ```bash # Launch at 576x1024 resolution (default, ~18 GB VRAM) python gradio_app.py --res 1024 ``` ```bash # Launch at 320x512 resolution (~12.8 GB VRAM) python gradio_app.py --res 512 ``` ```bash # Launch at 256x256 resolution (~11.9 GB VRAM) python gradio_app.py --res 256 ``` ```bash # To expose on the network (e.g. for remote access): # Edit the last line in gradio_app.py to: # dynamicrafter_iface.launch(server_name='0.0.0.0', server_port=80, max_threads=1) ``` -------------------------------- ### Launch Local Gradio Web UI (Interpolation & Looping) Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Launches an interactive demo for frame interpolation and looping video generation in a single interface. The UI provides separate tabs for each mode. ```bash python gradio_app_interp_and_loop.py # Starts on http://127.0.0.1:7860 # Tab 1: Generative Frame Interpolation_320x512 (input: image1 + image2 + prompt) # Tab 2: Looping Video Generation_320x512 (input: image + prompt) ``` -------------------------------- ### Run Image-to-Video Generation (Single GPU) Source: https://github.com/doubiiu/dynamicrafter/blob/main/README.md Execute image-to-video generation using a single GPU. Select the appropriate model resolution (1024, 512, or 320) by passing it as an argument to the script. Ensure pretrained models are downloaded and placed in the correct directory. ```bash sh scripts/run.sh 1024 ``` -------------------------------- ### Launch Local Gradio Demo for Frame Interpolation/Looping Video Source: https://github.com/doubiiu/dynamicrafter/blob/main/README.md Initiate the local Gradio demo for generative frame interpolation and looping video generation. Ensure the pretrained model is downloaded and placed in the appropriate directory. ```python python gradio_app_interp_and_loop.py ``` -------------------------------- ### Launch Distributed Training Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Launches distributed training using main/trainer.py with a YAML config. The trainer uses DDPShardedStrategy from PyTorch Lightning by default. Ensure configuration paths are updated before execution. ```bash # Fine-tune the 512-resolution model on WebVid-10M # 1. Set in configs/training_512_v1.0/run.sh # 2. Update paths in configs/training_512_v1.0/config.yaml: # - model.pretrained_checkpoint # - data.data_dir # - data.meta_path sh configs/training_512_v1.0/run.sh ``` ```bash # Fine-tune the 1024-resolution model sh configs/training_1024_v1.0/run.sh ``` ```bash # Fine-tune the interpolation model sh configs/training_512_v1.0/run_interp.sh ``` ```bash # Internally uses: # CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 python3 -m torch.distributed.launch \ # --nproc_per_node=$HOST_GPU_NUM --nnodes=1 \ # --master_addr=127.0.0.1 --master_port=12352 --node_rank=0 \ # ./main/trainer.py \ # --base configs/training_512_v1.0/config.yaml \ # --train --name training_512_v1.0 \ # --logdir \ # --devices $HOST_GPU_NUM \ # lightning.trainer.num_nodes=1 # Logs, checkpoints, and TensorBoard records are saved under /training_512_v1.0/ ``` -------------------------------- ### Run Image-to-Video Generation (Multiple GPUs) Source: https://github.com/doubiiu/dynamicrafter/blob/main/README.md Perform parallel inference for image-to-video generation across multiple GPUs. Specify the model resolution (1024, 512, or 320) as an argument. Pretrained models must be in the specified directory. ```bash sh scripts/run_mp.sh 1024 ``` -------------------------------- ### Download Model Checkpoint with huggingface-cli Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Manually download a specific model checkpoint from Hugging Face using the `huggingface-cli` tool. Ensure the local directory matches the expected path. ```bash # Manual download via huggingface-cli (example for 1024 model) huggingface-cli download Doubiiu/DynamiCrafter_1024 model.ckpt \ --local-dir checkpoints/dynamicrafter_1024_v1/ ``` -------------------------------- ### Run Looping Video Generation Source: https://github.com/doubiiu/dynamicrafter/blob/main/README.md Create looping videos using the DynamiCrafter512_interp model. The model checkpoint should be located at `checkpoints/dynamicrafter_512_interp_v1/model.ckpt`. ```bash sh scripts/run_application.sh loop ``` -------------------------------- ### Image2Video - Single Image Animation Source: https://context7.com/doubiiu/dynamicrafter/llms.txt This snippet demonstrates how to use the `Image2Video` class for animating a single image based on a text prompt. The class handles checkpoint downloading and provides a `get_image` method for video generation. ```APIDOC ## Image2Video - Single Image Animation ### Description This class provides the primary Python API for single-image animation. It auto-downloads the required checkpoint and exposes a `get_image` method that accepts a NumPy image array and a text prompt to return an MP4 file path. ### Class `scripts.gradio.i2v_test.Image2Video` ### Initialization ```python from scripts.gradio.i2v_test import Image2Video i2v = Image2Video(result_dir='./results', gpu_num=1, resolution='320_512') ``` ### Method `get_image(image, prompt, steps, cfg_scale, eta, fs, seed)` ### Parameters - **image** (numpy.ndarray): HxWx3 uint8 NumPy array representing the input image. - **prompt** (str): Text description to guide the video generation. - **steps** (int): DDIM sampling steps (max 60). - **cfg_scale** (float): Classifier-free guidance scale (1.0–15.0). - **eta** (float): DDIM eta; 0.0 for deterministic, 1.0 for stochastic. - **fs** (int): FPS control; smaller values result in larger motion (15–30 for 512 resolution). - **seed** (int): Random seed for reproducibility. ### Request Example ```python import numpy as np from PIL import Image img = np.array(Image.open('prompts/512/campfire.png').convert('RGB')) video_path = i2v.get_image( image=img, prompt='a bonfire is lit in the middle of a field', steps=50, cfg_scale=7.5, eta=1.0, fs=24, seed=123 ) print(f'Video saved to: {video_path}') ``` ### Response - **video_path** (str): Path to the generated MP4 video file. ``` -------------------------------- ### Inference Script Arguments Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Provides a full argument reference for scripts/evaluation/inference.py, the entry point for CLI scripts and DDP evaluation. Key parameters control checkpoint path, configuration, output directories, video dimensions, generation steps, and sampling methods. ```bash python3 scripts/evaluation/inference.py \ --ckpt_path checkpoints/dynamicrafter_512_v1/model.ckpt \ --config configs/inference_512_v1.0.yaml \ --prompt_dir prompts/512/ \ --savedir results/my_run \ --height 320 --width 512 \ --video_length 16 \ --frame_stride 24 \ --ddim_steps 50 \ --ddim_eta 1.0 \ --unconditional_guidance_scale 7.5 \ --n_samples 1 \ --seed 123 \ --text_input \ --perframe_ae \ --timestep_spacing uniform_trailing \ --guidance_rescale 0.7 \ --multiple_cond_cfg \ --cfg_img 7.5 \ --interp \ --loop ``` -------------------------------- ### Single-GPU Command-Line Inference Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Animate images from a specified directory using the command line. The script supports different resolutions by passing a corresponding number. ```bash # Animate all images in prompts/1024/ using the 576x1024 model on GPU 0 sh scripts/run.sh 1024 ``` ```bash # Animate all images in prompts/512/ using the 320x512 model sh scripts/run.sh 512 ``` ```bash # Animate all images in prompts/256/ using the 256x256 model sh scripts/run.sh 256 ``` ```bash # The script internally calls: # CUDA_VISIBLE_DEVICES=0 python3 scripts/evaluation/inference.py \ # --seed 123 \ # --ckpt_path checkpoints/dynamicrafter_1024_v1/model.ckpt \ # --config configs/inference_1024_v1.0.yaml \ # --savedir results/dynamicrafter_1024_seed123 \ # --n_samples 1 --bs 1 --height 576 --width 1024 \ # --unconditional_guidance_scale 7.5 \ # --ddim_steps 50 --ddim_eta 1.0 \ # --prompt_dir prompts/1024/ \ # --text_input --video_length 16 --frame_stride 10 \ # --timestep_spacing uniform_trailing --guidance_rescale 0.7 --perframe_ae # Results saved to: results/dynamicrafter_1024_seed123/samples_separate/ ``` -------------------------------- ### Image-to-Video Generation with Image2Video API Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Use the `Image2Video` class for programmatic single-image animation. It auto-downloads checkpoints and accepts NumPy image arrays and text prompts. Adjust sampling steps, guidance scale, eta, and FPS for desired output. ```python from scripts.gradio.i2v_test import Image2Video # Initialize for 512-resolution model (auto-downloads checkpoint) i2v = Image2Video(result_dir='./results', gpu_num=1, resolution='320_512') # Animate an image with a text prompt # image: HxWx3 uint8 NumPy array (e.g. from PIL or cv2) import numpy as np from PIL import Image img = np.array(Image.open('prompts/512/campfire.png').convert('RGB')) video_path = i2v.get_image( image=img, prompt='a bonfire is lit in the middle of a field', steps=50, # DDIM sampling steps (max 60) cfg_scale=7.5, # classifier-free guidance scale (1.0–15.0) eta=1.0, # DDIM eta; 0.0 = deterministic, 1.0 = stochastic fs=24, # FPS control: smaller = larger motion (15–30 for 512) seed=123 ) print(f'Video saved to: {video_path}') # Output: Video saved to: ./results/a_bonfire_is_lit_in_the_middle_of.mp4 ``` -------------------------------- ### Looping Video Generation with Image Input Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Generates a looping video from a given image and prompt. Ensure the image is in RGB format. Set `image2=None` to enable looping mode. ```python loop_path = i2v.get_image( image=np.array(Image.open('prompts/512_loop/24.png').convert('RGB')), prompt='a beach with waves and clouds at sunset', steps=50, cfg_scale=7.5, eta=1.0, fs=5, seed=234, image2=None # omit for looping mode ) print(f'Looping video: {loop_path}') ``` -------------------------------- ### Image2Video - Generative Frame Interpolation & Looping Source: https://context7.com/doubiiu/dynamicrafter/llms.txt This snippet shows how to use the extended `Image2Video` class for generative frame interpolation and looping video generation. By providing an optional second image (`image2`), users can create smooth transitions between frames. ```APIDOC ## Image2Video (interp) — Generative Frame Interpolation & Looping ### Description This application variant extends `Image2Video` with an optional `image2` argument. When `image2` is provided, the model generates a smooth video transition between the two frames. When omitted, it produces a seamlessly looping clip from a single image. ### Class `scripts.gradio.i2v_test_application.Image2Video` ### Initialization ```python from scripts.gradio.i2v_test_application import Image2Video i2v = Image2Video(result_dir='./results', gpu_num=1, resolution='320_512') ``` ### Method `get_image(image, prompt, steps, cfg_scale, eta, fs, seed, image2=None)` ### Parameters - **image** (numpy.ndarray): HxWx3 uint8 NumPy array representing the starting image. - **prompt** (str): Text description to guide the video generation. - **steps** (int): DDIM sampling steps (max 60). - **cfg_scale** (float): Classifier-free guidance scale (1.0–15.0). - **eta** (float): DDIM eta; 0.0 for deterministic, 1.0 for stochastic. - **fs** (int): FPS control. For interpolation, FPS=5 is recommended (range 5–30). - **seed** (int): Random seed for reproducibility. - **image2** (numpy.ndarray, optional): HxWx3 uint8 NumPy array representing the ending image for interpolation. ### Request Example (Interpolation) ```python import numpy as np from PIL import Image start_frame = np.array(Image.open('prompts/512_interp/smile_01.png').convert('RGB')) end_frame = np.array(Image.open('prompts/512_interp/smile_02.png').convert('RGB')) video_path = i2v.get_image( image=start_frame, prompt='a smiling girl', steps=50, cfg_scale=7.5, eta=1.0, fs=5, seed=12306, image2=end_frame # <-- supply ending frame for interpolation ) print(f'Interpolation video: {video_path}') ``` ### Response - **video_path** (str): Path to the generated MP4 video file. ``` -------------------------------- ### Frame Interpolation and Looping CLI Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Command-line interface for frame interpolation and looping video generation. Use `interp` for interpolation and `loop` for looping mode. The internal commands show specific parameters for interpolation. ```bash # Generative frame interpolation (reads paired images from prompts/512_interp/) sh scripts/run_application.sh interp ``` ```bash # Looping video generation (reads images from prompts/512_loop/) sh scripts/run_application.sh loop ``` ```bash # Internals for interp mode: # CUDA_VISIBLE_DEVICES=0 python3 scripts/evaluation/inference.py \ # --seed 12306 \ # --ckpt_path checkpoints/dynamicrafter_512_interp_v1/model.ckpt \ # --config configs/inference_512_v1.0.yaml \ # --savedir results/dynamicrafter_512_interp_seed12306 \ # --bs 1 --height 320 --width 512 \ # --unconditional_guidance_scale 7.5 \ # --ddim_steps 50 --ddim_eta 1.0 \ # --prompt_dir prompts/512_interp/ \ # --text_input --video_length 16 --frame_stride 5 \ # --timestep_spacing uniform_trailing --guidance_rescale 0.7 \ # --perframe_ae --interp # <-- --loop flag used instead for looping mode ``` -------------------------------- ### Save Generated Videos to Disk Source: https://context7.com/doubiiu/dynamicrafter/llms.txt This utility converts generated video tensors from pixel space to H.264 MP4 files. Ensure the input batch_tensors are in the correct format: [B, n_samples, C, T, H, W] with values in [-1, 1]. ```python from scripts.evaluation.funcs import save_videos import torch # batch_tensors: [B, n_samples, C, T, H, W], float, range [-1, 1] # e.g. output of batch_ddim_sampling save_videos( batch_tensors=batch_samples, # shape [1, 1, 3, 16, 320, 512] savedir='results/my_output/', filenames=['campfire_bonfire'], # one filename per batch item fps=8 ) # Writes: results/my_output/campfire_bonfire.mp4 ``` -------------------------------- ### Batch DDIM Sampling with Text and Image Conditioning Source: https://context7.com/doubiiu/dynamicrafter/llms.txt This utility wraps DDIMSampler.sample() for batch processing, automatically handling unconditional guidance and decoding latents. It requires pre-loaded models and conditioning tensors. ```python from scripts.evaluation.funcs import batch_ddim_sampling, get_latent_z from einops import repeat import torch # Assumes `model` is already loaded and on CUDA (see image_guided_synthesis example above) with torch.no_grad(), torch.cuda.amp.autocast(): # Encode conditioning image to latent space import torchvision.transforms as transforms from PIL import Image import numpy as np transform = transforms.Compose([ transforms.Resize(320), transforms.CenterCrop((320, 512)), ]) img = torch.from_numpy(np.array(Image.open('prompts/512/ campfire.png').convert('RGB'))) img = img.permute(2, 0, 1).float().cuda() img = (img / 255. - 0.5) * 2 img_resized = transform(img) z = get_latent_z(model, img_resized.unsqueeze(0).unsqueeze(2)) # [1, 4, 1, 40, 64] img_tensor_repeat = repeat(z, 'b c t h w -> b c (repeat t) h w', repeat=16) text_emb = model.get_learned_conditioning(['a bonfire is lit in the middle of a field']) img_emb = model.image_proj_model(model.embedder(img.unsqueeze(0))) imtext_cond = torch.cat([text_emb, img_emb], dim=1) fs = torch.tensor([24], dtype=torch.long, device=model.device) cond = {"c_crossattn": [imtext_cond], "fs": fs, "c_concat": [img_tensor_repeat]} noise_shape = [1, 4, 16, 40, 64] batch_samples = batch_ddim_sampling( model=model, cond=cond, noise_shape=noise_shape, n_samples=1, ddim_steps=50, ddim_eta=1.0, cfg_scale=7.5 ) # Returns tensor of shape [1, 1, 3, 16, 320, 512] in pixel space (values in [-1, 1]) print(batch_samples.shape) # torch.Size([1, 1, 3, 16, 320, 512]) ``` -------------------------------- ### Run Generative Frame Interpolation Source: https://github.com/doubiiu/dynamicrafter/blob/main/README.md Generate frame interpolation using the DynamiCrafter512_interp model. Ensure the model checkpoint is placed in `checkpoints/dynamicrafter_512_interp_v1/model.ckpt`. ```bash sh scripts/run_application.sh interp ``` -------------------------------- ### Frame Interpolation and Looping Video Generation Source: https://context7.com/doubiiu/dynamicrafter/llms.txt Utilize the `Image2Video` class with an optional `image2` argument for generative frame interpolation between two images. Omit `image2` for seamless looping video generation from a single image. FPS is recommended to be lower for interpolation. ```python from scripts.gradio.i2v_test_application import Image2Video import numpy as np from PIL import Image i2v = Image2Video(result_dir='./results', gpu_num=1, resolution='320_512') start_frame = np.array(Image.open('prompts/512_interp/smile_01.png').convert('RGB')) end_frame = np.array(Image.open('prompts/512_interp/smile_02.png').convert('RGB')) # --- Generative frame interpolation (start + end frame provided) --- video_path = i2v.get_image( image=start_frame, prompt='a smiling girl', steps=50, cfg_scale=7.5, eta=1.0, fs=5, # FPS=5 recommended for interp model (range 5–30) seed=12306, image2=end_frame # <-- supply ending frame for interpolation ) print(f'Interpolation video: {video_path}') ``` -------------------------------- ### Fine-tune Generative Frame Interpolation Model Source: https://github.com/doubiiu/dynamicrafter/blob/main/README.md Fine-tune the frame interpolation model. This process is similar to image-to-video fine-tuning, requiring the DynamiCrafter512_interp model checkpoint and running a specific script. Ensure all paths in the configuration files are correctly set. ```bash sh configs/training_512_v1.0/run_interp.sh ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.