xinjie.wang commited on
Commit
3150e46
·
1 Parent(s): c2edd8f
Files changed (1) hide show
  1. asset3d_gen/models/delight_model.py +26 -22
asset3d_gen/models/delight_model.py CHANGED
@@ -28,6 +28,7 @@ class DelightingModel(object):
28
  device: str = "cuda",
29
  seed: int = 0,
30
  ) -> None:
 
31
  self.image_guide_scale = image_guide_scale
32
  self.text_guide_scale = text_guide_scale
33
  self.num_infer_step = num_infer_step
@@ -37,29 +38,30 @@ class DelightingModel(object):
37
  )
38
  self.seed = seed
39
  self.device = device
40
-
41
- if model_path is None:
42
- suffix = "hunyuan3d-delight-v2-0"
43
- model_path = snapshot_download(
44
- repo_id="tencent/Hunyuan3D-2", allow_patterns=f"{suffix}/*"
 
 
 
 
 
 
 
 
 
 
 
45
  )
46
- model_path = os.path.join(model_path, suffix)
47
-
48
- pipeline = StableDiffusionInstructPix2PixPipeline.from_pretrained(
49
- model_path,
50
- torch_dtype=torch.float16,
51
- safety_checker=None,
52
- )
53
- pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(
54
- pipeline.scheduler.config
55
- )
56
- pipeline.set_progress_bar_config(disable=True)
57
 
58
- pipeline.to(self.device, torch.float16)
59
- # pipeline.enable_model_cpu_offload()
60
- # pipeline.enable_xformers_memory_efficient_attention()
61
-
62
- self.pipeline = pipeline
63
 
64
  def recenter_image(
65
  self, image: Image.Image, border_ratio: float = 0.2
@@ -110,6 +112,8 @@ class DelightingModel(object):
110
  preprocess: bool = False,
111
  target_wh: tuple[int, int] = None,
112
  ) -> Image.Image:
 
 
113
  if isinstance(image, str):
114
  image = Image.open(image)
115
  elif isinstance(image, np.ndarray):
@@ -153,7 +157,7 @@ if __name__ == "__main__":
153
  delighting_model = DelightingModel(
154
  # model_path="/horizon-bucket/robot_lab/users/xinjie.wang/weights/hunyuan3d-delight-v2-0" # noqa
155
  )
156
- image_path = "scripts/apps/assets/example_image/room_bottle_002.jpeg"
157
  image = delighting_model(
158
  image_path, preprocess=True, target_wh=(512, 512)
159
  ) # noqa
 
28
  device: str = "cuda",
29
  seed: int = 0,
30
  ) -> None:
31
+ self.model_path = model_path
32
  self.image_guide_scale = image_guide_scale
33
  self.text_guide_scale = text_guide_scale
34
  self.num_infer_step = num_infer_step
 
38
  )
39
  self.seed = seed
40
  self.device = device
41
+ self.pipeline = None # lazy load model adapt to @spaces.GPU
42
+
43
+ def _lazy_init_pipeline(self):
44
+ if self.pipeline is None:
45
+ model_path = self.model_path
46
+ if model_path is None:
47
+ suffix = "hunyuan3d-delight-v2-0"
48
+ model_path = snapshot_download(
49
+ repo_id="tencent/Hunyuan3D-2", allow_patterns=f"{suffix}/*"
50
+ )
51
+ model_path = os.path.join(model_path, suffix)
52
+
53
+ pipeline = StableDiffusionInstructPix2PixPipeline.from_pretrained(
54
+ model_path,
55
+ torch_dtype=torch.float16,
56
+ safety_checker=None,
57
  )
58
+ pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(
59
+ pipeline.scheduler.config
60
+ )
61
+ pipeline.set_progress_bar_config(disable=True)
 
 
 
 
 
 
 
62
 
63
+ pipeline.to(self.device, torch.float16)
64
+ self.pipeline = pipeline
 
 
 
65
 
66
  def recenter_image(
67
  self, image: Image.Image, border_ratio: float = 0.2
 
112
  preprocess: bool = False,
113
  target_wh: tuple[int, int] = None,
114
  ) -> Image.Image:
115
+ self._lazy_init_pipeline()
116
+
117
  if isinstance(image, str):
118
  image = Image.open(image)
119
  elif isinstance(image, np.ndarray):
 
157
  delighting_model = DelightingModel(
158
  # model_path="/horizon-bucket/robot_lab/users/xinjie.wang/weights/hunyuan3d-delight-v2-0" # noqa
159
  )
160
+ image_path = "/home/users/xinjie.wang/xinjie/asset3d-gen/scripts/apps/assets/example_image/room_bottle_002.jpeg"
161
  image = delighting_model(
162
  image_path, preprocess=True, target_wh=(512, 512)
163
  ) # noqa