aiqtech commited on
Commit
92dcaec
ยท
verified ยท
1 Parent(s): 9d1df11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -25
app.py CHANGED
@@ -40,6 +40,7 @@ def initialize_models(device):
40
  g.trellis_pipeline = TrellisImageTo3DPipeline.from_pretrained(
41
  "JeffreyXiang/TRELLIS-image-large"
42
  )
 
43
 
44
  # ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ
45
  print("Loading flux_pipe...")
@@ -48,6 +49,7 @@ def initialize_models(device):
48
  torch_dtype=torch.bfloat16,
49
  device_map="balanced"
50
  )
 
51
 
52
  # Hyper-SD LoRA ๋กœ๋“œ
53
  print("Loading LoRA weights...")
@@ -58,6 +60,7 @@ def initialize_models(device):
58
  )
59
  g.flux_pipe.load_lora_weights(lora_path)
60
  g.flux_pipe.fuse_lora(lora_scale=0.125)
 
61
 
62
  # ๋ฒˆ์—ญ๊ธฐ ์ดˆ๊ธฐํ™”
63
  print("Initializing translator...")
@@ -77,12 +80,14 @@ torch.cuda.empty_cache()
77
  torch.backends.cuda.matmul.allow_tf32 = True
78
  torch.backends.cudnn.benchmark = True
79
 
 
80
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
81
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
82
  os.environ['SPCONV_ALGO'] = 'native'
83
  os.environ['SPARSE_BACKEND'] = 'native'
84
  os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
85
  os.environ['XFORMERS_FORCE_DISABLE_TRITON'] = '1'
 
86
 
87
  # CUDA ์ดˆ๊ธฐํ™” ๋ฐฉ์ง€
88
  torch.set_grad_enabled(False)
@@ -214,35 +219,55 @@ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_stre
214
  print(f"Successfully loaded image with size: {image.size}")
215
 
216
  # GPU ์ž‘์—… ์‹œ์ž‘
217
- with torch.inference_mode():
218
- outputs = g.trellis_pipeline.run(
219
- image,
220
- seed=seed,
221
- formats=["gaussian", "mesh"],
222
- preprocess_image=False,
223
- sparse_structure_sampler_params={
224
- "steps": ss_sampling_steps,
225
- "cfg_strength": ss_guidance_strength,
226
- },
227
- slat_sampler_params={
228
- "steps": slat_sampling_steps,
229
- "cfg_strength": slat_guidance_strength,
230
- },
231
- )
232
-
233
- video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
234
- video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
235
- video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
236
- new_trial_id = str(uuid.uuid4())
237
- video_path = f"{TMP_DIR}/{new_trial_id}.mp4"
238
- os.makedirs(os.path.dirname(video_path), exist_ok=True)
239
- imageio.mimsave(video_path, video, fps=15)
240
- state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], new_trial_id)
241
 
242
- return state, video_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
 
244
  except Exception as e:
245
  print(f"Error in image_to_3d: {str(e)}")
 
 
 
 
246
  return None, None
247
 
248
 
 
40
  g.trellis_pipeline = TrellisImageTo3DPipeline.from_pretrained(
41
  "JeffreyXiang/TRELLIS-image-large"
42
  )
43
+ print("TrellisImageTo3DPipeline loaded successfully")
44
 
45
  # ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ
46
  print("Loading flux_pipe...")
 
49
  torch_dtype=torch.bfloat16,
50
  device_map="balanced"
51
  )
52
+ print("FluxPipeline loaded successfully")
53
 
54
  # Hyper-SD LoRA ๋กœ๋“œ
55
  print("Loading LoRA weights...")
 
60
  )
61
  g.flux_pipe.load_lora_weights(lora_path)
62
  g.flux_pipe.fuse_lora(lora_scale=0.125)
63
+ print("LoRA weights loaded successfully")
64
 
65
  # ๋ฒˆ์—ญ๊ธฐ ์ดˆ๊ธฐํ™”
66
  print("Initializing translator...")
 
80
  torch.backends.cuda.matmul.allow_tf32 = True
81
  torch.backends.cudnn.benchmark = True
82
 
83
+ # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
84
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
85
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
86
  os.environ['SPCONV_ALGO'] = 'native'
87
  os.environ['SPARSE_BACKEND'] = 'native'
88
  os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
89
  os.environ['XFORMERS_FORCE_DISABLE_TRITON'] = '1'
90
+ os.environ['XFORMERS_ENABLE_FLASH_ATTENTION'] = '1'
91
 
92
  # CUDA ์ดˆ๊ธฐํ™” ๋ฐฉ์ง€
93
  torch.set_grad_enabled(False)
 
219
  print(f"Successfully loaded image with size: {image.size}")
220
 
221
  # GPU ์ž‘์—… ์‹œ์ž‘
222
+ with torch.cuda.device(0):
223
+ # ๋ชจ๋ธ์„ GPU๋กœ ์ด๋™
224
+ g.trellis_pipeline.to('cuda')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
+ with torch.inference_mode(), torch.cuda.amp.autocast():
227
+ outputs = g.trellis_pipeline.run(
228
+ image,
229
+ seed=seed,
230
+ formats=["gaussian", "mesh"],
231
+ preprocess_image=False,
232
+ sparse_structure_sampler_params={
233
+ "steps": ss_sampling_steps,
234
+ "cfg_strength": ss_guidance_strength,
235
+ },
236
+ slat_sampler_params={
237
+ "steps": slat_sampling_steps,
238
+ "cfg_strength": slat_guidance_strength,
239
+ },
240
+ )
241
+
242
+ # GPU์—์„œ ๋น„๋””์˜ค ๋ Œ๋”๋ง
243
+ video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
244
+ video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
245
+
246
+ # CPU๋กœ ๋ฐ์ดํ„ฐ ์ด๋™
247
+ video = [v.cpu().numpy() if torch.is_tensor(v) else v for v in video]
248
+ video_geo = [v.cpu().numpy() if torch.is_tensor(v) else v for v in video_geo]
249
+
250
+ video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
251
+ new_trial_id = str(uuid.uuid4())
252
+ video_path = f"{TMP_DIR}/{new_trial_id}.mp4"
253
+ os.makedirs(os.path.dirname(video_path), exist_ok=True)
254
+ imageio.mimsave(video_path, video, fps=15)
255
+
256
+ # ์ƒํƒœ ์ €์žฅ ์ „์— CPU๋กœ ์ด๋™
257
+ state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], new_trial_id)
258
+
259
+ # ๋ชจ๋ธ์„ ๋‹ค์‹œ CPU๋กœ
260
+ g.trellis_pipeline.to('cpu')
261
+ torch.cuda.empty_cache()
262
+
263
+ return state, video_path
264
 
265
  except Exception as e:
266
  print(f"Error in image_to_3d: {str(e)}")
267
+ # ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ์—๋„ ๋ชจ๋ธ์„ CPU๋กœ ๋˜๋Œ๋ฆผ
268
+ if hasattr(g.trellis_pipeline, 'to'):
269
+ g.trellis_pipeline.to('cpu')
270
+ torch.cuda.empty_cache()
271
  return None, None
272
 
273