Commit
Β·
bce015a
1
Parent(s):
8a16430
debug
Browse files
app.py
CHANGED
@@ -74,11 +74,12 @@ def check_gpu():
|
|
74 |
os.environ['LD_LIBRARY_PATH'] = "/usr/local/cuda-12.1/lib64:" + os.environ.get('LD_LIBRARY_PATH', '')
|
75 |
subprocess.run(['nvidia-smi']) # Test if CUDA is available
|
76 |
print(f"torch.cuda.is_available:{torch.cuda.is_available()}")
|
77 |
-
print("Device count:", torch.cuda.device_count())
|
|
|
78 |
# test nvdiffrast
|
79 |
import nvdiffrast.torch as dr
|
80 |
dr.RasterizeCudaContext(device="cuda:0")
|
81 |
-
print("nvdiffrast initialized successfully")
|
82 |
|
83 |
|
84 |
# Only check GPU in non-UI debug mode
|
@@ -163,137 +164,114 @@ def save_py3dmesh_with_trimesh_fast(meshes, save_glb_path=TEMP_MESH_ADDRESS, app
|
|
163 |
fix_vert_color_glb(save_glb_path)
|
164 |
print(f"saving to {save_glb_path}")
|
165 |
|
166 |
-
|
167 |
-
def
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
return
|
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 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
reconstruction_stage2_steps = 50,
|
273 |
-
save_intermediate_results=False
|
274 |
-
):
|
275 |
-
global mesh_cache
|
276 |
-
print(f"Before bundle_image_to_mesh: {torch.cuda.memory_allocated() / 1024**3} GB")
|
277 |
-
k3d_wrapper.recon_model.init_flexicubes_geometry("cuda:0", fovy=50.0)
|
278 |
-
print(f"init_flexicubes_geometry done")
|
279 |
-
# TODO: delete this later
|
280 |
-
k3d_wrapper.del_llm_model()
|
281 |
-
|
282 |
-
print(f"Before bundle_image_to_mesh after deleting llm model: {torch.cuda.memory_allocated() / 1024**3} GB")
|
283 |
-
|
284 |
-
gen_3d_bundle_image = torch.tensor(gen_3d_bundle_image).permute(2,0,1)/255
|
285 |
-
|
286 |
-
recon_mesh_path = k3d_wrapper.reconstruct_3d_bundle_image(gen_3d_bundle_image, camera_radius=camera_radius, lrm_render_radius=lrm_radius, isomer_radius=isomer_radius, save_intermediate_results=save_intermediate_results, reconstruction_stage1_steps=int(reconstruction_stage1_steps), reconstruction_stage2_steps=int(reconstruction_stage2_steps))
|
287 |
-
mesh_cache = recon_mesh_path
|
288 |
-
|
289 |
-
print(f"Mesh generated at: {mesh_cache}")
|
290 |
|
291 |
-
|
292 |
-
if not os.path.exists(mesh_cache):
|
293 |
-
print(f"Warning: Generated mesh file does not exist: {mesh_cache}")
|
294 |
-
return None, mesh_cache
|
295 |
-
|
296 |
-
return recon_mesh_path, mesh_cache
|
297 |
|
298 |
# _HEADER_=f"""
|
299 |
# <img src="{LOGO_PATH}">
|
|
|
74 |
os.environ['LD_LIBRARY_PATH'] = "/usr/local/cuda-12.1/lib64:" + os.environ.get('LD_LIBRARY_PATH', '')
|
75 |
subprocess.run(['nvidia-smi']) # Test if CUDA is available
|
76 |
print(f"torch.cuda.is_available:{torch.cuda.is_available()}")
|
77 |
+
print("Device count:", torch.cuda.device_count())
|
78 |
+
|
79 |
# test nvdiffrast
|
80 |
import nvdiffrast.torch as dr
|
81 |
dr.RasterizeCudaContext(device="cuda:0")
|
82 |
+
print("nvdiffrast initialized successfully")
|
83 |
|
84 |
|
85 |
# Only check GPU in non-UI debug mode
|
|
|
164 |
fix_vert_color_glb(save_glb_path)
|
165 |
print(f"saving to {save_glb_path}")
|
166 |
|
167 |
+
@spaces.GPU
|
168 |
+
def text_to_detailed(prompt, seed=None):
|
169 |
+
# test nvdiffrast
|
170 |
+
import nvdiffrast.torch as dr
|
171 |
+
dr.RasterizeCudaContext(device="cuda:0")
|
172 |
+
print("nvdiffrast initialized successfully")
|
173 |
+
|
174 |
+
print(f"torch.cuda.is_available():{torch.cuda.is_available()}")
|
175 |
+
# print(f"Before text_to_detailed: {torch.cuda.memory_allocated() / 1024**3} GB")
|
176 |
+
return k3d_wrapper.get_detailed_prompt(prompt, seed)
|
177 |
+
|
178 |
+
@spaces.GPU(duration=120)
|
179 |
+
def text_to_image(prompt, seed=None, strength=1.0,lora_scale=1.0, num_inference_steps=18, redux_hparam=None, init_image=None, **kwargs):
|
180 |
+
# subprocess.run("rm -rf /data-nvme/zerogpu-offload/*", env={}, shell=True)
|
181 |
+
# print(f"Before text_to_image: {torch.cuda.memory_allocated() / 1024**3} GB")
|
182 |
+
# k3d_wrapper.flux_pipeline.enable_xformers_memory_efficient_attention()
|
183 |
+
k3d_wrapper.renew_uuid()
|
184 |
+
init_image = None
|
185 |
+
# if init_image_path is not None:
|
186 |
+
# init_image = Image.open(init_image_path)
|
187 |
+
subprocess.run(['nvidia-smi']) # Test if CUDA is available
|
188 |
+
with torch.no_grad():
|
189 |
+
result = k3d_wrapper.generate_3d_bundle_image_text(
|
190 |
+
prompt,
|
191 |
+
image=init_image,
|
192 |
+
strength=strength,
|
193 |
+
lora_scale=lora_scale,
|
194 |
+
num_inference_steps=num_inference_steps,
|
195 |
+
seed=int(seed) if seed is not None else None,
|
196 |
+
redux_hparam=redux_hparam,
|
197 |
+
save_intermediate_results=True,
|
198 |
+
**kwargs)
|
199 |
+
return result[-1]
|
200 |
+
|
201 |
+
@spaces.GPU(duration=120)
|
202 |
+
def image2mesh_preprocess_(input_image_, seed, use_mv_rgb=True):
|
203 |
+
global preprocessed_input_image
|
204 |
+
|
205 |
+
seed = int(seed) if seed is not None else None
|
206 |
+
|
207 |
+
# TODO: delete this later
|
208 |
+
# k3d_wrapper.del_llm_model()
|
209 |
+
|
210 |
+
input_image_save_path, reference_save_path, caption = image2mesh_preprocess(k3d_wrapper, input_image_, seed, use_mv_rgb)
|
211 |
+
|
212 |
+
preprocessed_input_image = Image.open(input_image_save_path)
|
213 |
+
return reference_save_path, caption
|
214 |
+
|
215 |
+
|
216 |
+
@spaces.GPU(duration=120)
|
217 |
+
def image2mesh_main_(reference_3d_bundle_image, caption, seed, strength1=0.5, strength2=0.95, enable_redux=True, use_controlnet=True, if_video=True):
|
218 |
+
subprocess.run(['nvidia-smi'])
|
219 |
+
global mesh_cache
|
220 |
+
seed = int(seed) if seed is not None else None
|
221 |
+
|
222 |
+
|
223 |
+
# TODO: delete this later
|
224 |
+
# k3d_wrapper.del_llm_model()
|
225 |
+
|
226 |
+
input_image = preprocessed_input_image
|
227 |
+
|
228 |
+
reference_3d_bundle_image = torch.tensor(reference_3d_bundle_image).permute(2,0,1)/255
|
229 |
+
|
230 |
+
gen_save_path, recon_mesh_path = image2mesh_main(k3d_wrapper, input_image, reference_3d_bundle_image, caption=caption, seed=seed, strength1=strength1, strength2=strength2, enable_redux=enable_redux, use_controlnet=use_controlnet)
|
231 |
+
mesh_cache = recon_mesh_path
|
232 |
+
|
233 |
+
|
234 |
+
if if_video:
|
235 |
+
video_path = recon_mesh_path.replace('.obj','.mp4').replace('.glb','.mp4')
|
236 |
+
render_video_from_obj(recon_mesh_path, video_path)
|
237 |
+
print(f"After bundle_image_to_mesh: {torch.cuda.memory_allocated() / 1024**3} GB")
|
238 |
+
return gen_save_path, video_path, mesh_cache
|
239 |
+
else:
|
240 |
+
return gen_save_path, recon_mesh_path, mesh_cache
|
241 |
+
# return gen_save_path, recon_mesh_path
|
242 |
+
|
243 |
+
@spaces.GPU(duration=120)
|
244 |
+
def bundle_image_to_mesh(
|
245 |
+
gen_3d_bundle_image,
|
246 |
+
camera_radius=3.5,
|
247 |
+
lrm_radius = 3.5,
|
248 |
+
isomer_radius = 4.2,
|
249 |
+
reconstruction_stage1_steps = 0,
|
250 |
+
reconstruction_stage2_steps = 50,
|
251 |
+
save_intermediate_results=False
|
252 |
+
):
|
253 |
+
global mesh_cache
|
254 |
+
print(f"Before bundle_image_to_mesh: {torch.cuda.memory_allocated() / 1024**3} GB")
|
255 |
+
k3d_wrapper.recon_model.init_flexicubes_geometry("cuda:0", fovy=50.0)
|
256 |
+
print(f"init_flexicubes_geometry done")
|
257 |
+
# TODO: delete this later
|
258 |
+
k3d_wrapper.del_llm_model()
|
259 |
+
|
260 |
+
print(f"Before bundle_image_to_mesh after deleting llm model: {torch.cuda.memory_allocated() / 1024**3} GB")
|
261 |
+
|
262 |
+
gen_3d_bundle_image = torch.tensor(gen_3d_bundle_image).permute(2,0,1)/255
|
263 |
+
|
264 |
+
recon_mesh_path = k3d_wrapper.reconstruct_3d_bundle_image(gen_3d_bundle_image, camera_radius=camera_radius, lrm_render_radius=lrm_radius, isomer_radius=isomer_radius, save_intermediate_results=save_intermediate_results, reconstruction_stage1_steps=int(reconstruction_stage1_steps), reconstruction_stage2_steps=int(reconstruction_stage2_steps))
|
265 |
+
mesh_cache = recon_mesh_path
|
266 |
+
|
267 |
+
print(f"Mesh generated at: {mesh_cache}")
|
268 |
+
|
269 |
+
# Check if file exists
|
270 |
+
if not os.path.exists(mesh_cache):
|
271 |
+
print(f"Warning: Generated mesh file does not exist: {mesh_cache}")
|
272 |
+
return None, mesh_cache
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
|
274 |
+
return recon_mesh_path, mesh_cache
|
|
|
|
|
|
|
|
|
|
|
275 |
|
276 |
# _HEADER_=f"""
|
277 |
# <img src="{LOGO_PATH}">
|