DawnC commited on
Commit
f193391
1 Parent(s): b19a14f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -540,13 +540,27 @@ import asyncio
540
  import traceback
541
 
542
  def get_device():
543
- if torch.cuda.is_available():
544
- device = torch.device('cuda')
 
 
 
 
 
 
 
 
 
 
 
 
 
545
  print(f"Using GPU: {torch.cuda.get_device_name(0)}")
546
  print(f"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB")
 
547
  return device
548
- else:
549
- print("Using CPU")
550
  return torch.device('cpu')
551
 
552
  device = get_device()
@@ -617,12 +631,12 @@ class BaseModel(nn.Module):
617
  self.device = device
618
  print(f"Initializing model on device: {device}")
619
 
620
- self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1)
621
  self.feature_dim = self.backbone.classifier[1].in_features
622
  self.backbone.classifier = nn.Identity()
623
 
624
  self.num_heads = max(1, min(8, self.feature_dim // 64))
625
- self.attention = MultiHeadAttention(self.feature_dim, num_heads=self.num_heads)
626
 
627
  self.classifier = nn.Sequential(
628
  nn.LayerNorm(self.feature_dim),
@@ -670,8 +684,7 @@ def preprocess_image(image):
670
 
671
 
672
  model_yolo = YOLO('yolov8l.pt')
673
- if torch.cuda.is_available():
674
- model_yolo.to(device)
675
 
676
  async def predict_single_dog(image):
677
  """
@@ -939,6 +952,10 @@ def show_details_html(choice, previous_output, initial_state):
939
  def main():
940
  if torch.cuda.is_available():
941
  torch.cuda.empty_cache()
 
 
 
 
942
 
943
  with gr.Blocks(css=get_css_styles()) as iface:
944
  # Header HTML
 
540
  import traceback
541
 
542
  def get_device():
543
+ print("Initializing CUDA environment...")
544
+ if not torch.cuda.is_available():
545
+ print("CUDA is not available, using CPU")
546
+ return torch.device('cpu')
547
+
548
+ try:
549
+ # 初始化 CUDA
550
+ torch.cuda.init()
551
+ torch.cuda.empty_cache()
552
+
553
+ # 設置當前設備
554
+ device = torch.device('cuda:0')
555
+ torch.cuda.set_device(device)
556
+
557
+ # 顯示詳細信息
558
  print(f"Using GPU: {torch.cuda.get_device_name(0)}")
559
  print(f"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB")
560
+ print(f"CUDA Version: {torch.version.cuda}")
561
  return device
562
+ except Exception as e:
563
+ print(f"CUDA initialization failed: {str(e)}")
564
  return torch.device('cpu')
565
 
566
  device = get_device()
 
631
  self.device = device
632
  print(f"Initializing model on device: {device}")
633
 
634
+ self.backbone = efficientnet_v2_m(weights=EfficientNet_V2_M_Weights.IMAGENET1K_V1).to(device)
635
  self.feature_dim = self.backbone.classifier[1].in_features
636
  self.backbone.classifier = nn.Identity()
637
 
638
  self.num_heads = max(1, min(8, self.feature_dim // 64))
639
+ self.attention = MultiHeadAttention(self.feature_dim, num_heads=self.num_heads).to(device)
640
 
641
  self.classifier = nn.Sequential(
642
  nn.LayerNorm(self.feature_dim),
 
684
 
685
 
686
  model_yolo = YOLO('yolov8l.pt')
687
+ model_yolo.to(device)
 
688
 
689
  async def predict_single_dog(image):
690
  """
 
952
  def main():
953
  if torch.cuda.is_available():
954
  torch.cuda.empty_cache()
955
+ print(f"Initial GPU memory allocated: {torch.cuda.memory_allocated(0) / 1e9:.2f} GB")
956
+
957
+ print(f"CUDA initialized: {torch.cuda.is_initialized()}")
958
+ print(f"Current device: {torch.cuda.current_device() if torch.cuda.is_available() else 'CPU'}")
959
 
960
  with gr.Blocks(css=get_css_styles()) as iface:
961
  # Header HTML