ironjr commited on
Commit
81ff5e6
1 Parent(s): ad5269f

Upload luciddreamer.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. luciddreamer.py +23 -12
luciddreamer.py CHANGED
@@ -75,7 +75,7 @@ class LucidDreamer:
75
  self.lama = None
76
  self.current_model = self.default_model
77
 
78
- def load_model(self, model_name, use_lama=False):
79
  if model_name is None:
80
  model_name = self.default_model
81
  if self.current_model == model_name:
@@ -108,33 +108,36 @@ class LucidDreamer:
108
  self.current_model = model_name
109
 
110
  def rgb(self, prompt, image, negative_prompt='', generator=None, num_inference_steps=50, mask_image=None):
 
 
111
  if self.current_model == self.default_model:
112
  return self.rgb_model(
113
  prompt=prompt,
114
  negative_prompt=negative_prompt,
115
  generator=generator,
116
  num_inference_steps=num_inference_steps,
117
- image=image,
118
- mask_image=mask_image,
119
  ).images[0]
120
 
121
  kwargs = {
122
  'negative_prompt': negative_prompt,
123
  'generator': generator,
124
- 'strength': 0.8,
125
  'num_inference_steps': num_inference_steps,
126
  'height': self.cam.H,
127
  'width': self.cam.W,
128
  }
129
 
130
- image_np = np.array(image).astype(float) / 255.0
131
- mask_np = np.array(mask_image) / 255.0
132
- mask_sum = np.clip((image_np.prod(axis=-1) == 0) + (1 - mask_np), 0, 1)
 
133
  mask_padded = pad_mask(mask_sum, 3)
134
  masked = image_np * np.logical_not(mask_padded[..., None])
135
 
136
  if self.lama is not None:
137
- lama_image = Image.fromarray(lama(masked, mask_padded).astype(np.uint8))
138
  else:
139
  lama_image = image
140
 
@@ -309,10 +312,15 @@ class LucidDreamer:
309
  image_in, mask_in = np.zeros((in_res, in_res, 3), dtype=np.uint8), 255*np.ones((in_res, in_res, 3), dtype=np.uint8)
310
  image_in[int(in_res/2-h_in/2):int(in_res/2+h_in/2), int(in_res/2-w_in/2):int(in_res/2+w_in/2)] = np.array(rgb_cond)
311
  mask_in[int(in_res/2-h_in/2):int(in_res/2+h_in/2), int(in_res/2-w_in/2):int(in_res/2+w_in/2)] = 0
 
 
 
312
  image_curr = self.rgb(
313
- prompt=prompt, image=Image.fromarray(image_in).resize((self.cam.W, self.cam.H)),
 
314
  negative_prompt=negative_prompt, generator=generator,
315
- mask_image=Image.fromarray(mask_in).resize((self.cam.W, self.cam.H)))
 
316
 
317
  else: # if there is a large gap between height and width, do inpainting
318
  if w_in > h_in:
@@ -379,9 +387,10 @@ class LucidDreamer:
379
  border_valid_idx = np.where(mask_hf[round_coord_cam2[1], round_coord_cam2[0]] == 1)[0] # use valid_idx[border_valid_idx] for world1
380
 
381
  image_curr = self.rgb(
382
- prompt=prompt, image=Image.fromarray(np.round(image2*255.).astype(np.uint8)),
383
  negative_prompt=negative_prompt, generator=generator, num_inference_steps=diff_steps,
384
- mask_image=Image.fromarray(np.round((1-mask2[:,:])*255.).astype(np.uint8)))
 
385
  depth_curr = self.d(image_curr)
386
 
387
 
@@ -502,6 +511,8 @@ class LucidDreamer:
502
  pixel_coord_camj[0]/pixel_coord_camj[2]<=W-1,
503
  pixel_coord_camj[1]/pixel_coord_camj[2]>=0,
504
  pixel_coord_camj[1]/pixel_coord_camj[2]<=H-1)))[0]
 
 
505
  pts_depthsj = pixel_coord_camj[-1:, valid_idxj]
506
  pixel_coord_camj = pixel_coord_camj[:2, valid_idxj]/pixel_coord_camj[-1:, valid_idxj]
507
  round_coord_camj = np.round(pixel_coord_camj).astype(np.int32)
 
75
  self.lama = None
76
  self.current_model = self.default_model
77
 
78
+ def load_model(self, model_name, use_lama=True):
79
  if model_name is None:
80
  model_name = self.default_model
81
  if self.current_model == model_name:
 
108
  self.current_model = model_name
109
 
110
  def rgb(self, prompt, image, negative_prompt='', generator=None, num_inference_steps=50, mask_image=None):
111
+ image_pil = Image.fromarray(np.round(image * 255.).astype(np.uint8))
112
+ mask_pil = Image.fromarray(np.round((1 - mask_image) * 255.).astype(np.uint8))
113
  if self.current_model == self.default_model:
114
  return self.rgb_model(
115
  prompt=prompt,
116
  negative_prompt=negative_prompt,
117
  generator=generator,
118
  num_inference_steps=num_inference_steps,
119
+ image=image_pil,
120
+ mask_image=mask_pil,
121
  ).images[0]
122
 
123
  kwargs = {
124
  'negative_prompt': negative_prompt,
125
  'generator': generator,
126
+ 'strength': 0.9,
127
  'num_inference_steps': num_inference_steps,
128
  'height': self.cam.H,
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)
137
  masked = image_np * np.logical_not(mask_padded[..., None])
138
 
139
  if self.lama is not None:
140
+ lama_image = Image.fromarray(self.lama(masked, mask_padded).astype(np.uint8))
141
  else:
142
  lama_image = image
143
 
 
312
  image_in, mask_in = np.zeros((in_res, in_res, 3), dtype=np.uint8), 255*np.ones((in_res, in_res, 3), dtype=np.uint8)
313
  image_in[int(in_res/2-h_in/2):int(in_res/2+h_in/2), int(in_res/2-w_in/2):int(in_res/2+w_in/2)] = np.array(rgb_cond)
314
  mask_in[int(in_res/2-h_in/2):int(in_res/2+h_in/2), int(in_res/2-w_in/2):int(in_res/2+w_in/2)] = 0
315
+
316
+ image2 = np.array(Image.fromarray(image_in).resize((self.cam.W, self.cam.H))).astype(float) / 255.0
317
+ mask2 = np.array(Image.fromarray(mask_in).resize((self.cam.W, self.cam.H))).astype(float) / 255.0
318
  image_curr = self.rgb(
319
+ prompt=prompt,
320
+ image=image2,
321
  negative_prompt=negative_prompt, generator=generator,
322
+ mask_image=mask2,
323
+ )
324
 
325
  else: # if there is a large gap between height and width, do inpainting
326
  if w_in > h_in:
 
387
  border_valid_idx = np.where(mask_hf[round_coord_cam2[1], round_coord_cam2[0]] == 1)[0] # use valid_idx[border_valid_idx] for world1
388
 
389
  image_curr = self.rgb(
390
+ prompt=prompt, image=image2, #Image.fromarray(np.round(image2*255.).astype(np.uint8)),
391
  negative_prompt=negative_prompt, generator=generator, num_inference_steps=diff_steps,
392
+ mask_image=mask2, #Image.fromarray(np.round((1-mask2[:,:])*255.).astype(np.uint8))
393
+ )
394
  depth_curr = self.d(image_curr)
395
 
396
 
 
511
  pixel_coord_camj[0]/pixel_coord_camj[2]<=W-1,
512
  pixel_coord_camj[1]/pixel_coord_camj[2]>=0,
513
  pixel_coord_camj[1]/pixel_coord_camj[2]<=H-1)))[0]
514
+ if len(valid_idxj) == 0:
515
+ continue
516
  pts_depthsj = pixel_coord_camj[-1:, valid_idxj]
517
  pixel_coord_camj = pixel_coord_camj[:2, valid_idxj]/pixel_coord_camj[-1:, valid_idxj]
518
  round_coord_camj = np.round(pixel_coord_camj).astype(np.int32)