Drexubery commited on
Commit
f80de23
1 Parent(s): de5238b
Files changed (2) hide show
  1. app.py +1 -1
  2. app_new.py +145 -0
app.py CHANGED
@@ -7,7 +7,7 @@ import spaces
7
  # os.system("pip install -v -v -v 'git+https://github.com/facebookresearch/pytorch3d.git@stable'")
8
  # os.system("cd pytorch3d && pip install -e . && cd ..")
9
  # os.system("pip install 'git+https://github.com/facebookresearch/pytorch3d.git'")
10
-
11
 
12
  import gradio as gr
13
  import random
 
7
  # os.system("pip install -v -v -v 'git+https://github.com/facebookresearch/pytorch3d.git@stable'")
8
  # os.system("cd pytorch3d && pip install -e . && cd ..")
9
  # os.system("pip install 'git+https://github.com/facebookresearch/pytorch3d.git'")
10
+ os.system("mkdir -p checkpoints/ && wget https://download.europe.naverlabs.com/ComputerVision/DUSt3R/DUSt3R_ViTLarge_BaseDecoder_512_dpt.pth -P checkpoints/")
11
 
12
  import gradio as gr
13
  import random
app_new.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import sys
4
+ import gradio as gr
5
+ import random
6
+ from configs.infer_config import get_parser
7
+ from huggingface_hub import hf_hub_download
8
+ sys.path.append('./extern/dust3r')
9
+ from dust3r.inference import inference, load_model
10
+ from omegaconf import OmegaConf
11
+ from pytorch_lightning import seed_everything
12
+ from utils.diffusion_utils import instantiate_from_config,load_model_checkpoint,image_guided_synthesis
13
+
14
+
15
+ i2v_examples = [
16
+ ['test/images/boy.png', 0, 1.0, '0 40', '0 0', '0 0', 50, 123],
17
+ ['test/images/car.jpeg', 0, 1.0, '0 -35', '0 0', '0 -0.1', 50, 123],
18
+ ['test/images/fruit.jpg', 0, 1.0, '0 -3 -15 -20 -17 -5 0', '0 -2 -5 -10 -8 -5 0 2 5 3 0', '0 0', 50, 123],
19
+ ['test/images/room.png', 5, 1.0, '0 3 10 20 17 10 0', '0 -2 -8 -6 0 2 5 3 0', '0 -0.02 -0.09 -0.16 -0.09 0', 50, 123],
20
+ ['test/images/castle.png', 0, 1.0, '0 30', '0 -1 -5 -4 0 1 5 4 0', '0 -0.2', 50, 123],
21
+ ]
22
+
23
+ max_seed = 2 ** 31
24
+
25
+ def download_model():
26
+ REPO_ID = 'Drexubery/ViewCrafter_25'
27
+ filename_list = ['model.ckpt']
28
+ for filename in filename_list:
29
+ local_file = os.path.join('./checkpoints/', filename)
30
+ if not os.path.exists(local_file):
31
+ hf_hub_download(repo_id=REPO_ID, filename=filename, local_dir='./checkpoints/', force_download=True)
32
+
33
+ download_model()
34
+
35
+
36
+ css = """#input_img {max-width: 1024px !important} #output_vid {max-width: 1024px; max-height:576px} #random_button {max-width: 100px !important}"""
37
+ parser = get_parser() # infer_config.py
38
+ opts = parser.parse_args() # default device: 'cuda:0'
39
+ opts.save_dir = './'
40
+ os.makedirs(opts.save_dir,exist_ok=True)
41
+ test_tensor = torch.Tensor([0]).cuda()
42
+ opts.device = str(test_tensor.device)
43
+
44
+ dust3r = load_model(opts.model_path, opts.device)
45
+ config = OmegaConf.load(opts.config)
46
+ model_config = config.pop("model", OmegaConf.create())
47
+ model_config['params']['unet_config']['params']['use_checkpoint'] = False
48
+ model = instantiate_from_config(model_config)
49
+ model = model.to(opts.device)
50
+ model.cond_stage_model.device = opts.device
51
+ model.perframe_ae = opts.perframe_ae
52
+ assert os.path.exists(opts.ckpt_path), "Error: checkpoint Not Found!"
53
+ model = load_model_checkpoint(model, opts.ckpt_path)
54
+ model.eval()
55
+ diffusion = model
56
+ transform = transforms.Compose([
57
+ transforms.Resize(576),
58
+ transforms.CenterCrop((576,1024)),
59
+ ])
60
+
61
+ def infer(opts,i2v_input_image, i2v_elevation, i2v_center_scale, i2v_d_phi, i2v_d_theta, i2v_d_r, i2v_steps, i2v_seed):
62
+ elevation = float(i2v_elevation)
63
+ center_scale = float(i2v_center_scale)
64
+ ddim_steps = i2v_steps
65
+ gradio_traj = [float(i) for i in i2v_d_phi.split()],[float(i) for i in i2v_d_theta.split()],[float(i) for i in i2v_d_r.split()]
66
+ seed_everything(i2v_seed)
67
+
68
+ torch.cuda.empty_cache()
69
+ img_tensor = torch.from_numpy(i2v_input_image).permute(2, 0, 1).unsqueeze(0).float().to(self.device)
70
+ img_tensor = (img_tensor / 255. - 0.5) * 2
71
+ image_tensor_resized = transform(img_tensor) #1,3,h,w
72
+ images = get_input_dict(image_tensor_resized,idx = 0,dtype = torch.float32)
73
+ images = [images, copy.deepcopy(images)]
74
+ images[1]['idx'] = 1
75
+ se_images = images
76
+ se_img_ori = (image_tensor_resized.squeeze(0).permute(1,2,0) + 1.)/2.
77
+
78
+ run_dust3r(input_images=self.images)
79
+ nvs_single_view(gradio=True)
80
+
81
+ traj_dir = os.path.join(self.opts.save_dir, "viz_traj.mp4")
82
+ gen_dir = os.path.join(self.opts.save_dir, "diffusion0.mp4")
83
+ return i2v_traj_path,i2v_output_path
84
+
85
+ with gr.Blocks(analytics_enabled=False, css=css) as viewcrafter_iface:
86
+ gr.Markdown("<div align='center'> <h1> ViewCrafter: Taming Video Diffusion Models for High-fidelity Novel View Synthesis </span> </h1> \
87
+ <h2 style='font-weight: 450; font-size: 1rem; margin: 0rem'>\
88
+ <a href='https://scholar.google.com/citations?user=UOE8-qsAAAAJ&hl=zh-CN'>Wangbo Yu</a>, \
89
+ <a href='https://doubiiu.github.io/'>Jinbo Xing</a>, <a href=''>Li Yuan</a>, \
90
+ <a href='https://wbhu.github.io/'>Wenbo Hu</a>, <a href='https://xiaoyu258.github.io/'>Xiaoyu Li</a>,\
91
+ <a href=''>Zhipeng Huang</a>, <a href='https://scholar.google.com/citations?user=qgdesEcAAAAJ&hl=en/'>Xiangjun Gao</a>,\
92
+ <a href='https://www.cse.cuhk.edu.hk/~ttwong/myself.html/'>Tien-Tsin Wong</a>,\
93
+ <a href='https://scholar.google.com/citations?hl=en&user=4oXBp9UAAAAJ&view_op=list_works&sortby=pubdate/'>Ying Shan</a>\
94
+ <a href=''>Yonghong Tian</a>\
95
+ </h2> \
96
+ <a style='font-size:18px;color: #FF5DB0' href='https://github.com/Drexubery/ViewCrafter/blob/main/docs/render_help.md'> [Guideline] </a>\
97
+ <a style='font-size:18px;color: #000000' href=''> [ArXiv] </a>\
98
+ <a style='font-size:18px;color: #000000' href='https://drexubery.github.io/ViewCrafter/'> [Project Page] </a>\
99
+ <a style='font-size:18px;color: #000000' href='https://github.com/Drexubery/ViewCrafter'> [Github] </a> </div>")
100
+
101
+ #######image2video######
102
+ with gr.Tab(label="ViewCrafter_25, 'single_view_txt' mode"):
103
+ with gr.Column():
104
+ with gr.Row():
105
+ with gr.Column():
106
+ with gr.Row():
107
+ i2v_input_image = gr.Image(label="Input Image",elem_id="input_img")
108
+ with gr.Row():
109
+ i2v_elevation = gr.Slider(minimum=-45, maximum=45, step=1, elem_id="elevation", label="elevation", value=5)
110
+ with gr.Row():
111
+ i2v_center_scale = gr.Slider(minimum=0.1, maximum=2, step=0.1, elem_id="i2v_center_scale", label="center_scale", value=1)
112
+ with gr.Row():
113
+ i2v_d_phi = gr.Text(label='d_phi sequence, should start with 0')
114
+ with gr.Row():
115
+ i2v_d_theta = gr.Text(label='d_theta sequence, should start with 0')
116
+ with gr.Row():
117
+ i2v_d_r = gr.Text(label='d_r sequence, should start with 0')
118
+ with gr.Row():
119
+ i2v_steps = gr.Slider(minimum=1, maximum=50, step=1, elem_id="i2v_steps", label="Sampling steps", value=50)
120
+ with gr.Row():
121
+ i2v_seed = gr.Slider(label='Random Seed', minimum=0, maximum=max_seed, step=1, value=123)
122
+ i2v_end_btn = gr.Button("Generate")
123
+ # with gr.Tab(label='Result'):
124
+ with gr.Column():
125
+ with gr.Row():
126
+ i2v_traj_video = gr.Video(label="Camera Trajectory",elem_id="traj_vid",autoplay=True,show_share_button=True)
127
+ with gr.Row():
128
+ i2v_output_video = gr.Video(label="Generated Video",elem_id="output_vid",autoplay=True,show_share_button=True)
129
+
130
+ gr.Examples(examples=i2v_examples,
131
+ inputs=[opts,i2v_input_image, i2v_elevation, i2v_center_scale, i2v_d_phi, i2v_d_theta, i2v_d_r, i2v_steps, i2v_seed],
132
+ outputs=[i2v_traj_video,i2v_output_video],
133
+ fn = infer,
134
+ cache_examples=False,
135
+ )
136
+
137
+ # image2video.run_gradio(i2v_input_image='test/images/boy.png', i2v_elevation='10', i2v_d_phi='0 40', i2v_d_theta='0 0', i2v_d_r='0 0', i2v_center_scale=1, i2v_steps=50, i2v_seed=123)
138
+ i2v_end_btn.click(inputs=[opts,i2v_input_image, i2v_elevation, i2v_center_scale, i2v_d_phi, i2v_d_theta, i2v_d_r, i2v_steps, i2v_seed],
139
+ outputs=[i2v_traj_video,i2v_output_video],
140
+ fn = infer
141
+ )
142
+
143
+ viewcrafter_iface.queue(max_size=12).launch(show_api=True)
144
+
145
+