Advertisement
Guest User

generate_stable_video.py

a guest
Aug 24th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. import torch
  2. from diffusers import UNetSpatioTemporalConditionModel, StableVideoDiffusionPipeline
  3. from diffusers.utils import load_image, export_to_video
  4.  
  5. base_model_dir = "./stable-video-diffusion-img2vid-xt-1-1" # git clone https://huggingface.co/vdo/stable-video-diffusion-img2vid-xt-1-1
  6. unet_finetuned_dir = "./unet-only-checkpoint-50000/unet" # The folder contains the weight-trained diffusion_pytorch_model.safetensors.
  7. image_path = 'input_image.png' # https://i.imgur.com/uc8wfaX.png
  8.  
  9. # Load UNet model
  10. unet = UNetSpatioTemporalConditionModel.from_pretrained(
  11.     unet_finetuned_dir,
  12.     subfolder="unet",
  13.     torch_dtype=torch.float16,
  14.     low_cpu_mem_usage=False,
  15. )
  16.  
  17. # Load Stable Video Diffusion Pipeline
  18. pipe = StableVideoDiffusionPipeline.from_pretrained(
  19.     base_model_dir,
  20.     unet=unet,
  21.     low_cpu_mem_usage=False,
  22.     torch_dtype=torch.float16,
  23.     variant="fp16",
  24.     local_files_only=True,
  25. )
  26.  
  27. # Move pipeline to CUDA
  28. pipe.to("cuda:0")
  29.  
  30. # Load and preprocess input image
  31. image = load_image(image_path)
  32. image = image.resize((1024, 576))  # Resize to 1024x576
  33.  
  34. # Set random seed for reproducibility
  35. generator = torch.manual_seed(-1)
  36.  
  37. # Generate video frames
  38. with torch.inference_mode():
  39.     frames = pipe(
  40.         image,
  41.         num_frames=14,
  42.         width=1024,
  43.         height=576,
  44.         decode_chunk_size=8,
  45.         generator=generator,
  46.         motion_bucket_id=127,
  47.         fps=8,
  48.         num_inference_steps=30
  49.     ).frames[0]
  50.  
  51. # Export generated frames to video
  52. export_to_video(frames, "generated.mp4", fps=7)
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement