sushann222 commited on
Commit
f45bef0
·
verified ·
1 Parent(s): a35cecf

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +46 -0
  2. app.py +73 -19
README.md ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SV3D-diffusers
2
+
3
+ ![](assets/sv3doutputs.gif)
4
+
5
+ This repo provides scripts about:
6
+
7
+ 1. Spatio-temporal UNet (`SV3DUNetSpatioTemporalConditionModel`) and pipeline (`StableVideo3DDiffusionPipeline`) modified from [SVD](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py) for [SV3D](https://sv3d.github.io) in the [diffusers](https://github.com/huggingface/diffusers) convention.
8
+
9
+ 2. Converting the [Stability-AI](https://github.com/Stability-AI/generative-models)'s [SV3D-p UNet checkpoint](https://huggingface.co/stabilityai/sv3d) to the [diffusers](https://github.com/huggingface/diffusers) convention.
10
+
11
+ 3. Infering the `SV3D-p` model with the [diffusers](https://github.com/huggingface/diffusers) library to synthesize a 21-frame orbital video around a 3D object from a single-view image (preprocessed by removing background and centering first).
12
+
13
+ Converted SV3D-p checkpoints have been uploaded to HuggingFace🤗 [chenguolin/sv3d-diffusers](https://huggingface.co/chenguolin/sv3d-diffusers).
14
+
15
+
16
+ ## 🚀 Usage
17
+ ```bash
18
+ git clone https://github.com/chenguolin/sv3d-diffusers.git
19
+ # Please install PyTorch first according to your CUDA version
20
+ pip3 install -r requirements.txt
21
+ # If you can't access to HuggingFace🤗, try:
22
+ # export HF_ENDPOINT=https://hf-mirror.com
23
+ python3 infer.py --output_dir out/ --image_path assets/images/sculpture.png --elevation 10 --half_precision --seed -1
24
+ ```
25
+ The synthesized video will save at `out/` as a `.gif` file.
26
+
27
+
28
+ ## 📸 Results
29
+ > Image preprocessing and random seed for different implementations are different, so the results are presented only for reference.
30
+
31
+ | Implementation | sculpture | bag | kunkun |
32
+ | :------------- | :------: | :----: | :----: |
33
+ | **SV3D-diffusers (Ours)** | ![](assets/sculpture.gif) | ![](assets/bag.gif) | ![](assets/kunkun.gif) |
34
+ | **Official SV3D** | ![](assets/sculpture_official.gif) | ![](assets/bag_official.gif) | ![](assets/kunkun_official.gif) |
35
+
36
+
37
+ ## 📚 Citation
38
+ If you find this repo helpful, please consider giving this repository a star 🌟 and citing the original SV3D paper.
39
+ ```
40
+ @inproceedings{voleti2024sv3d,
41
+ author={Voleti, Vikram and Yao, Chun-Han and Boss, Mark and Letts, Adam and Pankratz, David and Tochilkin, Dmitrii and Laforte, Christian and Rombach, Robin and Jampani, Varun},
42
+ title={{SV3D}: Novel Multi-view Synthesis and {3D} Generation from a Single Image using Latent Video Diffusion},
43
+ booktitle={European Conference on Computer Vision (ECCV)},
44
+ year={2024},
45
+ }
46
+ ```
app.py CHANGED
@@ -1,19 +1,73 @@
1
- import gradio as gr
2
- from infer import generate_video
3
-
4
- def gradio_generate_video(image, elevation=10):
5
- image_path = image.name # Save the image to a temporary file
6
- image.save(image_path) # Save uploaded image to disk
7
- output_gif = generate_video(image_path=image_path, output_dir="out/", elevation=elevation)
8
- return output_gif # Return the generated video path
9
-
10
- iface = gr.Interface(
11
- fn=gradio_generate_video,
12
- inputs=[
13
- gr.inputs.Image(type="filepath"),
14
- gr.inputs.Slider(minimum=0, maximum=90, default=10, label="Camera Elevation"),
15
- ],
16
- outputs=gr.outputs.Video(),
17
- )
18
-
19
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()