DawnC commited on
Commit
2e764f6
1 Parent(s): cec5334

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -27
app.py CHANGED
@@ -35,6 +35,7 @@ from urllib.parse import quote
35
  from ultralytics import YOLO
36
  import traceback
37
  import spaces
 
38
 
39
  # model_yolo = YOLO('yolov8l.pt')
40
 
@@ -601,55 +602,74 @@ def preprocess_image(image):
601
  ])
602
 
603
  return transform(image).unsqueeze(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
604
 
605
  @spaces.GPU
606
  async def predict_single_dog(image):
607
  """
608
- Predicts the dog breed using only the classifier.
609
- Args:
610
- image: PIL Image or numpy array
611
- Returns:
612
- tuple: (top1_prob, topk_breeds, relative_probs)
613
  """
614
- if not hasattr(predict_single_dog, 'model'):
615
- num_classes = len(dog_breeds)
616
- predict_single_dog.model = BaseModel(num_classes=len(dog_breeds), device='cuda').to('cuda')
617
- model_path = '124_best_model_dog.pth'
618
- checkpoint = torch.load(model_path, map_location='cuda')
619
- predict_single_dog.model.load_state_dict(checkpoint['base_model'], strict=False)
620
- predict_single_dog.model.eval()
621
 
622
  image_tensor = preprocess_image(image).to('cuda')
623
 
624
  with torch.no_grad():
625
- # Get model outputs (只使用logits,不需要features)
626
- logits = predict_single_dog.model(image_tensor)[0]
627
  probs = F.softmax(logits, dim=1)
628
 
629
- # Classifier prediction
630
  top5_prob, top5_idx = torch.topk(probs, k=5)
631
  breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
632
  probabilities = [prob.item() for prob in top5_prob[0]]
633
 
634
- # Calculate relative probabilities
635
- sum_probs = sum(probabilities[:3]) # 只取前三個來計算相對概率
636
  relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
637
 
638
- # Debug output
639
- print("\nClassifier Predictions:")
640
- for breed, prob in zip(breeds[:5], probabilities[:5]):
641
- print(f"{breed}: {prob:.4f}")
642
-
643
  return probabilities[0], breeds[:3], relative_probs
644
 
645
 
646
  @spaces.GPU(duration=120)
647
  async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
648
 
649
- if not hasattr(detect_multiple_dogs, 'model_yolo'):
650
- detect_multiple_dogs.model_yolo = YOLO('yolov8l.pt')
651
-
652
- results = detect_multiple_dogs.model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
 
 
 
653
  dogs = []
654
  boxes = []
655
  for box in results.boxes:
@@ -881,7 +901,7 @@ def show_details_html(choice, previous_output, initial_state):
881
 
882
  def main():
883
  with gr.Blocks(css=get_css_styles()) as iface:
884
- # Header HTML
885
 
886
  gr.HTML("""
887
  <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>
 
35
  from ultralytics import YOLO
36
  import traceback
37
  import spaces
38
+ import asyncio
39
 
40
  # model_yolo = YOLO('yolov8l.pt')
41
 
 
602
  ])
603
 
604
  return transform(image).unsqueeze(0)
605
+
606
+ global_models = {
607
+ 'classification_model': None,
608
+ 'detection_model': None
609
+ }
610
+
611
+ @spaces.GPU
612
+ async def initialize_models():
613
+ """
614
+ 初始化並預熱所有模型
615
+ 這個函數集中管理所有模型的初始化,確保它們只被載入一次
616
+ """
617
+ try:
618
+ # 初始化分類模型
619
+ if global_models['classification_model'] is None:
620
+ model = BaseModel(num_classes=len(dog_breeds), device='cuda').to('cuda')
621
+ checkpoint = torch.load('124_best_model_dog.pth', map_location='cuda')
622
+ model.load_state_dict(checkpoint['base_model'], strict=False)
623
+ model.eval()
624
+ global_models['classification_model'] = model
625
+
626
+ # 初始化檢測模型
627
+ if global_models['detection_model'] is None:
628
+ global_models['detection_model'] = YOLO('yolov8l.pt')
629
+
630
+ return True
631
+ except Exception as e:
632
+ print(f"模型初始化失敗: {str(e)}")
633
+ return False
634
+
635
 
636
  @spaces.GPU
637
  async def predict_single_dog(image):
638
  """
639
+ 預測狗狗品種
640
+ 使用全域模型進行預測,避免重複載入
 
 
 
641
  """
642
+ model = global_models['classification_model']
643
+ if model is None:
644
+ await initialize_models()
645
+ model = global_models['classification_model']
 
 
 
646
 
647
  image_tensor = preprocess_image(image).to('cuda')
648
 
649
  with torch.no_grad():
650
+ logits = model(image_tensor)[0]
 
651
  probs = F.softmax(logits, dim=1)
652
 
 
653
  top5_prob, top5_idx = torch.topk(probs, k=5)
654
  breeds = [dog_breeds[idx.item()] for idx in top5_idx[0]]
655
  probabilities = [prob.item() for prob in top5_prob[0]]
656
 
657
+ sum_probs = sum(probabilities[:3])
 
658
  relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in probabilities[:3]]
659
 
 
 
 
 
 
660
  return probabilities[0], breeds[:3], relative_probs
661
 
662
 
663
  @spaces.GPU(duration=120)
664
  async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
665
 
666
+ model_yolo = global_models['detection_model']
667
+ if model_yolo is None:
668
+ await initialize_models()
669
+ model_yolo = global_models['detection_model']
670
+
671
+ results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
672
+
673
  dogs = []
674
  boxes = []
675
  for box in results.boxes:
 
901
 
902
  def main():
903
  with gr.Blocks(css=get_css_styles()) as iface:
904
+ asyncio.create_task(initialize_models())
905
 
906
  gr.HTML("""
907
  <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>