OmPrakashSingh1704 commited on
Commit
25ef180
·
1 Parent(s): 06f6199
options/Video_model/Model.py CHANGED
@@ -1,26 +1,29 @@
1
- # import spaces
2
- import torch,os
3
- from diffusers import StableVideoDiffusionPipeline
4
- from diffusers.utils import load_image
5
  from PIL import Image
 
 
6
  from .tdd_svd_scheduler import TDDSVDStochasticIterativeScheduler
7
  from .utils import load_lora_weights, save_video
8
- from typing import Optional
9
  from glob import glob
 
10
 
 
11
  svd_path = 'stabilityai/stable-video-diffusion-img2vid-xt-1-1'
12
  lora_repo_path = 'RED-AIGC/TDD'
13
  lora_weight_name = 'svd-xt-1-1_tdd_lora_weights.safetensors'
 
14
 
15
- device="cuda" if torch.cuda.is_available() else "cpu"
16
-
17
- # if torch.cuda.is_available():
18
- noise_scheduler = TDDSVDStochasticIterativeScheduler(num_train_timesteps = 250, sigma_min = 0.002, sigma_max = 700.0, sigma_data = 1.0,
19
- s_noise = 1.0, rho = 7, clip_denoised = False)
20
- pipeline = StableVideoDiffusionPipeline.from_pretrained(svd_path, scheduler = noise_scheduler, torch_dtype = torch.float16, variant = "fp16").to(device)
21
- load_lora_weights(pipeline.unet, lora_repo_path, weight_name = lora_weight_name)
 
 
22
 
23
- # @spaces.GPU
24
  def Video(
25
  image: Image,
26
  seed: Optional[int] = 1,
@@ -36,30 +39,36 @@ def Video(
36
  motion_bucket_id: int = 127,
37
  output_folder: str = "outputs_gradio",
38
  ):
 
39
  pipeline.scheduler.set_eta(eta)
40
 
 
41
  if randomize_seed:
42
- seed = random.randint(0, max_64_bit_int)
43
  generator = torch.manual_seed(seed)
44
- image=Image.fromarray(image)
45
-
 
46
  os.makedirs(output_folder, exist_ok=True)
47
  base_count = len(glob(os.path.join(output_folder, "*.mp4")))
48
  video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
49
-
50
 
51
- with torch.autocast(device):
 
 
52
  frames = pipeline(
53
- image, height = height, width = width,
54
- num_inference_steps = num_inference_steps,
55
- min_guidance_scale = min_guidance_scale,
56
- max_guidance_scale = max_guidance_scale,
57
- num_frames = num_frames, fps = fps, motion_bucket_id = motion_bucket_id,
58
- decode_chunk_size = 8,
59
- noise_aug_strength = 0.02,
60
- generator = generator,
61
  ).frames[0]
62
- save_video(frames, video_path, fps = fps, quality = 5.0)
 
 
63
  torch.manual_seed(seed)
64
 
65
- return video_path, seed
 
1
+ import torch
 
 
 
2
  from PIL import Image
3
+ import os
4
+ from diffusers import StableVideoDiffusionPipeline
5
  from .tdd_svd_scheduler import TDDSVDStochasticIterativeScheduler
6
  from .utils import load_lora_weights, save_video
 
7
  from glob import glob
8
+ from typing import Optional
9
 
10
+ # Define paths and device
11
  svd_path = 'stabilityai/stable-video-diffusion-img2vid-xt-1-1'
12
  lora_repo_path = 'RED-AIGC/TDD'
13
  lora_weight_name = 'svd-xt-1-1_tdd_lora_weights.safetensors'
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
 
16
+ # Initialize the noise scheduler and pipeline
17
+ noise_scheduler = TDDSVDStochasticIterativeScheduler(
18
+ num_train_timesteps=250, sigma_min=0.002, sigma_max=700.0,
19
+ sigma_data=1.0, s_noise=1.0, rho=7, clip_denoised=False
20
+ )
21
+ pipeline = StableVideoDiffusionPipeline.from_pretrained(
22
+ svd_path, scheduler=noise_scheduler, torch_dtype=torch.float16, variant="fp16"
23
+ ).to(device)
24
+ load_lora_weights(pipeline.unet, lora_repo_path, weight_name=lora_weight_name)
25
 
26
+ # Video function definition
27
  def Video(
28
  image: Image,
29
  seed: Optional[int] = 1,
 
39
  motion_bucket_id: int = 127,
40
  output_folder: str = "outputs_gradio",
41
  ):
42
+ # Set the eta value in the scheduler
43
  pipeline.scheduler.set_eta(eta)
44
 
45
+ # Handle seed randomness
46
  if randomize_seed:
47
+ seed = random.randint(0, 2**64 - 1)
48
  generator = torch.manual_seed(seed)
49
+
50
+ # Ensure the image is converted to a format that the model can use
51
+ image = Image.fromarray(image)
52
  os.makedirs(output_folder, exist_ok=True)
53
  base_count = len(glob(os.path.join(output_folder, "*.mp4")))
54
  video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
 
55
 
56
+ # Use float32 for image processing to avoid BFloat16 errors
57
+ image = image.convert("RGB") # Ensure image is in RGB format
58
+ with torch.autocast(device, dtype=torch.float32):
59
  frames = pipeline(
60
+ image, height=height, width=width,
61
+ num_inference_steps=num_inference_steps,
62
+ min_guidance_scale=min_guidance_scale,
63
+ max_guidance_scale=max_guidance_scale,
64
+ num_frames=num_frames, fps=fps, motion_bucket_id=motion_bucket_id,
65
+ decode_chunk_size=8,
66
+ noise_aug_strength=0.02,
67
+ generator=generator,
68
  ).frames[0]
69
+
70
+ # Save the generated video
71
+ save_video(frames, video_path, fps=fps, quality=5.0)
72
  torch.manual_seed(seed)
73
 
74
+ return video_path, seed
options/Video_model/__pycache__/Model.cpython-310.pyc CHANGED
Binary files a/options/Video_model/__pycache__/Model.cpython-310.pyc and b/options/Video_model/__pycache__/Model.cpython-310.pyc differ