sushann222 commited on
Commit
69faa81
·
verified ·
1 Parent(s): 2ef0709

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import argparse
3
+
4
+ from PIL import Image
5
+ import numpy as np
6
+ import torch
7
+ from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection
8
+ from diffusers import AutoencoderKL, EulerDiscreteScheduler
9
+ from diffusers.utils import export_to_gif
10
+
11
+ from diffusers_sv3d import SV3DUNetSpatioTemporalConditionModel, StableVideo3DDiffusionPipeline
12
+
13
+ SV3D_DIFFUSERS = "chenguolin/sv3d-diffusers"
14
+
15
+ # os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
16
+ os.environ["HF_HOME"] = "~/.cache/huggingface"
17
+
18
+
19
+ def main():
20
+ parser = argparse.ArgumentParser()
21
+ parser.add_argument("--output_dir", default="out/", type=str, help="Output filepath")
22
+ parser.add_argument("--image_path", default="assets/img1.jpg", type=str, help="Image filepath")
23
+ parser.add_argument("--elevation", default=10, type=float, help="Camera elevation of the input image")
24
+ parser.add_argument("--half_precision", action="store_true", help="Use fp16 half precision")
25
+ parser.add_argument("--seed", default=-1, type=int, help="Random seed")
26
+ args = parser.parse_args()
27
+
28
+ unet = SV3DUNetSpatioTemporalConditionModel.from_pretrained(SV3D_DIFFUSERS, subfolder="unet")
29
+ vae = AutoencoderKL.from_pretrained(SV3D_DIFFUSERS, subfolder="vae")
30
+ scheduler = EulerDiscreteScheduler.from_pretrained(SV3D_DIFFUSERS, subfolder="scheduler")
31
+ image_encoder = CLIPVisionModelWithProjection.from_pretrained(SV3D_DIFFUSERS, subfolder="image_encoder")
32
+ feature_extractor = CLIPImageProcessor.from_pretrained(SV3D_DIFFUSERS, subfolder="feature_extractor")
33
+
34
+ pipeline = StableVideo3DDiffusionPipeline(
35
+ image_encoder=image_encoder, feature_extractor=feature_extractor,
36
+ unet=unet, vae=vae,
37
+ scheduler=scheduler,
38
+ )
39
+
40
+ num_frames, sv3d_res = 21, 576
41
+ elevations_deg = [args.elevation] * num_frames
42
+ polars_rad = [np.deg2rad(90 - e) for e in elevations_deg]
43
+ azimuths_deg = np.linspace(0, 360, num_frames + 1)[1:] % 360
44
+ azimuths_rad = [np.deg2rad((a - azimuths_deg[-1]) % 360) for a in azimuths_deg]
45
+ azimuths_rad[:-1].sort()
46
+ pipeline = pipeline.to("cuda")
47
+ with torch.no_grad():
48
+ with torch.autocast("cuda", dtype=torch.float16 if args.half_precision else torch.float32, enabled=True):
49
+ image = Image.open(args.image_path)
50
+ image.load() # required for `.split()`
51
+ if len(image.split()) == 4: # RGBA
52
+ input_image = Image.new("RGB", image.size, (255, 255, 255)) # pure white bg
53
+ input_image.paste(image, mask=image.split()[3]) # 3rd is the alpha channel
54
+ else:
55
+ input_image = image
56
+ video_frames = pipeline(
57
+ input_image.resize((sv3d_res, sv3d_res)),
58
+ height=sv3d_res,
59
+ width=sv3d_res,
60
+ num_frames=num_frames,
61
+ decode_chunk_size=8, # smaller to save memory
62
+ polars_rad=polars_rad,
63
+ azimuths_rad=azimuths_rad,
64
+ generator=torch.manual_seed(args.seed) if args.seed >= 0 else None,
65
+ ).frames[0]
66
+
67
+ os.makedirs(args.output_dir, exist_ok=True)
68
+ name = os.path.basename(args.image_path).split(".")[0]
69
+ export_to_gif(video_frames, f"{args.output_dir}/{name}.gif", fps=7)
70
+
71
+
72
+ if __name__ == "__main__":
73
+ main()