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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -43
app.py CHANGED
@@ -38,8 +38,7 @@ def initialize_models(device):
38
  print("Initializing models...")
39
  # 3D ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ
40
  g.trellis_pipeline = TrellisImageTo3DPipeline.from_pretrained(
41
- "JeffreyXiang/TRELLIS-image-large",
42
- torch_dtype=torch.float32 # ๋ช…์‹œ์ ์œผ๋กœ dtype ์ง€์ •
43
  )
44
 
45
  # ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ
@@ -78,15 +77,12 @@ torch.cuda.empty_cache()
78
  torch.backends.cuda.matmul.allow_tf32 = True
79
  torch.backends.cudnn.benchmark = True
80
 
81
- # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
82
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
83
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
84
  os.environ['SPCONV_ALGO'] = 'native'
85
  os.environ['SPARSE_BACKEND'] = 'native'
86
  os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
87
- os.environ['TORCH_USE_CUDA_DSA'] = '1'
88
- os.environ['CUDA_VISIBLE_DEVICES'] = '0'
89
- os.environ['XFORMERS_FORCE_DISABLE_TRITON'] = '1' # xformers ๊ด€๋ จ ์„ค์ • ์ถ”๊ฐ€
90
 
91
  # CUDA ์ดˆ๊ธฐํ™” ๋ฐฉ์ง€
92
  torch.set_grad_enabled(False)
@@ -217,47 +213,36 @@ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_stre
217
  image = Image.open(image_path)
218
  print(f"Successfully loaded image with size: {image.size}")
219
 
220
- # Move model to GPU
221
- g.trellis_pipeline.to("cuda")
222
-
223
- try:
224
- with torch.inference_mode():
225
- with torch.cuda.amp.autocast():
226
- outputs = g.trellis_pipeline.run(
227
- image,
228
- seed=seed,
229
- formats=["gaussian", "mesh"],
230
- preprocess_image=False,
231
- sparse_structure_sampler_params={
232
- "steps": ss_sampling_steps,
233
- "cfg_strength": ss_guidance_strength,
234
- },
235
- slat_sampler_params={
236
- "steps": slat_sampling_steps,
237
- "cfg_strength": slat_guidance_strength,
238
- },
239
- )
240
 
241
- video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
242
- video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
243
- video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
244
- new_trial_id = str(uuid.uuid4())
245
- video_path = f"{TMP_DIR}/{new_trial_id}.mp4"
246
- os.makedirs(os.path.dirname(video_path), exist_ok=True)
247
- imageio.mimsave(video_path, video, fps=15)
248
- state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], new_trial_id)
249
-
250
- return state, video_path
251
- finally:
252
- # Move model back to CPU
253
- g.trellis_pipeline.to("cpu")
254
- torch.cuda.empty_cache()
255
 
256
  except Exception as e:
257
  print(f"Error in image_to_3d: {str(e)}")
258
- if hasattr(g.trellis_pipeline, 'device') and g.trellis_pipeline.device.type == "cuda":
259
- g.trellis_pipeline.to("cpu")
260
- torch.cuda.empty_cache()
261
  return None, None
262
 
263
 
 
38
  print("Initializing models...")
39
  # 3D ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ
40
  g.trellis_pipeline = TrellisImageTo3DPipeline.from_pretrained(
41
+ "JeffreyXiang/TRELLIS-image-large"
 
42
  )
43
 
44
  # ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ
 
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)
 
213
  image = Image.open(image_path)
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