-
Notifications
You must be signed in to change notification settings - Fork 23
/
gradio_app.py
209 lines (149 loc) · 7.12 KB
/
gradio_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import torch
import argparse
from nerf.provider import NeRFDataset
from nerf.utils_neurallift import *
import gradio as gr
import gc
from optimizer import Shampoo
import pdb
import os
import yaml, json, types
css="""
.gradio-container {
max-width: 512px; margin: auto;
}
"""
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default='configs/cabin.yaml', help='load config')
parser.add_argument('--share', action='store_true', help="do you want to share gradio app to external network?")
args = parser.parse_args()
with open(args.config, "r") as stream:
try:
opt = (yaml.safe_load(stream))
except yaml.YAMLError as exc:
print(exc)
def load_object(dct):
return types.SimpleNamespace(**dct)
opt = json.loads(json.dumps(opt), object_hook=load_object)
print(opt)
# from IPython import embed
# embed()
from datetime import datetime
opt.workspace = os.path.basename(args.config).replace('.yaml', '')
opt.workspace = os.path.join('logs', str(datetime.today().strftime('%Y-%m-%d')), opt.workspace + '_' + datetime.today().strftime('%H:%M:%S'))
import os, shutil
os.makedirs(opt.workspace, exist_ok=True)
shutil.copy(args.config, os.path.join(opt.workspace, os.path.basename(args.config)))
print('Double Check data path:')
print(opt.mask_path)
print(opt.rgb_path)
print(opt.depth_path)
print('====================')
if opt.backbone == 'vanilla':
from nerf.network import NeRFNetwork
elif opt.backbone == 'grid_finite':
from nerf.network_grid_finite import NeRFNetwork
else:
raise NotImplementedError(f'--backbone {opt.backbone} is not implemented!')
print(opt)
import time
# seed_everything(np.random.randint(10))
trainer = None
model = None
# define UI
with gr.Blocks(css=css) as demo:
# title
gr.Markdown('[NeuralLift-360](https://github.com/VITA-Group/NeuralLift-360) Image-to-3D Example')
# inputs
with gr.Row().style(equal_height=True):
ref_im = gr.Image(label="reference_image", elem_id="ref_im", value=opt.rgb_path)
mask = gr.Image(label="reference_mask", elem_id="ref_mask", value=opt.mask_path)
with gr.Column(scale=1, min_width=600):
prompt = gr.Textbox(label="Prompt", max_lines=1, value=opt.text)
iters = gr.Slider(label="Iters", minimum=1000, maximum=20000, value=opt.iters, step=100)
seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True)
button = gr.Button('Generate')
# outputs
image = gr.Image(label="image", visible=True)
video = gr.Video(label="video", visible=False)
logs = gr.Textbox(label="logging")
def submit(text, iters, seed):
global trainer, model
opt.seed = seed
opt.text = text
opt.iters = iters
seed_everything(opt.seed)
# clean up
if trainer is not None:
del model
del trainer
gc.collect()
torch.cuda.empty_cache()
print('[INFO] clean up!')
model = NeRFNetwork(opt)
print(model)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if opt.guidance == 'sd_clipguide':
from nerf.sd_clipguide import StableDiffusion
guidance = StableDiffusion(opt, device, sd_name=opt.sd_name)
else:
raise NotImplementedError(f'--guidance {opt.guidance} is not implemented.')
optimizer = lambda model: torch.optim.AdamW(model.get_params(opt.lr), betas=(0.9, 0.99), eps=1e-15)
train_loader = NeRFDataset(opt, device=device, type='train', H=opt.h, W=opt.w, size=100).dataloader()
test_loader = NeRFDataset(opt, device=device, type='test', H=opt.H, W=opt.W, size=100).dataloader()
opt.max_epoch = np.ceil(opt.iters / len(train_loader)).astype(np.int32)
scheduler = lambda optimizer: optim.lr_scheduler.LambdaLR(optimizer, lambda iter: 1) # fixed
trainer = Trainer('lift', opt, model, guidance, device=device, workspace=opt.workspace, optimizer=optimizer, ema_decay=None, fp16=opt.fp16, lr_scheduler=scheduler, use_checkpoint=opt.ckpt, eval_interval=opt.eval_interval, scheduler_update_every_step=True)
trainer.writer = tensorboardX.SummaryWriter(os.path.join(opt.workspace, "run", 'lift'))
valid_loader = NeRFDataset(opt, device=device, type='val', H=opt.H, W=opt.W, size=5).dataloader()
opt.max_epoch = np.ceil(opt.iters / len(train_loader)).astype(np.int32)
# we have to get the explicit training loop out here to yield progressive results...
loader = iter(valid_loader)
start_t = time.time()
for epoch in tqdm.tqdm(range(opt.max_epoch)):
STEPS = 100
trainer.train_gui(train_loader,
epoch=epoch, step=STEPS)
# manual test and get intermediate results
try:
data = next(loader)
except StopIteration:
loader = iter(valid_loader)
data = next(loader)
trainer.model.eval()
if trainer.ema is not None:
trainer.ema.store()
trainer.ema.copy_to()
with torch.no_grad():
with torch.cuda.amp.autocast(enabled=trainer.fp16):
preds, preds_depth, pred_mask = trainer.test_step(data, perturb=False)
if trainer.ema is not None:
trainer.ema.restore()
pred = preds[0].detach().cpu().numpy()
# pred_depth = preds_depth[0].detach().cpu().numpy()
pred = (pred * 255).astype(np.uint8)
yield {
image: gr.update(value=pred, visible=True),
video: gr.update(visible=False),
logs: f"training iters: {epoch * STEPS} / {iters}, lr: {trainer.optimizer.param_groups[0]['lr']:.6f}",
}
# test
trainer.test(test_loader)
results = glob.glob(os.path.join(opt.workspace, 'results', '*rgb*.mp4'))
assert results is not None, "cannot retrieve results!"
results.sort(key=lambda x: os.path.getmtime(x)) # sort by mtime
end_t = time.time()
yield {
image: gr.update(visible=False),
video: gr.update(value=results[-1], visible=True),
logs: f"Generation Finished in {(end_t - start_t)/ 60:.4f} minutes!",
}
button.click(
submit,
[prompt, iters, seed],
[image, video, logs]
)
# concurrency_count: only allow ONE running progress, else GPU will OOM.
demo.queue(concurrency_count=1)
demo.launch(share=args.share, debug=True)