Commit
·
597a667
1
Parent(s):
1d3fed2
Update device handling across multiple modules to support automatic selection of CUDA or CPU based on availability. This change enhances compatibility and performance on systems with or without GPU support, ensuring consistent behavior in model loading and data processing.
Browse files- app.py +4 -3
- imagedream/ldm/modules/encoders/modules.py +2 -1
- inference.py +2 -2
- libs/sample.py +13 -11
- pipelines.py +3 -2
- util/flexicubes.py +2 -2
- util/flexicubes_geometry.py +2 -1
app.py
CHANGED
@@ -161,8 +161,8 @@ args = parser.parse_args()
|
|
161 |
crm_path = hf_hub_download(repo_id="Zhengyi/CRM", filename="CRM.pth")
|
162 |
specs = json.load(open("configs/specs_objaverse_total.json"))
|
163 |
model = CRM(specs)
|
164 |
-
model.load_state_dict(torch.load(crm_path, map_location="cpu"), strict=False)
|
165 |
-
model = model.to("cpu")
|
166 |
|
167 |
stage1_config = OmegaConf.load(args.stage1_config).config
|
168 |
stage2_config = OmegaConf.load(args.stage2_config).config
|
@@ -177,12 +177,13 @@ pixel_path = hf_hub_download(repo_id="Zhengyi/CRM", filename="pixel-diffusion.pt
|
|
177 |
stage1_model_config.resume = pixel_path
|
178 |
stage2_model_config.resume = xyz_path
|
179 |
|
|
|
180 |
pipeline = TwoStagePipeline(
|
181 |
stage1_model_config,
|
182 |
stage2_model_config,
|
183 |
stage1_sampler_config,
|
184 |
stage2_sampler_config,
|
185 |
-
device=
|
186 |
dtype=torch.float32
|
187 |
)
|
188 |
|
|
|
161 |
crm_path = hf_hub_download(repo_id="Zhengyi/CRM", filename="CRM.pth")
|
162 |
specs = json.load(open("configs/specs_objaverse_total.json"))
|
163 |
model = CRM(specs)
|
164 |
+
model.load_state_dict(torch.load(crm_path, map_location="cuda" if torch.cuda.is_available() else "cpu"), strict=False)
|
165 |
+
model = model.to("cuda" if torch.cuda.is_available() else "cpu")
|
166 |
|
167 |
stage1_config = OmegaConf.load(args.stage1_config).config
|
168 |
stage2_config = OmegaConf.load(args.stage2_config).config
|
|
|
177 |
stage1_model_config.resume = pixel_path
|
178 |
stage2_model_config.resume = xyz_path
|
179 |
|
180 |
+
device = args.device if hasattr(args, 'device') else ("cuda" if torch.cuda.is_available() else "cpu")
|
181 |
pipeline = TwoStagePipeline(
|
182 |
stage1_model_config,
|
183 |
stage2_model_config,
|
184 |
stage1_sampler_config,
|
185 |
stage2_sampler_config,
|
186 |
+
device=device,
|
187 |
dtype=torch.float32
|
188 |
)
|
189 |
|
imagedream/ldm/modules/encoders/modules.py
CHANGED
@@ -306,10 +306,11 @@ class FrozenCLIPT5Encoder(AbstractEncoder):
|
|
306 |
self,
|
307 |
clip_version="openai/clip-vit-large-patch14",
|
308 |
t5_version="google/t5-v1_1-xl",
|
309 |
-
device=
|
310 |
clip_max_length=77,
|
311 |
t5_max_length=77,
|
312 |
):
|
|
|
313 |
super().__init__()
|
314 |
self.clip_encoder = FrozenCLIPEmbedder(
|
315 |
clip_version, device, max_length=clip_max_length
|
|
|
306 |
self,
|
307 |
clip_version="openai/clip-vit-large-patch14",
|
308 |
t5_version="google/t5-v1_1-xl",
|
309 |
+
device=None,
|
310 |
clip_max_length=77,
|
311 |
t5_max_length=77,
|
312 |
):
|
313 |
+
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
314 |
super().__init__()
|
315 |
self.clip_encoder = FrozenCLIPEmbedder(
|
316 |
clip_version, device, max_length=clip_max_length
|
inference.py
CHANGED
@@ -153,8 +153,8 @@ def generate3d(model, rgb, ccm, device):
|
|
153 |
|
154 |
from kiui.mesh_utils import clean_mesh
|
155 |
verts, faces = clean_mesh(data_config['verts'].squeeze().cpu().numpy().astype(np.float32), data_config['faces'].squeeze().cpu().numpy().astype(np.int32), repair = False, remesh=True, remesh_size=0.005, remesh_iters=1)
|
156 |
-
data_config['verts'] = torch.from_numpy(verts).to(
|
157 |
-
data_config['faces'] = torch.from_numpy(faces).to(
|
158 |
|
159 |
start_time = time.time()
|
160 |
with torch.no_grad():
|
|
|
153 |
|
154 |
from kiui.mesh_utils import clean_mesh
|
155 |
verts, faces = clean_mesh(data_config['verts'].squeeze().cpu().numpy().astype(np.float32), data_config['faces'].squeeze().cpu().numpy().astype(np.int32), repair = False, remesh=True, remesh_size=0.005, remesh_iters=1)
|
156 |
+
data_config['verts'] = torch.from_numpy(verts).to(device).contiguous()
|
157 |
+
data_config['faces'] = torch.from_numpy(faces).to(device).contiguous()
|
158 |
|
159 |
start_time = time.time()
|
160 |
with torch.no_grad():
|
libs/sample.py
CHANGED
@@ -11,18 +11,19 @@ class ImageDreamDiffusion:
|
|
11 |
def __init__(
|
12 |
self,
|
13 |
model,
|
14 |
-
device,
|
15 |
-
dtype,
|
16 |
-
mode,
|
17 |
-
num_frames,
|
18 |
-
camera_views,
|
19 |
-
ref_position,
|
20 |
random_background=False,
|
21 |
offset_noise=False,
|
22 |
resize_rate=1,
|
23 |
image_size=256,
|
24 |
seed=1234,
|
25 |
) -> None:
|
|
|
26 |
assert mode in ["pixel", "local"]
|
27 |
size = image_size
|
28 |
self.seed = seed
|
@@ -204,11 +205,11 @@ class ImageDreamDiffusionStage2:
|
|
204 |
def __init__(
|
205 |
self,
|
206 |
model,
|
207 |
-
device,
|
208 |
-
dtype,
|
209 |
-
num_frames,
|
210 |
-
camera_views,
|
211 |
-
ref_position,
|
212 |
random_background=False,
|
213 |
offset_noise=False,
|
214 |
resize_rate=1,
|
@@ -216,6 +217,7 @@ class ImageDreamDiffusionStage2:
|
|
216 |
image_size=256,
|
217 |
seed=1234,
|
218 |
) -> None:
|
|
|
219 |
assert mode in ["pixel", "local"]
|
220 |
|
221 |
size = image_size
|
|
|
11 |
def __init__(
|
12 |
self,
|
13 |
model,
|
14 |
+
device=None,
|
15 |
+
dtype=None,
|
16 |
+
mode=None,
|
17 |
+
num_frames=None,
|
18 |
+
camera_views=None,
|
19 |
+
ref_position=None,
|
20 |
random_background=False,
|
21 |
offset_noise=False,
|
22 |
resize_rate=1,
|
23 |
image_size=256,
|
24 |
seed=1234,
|
25 |
) -> None:
|
26 |
+
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
27 |
assert mode in ["pixel", "local"]
|
28 |
size = image_size
|
29 |
self.seed = seed
|
|
|
205 |
def __init__(
|
206 |
self,
|
207 |
model,
|
208 |
+
device=None,
|
209 |
+
dtype=None,
|
210 |
+
num_frames=None,
|
211 |
+
camera_views=None,
|
212 |
+
ref_position=None,
|
213 |
random_background=False,
|
214 |
offset_noise=False,
|
215 |
resize_rate=1,
|
|
|
217 |
image_size=256,
|
218 |
seed=1234,
|
219 |
) -> None:
|
220 |
+
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
221 |
assert mode in ["pixel", "local"]
|
222 |
|
223 |
size = image_size
|
pipelines.py
CHANGED
@@ -25,14 +25,15 @@ class TwoStagePipeline(object):
|
|
25 |
- the first stage was condition on single pixel image, gererate multi-view pixel image, based on the v2pp config
|
26 |
- the second stage was condition on multiview pixel image generated by the first stage, generate the final image, based on the stage2-test config
|
27 |
"""
|
|
|
28 |
self.resize_rate = resize_rate
|
29 |
|
30 |
self.stage1_model = instantiate_from_config(OmegaConf.load(stage1_model_config.config).model)
|
31 |
-
self.stage1_model.load_state_dict(torch.load(stage1_model_config.resume, map_location=
|
32 |
self.stage1_model = self.stage1_model.to(device).to(dtype)
|
33 |
|
34 |
self.stage2_model = instantiate_from_config(OmegaConf.load(stage2_model_config.config).model)
|
35 |
-
sd = torch.load(stage2_model_config.resume, map_location=
|
36 |
self.stage2_model.load_state_dict(sd, strict=False)
|
37 |
self.stage2_model = self.stage2_model.to(device).to(dtype)
|
38 |
|
|
|
25 |
- the first stage was condition on single pixel image, gererate multi-view pixel image, based on the v2pp config
|
26 |
- the second stage was condition on multiview pixel image generated by the first stage, generate the final image, based on the stage2-test config
|
27 |
"""
|
28 |
+
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
29 |
self.resize_rate = resize_rate
|
30 |
|
31 |
self.stage1_model = instantiate_from_config(OmegaConf.load(stage1_model_config.config).model)
|
32 |
+
self.stage1_model.load_state_dict(torch.load(stage1_model_config.resume, map_location=device), strict=False)
|
33 |
self.stage1_model = self.stage1_model.to(device).to(dtype)
|
34 |
|
35 |
self.stage2_model = instantiate_from_config(OmegaConf.load(stage2_model_config.config).model)
|
36 |
+
sd = torch.load(stage2_model_config.resume, map_location=device)
|
37 |
self.stage2_model.load_state_dict(sd, strict=False)
|
38 |
self.stage2_model = self.stage2_model.to(device).to(dtype)
|
39 |
|
util/flexicubes.py
CHANGED
@@ -64,8 +64,8 @@ class FlexiCubes:
|
|
64 |
The scale of weights in FlexiCubes. Should be between 0 and 1.
|
65 |
"""
|
66 |
|
67 |
-
def __init__(self, device=
|
68 |
-
|
69 |
self.device = device
|
70 |
self.dmc_table = torch.tensor(dmc_table, dtype=torch.long, device=device, requires_grad=False)
|
71 |
self.num_vd_table = torch.tensor(num_vd_table,
|
|
|
64 |
The scale of weights in FlexiCubes. Should be between 0 and 1.
|
65 |
"""
|
66 |
|
67 |
+
def __init__(self, device=None, qef_reg_scale=1e-3, weight_scale=0.99):
|
68 |
+
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
69 |
self.device = device
|
70 |
self.dmc_table = torch.tensor(dmc_table, dtype=torch.long, device=device, requires_grad=False)
|
71 |
self.num_vd_table = torch.tensor(num_vd_table,
|
util/flexicubes_geometry.py
CHANGED
@@ -31,8 +31,9 @@ def get_center_boundary_index(grid_res, device):
|
|
31 |
###############################################################################
|
32 |
class FlexiCubesGeometry(object):
|
33 |
def __init__(
|
34 |
-
self, grid_res=64, scale=2.0, device=
|
35 |
render_type='neural_render', args=None):
|
|
|
36 |
super(FlexiCubesGeometry, self).__init__()
|
37 |
self.grid_res = grid_res
|
38 |
self.device = device
|
|
|
31 |
###############################################################################
|
32 |
class FlexiCubesGeometry(object):
|
33 |
def __init__(
|
34 |
+
self, grid_res=64, scale=2.0, device=None, renderer=None,
|
35 |
render_type='neural_render', args=None):
|
36 |
+
device = device or ("cuda" if torch.cuda.is_available() else "cpu")
|
37 |
super(FlexiCubesGeometry, self).__init__()
|
38 |
self.grid_res = grid_res
|
39 |
self.device = device
|