Spaces:
Runtime error
Runtime error
Upload luciddreamer.py with huggingface_hub
Browse files- luciddreamer.py +48 -22
luciddreamer.py
CHANGED
|
@@ -56,9 +56,11 @@ pad_mask = lambda x, padamount=1: t2np(
|
|
| 56 |
|
| 57 |
|
| 58 |
class LucidDreamer:
|
| 59 |
-
def __init__(self):
|
| 60 |
self.opt = GSParams()
|
| 61 |
self.cam = CameraParams()
|
|
|
|
|
|
|
| 62 |
self.root = 'outputs'
|
| 63 |
self.default_model = 'SD1.5 (default)'
|
| 64 |
self.timestamp = datetime.datetime.now().strftime('%y%m%d_%H%M%S')
|
|
@@ -69,7 +71,8 @@ class LucidDreamer:
|
|
| 69 |
self.background = torch.tensor(bg_color, dtype=torch.float32, device='cuda')
|
| 70 |
|
| 71 |
self.rgb_model = StableDiffusionInpaintPipeline.from_pretrained(
|
| 72 |
-
'
|
|
|
|
| 73 |
self.d_model = torch.hub.load('./ZoeDepth', 'ZoeD_N', source='local', pretrained=True).to('cuda')
|
| 74 |
self.controlnet = None
|
| 75 |
self.lama = None
|
|
@@ -129,8 +132,6 @@ class LucidDreamer:
|
|
| 129 |
'width': self.cam.W,
|
| 130 |
}
|
| 131 |
|
| 132 |
-
# image_np = np.array(image).astype(float) / 255.0
|
| 133 |
-
# mask_np = 1.0 - np.array(mask_image) / 255.0
|
| 134 |
image_np = np.round(np.clip(image, 0, 1) * 255.).astype(np.uint8)
|
| 135 |
mask_sum = np.clip((image.prod(axis=-1) == 0) + (1 - mask_image), 0, 1)
|
| 136 |
mask_padded = pad_mask(mask_sum, 3)
|
|
@@ -166,16 +167,21 @@ class LucidDreamer:
|
|
| 166 |
return image
|
| 167 |
|
| 168 |
def run(self, rgb_cond, txt_cond, neg_txt_cond, pcdgenpath, seed, diff_steps, render_camerapath, model_name=None, example_name=None):
|
| 169 |
-
|
|
|
|
| 170 |
gaussians = self.create(
|
| 171 |
rgb_cond, txt_cond, neg_txt_cond, pcdgenpath, seed, diff_steps, model_name, example_name)
|
|
|
|
|
|
|
| 172 |
gallery, depth = self.render_video(render_camerapath, example_name=example_name)
|
| 173 |
return (gaussians, gallery, depth)
|
| 174 |
-
# return (gaussians, default_gallery, gallery)
|
| 175 |
|
| 176 |
def create(self, rgb_cond, txt_cond, neg_txt_cond, pcdgenpath, seed, diff_steps, model_name=None, example_name=None):
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
| 179 |
if example_name and example_name != 'DON\'T':
|
| 180 |
outfile = os.path.join('examples', f'{example_name}.ply')
|
| 181 |
if not os.path.exists(outfile):
|
|
@@ -188,9 +194,8 @@ class LucidDreamer:
|
|
| 188 |
self.scene = Scene(self.traindata, self.gaussians, self.opt)
|
| 189 |
self.training()
|
| 190 |
self.timestamp = datetime.datetime.now().strftime('%y%m%d_%H%M%S')
|
| 191 |
-
outfile = self.save_ply()
|
| 192 |
-
|
| 193 |
-
return outfile #, default_gallery
|
| 194 |
|
| 195 |
def save_ply(self, fpath=None):
|
| 196 |
if fpath is None:
|
|
@@ -218,8 +223,12 @@ class LucidDreamer:
|
|
| 218 |
videopath = os.path.join('examples', f'{example_name}_{preset}.mp4')
|
| 219 |
depthpath = os.path.join('examples', f'depth_{example_name}_{preset}.mp4')
|
| 220 |
else:
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
if os.path.exists(videopath) and os.path.exists(depthpath):
|
| 224 |
return videopath, depthpath
|
| 225 |
|
|
@@ -231,8 +240,14 @@ class LucidDreamer:
|
|
| 231 |
framelist = []
|
| 232 |
depthlist = []
|
| 233 |
dmin, dmax = 1e8, -1e8
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
frame, depth = results['render'], results['depth']
|
| 237 |
framelist.append(
|
| 238 |
np.round(frame.permute(1,2,0).detach().cpu().numpy().clip(0,1)*255.).astype(np.uint8))
|
|
@@ -259,7 +274,12 @@ class LucidDreamer:
|
|
| 259 |
if not self.scene:
|
| 260 |
raise('Build 3D Scene First!')
|
| 261 |
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
self.gaussians.update_learning_rate(iteration)
|
| 264 |
|
| 265 |
# Every 1000 its we increase the levels of SH up to a maximum degree
|
|
@@ -352,10 +372,13 @@ class LucidDreamer:
|
|
| 352 |
|
| 353 |
pts_coord_world, pts_colors = new_pts_coord_world2.copy(), new_pts_colors2.copy()
|
| 354 |
|
| 355 |
-
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
-
for i in
|
| 359 |
R, T = render_poses[i,:3,:3], render_poses[i,:3,3:4]
|
| 360 |
|
| 361 |
### Transform world to pixel
|
|
@@ -483,10 +506,13 @@ class LucidDreamer:
|
|
| 483 |
# render_poses = get_pcdGenPoses(pcdgenpath)
|
| 484 |
internel_render_poses = get_pcdGenPoses('hemisphere', {'center_depth': center_depth})
|
| 485 |
|
| 486 |
-
|
| 487 |
-
|
|
|
|
|
|
|
|
|
|
| 488 |
|
| 489 |
-
for i in
|
| 490 |
for j in range(len(internel_render_poses)):
|
| 491 |
idx = i * len(internel_render_poses) + j
|
| 492 |
print(f'{idx+1} / {len(render_poses)*len(internel_render_poses)}')
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
class LucidDreamer:
|
| 59 |
+
def __init__(self, for_gradio=True, save_dir=None):
|
| 60 |
self.opt = GSParams()
|
| 61 |
self.cam = CameraParams()
|
| 62 |
+
self.save_dir = save_dir
|
| 63 |
+
self.for_gradio = for_gradio
|
| 64 |
self.root = 'outputs'
|
| 65 |
self.default_model = 'SD1.5 (default)'
|
| 66 |
self.timestamp = datetime.datetime.now().strftime('%y%m%d_%H%M%S')
|
|
|
|
| 71 |
self.background = torch.tensor(bg_color, dtype=torch.float32, device='cuda')
|
| 72 |
|
| 73 |
self.rgb_model = StableDiffusionInpaintPipeline.from_pretrained(
|
| 74 |
+
'runwayml/stable-diffusion-inpainting', revision='fp16', torch_dtype=torch.float16).to('cuda')
|
| 75 |
+
# 'stablediffusion/SD1-5', revision='fp16', torch_dtype=torch.float16).to('cuda')
|
| 76 |
self.d_model = torch.hub.load('./ZoeDepth', 'ZoeD_N', source='local', pretrained=True).to('cuda')
|
| 77 |
self.controlnet = None
|
| 78 |
self.lama = None
|
|
|
|
| 132 |
'width': self.cam.W,
|
| 133 |
}
|
| 134 |
|
|
|
|
|
|
|
| 135 |
image_np = np.round(np.clip(image, 0, 1) * 255.).astype(np.uint8)
|
| 136 |
mask_sum = np.clip((image.prod(axis=-1) == 0) + (1 - mask_image), 0, 1)
|
| 137 |
mask_padded = pad_mask(mask_sum, 3)
|
|
|
|
| 167 |
return image
|
| 168 |
|
| 169 |
def run(self, rgb_cond, txt_cond, neg_txt_cond, pcdgenpath, seed, diff_steps, render_camerapath, model_name=None, example_name=None):
|
| 170 |
+
if rgb_cond is None or txt_cond is None:
|
| 171 |
+
return (None, None, None)
|
| 172 |
gaussians = self.create(
|
| 173 |
rgb_cond, txt_cond, neg_txt_cond, pcdgenpath, seed, diff_steps, model_name, example_name)
|
| 174 |
+
if gaussians is None:
|
| 175 |
+
return (None, None, None)
|
| 176 |
gallery, depth = self.render_video(render_camerapath, example_name=example_name)
|
| 177 |
return (gaussians, gallery, depth)
|
|
|
|
| 178 |
|
| 179 |
def create(self, rgb_cond, txt_cond, neg_txt_cond, pcdgenpath, seed, diff_steps, model_name=None, example_name=None):
|
| 180 |
+
if rgb_cond is None or txt_cond is None:
|
| 181 |
+
return None
|
| 182 |
+
if self.for_gradio:
|
| 183 |
+
self.cleaner()
|
| 184 |
+
self.load_model(model_name)
|
| 185 |
if example_name and example_name != 'DON\'T':
|
| 186 |
outfile = os.path.join('examples', f'{example_name}.ply')
|
| 187 |
if not os.path.exists(outfile):
|
|
|
|
| 194 |
self.scene = Scene(self.traindata, self.gaussians, self.opt)
|
| 195 |
self.training()
|
| 196 |
self.timestamp = datetime.datetime.now().strftime('%y%m%d_%H%M%S')
|
| 197 |
+
outfile = self.save_ply(os.path.join(self.save_dir, 'gsplat.ply'))
|
| 198 |
+
return outfile
|
|
|
|
| 199 |
|
| 200 |
def save_ply(self, fpath=None):
|
| 201 |
if fpath is None:
|
|
|
|
| 223 |
videopath = os.path.join('examples', f'{example_name}_{preset}.mp4')
|
| 224 |
depthpath = os.path.join('examples', f'depth_{example_name}_{preset}.mp4')
|
| 225 |
else:
|
| 226 |
+
if self.for_gradio:
|
| 227 |
+
videopath = os.path.join(self.root, self.timestamp, f'{preset}.mp4')
|
| 228 |
+
depthpath = os.path.join(self.root, self.timestamp, f'depth_{preset}.mp4')
|
| 229 |
+
else:
|
| 230 |
+
videopath = os.path.join(self.save_dir, f'{preset}.mp4')
|
| 231 |
+
depthpath = os.path.join(self.save_dir, f'depth_{preset}.mp4')
|
| 232 |
if os.path.exists(videopath) and os.path.exists(depthpath):
|
| 233 |
return videopath, depthpath
|
| 234 |
|
|
|
|
| 240 |
framelist = []
|
| 241 |
depthlist = []
|
| 242 |
dmin, dmax = 1e8, -1e8
|
| 243 |
+
|
| 244 |
+
if self.for_gradio:
|
| 245 |
+
iterable_render = progress.tqdm(views, desc='[4/4] Rendering a video')
|
| 246 |
+
else:
|
| 247 |
+
iterable_render = views
|
| 248 |
+
|
| 249 |
+
for view in iterable_render:
|
| 250 |
+
results = render(view, self.gaussians, self.opt, self.background)
|
| 251 |
frame, depth = results['render'], results['depth']
|
| 252 |
framelist.append(
|
| 253 |
np.round(frame.permute(1,2,0).detach().cpu().numpy().clip(0,1)*255.).astype(np.uint8))
|
|
|
|
| 274 |
if not self.scene:
|
| 275 |
raise('Build 3D Scene First!')
|
| 276 |
|
| 277 |
+
if self.for_gradio:
|
| 278 |
+
iterable_gauss = progress.tqdm(range(1, self.opt.iterations + 1), desc='[3/4] Baking Gaussians')
|
| 279 |
+
else:
|
| 280 |
+
iterable_gauss = range(1, self.opt.iterations + 1)
|
| 281 |
+
|
| 282 |
+
for iteration in iterable_gauss:
|
| 283 |
self.gaussians.update_learning_rate(iteration)
|
| 284 |
|
| 285 |
# Every 1000 its we increase the levels of SH up to a maximum degree
|
|
|
|
| 372 |
|
| 373 |
pts_coord_world, pts_colors = new_pts_coord_world2.copy(), new_pts_colors2.copy()
|
| 374 |
|
| 375 |
+
if self.for_gradio:
|
| 376 |
+
progress(0, desc='[1/4] Dreaming...')
|
| 377 |
+
iterable_dream = progress.tqdm(range(1, len(render_poses)), desc='[1/4] Dreaming')
|
| 378 |
+
else:
|
| 379 |
+
iterable_dream = range(1, len(render_poses))
|
| 380 |
|
| 381 |
+
for i in iterable_dream:
|
| 382 |
R, T = render_poses[i,:3,:3], render_poses[i,:3,3:4]
|
| 383 |
|
| 384 |
### Transform world to pixel
|
|
|
|
| 506 |
# render_poses = get_pcdGenPoses(pcdgenpath)
|
| 507 |
internel_render_poses = get_pcdGenPoses('hemisphere', {'center_depth': center_depth})
|
| 508 |
|
| 509 |
+
if self.for_gradio:
|
| 510 |
+
progress(0, desc='[2/4] Aligning...')
|
| 511 |
+
iterable_align = progress.tqdm(range(len(render_poses)), desc='[2/4] Aligning')
|
| 512 |
+
else:
|
| 513 |
+
iterable_align = range(len(render_poses))
|
| 514 |
|
| 515 |
+
for i in iterable_align:
|
| 516 |
for j in range(len(internel_render_poses)):
|
| 517 |
idx = i * len(internel_render_poses) + j
|
| 518 |
print(f'{idx+1} / {len(render_poses)*len(internel_render_poses)}')
|